From 1d4953ff425f8aea76c35541343d1e85dc1f6f47 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Wed, 8 Jun 2022 13:53:08 +0200
Subject: [PATCH] Add MyIdentityState.isMakingIdentity property

---
 .../MyIdentityFeature/MyIdentityFeature.swift | 20 ++++++++++++-------
 .../MyIdentityFeatureTests.swift              | 15 +++++++++++---
 2 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/Example/example-app/Sources/MyIdentityFeature/MyIdentityFeature.swift b/Example/example-app/Sources/MyIdentityFeature/MyIdentityFeature.swift
index 0b7bb5b7..8bc65764 100644
--- a/Example/example-app/Sources/MyIdentityFeature/MyIdentityFeature.swift
+++ b/Example/example-app/Sources/MyIdentityFeature/MyIdentityFeature.swift
@@ -7,14 +7,17 @@ import ErrorFeature
 public struct MyIdentityState: Equatable {
   public init(
     id: UUID,
+    isMakingIdentity: Bool = false,
     error: ErrorState? = nil
   ) {
     self.id = id
+    self.isMakingIdentity = isMakingIdentity
     self.error = error
   }
 
   public var id: UUID
   public var identity: Identity?
+  public var isMakingIdentity: Bool
   public var error: ErrorState?
 }
 
@@ -23,7 +26,7 @@ public enum MyIdentityAction: Equatable {
   case observeMyIdentity
   case didUpdateMyIdentity(Identity?)
   case makeIdentity
-  case didFailMakingIdentity(NSError)
+  case didFinishMakingIdentity(NSError?)
   case didDismissError
   case error(ErrorAction)
 }
@@ -78,14 +81,14 @@ public let myIdentityReducer = Reducer<MyIdentityState, MyIdentityAction, MyIden
     return .none
 
   case .makeIdentity:
-    return Effect.run { subscriber in
+    state.isMakingIdentity = true
+    return Effect.future { fulfill in
       do {
         env.updateIdentity(try env.getClient()?.makeIdentity())
+        fulfill(.success(.didFinishMakingIdentity(nil)))
       } catch {
-        subscriber.send(.didFailMakingIdentity(error as NSError))
+        fulfill(.success(.didFinishMakingIdentity(error as NSError)))
       }
-      subscriber.send(completion: .finished)
-      return AnyCancellable {}
     }
     .subscribe(on: env.bgScheduler)
     .receive(on: env.mainScheduler)
@@ -95,8 +98,11 @@ public let myIdentityReducer = Reducer<MyIdentityState, MyIdentityAction, MyIden
     state.error = nil
     return .none
 
-  case .didFailMakingIdentity(let error):
-    state.error = ErrorState(error: error)
+  case .didFinishMakingIdentity(let error):
+    state.isMakingIdentity = false
+    if let error = error {
+      state.error = ErrorState(error: error)
+    }
     return .none
   }
 }
diff --git a/Example/example-app/Tests/MyIdentityFeatureTests/MyIdentityFeatureTests.swift b/Example/example-app/Tests/MyIdentityFeatureTests/MyIdentityFeatureTests.swift
index 64bb06d2..b426cc0f 100644
--- a/Example/example-app/Tests/MyIdentityFeatureTests/MyIdentityFeatureTests.swift
+++ b/Example/example-app/Tests/MyIdentityFeatureTests/MyIdentityFeatureTests.swift
@@ -68,13 +68,19 @@ final class MyIdentityFeatureTests: XCTestCase {
       environment: env
     )
 
-    store.send(.makeIdentity)
+    store.send(.makeIdentity) {
+      $0.isMakingIdentity = true
+    }
 
     bgScheduler.advance()
 
     XCTAssertNoDifference(didUpdateIdentity, [newIdentity])
 
     mainScheduler.advance()
+
+    store.receive(.didFinishMakingIdentity(nil)) {
+      $0.isMakingIdentity = false
+    }
   }
 
   func testMakeIdentityFailure() {
@@ -97,12 +103,15 @@ final class MyIdentityFeatureTests: XCTestCase {
       environment: env
     )
 
-    store.send(.makeIdentity)
+    store.send(.makeIdentity) {
+      $0.isMakingIdentity = true
+    }
 
     bgScheduler.advance()
     mainScheduler.advance()
 
-    store.receive(.didFailMakingIdentity(error)) {
+    store.receive(.didFinishMakingIdentity(error)) {
+      $0.isMakingIdentity = false
       $0.error = ErrorState(error: error)
     }
 
-- 
GitLab