-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c051335
commit 031f0e4
Showing
41 changed files
with
3,004 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...tate-management-wtp/PrimeTime/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
0075-modular-state-management-wtp/PrimeTime/ComposableArchitecture/ComposableArchitecture.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// ComposableArchitecture.h | ||
// ComposableArchitecture | ||
// | ||
// Created by Stephen Celis on 9/8/19. | ||
// Copyright © 2019 Point-Free. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
//! Project version number for ComposableArchitecture. | ||
FOUNDATION_EXPORT double ComposableArchitectureVersionNumber; | ||
|
||
//! Project version string for ComposableArchitecture. | ||
FOUNDATION_EXPORT const unsigned char ComposableArchitectureVersionString[]; | ||
|
||
// In this header, you should import all the public headers of your framework using statements like #import <ComposableArchitecture/PublicHeader.h> | ||
|
||
|
87 changes: 87 additions & 0 deletions
87
...odular-state-management-wtp/PrimeTime/ComposableArchitecture/ComposableArchitecture.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import Combine | ||
import SwiftUI | ||
|
||
public final class Store<Value, Action>: ObservableObject { | ||
private let reducer: (inout Value, Action) -> Void | ||
@Published public private(set) var value: Value | ||
private var cancellable: Cancellable? | ||
|
||
public init(initialValue: Value, reducer: @escaping (inout Value, Action) -> Void) { | ||
// self.objectWillChange | ||
// self.$value.sink(receiveValue: <#T##((Value) -> Void)##((Value) -> Void)##(Value) -> Void#>) | ||
self.reducer = reducer | ||
self.value = initialValue | ||
} | ||
|
||
public func send(_ action: Action) { | ||
self.reducer(&self.value, action) | ||
} | ||
|
||
// ((Value) -> LocalValue) -> (Store<Value ,_>) -> Store<LocalValue, _> | ||
// ((A) -> B) -> (Store<A ,_>) -> Store<B, _> | ||
// map: ((A) -> B) -> (F<A>) -> F<B> | ||
|
||
public func view<LocalValue, LocalAction>( | ||
value toLocalValue: @escaping (Value) -> LocalValue, | ||
action toGlobalAction: @escaping (LocalAction) -> Action | ||
) -> Store<LocalValue, LocalAction> { | ||
let localStore = Store<LocalValue, LocalAction>( | ||
initialValue: toLocalValue(self.value), | ||
reducer: { localValue, localAction in | ||
self.send(toGlobalAction(localAction)) | ||
localValue = toLocalValue(self.value) | ||
} | ||
) | ||
localStore.cancellable = self.$value.sink { [weak localStore] newValue in | ||
localStore?.value = toLocalValue(newValue) | ||
} | ||
return localStore | ||
} | ||
|
||
// ((LocalAction) -> Action) -> (Store<_, Action>) -> Store<_, LocalAction> | ||
// ((B) -> A) -> (Store<_, A>) -> Store<_, B> | ||
// pullback: ((B) -> A) -> (F<A>) -> F<B> | ||
} | ||
|
||
|
||
func transform<A, B, Action>( | ||
_ reducer: (A, Action) -> A, | ||
_ f: (A) -> B | ||
) -> (B, Action) -> B { | ||
fatalError() | ||
} | ||
|
||
|
||
public func combine<Value, Action>( | ||
_ reducers: (inout Value, Action) -> Void... | ||
) -> (inout Value, Action) -> Void { | ||
return { value, action in | ||
for reducer in reducers { | ||
reducer(&value, action) | ||
} | ||
} | ||
} | ||
|
||
public func pullback<LocalValue, GlobalValue, LocalAction, GlobalAction>( | ||
_ reducer: @escaping (inout LocalValue, LocalAction) -> Void, | ||
value: WritableKeyPath<GlobalValue, LocalValue>, | ||
action: WritableKeyPath<GlobalAction, LocalAction?> | ||
) -> (inout GlobalValue, GlobalAction) -> Void { | ||
return { globalValue, globalAction in | ||
guard let localAction = globalAction[keyPath: action] else { return } | ||
reducer(&globalValue[keyPath: value], localAction) | ||
} | ||
} | ||
|
||
public func logging<Value, Action>( | ||
_ reducer: @escaping (inout Value, Action) -> Void | ||
) -> (inout Value, Action) -> Void { | ||
return { value, action in | ||
reducer(&value, action) | ||
print("Action: \(action)") | ||
print("Value:") | ||
dump(value) | ||
print("---") | ||
} | ||
} | ||
|
22 changes: 22 additions & 0 deletions
22
0075-modular-state-management-wtp/PrimeTime/ComposableArchitecture/Info.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>$(DEVELOPMENT_LANGUAGE)</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>$(CURRENT_PROJECT_VERSION)</string> | ||
</dict> | ||
</plist> |
34 changes: 34 additions & 0 deletions
34
...te-management-wtp/PrimeTime/ComposableArchitectureTests/ComposableArchitectureTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// ComposableArchitectureTests.swift | ||
// ComposableArchitectureTests | ||
// | ||
// Created by Stephen Celis on 9/8/19. | ||
// Copyright © 2019 Point-Free. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import ComposableArchitecture | ||
|
||
class ComposableArchitectureTests: XCTestCase { | ||
|
||
override func setUp() { | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
} | ||
|
||
override func tearDown() { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
} | ||
|
||
func testExample() { | ||
// This is an example of a functional test case. | ||
// Use XCTAssert and related functions to verify your tests produce the correct results. | ||
} | ||
|
||
func testPerformanceExample() { | ||
// This is an example of a performance test case. | ||
self.measure { | ||
// Put the code you want to measure the time of here. | ||
} | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
0075-modular-state-management-wtp/PrimeTime/ComposableArchitectureTests/Info.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>$(DEVELOPMENT_LANGUAGE)</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>1</string> | ||
</dict> | ||
</plist> |
19 changes: 19 additions & 0 deletions
19
0075-modular-state-management-wtp/PrimeTime/Counter/Counter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Counter.h | ||
// Counter | ||
// | ||
// Created by Stephen Celis on 9/8/19. | ||
// Copyright © 2019 Point-Free. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
//! Project version number for Counter. | ||
FOUNDATION_EXPORT double CounterVersionNumber; | ||
|
||
//! Project version string for Counter. | ||
FOUNDATION_EXPORT const unsigned char CounterVersionString[]; | ||
|
||
// In this header, you should import all the public headers of your framework using statements like #import <Counter/PublicHeader.h> | ||
|
||
|
133 changes: 133 additions & 0 deletions
133
0075-modular-state-management-wtp/PrimeTime/Counter/Counter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import ComposableArchitecture | ||
import PrimeModal | ||
import SwiftUI | ||
|
||
public enum CounterAction { | ||
case decrTapped | ||
case incrTapped | ||
} | ||
|
||
public func counterReducer(state: inout Int, action: CounterAction) { | ||
switch action { | ||
case .decrTapped: | ||
state -= 1 | ||
|
||
case .incrTapped: | ||
state += 1 | ||
} | ||
} | ||
|
||
public let counterViewReducer = combine( | ||
pullback(counterReducer, value: \CounterViewState.count, action: \CounterViewAction.counter), | ||
pullback(primeModalReducer, value: \.self, action: \.primeModal) | ||
) | ||
|
||
struct PrimeAlert: Identifiable { | ||
let prime: Int | ||
var id: Int { self.prime } | ||
} | ||
|
||
public typealias CounterViewState = (count: Int, favoritePrimes: [Int]) | ||
|
||
public enum CounterViewAction { | ||
case counter(CounterAction) | ||
case primeModal(PrimeModalAction) | ||
|
||
var counter: CounterAction? { | ||
get { | ||
guard case let .counter(value) = self else { return nil } | ||
return value | ||
} | ||
set { | ||
guard case .counter = self, let newValue = newValue else { return } | ||
self = .counter(newValue) | ||
} | ||
} | ||
|
||
var primeModal: PrimeModalAction? { | ||
get { | ||
guard case let .primeModal(value) = self else { return nil } | ||
return value | ||
} | ||
set { | ||
guard case .primeModal = self, let newValue = newValue else { return } | ||
self = .primeModal(newValue) | ||
} | ||
} | ||
|
||
} | ||
|
||
public struct CounterView: View { | ||
@ObservedObject var store: Store<CounterViewState, CounterViewAction> | ||
@State var isPrimeModalShown = false | ||
@State var alertNthPrime: PrimeAlert? | ||
@State var isNthPrimeButtonDisabled = false | ||
|
||
public init(store: Store<CounterViewState, CounterViewAction>) { | ||
self.store = store | ||
} | ||
|
||
public var body: some View { | ||
VStack { | ||
HStack { | ||
Button("-") { self.store.send(.counter(.decrTapped)) } | ||
Text("\(self.store.value.count)") | ||
Button("+") { self.store.send(.counter(.incrTapped)) } | ||
} | ||
Button("Is this prime?") { self.isPrimeModalShown = true } | ||
Button( | ||
"What is the \(ordinal(self.store.value.count)) prime?", | ||
action: self.nthPrimeButtonAction | ||
) | ||
.disabled(self.isNthPrimeButtonDisabled) | ||
} | ||
.font(.title) | ||
.navigationBarTitle("Counter demo") | ||
.sheet(isPresented: self.$isPrimeModalShown) { | ||
IsPrimeModalView( | ||
store: self.store | ||
.view( | ||
value: { ($0.count, $0.favoritePrimes) }, | ||
action: { .primeModal($0) } | ||
) | ||
) | ||
} | ||
.alert(item: self.$alertNthPrime) { alert in | ||
Alert( | ||
title: Text("The \(ordinal(self.store.value.count)) prime is \(alert.prime)"), | ||
dismissButton: .default(Text("Ok")) | ||
) | ||
} | ||
} | ||
|
||
func nthPrimeButtonAction() { | ||
self.isNthPrimeButtonDisabled = true | ||
nthPrime(self.store.value.count) { prime in | ||
self.alertNthPrime = prime.map(PrimeAlert.init(prime:)) | ||
self.isNthPrimeButtonDisabled = false | ||
} | ||
} | ||
} | ||
|
||
func nthPrime(_ n: Int, callback: @escaping (Int?) -> Void) -> Void { | ||
wolframAlpha(query: "prime \(n)") { result in | ||
callback( | ||
result | ||
.flatMap { | ||
$0.queryresult | ||
.pods | ||
.first(where: { $0.primary == .some(true) })? | ||
.subpods | ||
.first? | ||
.plaintext | ||
} | ||
.flatMap(Int.init) | ||
) | ||
} | ||
} | ||
|
||
func ordinal(_ n: Int) -> String { | ||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .ordinal | ||
return formatter.string(for: n) ?? "" | ||
} |
22 changes: 22 additions & 0 deletions
22
0075-modular-state-management-wtp/PrimeTime/Counter/Info.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>$(DEVELOPMENT_LANGUAGE)</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>$(CURRENT_PROJECT_VERSION)</string> | ||
</dict> | ||
</plist> |
Oops, something went wrong.