-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WorkflowSwiftUI Perception #276
Draft
watt
wants to merge
20
commits into
tomb/swiftui-testbed-workflowswiftui
Choose a base branch
from
awatt/swiftui-perception
base: tomb/swiftui-testbed-workflowswiftui
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6b24cfd
remove ObservedObject property wrapper
square-tomb aa7cfa7
implement Store
square-tomb 2be92b5
introduce new SwiftUIScreen protocol
square-tomb 647c261
use BindableStore to generate bindings
square-tomb a5c285e
eliminate runtime warnings in Binding getters
square-tomb 32668e4
send actions to store
square-tomb 46b4cb9
pin to cocoapods 1.15.0
watt ab29b69
bindings
watt 6519251
update bundler
watt fa8ab45
counters demo start
watt 6b29baa
counter demos working
watt 31b4243
cached bindings
watt 1d457b1
composable models
watt fb773a8
cocoapods spm dependency via fork
watt bb94d53
separate action from state
watt efbb2fa
centralize binding cache
watt 7dfc458
convert bindings to subscript into root binding
watt f12427d
restore binding cache with invalidation on set
watt c41e83b
reset state
watt 39ede5a
cleanup
watt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'cocoapods-trunk', '>=1.6.0' | ||
gem 'cocoapods' | ||
gem 'cocoapods', git: 'https://github.com/watt/cocoapods', branch: 'podspec-spm' | ||
gem 'cocoapods-core', git: 'https://github.com/watt/cocoapods-core', branch: 'podspec-spm' | ||
|
||
gem 'cocoapods-generate' |
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
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,71 @@ | ||
import SwiftUI | ||
import MarketUI | ||
import MarketWorkflowUI | ||
import ViewEnvironment | ||
import WorkflowUI | ||
import Perception | ||
|
||
struct CounterScreen: SwiftUIScreen, Screen { | ||
var model: Model | ||
|
||
typealias State = CounterWorkflow.State | ||
typealias Action = CounterWorkflow.Action | ||
typealias Model = StoreModel<State, Action> | ||
|
||
static func makeView(store: Store<Model>) -> some View { | ||
CounterScreenView(store: store) | ||
} | ||
} | ||
|
||
extension CounterScreen: MarketBackStackContentScreen { | ||
func backStackItem(in environment: ViewEnvironment) -> MarketUI.MarketNavigationItem { | ||
MarketNavigationItem( | ||
title: .text(.init(regular: "Counters")), | ||
backButton: .automatic() | ||
) | ||
} | ||
|
||
var backStackIdentifier: AnyHashable? { nil } | ||
} | ||
|
||
struct CounterScreenView: View { | ||
typealias Model = StoreModel<CounterWorkflow.State, CounterWorkflow.Action> | ||
|
||
let store: Store<Model> | ||
|
||
var body: some View { | ||
WithPerceptionTracking { | ||
let _ = Self._printChanges() | ||
CounterView(store: store, index: 0) | ||
} | ||
} | ||
} | ||
|
||
struct CounterView: View { | ||
typealias Model = StoreModel<CounterWorkflow.State, CounterWorkflow.Action> | ||
let store: Store<Model> | ||
let index: Int | ||
|
||
var body: some View { | ||
WithPerceptionTracking { | ||
let _ = print("Evaluating CounterView[\(index)].body") | ||
HStack { | ||
Button { | ||
store.send(.decrement) | ||
} label: { | ||
Image(systemName: "minus") | ||
} | ||
|
||
Text("\(store.count)") | ||
.monospacedDigit() | ||
|
||
Button { | ||
store.send(.increment) | ||
} label: { | ||
Image(systemName: "plus") | ||
} | ||
} | ||
.padding() | ||
} | ||
} | ||
} |
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,57 @@ | ||
import Workflow | ||
import ComposableArchitecture | ||
|
||
struct CounterWorkflow: Workflow { | ||
|
||
var resetToken: ResetToken | ||
|
||
@ObservableState | ||
struct State { | ||
var count = 0 | ||
} | ||
|
||
enum Action: WorkflowAction { | ||
typealias WorkflowType = CounterWorkflow | ||
|
||
case increment | ||
case decrement | ||
|
||
func apply(toState state: inout CounterWorkflow.State) -> CounterWorkflow.Output? { | ||
switch self { | ||
case .increment: | ||
state.count += 1 | ||
case .decrement: | ||
state.count -= 1 | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
typealias Output = Never | ||
|
||
func makeInitialState() -> State { | ||
State(count: resetToken.initialValue) | ||
} | ||
|
||
func workflowDidChange(from previousWorkflow: CounterWorkflow, state: inout State) { | ||
if resetToken != previousWorkflow.resetToken { | ||
// this state reset will totally invalidate the body even if `count` doesn't change | ||
state = State(count: resetToken.initialValue) | ||
} | ||
} | ||
|
||
typealias Rendering = StoreModel<State, Action> | ||
typealias Model = StoreModel<State, Action> | ||
|
||
func render(state: State, context: RenderContext<CounterWorkflow>) -> StoreModel<State, Action> { | ||
// print("CounterWorkflow.render") | ||
return context.makeStoreModel(state: state) | ||
} | ||
} | ||
|
||
extension CounterWorkflow { | ||
struct ResetToken: Equatable { | ||
let id = UUID() | ||
var initialValue = 0 | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume eventually, folks won't need to do this – we'll lift it a layer up?