Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Improve example app (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
reddavis authored May 22, 2022
1 parent 99455de commit 2756892
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
10 changes: 7 additions & 3 deletions Example/AppStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ fileprivate let reducer: Reducer<AppState, AppEvent, AppEnvironment> = Reducer {
return Effect(.increment)
.delay(for: 2)
.eraseToEffect()
case .toggleIsPresentingSheet:
state.isPresentingSheet.toggle()
case .presentSheet:
state.isPresentingSheet = true
return .none
case .dismissSheet:
state.isPresentingSheet = false
return .none
case .details:
return .none
Expand Down Expand Up @@ -69,7 +72,8 @@ enum AppEvent {
case increment
case decrement
case incrementWithDelayViaEffect
case toggleIsPresentingSheet
case presentSheet
case dismissSheet
case details(DetailsEvent)
}

Expand Down
13 changes: 7 additions & 6 deletions Example/Details/Details.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import RedUx


struct DetailsState: Equatable {
var isPresentingActionSheet: Bool = false
}



// MARK: Reducer

let detailsReducer: Reducer<DetailsState, DetailsEvent, AppEnvironment> = Reducer { state, event, environment in
switch event {
case .toggleActionSheet:
state.isPresentingActionSheet.toggle()
case .presentActionSheet:
state.isPresentingActionSheet = true
return .none
case .dismissActionSheet:
state.isPresentingActionSheet = false
return .none
}
}
Expand All @@ -22,5 +22,6 @@ let detailsReducer: Reducer<DetailsState, DetailsEvent, AppEnvironment> = Reduce
// MARK: Event

enum DetailsEvent {
case toggleActionSheet
case presentActionSheet
case dismissActionSheet
}
10 changes: 6 additions & 4 deletions Example/Details/DetailsScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ struct DetailsScreen: View, RedUxable {

var body: some View {
VStack(alignment: .center) {
Button("Toggle dialog") {
self.viewModel.send(.toggleActionSheet)
Button("Present dialog") {
self.viewModel.send(.presentActionSheet)
}
.buttonStyle(.bordered)
}
.confirmationDialog(
"",
isPresented: self.viewModel.binding(
value: \.isPresentingActionSheet,
event: .toggleActionSheet
event: .dismissActionSheet
),
actions: {
Button("A") { }
Button("A") {
self.viewModel.send(.dismissActionSheet)
}
}
)
}
Expand Down
7 changes: 3 additions & 4 deletions Example/Root/RootScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ struct RootScreen: View, RedUxable {
}

Button("Present sheet") {
self.viewModel.send(.toggleIsPresentingSheet)
self.viewModel.send(.presentSheet)
}
.buttonStyle(.bordered)
}
.sheet(
isPresented: self.viewModel.binding(
value: \.isPresentingSheet,
event: .toggleIsPresentingSheet
value: \.isPresentingSheet
),
onDismiss: nil,
onDismiss: { self.viewModel.send(.dismissSheet) },
content: {
DetailsScreen.make(
store: self.store.scope(
Expand Down
4 changes: 2 additions & 2 deletions ExampleTests/RootScreenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import XCTest
class RootScreenTests: XCTestCase {
var store: AppStore!

override func setUpWithError() throws {
self.store = .make()
override func setUp() async throws {
self.store = await .make()
}

// MARK: Test
Expand Down
10 changes: 4 additions & 6 deletions RedUx/Source/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ public final class Store<State, Event, Environment> {

guard let effect = effect else { return }

Task.detached(priority: .high) {
Task(priority: .high) {
guard !effect.isCancellation else {
await self.effectManager.removeTask(effect.id)
return
}

let task = effect.sink(
receiveValue: { [weak self] in await self?.send($0) },
receiveValue: { [weak self] in self?.send($0) },
receiveCompletion: { [weak self] _ in
await self?.effectManager.removeTask(effect.id)
}
Expand Down Expand Up @@ -141,17 +141,15 @@ extension Store {
let scopedStore = Store<ScopedState, ScopedEvent, ScopedEnvironment>(
state: toScopedState(self.state),
reducer: .init { state, event, _ in
state = toScopedState(self.state)
self.send(fromScopedEvent(event))
state = toScopedState(self.state)
return .none
},
environment: toScopedEnvironment(self.environment)
)

// Propagate changes to state to scoped store.
scopedStore.parentStatePropagationTask = Just(self.state)
.eraseToAnyAsyncSequenceable()
.chain(with: self.stateSequence)
scopedStore.parentStatePropagationTask = self.stateSequence
.sink(priority: .high) { [weak scopedStore] in
scopedStore?.state = toScopedState($0)
}
Expand Down

0 comments on commit 2756892

Please sign in to comment.