Skip to content

Commit

Permalink
Remove push presentation from RestoreFromBackup flow
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbzurovski committed Jun 10, 2024
1 parent b133540 commit c821a4e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,35 @@ extension RestoreProfileFromBackupCoordinator {
}

public var body: some SwiftUI.View {
NavigationStackStore(
store.scope(state: \.path, action: { .child(.path($0)) })
) {
path(for: store.scope(state: \.root, action: { .child(.root($0)) }))
} destination: {
path(for: $0)
}
SelectBackup.View(store: store.selectBackup)
.destinations(with: store)
}
}
}

private extension StoreOf<RestoreProfileFromBackupCoordinator> {
var selectBackup: StoreOf<SelectBackup> {
scope(state: \.selectBackup, action: \.child.selectBackup)
}

var destination: PresentationStoreOf<RestoreProfileFromBackupCoordinator.Destination> {
func scopeState(state: State) -> PresentationState<RestoreProfileFromBackupCoordinator.Destination.State> {
state.$destination
}
return scope(state: scopeState, action: Action.destination)
}
}

@MainActor
private extension View {
func destinations(with store: StoreOf<RestoreProfileFromBackupCoordinator>) -> some View {
let destinationStore = store.destination
return importMnemonics(with: destinationStore)
}

private func path(
for store: StoreOf<RestoreProfileFromBackupCoordinator.Path>
) -> some SwiftUI.View {
SwitchStore(store) { state in
switch state {
case .selectBackup:
CaseLet(
/RestoreProfileFromBackupCoordinator.Path.State.selectBackup,
action: RestoreProfileFromBackupCoordinator.Path.Action.selectBackup,
then: { SelectBackup.View(store: $0) }
)
case .importMnemonicsFlow:
CaseLet(
/RestoreProfileFromBackupCoordinator.Path.State.importMnemonicsFlow,
action: RestoreProfileFromBackupCoordinator.Path.Action.importMnemonicsFlow,
then: { ImportMnemonicsFlowCoordinator.View(store: $0) }
)
}
}
private func importMnemonics(with destinationStore: PresentationStoreOf<RestoreProfileFromBackupCoordinator.Destination>) -> some View {
sheet(store: destinationStore.scope(state: \.importMnemonicsFlow, action: \.importMnemonicsFlow)) {
ImportMnemonicsFlowCoordinator.View(store: $0)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,38 @@ public struct ProfileSelection: Sendable, Hashable {
// MARK: - RestoreProfileFromBackupCoordinator
public struct RestoreProfileFromBackupCoordinator: Sendable, FeatureReducer {
public struct State: Sendable, Hashable {
public var root: Path.State
public var path: StackState<Path.State> = .init()
public var selectBackup = SelectBackup.State()
public var profileSelection: ProfileSelection?

public init() {
self.root = .selectBackup(.init())
}
@PresentationState
public var destination: Destination.State?
}

public struct Path: Sendable, Hashable, Reducer {
public struct Destination: DestinationReducer {
@CasePathable
public enum State: Sendable, Hashable {
case selectBackup(SelectBackup.State)
case importMnemonicsFlow(ImportMnemonicsFlowCoordinator.State)
}

@CasePathable
public enum Action: Sendable, Equatable {
case selectBackup(SelectBackup.Action)
case importMnemonicsFlow(ImportMnemonicsFlowCoordinator.Action)
}

public var body: some ReducerOf<Self> {
Scope(state: \.selectBackup, action: \.selectBackup) {
SelectBackup()
}
Scope(state: \.importMnemonicsFlow, action: \.importMnemonicsFlow) {
ImportMnemonicsFlowCoordinator()
}
}
}

public enum InternalAction: Sendable, Equatable {
case delayedAppendToPath(RestoreProfileFromBackupCoordinator.Path.State)
case startImportMnemonicsFlow(Profile)
}

@CasePathable
public enum ChildAction: Sendable, Equatable {
case root(Path.Action)
case path(StackActionOf<Path>)
case selectBackup(SelectBackup.Action)
}

public enum DelegateAction: Sendable, Equatable {
Expand All @@ -69,44 +61,50 @@ public struct RestoreProfileFromBackupCoordinator: Sendable, FeatureReducer {
public init() {}

public var body: some ReducerOf<Self> {
Scope(state: \.root, action: \.child.root) {
Path()
Scope(state: \.selectBackup, action: \.child.selectBackup) {
SelectBackup()
}

Reduce(core)
.forEach(\.path, action: \.child.path) {
Path()
.ifLet(destinationPath, action: /Action.destination) {
Destination()
}
}

public func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case let .delayedAppendToPath(destination):
state.path.append(destination)
return .none
}
}
private let destinationPath: WritableKeyPath<State, PresentationState<Destination.State>> = \.$destination

public func reduce(into state: inout State, childAction: ChildAction) -> Effect<Action> {
switch childAction {
case let .root(.selectBackup(.delegate(.selectedProfile(profile, containsLegacyP2PLinks)))):
case let .selectBackup(.delegate(.selectedProfile(profile, containsLegacyP2PLinks))):
state.profileSelection = .init(profile: profile, containsP2PLinks: containsLegacyP2PLinks)

return .run { send in
try? await clock.sleep(for: .milliseconds(300))
_ = await radixConnectClient.loadP2PLinksAndConnectAll()
await send(.internal(.delayedAppendToPath(
.importMnemonicsFlow(.init(context: .fromOnboarding(profile: profile)))
)))
await send(.internal(.startImportMnemonicsFlow(profile)))
}

case .root(.selectBackup(.delegate(.backToStartOfOnboarding))):
case .selectBackup(.delegate(.backToStartOfOnboarding)):
return .send(.delegate(.backToStartOfOnboarding))

case .root(.selectBackup(.delegate(.profileCreatedFromImportedBDFS))):
case .selectBackup(.delegate(.profileCreatedFromImportedBDFS)):
return .send(.delegate(.profileCreatedFromImportedBDFS))

case let .path(.element(_, action: .importMnemonicsFlow(.delegate(.finishedImportingMnemonics(skipList, _, notYetSavedNewMainBDFS))))):
default:
return .none
}
}

public func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case let .startImportMnemonicsFlow(profile):
state.destination = .importMnemonicsFlow(.init(context: .fromOnboarding(profile: profile)))
return .none
}
}

public func reduce(into state: inout State, presentedAction: Destination.Action) -> Effect<Action> {
switch presentedAction {
case let .importMnemonicsFlow(.delegate(.finishedImportingMnemonics(skipList, _, notYetSavedNewMainBDFS))):
loggerGlobal.notice("Starting import snapshot process...")
guard let profileSelection = state.profileSelection else {
preconditionFailure("Expected to have a profile")
Expand All @@ -130,8 +128,8 @@ public struct RestoreProfileFromBackupCoordinator: Sendable, FeatureReducer {
errorQueue.schedule(error)
}

case let .path(.element(_, action: .importMnemonicsFlow(.delegate(.finishedEarly(didFail))))):
state.path.removeLast()
case let .importMnemonicsFlow(.delegate(.finishedEarly(didFail))):
state.destination = nil
return .run { send in
await radixConnectClient.disconnectAll()
if didFail {
Expand Down

0 comments on commit c821a4e

Please sign in to comment.