Skip to content

Commit

Permalink
235
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed May 16, 2023
1 parent 5c17384 commit 6247791
Showing 27 changed files with 3,367 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

struct Wrapper<Value> {
var value: Value {
get { self.storage.value }
set {
if Swift.isKnownUniquelyReferenced(&self.storage) {
self.storage.value = newValue
} else {
self.storage = Storage(value: newValue)
}
}
}

private var storage: Storage

init(value: Value) {
self.storage = Storage(value: value)
}

mutating func isKnownUniquelyReferenced() -> Bool {
Swift.isKnownUniquelyReferenced(&self.storage)
}

private class Storage {
var value: Value
init(value: Value) {
self.value = value
}
}
}

import Foundation

extension Wrapper: Equatable where Value: Equatable {
static func == (lhs: Self, rhs: Self) -> Bool {
if lhs.storage === rhs.storage {
return true
}
Thread.sleep(forTimeInterval: 3)
return lhs.value == rhs.value
}
}

var x = Wrapper(value: 1)
x.isKnownUniquelyReferenced()
var y = x
x.isKnownUniquelyReferenced()
y.isKnownUniquelyReferenced()
x == y
x.value = 2
y.value = 2
x == y
y.value
x.value
x.isKnownUniquelyReferenced()
y.isKnownUniquelyReferenced()

var z = 1
var w = z
z = 2
w
z
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1420"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2A3BE9F22994469500351060"
BuildableName = "Inventory.app"
BlueprintName = "Inventory"
ReferencedContainer = "container:Inventory.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2A3BEA022994469600351060"
BuildableName = "InventoryTests.xctest"
BlueprintName = "InventoryTests"
ReferencedContainer = "container:Inventory.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 = "2A3BE9F22994469500351060"
BuildableName = "Inventory.app"
BlueprintName = "Inventory"
ReferencedContainer = "container:Inventory.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2A3BE9F22994469500351060"
BuildableName = "Inventory.app"
BlueprintName = "Inventory"
ReferencedContainer = "container:Inventory.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
101 changes: 101 additions & 0 deletions 0235-composable-navigation-pt14/Inventory/Inventory/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import ComposableArchitecture
import SwiftUI

struct AppFeature: Reducer {
struct State: Equatable {
var firstTab = FirstTabFeature.State()
var inventory = InventoryFeature.State()
var selectedTab: Tab = .one
var thirdTab = ThirdTabFeature.State()
}
enum Action: Equatable {
case firstTab(FirstTabFeature.Action)
case inventory(InventoryFeature.Action)
case selectedTabChanged(Tab)
case thirdTab(ThirdTabFeature.Action)
}
var body: some ReducerOf<Self> {
Reduce<State, Action> { state, action in
switch action {
case let .firstTab(.delegate(action)):
switch action {
case .switchToInventoryTab:
state.selectedTab = .inventory
return .none
}

case let .selectedTabChanged(tab):
state.selectedTab = tab
return .none

case .firstTab, .inventory, .thirdTab:
return .none
}
}
Scope(state: \.firstTab, action: /Action.firstTab) {
FirstTabFeature()
}
Scope(state: \.inventory, action: /Action.inventory) {
InventoryFeature()
}
Scope(state: \.thirdTab, action: /Action.thirdTab) {
ThirdTabFeature()
}
}
}

enum Tab {
case one, inventory, three
}

struct ContentView: View {
//@State var selectedTab: Tab = .one
let store: StoreOf<AppFeature>
// Store<AppFeature.State, AppFeature.Action>

var body: some View {
WithViewStore(self.store, observe: \.selectedTab) { viewStore in
TabView(selection: viewStore.binding(send: AppFeature.Action.selectedTabChanged)) {
FirstTabView(
store: self.store.scope(
state: \.firstTab,
action: AppFeature.Action.firstTab
)
)
.tabItem { Text("One") }
.tag(Tab.one)

NavigationStack {
InventoryView(
store: self.store.scope(
state: \.inventory,
action: AppFeature.Action.inventory
)
)
}
.tabItem { Text("Inventory") }
.tag(Tab.inventory)

ThirdTabView(
store: self.store.scope(
state: \.thirdTab,
action: AppFeature.Action.thirdTab
)
)
.tabItem { Text("Three") }
.tag(Tab.three)
}
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(
store: Store(
initialState: AppFeature.State(),
reducer: AppFeature()
)
)
}
}
38 changes: 38 additions & 0 deletions 0235-composable-navigation-pt14/Inventory/Inventory/FirstTab.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import ComposableArchitecture
import SwiftUI

struct FirstTabFeature: Reducer {
struct State: Equatable {}
enum Action: Equatable {
case goToInventoryButtonTapped
case delegate(Delegate)

enum Delegate: Equatable {
case switchToInventoryTab
}
}

func reduce(into state: inout State, action: Action) -> Effect<Action> {
switch action {
case .delegate:
return .none

case .goToInventoryButtonTapped:
return .send(.delegate(.switchToInventoryTab))
}
}
}

struct FirstTabView: View {
let store: StoreOf<FirstTabFeature>

var body: some View {
WithViewStore(self.store, observe: { $0 }) { viewStore in
Button {
viewStore.send(.goToInventoryButtonTapped)
} label: {
Text("Go to inventory")
}
}
}
}
11 changes: 11 additions & 0 deletions 0235-composable-navigation-pt14/Inventory/Inventory/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
Loading

0 comments on commit 6247791

Please sign in to comment.