Skip to content
Snippets Groups Projects
Commit 43cbb8aa authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Remove AuthHandler and MessageListener from HomeFeature

parent bf618cfd
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!100Messenger - restore from backup
This commit is part of merge request !100. Comments created here will be created in the context of that merge request.
......@@ -111,11 +111,6 @@ extension AppEnvironment {
HomeEnvironment(
messenger: messenger,
dbManager: dbManager,
authHandler: authHandler,
messageListener: .live(
messenger: messenger,
db: dbManager.getDB
),
mainQueue: mainQueue,
bgQueue: bgQueue,
register: {
......
......@@ -14,8 +14,6 @@ import XXModels
public struct HomeState: Equatable {
public init(
failure: String? = nil,
authFailure: String? = nil,
messageListenerFailure: String? = nil,
isNetworkHealthy: Bool? = nil,
networkNodesReport: NodeRegistrationReport? = nil,
isDeletingAccount: Bool = false,
......@@ -25,8 +23,6 @@ public struct HomeState: Equatable {
userSearch: UserSearchState? = nil
) {
self.failure = failure
self.authFailure = authFailure
self.messageListenerFailure = messageListenerFailure
self.isNetworkHealthy = isNetworkHealthy
self.isDeletingAccount = isDeletingAccount
self.alert = alert
......@@ -36,8 +32,6 @@ public struct HomeState: Equatable {
}
public var failure: String?
public var authFailure: String?
public var messageListenerFailure: String?
public var isNetworkHealthy: Bool?
public var networkNodesReport: NodeRegistrationReport?
public var isDeletingAccount: Bool
......@@ -55,20 +49,6 @@ public enum HomeAction: Equatable {
case failure(NSError)
}
public enum AuthHandler: Equatable {
case start
case stop
case failure(NSError)
case failureDismissed
}
public enum MessageListener: Equatable {
case start
case stop
case failure(NSError)
case failureDismissed
}
public enum NetworkMonitor: Equatable {
case start
case stop
......@@ -84,8 +64,6 @@ public enum HomeAction: Equatable {
}
case messenger(Messenger)
case authHandler(AuthHandler)
case messageListener(MessageListener)
case networkMonitor(NetworkMonitor)
case deleteAccount(DeleteAccount)
case didDismissAlert
......@@ -103,8 +81,6 @@ public struct HomeEnvironment {
public init(
messenger: Messenger,
dbManager: DBManager,
authHandler: AuthCallbackHandler,
messageListener: MessageListenerHandler,
mainQueue: AnySchedulerOf<DispatchQueue>,
bgQueue: AnySchedulerOf<DispatchQueue>,
register: @escaping () -> RegisterEnvironment,
......@@ -113,8 +89,6 @@ public struct HomeEnvironment {
) {
self.messenger = messenger
self.dbManager = dbManager
self.authHandler = authHandler
self.messageListener = messageListener
self.mainQueue = mainQueue
self.bgQueue = bgQueue
self.register = register
......@@ -124,8 +98,6 @@ public struct HomeEnvironment {
public var messenger: Messenger
public var dbManager: DBManager
public var authHandler: AuthCallbackHandler
public var messageListener: MessageListenerHandler
public var mainQueue: AnySchedulerOf<DispatchQueue>
public var bgQueue: AnySchedulerOf<DispatchQueue>
public var register: () -> RegisterEnvironment
......@@ -137,8 +109,6 @@ extension HomeEnvironment {
public static let unimplemented = HomeEnvironment(
messenger: .unimplemented,
dbManager: .unimplemented,
authHandler: .unimplemented,
messageListener: .unimplemented,
mainQueue: .unimplemented,
bgQueue: .unimplemented,
register: { .unimplemented },
......@@ -151,14 +121,10 @@ public let homeReducer = Reducer<HomeState, HomeAction, HomeEnvironment>
{ state, action, env in
enum NetworkHealthEffectId {}
enum NetworkNodesEffectId {}
enum AuthCallbacksEffectId {}
enum MessageListenerEffectId {}
switch action {
case .messenger(.start):
return .merge(
Effect(value: .authHandler(.start)),
Effect(value: .messageListener(.start)),
Effect(value: .networkMonitor(.stop)),
Effect.result {
do {
......@@ -200,52 +166,6 @@ public let homeReducer = Reducer<HomeState, HomeAction, HomeEnvironment>
state.failure = error.localizedDescription
return .none
case .authHandler(.start):
return Effect.run { subscriber in
let cancellable = env.authHandler(onError: { error in
subscriber.send(.authHandler(.failure(error as NSError)))
})
return AnyCancellable { cancellable.cancel() }
}
.subscribe(on: env.bgQueue)
.receive(on: env.mainQueue)
.eraseToEffect()
.cancellable(id: AuthCallbacksEffectId.self, cancelInFlight: true)
case .authHandler(.stop):
return .cancel(id: AuthCallbacksEffectId.self)
case .authHandler(.failure(let error)):
state.authFailure = error.localizedDescription
return .none
case .authHandler(.failureDismissed):
state.authFailure = nil
return .none
case .messageListener(.start):
return Effect.run { subscriber in
let cancellable = env.messageListener(onError: { error in
subscriber.send(.messageListener(.failure(error as NSError)))
})
return AnyCancellable { cancellable.cancel() }
}
.subscribe(on: env.bgQueue)
.receive(on: env.mainQueue)
.eraseToEffect()
.cancellable(id: MessageListenerEffectId.self, cancelInFlight: true)
case .messageListener(.stop):
return .cancel(id: MessageListenerEffectId.self)
case .messageListener(.failure(let error)):
state.messageListenerFailure = error.localizedDescription
return .none
case .messageListener(.failureDismissed):
state.messageListenerFailure = nil
return .none
case .networkMonitor(.start):
return .merge(
Effect.run { subscriber in
......
......@@ -15,16 +15,12 @@ public struct HomeView: View {
struct ViewState: Equatable {
var failure: String?
var authFailure: String?
var messageListenerFailure: String?
var isNetworkHealthy: Bool?
var networkNodesReport: NodeRegistrationReport?
var isDeletingAccount: Bool
init(state: HomeState) {
failure = state.failure
authFailure = state.authFailure
messageListenerFailure = state.messageListenerFailure
isNetworkHealthy = state.isNetworkHealthy
isDeletingAccount = state.isDeletingAccount
networkNodesReport = state.networkNodesReport
......@@ -48,32 +44,6 @@ public struct HomeView: View {
}
}
if let authFailure = viewStore.authFailure {
Section {
Text(authFailure)
Button {
viewStore.send(.authHandler(.failureDismissed))
} label: {
Text("Dismiss")
}
} header: {
Text("Auth Error")
}
}
if let messageListenerFailure = viewStore.messageListenerFailure {
Section {
Text(messageListenerFailure)
Button {
viewStore.send(.messageListener(.failureDismissed))
} label: {
Text("Dismiss")
}
} header: {
Text("Message Listener Error")
}
}
Section {
HStack {
Text("Health")
......
......@@ -24,8 +24,6 @@ final class HomeFeatureTests: XCTestCase {
store.environment.bgQueue = .immediate
store.environment.mainQueue = .immediate
store.environment.authHandler.run = { _ in Cancellable {} }
store.environment.messageListener.run = { _ in Cancellable {} }
store.environment.messenger.start.run = { messengerDidStartWithTimeout.append($0) }
store.environment.messenger.isConnected.run = { false }
store.environment.messenger.connect.run = { messengerDidConnect += 1 }
......@@ -40,15 +38,10 @@ final class HomeFeatureTests: XCTestCase {
XCTAssertNoDifference(messengerDidConnect, 1)
XCTAssertNoDifference(messengerDidListenForMessages, 1)
store.receive(.authHandler(.start))
store.receive(.messageListener(.start))
store.receive(.networkMonitor(.stop))
store.receive(.messenger(.didStartUnregistered)) {
$0.register = RegisterState()
}
store.send(.authHandler(.stop))
store.send(.messageListener(.stop))
}
func testMessengerStartRegistered() {
......@@ -65,8 +58,6 @@ final class HomeFeatureTests: XCTestCase {
store.environment.bgQueue = .immediate
store.environment.mainQueue = .immediate
store.environment.authHandler.run = { _ in Cancellable {} }
store.environment.messageListener.run = { _ in Cancellable {} }
store.environment.messenger.start.run = { messengerDidStartWithTimeout.append($0) }
store.environment.messenger.isConnected.run = { false }
store.environment.messenger.connect.run = { messengerDidConnect += 1 }
......@@ -92,15 +83,11 @@ final class HomeFeatureTests: XCTestCase {
XCTAssertNoDifference(messengerDidListenForMessages, 1)
XCTAssertNoDifference(messengerDidLogIn, 1)
store.receive(.authHandler(.start))
store.receive(.messageListener(.start))
store.receive(.networkMonitor(.stop))
store.receive(.messenger(.didStartRegistered))
store.receive(.networkMonitor(.start))
store.send(.networkMonitor(.stop))
store.send(.authHandler(.stop))
store.send(.messageListener(.stop))
}
func testRegisterFinished() {
......@@ -117,8 +104,6 @@ final class HomeFeatureTests: XCTestCase {
store.environment.bgQueue = .immediate
store.environment.mainQueue = .immediate
store.environment.authHandler.run = { _ in Cancellable {} }
store.environment.messageListener.run = { _ in Cancellable {} }
store.environment.messenger.start.run = { messengerDidStartWithTimeout.append($0) }
store.environment.messenger.isConnected.run = { true }
store.environment.messenger.isListeningForMessages.run = { true }
......@@ -144,15 +129,11 @@ final class HomeFeatureTests: XCTestCase {
XCTAssertNoDifference(messengerDidStartWithTimeout, [30_000])
XCTAssertNoDifference(messengerDidLogIn, 1)
store.receive(.authHandler(.start))
store.receive(.messageListener(.start))
store.receive(.networkMonitor(.stop))
store.receive(.messenger(.didStartRegistered))
store.receive(.networkMonitor(.start))
store.send(.networkMonitor(.stop))
store.send(.authHandler(.stop))
store.send(.messageListener(.stop))
}
func testMessengerStartFailure() {
......@@ -167,21 +148,14 @@ final class HomeFeatureTests: XCTestCase {
store.environment.bgQueue = .immediate
store.environment.mainQueue = .immediate
store.environment.authHandler.run = { _ in Cancellable {} }
store.environment.messageListener.run = { _ in Cancellable {} }
store.environment.messenger.start.run = { _ in throw error }
store.send(.messenger(.start))
store.receive(.authHandler(.start))
store.receive(.messageListener(.start))
store.receive(.networkMonitor(.stop))
store.receive(.messenger(.failure(error as NSError))) {
$0.failure = error.localizedDescription
}
store.send(.authHandler(.stop))
store.send(.messageListener(.stop))
}
func testMessengerStartConnectFailure() {
......@@ -196,23 +170,16 @@ final class HomeFeatureTests: XCTestCase {
store.environment.bgQueue = .immediate
store.environment.mainQueue = .immediate
store.environment.authHandler.run = { _ in Cancellable {} }
store.environment.messageListener.run = { _ in Cancellable {} }
store.environment.messenger.start.run = { _ in }
store.environment.messenger.isConnected.run = { false }
store.environment.messenger.connect.run = { throw error }
store.send(.messenger(.start))
store.receive(.authHandler(.start))
store.receive(.messageListener(.start))
store.receive(.networkMonitor(.stop))
store.receive(.messenger(.failure(error as NSError))) {
$0.failure = error.localizedDescription
}
store.send(.authHandler(.stop))
store.send(.messageListener(.stop))
}
func testMessengerStartIsRegisteredFailure() {
......@@ -227,8 +194,6 @@ final class HomeFeatureTests: XCTestCase {
store.environment.bgQueue = .immediate
store.environment.mainQueue = .immediate
store.environment.authHandler.run = { _ in Cancellable {} }
store.environment.messageListener.run = { _ in Cancellable {} }
store.environment.messenger.start.run = { _ in }
store.environment.messenger.isConnected.run = { true }
store.environment.messenger.isListeningForMessages.run = { true }
......@@ -237,15 +202,10 @@ final class HomeFeatureTests: XCTestCase {
store.send(.messenger(.start))
store.receive(.authHandler(.start))
store.receive(.messageListener(.start))
store.receive(.networkMonitor(.stop))
store.receive(.messenger(.failure(error as NSError))) {
$0.failure = error.localizedDescription
}
store.send(.authHandler(.stop))
store.send(.messageListener(.stop))
}
func testMessengerStartLogInFailure() {
......@@ -260,8 +220,6 @@ final class HomeFeatureTests: XCTestCase {
store.environment.bgQueue = .immediate
store.environment.mainQueue = .immediate
store.environment.authHandler.run = { _ in Cancellable {} }
store.environment.messageListener.run = { _ in Cancellable {} }
store.environment.messenger.start.run = { _ in }
store.environment.messenger.isConnected.run = { true }
store.environment.messenger.isListeningForMessages.run = { true }
......@@ -271,15 +229,10 @@ final class HomeFeatureTests: XCTestCase {
store.send(.messenger(.start))
store.receive(.authHandler(.start))
store.receive(.messageListener(.start))
store.receive(.networkMonitor(.stop))
store.receive(.messenger(.failure(error as NSError))) {
$0.failure = error.localizedDescription
}
store.send(.authHandler(.stop))
store.send(.messageListener(.stop))
}
func testNetworkMonitorStart() {
......@@ -551,86 +504,4 @@ final class HomeFeatureTests: XCTestCase {
$0.contacts = nil
}
}
func testAuthCallbacks() {
let store = TestStore(
initialState: HomeState(),
reducer: homeReducer,
environment: .unimplemented
)
var didRunAuthHandler = 0
var didCancelAuthHandler = 0
var authHandlerOnError: [AuthCallbackHandler.OnError] = []
store.environment.mainQueue = .immediate
store.environment.bgQueue = .immediate
store.environment.authHandler.run = { onError in
didRunAuthHandler += 1
authHandlerOnError.append(onError)
return Cancellable { didCancelAuthHandler += 1 }
}
store.send(.authHandler(.start))
XCTAssertNoDifference(didRunAuthHandler, 1)
struct AuthHandlerError: Error { var id: Int }
authHandlerOnError.first?(AuthHandlerError(id: 1))
store.receive(.authHandler(.failure(AuthHandlerError(id: 1) as NSError))) {
$0.authFailure = AuthHandlerError(id: 1).localizedDescription
}
store.send(.authHandler(.failureDismissed)) {
$0.authFailure = nil
}
store.send(.authHandler(.stop))
XCTAssertNoDifference(didCancelAuthHandler, 1)
authHandlerOnError.first?(AuthHandlerError(id: 2))
}
func testMessageListener() {
let store = TestStore(
initialState: HomeState(),
reducer: homeReducer,
environment: .unimplemented
)
var didRunMessageListener = 0
var didCancelMessageListener = 0
var messageListenerOnError: [MessageListenerHandler.OnError] = []
store.environment.mainQueue = .immediate
store.environment.bgQueue = .immediate
store.environment.messageListener.run = { onError in
didRunMessageListener += 1
messageListenerOnError.append(onError)
return Cancellable { didCancelMessageListener += 1 }
}
store.send(.messageListener(.start))
XCTAssertNoDifference(didRunMessageListener, 1)
struct MessageListenerError: Error { var id: Int }
messageListenerOnError.first?(MessageListenerError(id: 1))
store.receive(.messageListener(.failure(MessageListenerError(id: 1) as NSError))) {
$0.messageListenerFailure = MessageListenerError(id: 1).localizedDescription
}
store.send(.messageListener(.failureDismissed)) {
$0.messageListenerFailure = nil
}
store.send(.messageListener(.stop))
XCTAssertNoDifference(didCancelMessageListener, 1)
messageListenerOnError.first?(MessageListenerError(id: 2))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment