Skip to content

Commit

Permalink
missing commits
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Apr 8, 2020
1 parent 9f8e08a commit d6a45cc
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 129 deletions.
136 changes: 12 additions & 124 deletions 0097-adaptive-state-management-pt4/PrimeTime/Counter/Counter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public typealias CounterState = (
alertNthPrime: PrimeAlert?,
count: Int,
isNthPrimeRequestInFlight: Bool,
isPrimeModalShown: Bool
// isPrimeModalShown: Bool
isPrimeDetailShown: Bool
)

public enum CounterAction: Equatable {
Expand All @@ -20,7 +21,8 @@ public enum CounterAction: Equatable {
case nthPrimeResponse(n: Int, prime: Int?)
case alertDismissButtonTapped
case isPrimeButtonTapped
case primeModalDismissed
// case primeModalDismissed
case primeDetailDismissed
// case doubleTap
}

Expand Down Expand Up @@ -60,11 +62,11 @@ public func counterReducer(
return []

case .isPrimeButtonTapped:
state.isPrimeModalShown = true
state.isPrimeDetailShown = true
return []

case .primeModalDismissed:
state.isPrimeModalShown = false
case .primeDetailDismissed:
state.isPrimeDetailShown = false
return []

// case .doubleTap:
Expand Down Expand Up @@ -100,7 +102,7 @@ public struct CounterFeatureState: Equatable {
public var favoritePrimes: [Int]
// public var isNthPrimeButtonDisabled: Bool
public var isNthPrimeRequestInFlight: Bool
public var isPrimeModalShown: Bool
public var isPrimeDetailShown: Bool

// public var isLoadingIndicatorHidden: Bool

Expand All @@ -109,18 +111,18 @@ public struct CounterFeatureState: Equatable {
count: Int = 0,
favoritePrimes: [Int] = [],
isNthPrimeRequestInFlight: Bool = false,
isPrimeModalShown: Bool = false
isPrimeDetailShown: Bool = false
) {
self.alertNthPrime = alertNthPrime
self.count = count
self.favoritePrimes = favoritePrimes
self.isNthPrimeRequestInFlight = isNthPrimeRequestInFlight
self.isPrimeModalShown = isPrimeModalShown
self.isPrimeDetailShown = isPrimeDetailShown
}

var counter: CounterState {
get { (self.alertNthPrime, self.count, self.isNthPrimeRequestInFlight, self.isPrimeModalShown) }
set { (self.alertNthPrime, self.count, self.isNthPrimeRequestInFlight, self.isPrimeModalShown) = newValue }
get { (self.alertNthPrime, self.count, self.isNthPrimeRequestInFlight, self.isPrimeDetailShown) }
set { (self.alertNthPrime, self.count, self.isNthPrimeRequestInFlight, self.isPrimeDetailShown) = newValue }
}

var primeModal: PrimeModalState {
Expand All @@ -133,117 +135,3 @@ public enum CounterFeatureAction: Equatable {
case counter(CounterAction)
case primeModal(PrimeModalAction)
}

public struct CounterView: View {
struct State: Equatable {
let alertNthPrime: PrimeAlert?
let count: Int
let isNthPrimeButtonDisabled: Bool
let isPrimeModalShown: Bool
let isIncrementButtonDisabled: Bool
let isDecrementButtonDisabled: Bool
let nthPrimeButtonTitle: String
}
public enum Action {
case decrTapped
case incrTapped
case nthPrimeButtonTapped
case alertDismissButtonTapped
case isPrimeButtonTapped
case primeModalDismissed
case doubleTap
}

let store: Store<CounterFeatureState, CounterFeatureAction>
@ObservedObject var viewStore: ViewStore<State, Action>

public init(store: Store<CounterFeatureState, CounterFeatureAction>) {
print("CounterView.init")
self.store = store
self.viewStore = self.store
.scope(
value: State.init,
action: CounterFeatureAction.init
)
.view
}

public var body: some View {
print("CounterView.body")
return VStack {
HStack {
Button("-") { self.viewStore.send(.decrTapped) }
.disabled(self.viewStore.value.isDecrementButtonDisabled)
Text("\(self.viewStore.value.count)")
Button("+") { self.viewStore.send(.incrTapped) }
.disabled(self.viewStore.value.isIncrementButtonDisabled)
}
Button("Is this prime?") { self.viewStore.send(.isPrimeButtonTapped) }
Button(self.viewStore.value.nthPrimeButtonTitle) {
self.viewStore.send(.nthPrimeButtonTapped)
}
.disabled(self.viewStore.value.isNthPrimeButtonDisabled)
}
.font(.title)
.navigationBarTitle("Counter demo")
.sheet(
isPresented: .constant(self.viewStore.value.isPrimeModalShown),
onDismiss: { self.viewStore.send(.primeModalDismissed) }
) {
IsPrimeModalView(
store: self.store.scope(
value: { ($0.count, $0.favoritePrimes) },
action: { .primeModal($0) }
)
)
}
.alert(
item: .constant(self.viewStore.value.alertNthPrime)
) { alert in
Alert(
title: Text(alert.title),
dismissButton: .default(Text("Ok")) {
self.viewStore.send(.alertDismissButtonTapped)
}
)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.white)
.onTapGesture(count: 2) {
self.viewStore.send(.doubleTap)
}
}
}

extension CounterView.State {
init(counterFeatureState: CounterFeatureState) {
self.alertNthPrime = counterFeatureState.alertNthPrime
self.count = counterFeatureState.count
self.isNthPrimeButtonDisabled = counterFeatureState.isNthPrimeRequestInFlight
self.isPrimeModalShown = counterFeatureState.isPrimeModalShown
self.isIncrementButtonDisabled = counterFeatureState.isNthPrimeRequestInFlight
self.isDecrementButtonDisabled = counterFeatureState.isNthPrimeRequestInFlight
self.nthPrimeButtonTitle = "What is the \(ordinal(counterFeatureState.count)) prime?"
}
}

extension CounterFeatureAction {
init(action: CounterView.Action) {
switch action {
case .decrTapped:
self = .counter(.decrTapped)
case .incrTapped:
self = .counter(.incrTapped)
case .nthPrimeButtonTapped:
self = .counter(.requestNthPrime)
case .alertDismissButtonTapped:
self = .counter(.alertDismissButtonTapped)
case .isPrimeButtonTapped:
self = .counter(.isPrimeButtonTapped)
case .primeModalDismissed:
self = .counter(.primeModalDismissed)
case .doubleTap:
self = .counter(.requestNthPrime)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#if os(iOS)
import Combine
import ComposableArchitecture
import PrimeAlert
import PrimeModal
import SwiftUI

public struct CounterView: View {
struct State: Equatable {
let alertNthPrime: PrimeAlert?
let count: Int
let isNthPrimeButtonDisabled: Bool
let isPrimeModalShown: Bool
let isIncrementButtonDisabled: Bool
let isDecrementButtonDisabled: Bool
let nthPrimeButtonTitle: String
}
public enum Action {
case decrTapped
case incrTapped
case nthPrimeButtonTapped
case alertDismissButtonTapped
case isPrimeButtonTapped
case primeModalDismissed
case doubleTap
}

let store: Store<CounterFeatureState, CounterFeatureAction>
@ObservedObject var viewStore: ViewStore<State, Action>

public init(store: Store<CounterFeatureState, CounterFeatureAction>) {
print("CounterView.init")
self.store = store
self.viewStore = self.store
.scope(
value: State.init,
action: CounterFeatureAction.init
)
.view
}

public var body: some View {
print("CounterView.body")
return VStack {
HStack {
Button("-") { self.viewStore.send(.decrTapped) }
.disabled(self.viewStore.value.isDecrementButtonDisabled)
Text("\(self.viewStore.value.count)")
Button("+") { self.viewStore.send(.incrTapped) }
.disabled(self.viewStore.value.isIncrementButtonDisabled)
}
Button("Is this prime?") { self.viewStore.send(.isPrimeButtonTapped) }
Button(self.viewStore.value.nthPrimeButtonTitle) {
self.viewStore.send(.nthPrimeButtonTapped)
}
.disabled(self.viewStore.value.isNthPrimeButtonDisabled)
}
.font(.title)
.navigationBarTitle("Counter demo")
.sheet(
isPresented: .constant(self.viewStore.value.isPrimeModalShown),
onDismiss: { self.viewStore.send(.primeModalDismissed) }
) {
IsPrimeModalView(
store: self.store.scope(
value: { ($0.count, $0.favoritePrimes) },
action: { .primeModal($0) }
)
)
}
.alert(
item: .constant(self.viewStore.value.alertNthPrime)
) { alert in
Alert(
title: Text(alert.title),
dismissButton: .default(Text("Ok")) {
self.viewStore.send(.alertDismissButtonTapped)
}
)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.white)
.onTapGesture(count: 2) {
self.viewStore.send(.doubleTap)
}
}
}

extension CounterView.State {
init(counterFeatureState: CounterFeatureState) {
self.alertNthPrime = counterFeatureState.alertNthPrime
self.count = counterFeatureState.count
self.isNthPrimeButtonDisabled = counterFeatureState.isNthPrimeRequestInFlight
self.isPrimeModalShown = counterFeatureState.isPrimeDetailShown
self.isIncrementButtonDisabled = counterFeatureState.isNthPrimeRequestInFlight
self.isDecrementButtonDisabled = counterFeatureState.isNthPrimeRequestInFlight
self.nthPrimeButtonTitle = "What is the \(ordinal(counterFeatureState.count)) prime?"
}
}

extension CounterFeatureAction {
init(action: CounterView.Action) {
switch action {
case .decrTapped:
self = .counter(.decrTapped)
case .incrTapped:
self = .counter(.incrTapped)
case .nthPrimeButtonTapped:
self = .counter(.requestNthPrime)
case .alertDismissButtonTapped:
self = .counter(.alertDismissButtonTapped)
case .isPrimeButtonTapped:
self = .counter(.isPrimeButtonTapped)
case .primeModalDismissed:
self = .counter(.primeDetailDismissed)
case .doubleTap:
self = .counter(.requestNthPrime)
}
}
}
#endif
Loading

0 comments on commit d6a45cc

Please sign in to comment.