-
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
3883fea
commit ea395be
Showing
16 changed files
with
909 additions
and
0 deletions.
There are no files selected for viewing
480 changes: 480 additions & 0 deletions
480
...observation-pt1/ObservationExplorations/ObservationExplorations.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
...plorations/ObservationExplorations.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...servationExplorations.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.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,8 @@ | ||
<?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>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
106 changes: 106 additions & 0 deletions
106
...ObservationExplorations.xcodeproj/xcshareddata/xcschemes/ObservationExplorations.xcscheme
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,106 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1500" | ||
version = "1.7"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "2AD14E1B2ACDC3EC0015EB20" | ||
BuildableName = "ObservationExplorations.app" | ||
BlueprintName = "ObservationExplorations" | ||
ReferencedContainer = "container:ObservationExplorations.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<TestPlans> | ||
<TestPlanReference | ||
reference = "container:ObservationExplorationsTests/ObservationExplorations.xctestplan" | ||
default = "YES"> | ||
</TestPlanReference> | ||
</TestPlans> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO" | ||
parallelizable = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "2AD14E2B2ACDC3ED0015EB20" | ||
BuildableName = "ObservationExplorationsTests.xctest" | ||
BlueprintName = "ObservationExplorationsTests" | ||
ReferencedContainer = "container:ObservationExplorations.xcodeproj"> | ||
</BuildableReference> | ||
</TestableReference> | ||
<TestableReference | ||
skipped = "NO" | ||
parallelizable = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "2AD14E352ACDC3ED0015EB20" | ||
BuildableName = "ObservationExplorationsUITests.xctest" | ||
BlueprintName = "ObservationExplorationsUITests" | ||
ReferencedContainer = "container:ObservationExplorations.xcodeproj"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
<BuildableProductRunnable | ||
runnableDebuggingMode = "0"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "2AD14E1B2ACDC3EC0015EB20" | ||
BuildableName = "ObservationExplorations.app" | ||
BlueprintName = "ObservationExplorations" | ||
ReferencedContainer = "container:ObservationExplorations.xcodeproj"> | ||
</BuildableReference> | ||
</BuildableProductRunnable> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<BuildableProductRunnable | ||
runnableDebuggingMode = "0"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "2AD14E1B2ACDC3EC0015EB20" | ||
BuildableName = "ObservationExplorations.app" | ||
BlueprintName = "ObservationExplorations" | ||
ReferencedContainer = "container:ObservationExplorations.xcodeproj"> | ||
</BuildableReference> | ||
</BuildableProductRunnable> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
58 changes: 58 additions & 0 deletions
58
0252-observation-pt1/ObservationExplorations/ObservationExplorations/AppView.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,58 @@ | ||
import Combine | ||
import SwiftUI | ||
|
||
class AppModel: ObservableObject { | ||
@Published var tab1 = CounterModel() | ||
@Published var tab2 = CounterModel() | ||
@Published var tab3 = CounterModel() | ||
|
||
private var cancellables: Set<AnyCancellable> = [] | ||
|
||
init() { | ||
self.tab1.$count.sink { [weak self] _ in | ||
self?.objectWillChange.send() | ||
} | ||
.store(in: &self.cancellables) | ||
self.tab2.objectWillChange.sink { [weak self] in | ||
self?.objectWillChange.send() | ||
} | ||
.store(in: &self.cancellables) | ||
self.tab3.objectWillChange.sink { [weak self] in | ||
self?.objectWillChange.send() | ||
} | ||
.store(in: &self.cancellables) | ||
} | ||
} | ||
|
||
struct AppView: View { | ||
@ObservedObject var model: AppModel | ||
|
||
var body: some View { | ||
let _ = Self._printChanges() | ||
|
||
TabView { | ||
NavigationStack { | ||
CounterView(model: self.model.tab1) | ||
.navigationTitle(Text("Counter 1")) | ||
} | ||
.badge(self.model.tab1.count) | ||
.tabItem { Text("Counter 1") } | ||
|
||
NavigationStack { | ||
CounterView(model: self.model.tab2) | ||
.navigationTitle(Text("Counter 2")) | ||
} | ||
.tabItem { Text("Counter 2") } | ||
|
||
NavigationStack { | ||
CounterView(model: self.model.tab3) | ||
.navigationTitle(Text("Counter 3")) | ||
} | ||
.tabItem { Text("Counter 3") } | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
AppView(model: AppModel()) | ||
} |
11 changes: 11 additions & 0 deletions
11
...onExplorations/ObservationExplorations/Assets.xcassets/AccentColor.colorset/Contents.json
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,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...tionExplorations/ObservationExplorations/Assets.xcassets/AppIcon.appiconset/Contents.json
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,13 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...rvation-pt1/ObservationExplorations/ObservationExplorations/Assets.xcassets/Contents.json
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,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
0252-observation-pt1/ObservationExplorations/ObservationExplorations/CounterView.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,74 @@ | ||
import SwiftUI | ||
|
||
class CounterModel: ObservableObject { | ||
@Published var count = 0 | ||
@Published var secondsElapsed = 0 | ||
@Published private var timerTask: Task<Void, Error>? | ||
var isTimerOn: Bool { | ||
self.timerTask != nil | ||
} | ||
|
||
func decrementButtonTapped() { | ||
self.count -= 1 | ||
} | ||
func incrementButtonTapped() { | ||
self.count += 1 | ||
} | ||
|
||
func startTimerButtonTapped() { | ||
self.timerTask?.cancel() | ||
self.timerTask = Task { @MainActor in | ||
while true { | ||
try await Task.sleep(for: .seconds(1)) | ||
self.secondsElapsed += 1 | ||
print("secondsElapsed", self.secondsElapsed) | ||
} | ||
} | ||
} | ||
|
||
func stopTimerButtonTapped() { | ||
self.timerTask?.cancel() | ||
self.timerTask = nil | ||
} | ||
} | ||
|
||
struct CounterView: View { | ||
@ObservedObject var model: CounterModel | ||
|
||
var body: some View { | ||
let _ = Self._printChanges() | ||
Form { | ||
Section { | ||
Text(self.model.count.description) | ||
Button("Decrement") { self.model.decrementButtonTapped() } | ||
Button("Increment") { self.model.incrementButtonTapped() } | ||
} header: { | ||
Text("Counter") | ||
} | ||
Section { | ||
//Text("Seconds elapsed: \(self.model.secondsElapsed)") | ||
if !self.model.isTimerOn { | ||
Button("Start timer") { | ||
self.model.startTimerButtonTapped() | ||
} | ||
} else { | ||
Button { | ||
self.model.stopTimerButtonTapped() | ||
} label: { | ||
HStack { | ||
Text("Stop timer") | ||
Spacer() | ||
ProgressView().id(UUID()) | ||
} | ||
} | ||
} | ||
} header: { | ||
Text("Timer") | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
CounterView(model: CounterModel()) | ||
} |
59 changes: 59 additions & 0 deletions
59
0252-observation-pt1/ObservationExplorations/ObservationExplorations/CounterView_State.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,59 @@ | ||
import SwiftUI | ||
|
||
struct CounterView_State: View { | ||
@State var count = 0 | ||
@State var isDisplayingSecondsElapsed = false | ||
@State var secondsElapsed = 0 | ||
@State var timerTask: Task<Void, Error>? | ||
|
||
var body: some View { | ||
let _ = Self._printChanges() | ||
Form { | ||
Section { | ||
Text(self.count.description) | ||
Button("Decrement") { self.count -= 1 } | ||
Button("Increment") { self.count += 1 } | ||
} header: { | ||
Text("Counter") | ||
} | ||
Section { | ||
if self.isDisplayingSecondsElapsed { | ||
Text("Seconds elapsed: \(self.secondsElapsed)") | ||
} | ||
if self.timerTask == nil { | ||
Button("Start timer") { | ||
self.secondsElapsed = 0 | ||
self.timerTask?.cancel() | ||
self.timerTask = Task { | ||
while true { | ||
try await Task.sleep(for: .seconds(1)) | ||
print("secondsElapsed", self.secondsElapsed) | ||
self.secondsElapsed += 1 | ||
} | ||
} | ||
} | ||
} else { | ||
Button { | ||
self.timerTask?.cancel() | ||
self.timerTask = nil | ||
} label: { | ||
HStack { | ||
Text("Stop timer") | ||
Spacer() | ||
ProgressView().id(UUID()) | ||
} | ||
} | ||
} | ||
Toggle(isOn: self.$isDisplayingSecondsElapsed) { | ||
Text("Display seconds") | ||
} | ||
} header: { | ||
Text("Timer") | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
CounterView_State() | ||
} |
10 changes: 10 additions & 0 deletions
10
...tion-pt1/ObservationExplorations/ObservationExplorations/ObservationExplorationsApp.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,10 @@ | ||
import SwiftUI | ||
|
||
@main | ||
struct ObservationExplorationsApp: App { | ||
var body: some Scene { | ||
WindowGroup { | ||
CounterView(model: CounterModel()) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...xplorations/ObservationExplorations/Preview Content/Preview Assets.xcassets/Contents.json
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,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...1/ObservationExplorations/ObservationExplorationsTests/ObservationExplorations.xctestplan
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,29 @@ | ||
{ | ||
"configurations" : [ | ||
{ | ||
"id" : "49AD212A-15DA-46EA-9AD3-B8F6372909AB", | ||
"name" : "Test Scheme Action", | ||
"options" : { | ||
|
||
} | ||
} | ||
], | ||
"defaultOptions" : { | ||
"targetForVariableExpansion" : { | ||
"containerPath" : "container:ObservationExplorations.xcodeproj", | ||
"identifier" : "2AD14E1B2ACDC3EC0015EB20", | ||
"name" : "ObservationExplorations" | ||
} | ||
}, | ||
"testTargets" : [ | ||
{ | ||
"parallelizable" : true, | ||
"target" : { | ||
"containerPath" : "container:ObservationExplorations.xcodeproj", | ||
"identifier" : "2AD14E2B2ACDC3ED0015EB20", | ||
"name" : "ObservationExplorationsTests" | ||
} | ||
} | ||
], | ||
"version" : 1 | ||
} |
Oops, something went wrong.