Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Examples/Todos/Todos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
CA93D060249BF4D000A6F65D /* Todo.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA93D05F249BF4D000A6F65D /* Todo.swift */; };
DC1394322469E57000EE1157 /* ComposableArchitecture in Frameworks */ = {isa = PBXBuildFile; productRef = DC1394312469E57000EE1157 /* ComposableArchitecture */; };
DCBCB77624290F6C00DE1F59 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCBCB77524290F6C00DE1F59 /* SceneDelegate.swift */; };
DCBCB77A24290F6D00DE1F59 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DCBCB77924290F6D00DE1F59 /* Assets.xcassets */; };
Expand Down Expand Up @@ -48,6 +49,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
CA93D05F249BF4D000A6F65D /* Todo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Todo.swift; sourceTree = "<group>"; };
DC85B441242D0286009784B0 /* swift-composable-architecture */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "swift-composable-architecture"; path = ../..; sourceTree = "<group>"; };
DCBCB77024290F6C00DE1F59 /* Todos.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Todos.app; sourceTree = BUILT_PRODUCTS_DIR; };
DCBCB77524290F6C00DE1F59 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -100,10 +102,11 @@
DCBCB77224290F6C00DE1F59 /* Todos */ = {
isa = PBXGroup;
children = (
DCBCB78124290F6D00DE1F59 /* Info.plist */,
DCBCB77524290F6C00DE1F59 /* SceneDelegate.swift */,
CA93D05F249BF4D000A6F65D /* Todo.swift */,
DCBCB79A24290FEB00DE1F59 /* Todos.swift */,
DCBCB77924290F6D00DE1F59 /* Assets.xcassets */,
DCBCB78124290F6D00DE1F59 /* Info.plist */,
);
path = Todos;
sourceTree = "<group>";
Expand Down Expand Up @@ -232,6 +235,7 @@
buildActionMask = 2147483647;
files = (
DCBCB77624290F6C00DE1F59 /* SceneDelegate.swift in Sources */,
CA93D060249BF4D000A6F65D /* Todo.swift in Sources */,
DCBCB79B24290FEB00DE1F59 /* Todos.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
49 changes: 49 additions & 0 deletions Examples/Todos/Todos/Todo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import ComposableArchitecture
import Foundation
import SwiftUI

struct Todo: Equatable, Identifiable {
var description = ""
let id: UUID
var isComplete = false
}

enum TodoAction: Equatable {
case checkBoxToggled
case textFieldChanged(String)
}

struct TodoEnvironment {}

let todoReducer = Reducer<Todo, TodoAction, TodoEnvironment> { todo, action, _ in
switch action {
case .checkBoxToggled:
todo.isComplete.toggle()
return .none

case let .textFieldChanged(description):
todo.description = description
return .none
}
}

struct TodoView: View {
let store: Store<Todo, TodoAction>

var body: some View {
WithViewStore(self.store) { viewStore in
HStack {
Button(action: { viewStore.send(.checkBoxToggled) }) {
Image(systemName: viewStore.isComplete ? "checkmark.square" : "square")
}
.buttonStyle(PlainButtonStyle())

TextField(
"Untitled Todo",
text: viewStore.binding(get: { $0.description }, send: TodoAction.textFieldChanged)
)
}
.foregroundColor(viewStore.isComplete ? .gray : nil)
}
}
}
46 changes: 0 additions & 46 deletions Examples/Todos/Todos/Todos.swift
Original file line number Diff line number Diff line change
@@ -1,52 +1,6 @@
import ComposableArchitecture
import SwiftUI

struct Todo: Equatable, Identifiable {
var description = ""
let id: UUID
var isComplete = false
}

enum TodoAction: Equatable {
case checkBoxToggled
case textFieldChanged(String)
}

struct TodoEnvironment {}

let todoReducer = Reducer<Todo, TodoAction, TodoEnvironment> { todo, action, _ in
switch action {
case .checkBoxToggled:
todo.isComplete.toggle()
return .none

case let .textFieldChanged(description):
todo.description = description
return .none
}
}

struct TodoView: View {
let store: Store<Todo, TodoAction>

var body: some View {
WithViewStore(self.store) { viewStore in
HStack {
Button(action: { viewStore.send(.checkBoxToggled) }) {
Image(systemName: viewStore.isComplete ? "checkmark.square" : "square")
}
.buttonStyle(PlainButtonStyle())

TextField(
"Untitled Todo",
text: viewStore.binding(get: { $0.description }, send: TodoAction.textFieldChanged)
)
}
.foregroundColor(viewStore.isComplete ? .gray : nil)
}
}
}

enum Filter: LocalizedStringKey, CaseIterable, Hashable {
case all = "All"
case active = "Active"
Expand Down
13 changes: 10 additions & 3 deletions Examples/VoiceMemos/VoiceMemos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
CA93D05C249BF42500A6F65D /* VoiceMemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA93D05B249BF42500A6F65D /* VoiceMemo.swift */; };
CA93D05E249BF46E00A6F65D /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA93D05D249BF46E00A6F65D /* Helpers.swift */; };
DC1394342469E59600EE1157 /* ComposableArchitecture in Frameworks */ = {isa = PBXBuildFile; productRef = DC1394332469E59600EE1157 /* ComposableArchitecture */; };
DC5BDCB024589177009C65A3 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5BDCAF24589177009C65A3 /* SceneDelegate.swift */; };
DC5BDCB224589177009C65A3 /* VoiceMemos.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5BDCB124589177009C65A3 /* VoiceMemos.swift */; };
Expand Down Expand Up @@ -54,6 +56,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
CA93D05B249BF42500A6F65D /* VoiceMemo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VoiceMemo.swift; sourceTree = "<group>"; };
CA93D05D249BF46E00A6F65D /* Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = "<group>"; };
DC5BDCAA24589177009C65A3 /* VoiceMemos.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = VoiceMemos.app; sourceTree = BUILT_PRODUCTS_DIR; };
DC5BDCAF24589177009C65A3 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
DC5BDCB124589177009C65A3 /* VoiceMemos.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VoiceMemos.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -112,12 +116,14 @@
DC5BDCAC24589177009C65A3 /* VoiceMemos */ = {
isa = PBXGroup;
children = (
DC5BDCBB24589178009C65A3 /* Info.plist */,
CA93D05D249BF46E00A6F65D /* Helpers.swift */,
DC5BDCAF24589177009C65A3 /* SceneDelegate.swift */,
CA93D05B249BF42500A6F65D /* VoiceMemo.swift */,
DC5BDCB124589177009C65A3 /* VoiceMemos.swift */,
DC5BDCB324589178009C65A3 /* Assets.xcassets */,
DC5BDF3B245893DB009C65A3 /* AudioPlayerClient */,
DC5BDF3424589389009C65A3 /* AudioRecorderClient */,
DC5BDCB324589178009C65A3 /* Assets.xcassets */,
DC5BDCBB24589178009C65A3 /* Info.plist */,
);
path = VoiceMemos;
sourceTree = "<group>";
Expand Down Expand Up @@ -267,9 +273,11 @@
DC5BDF362458939C009C65A3 /* AudioRecorderClient.swift in Sources */,
DC94F0122458E0AA00082DE9 /* MockAudioRecorderClient.swift in Sources */,
DC5BDF3D245893E6009C65A3 /* AudioPlayerClient.swift in Sources */,
CA93D05E249BF46E00A6F65D /* Helpers.swift in Sources */,
DC5BDF3A245893C1009C65A3 /* LiveAudioRecorderClient.swift in Sources */,
DC5BDF3F24589406009C65A3 /* LiveAudioPlayerClient.swift in Sources */,
DC94F0102458E09900082DE9 /* MockAudioPlayerClient.swift in Sources */,
CA93D05C249BF42500A6F65D /* VoiceMemo.swift in Sources */,
DC5BDCB024589177009C65A3 /* SceneDelegate.swift in Sources */,
DC5BDCB224589177009C65A3 /* VoiceMemos.swift in Sources */,
);
Expand All @@ -293,7 +301,6 @@
};
DCFC51462466F42900A0B8CF /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
productRef = DCFC51452466F42900A0B8CF /* ComposableArchitecture */;
};
/* End PBXTargetDependency section */

Expand Down
20 changes: 20 additions & 0 deletions Examples/VoiceMemos/VoiceMemos/Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

struct AlertData: Identifiable {
var message: String
var id: String { self.message }
}

let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
return formatter
}()

let dateComponentsFormatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.minute, .second]
formatter.zeroFormattingBehavior = .pad
return formatter
}()
156 changes: 156 additions & 0 deletions Examples/VoiceMemos/VoiceMemos/VoiceMemo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import ComposableArchitecture
import Foundation
import SwiftUI

struct VoiceMemo: Equatable {
var date: Date
var duration: TimeInterval
var mode = Mode.notPlaying
var title = ""
var url: URL

enum Mode: Equatable {
case notPlaying
case playing(progress: Double)

var isPlaying: Bool {
if case .playing = self { return true }
return false
}

var progress: Double? {
if case let .playing(progress) = self { return progress }
return nil
}
}
}

enum VoiceMemoAction: Equatable {
case audioPlayerClient(Result<AudioPlayerClient.Action, AudioPlayerClient.Failure>)
case playButtonTapped
case delete
case timerUpdated(TimeInterval)
case titleTextFieldChanged(String)
}

struct VoiceMemoEnvironment {
var audioPlayerClient: AudioPlayerClient
var mainQueue: AnySchedulerOf<DispatchQueue>
}

let voiceMemoReducer = Reducer<VoiceMemo, VoiceMemoAction, VoiceMemoEnvironment> {
memo, action, environment in
struct PlayerId: Hashable {}
struct TimerId: Hashable {}

switch action {
case .audioPlayerClient(.success(.didFinishPlaying)), .audioPlayerClient(.failure):
memo.mode = .notPlaying
return .cancel(id: TimerId())

case .delete:
return .merge(
.cancel(id: PlayerId()),
.cancel(id: TimerId())
)

case .playButtonTapped:
switch memo.mode {
case .notPlaying:
memo.mode = .playing(progress: 0)
let start = environment.mainQueue.now
return .merge(
environment.audioPlayerClient
.play(PlayerId(), memo.url)
.catchToEffect()
.map(VoiceMemoAction.audioPlayerClient)
.cancellable(id: PlayerId()),

Effect.timer(id: TimerId(), every: 0.5, on: environment.mainQueue)
.map {
.timerUpdated(
TimeInterval($0.dispatchTime.uptimeNanoseconds - start.dispatchTime.uptimeNanoseconds)
/ TimeInterval(NSEC_PER_SEC)
)
}
)

case .playing:
memo.mode = .notPlaying
return .concatenate(
.cancel(id: TimerId()),
environment.audioPlayerClient
.stop(PlayerId())
.fireAndForget()
)
}

case let .timerUpdated(time):
switch memo.mode {
case .notPlaying:
break
case let .playing(progress: progress):
memo.mode = .playing(progress: time / memo.duration)
}
return .none

case let .titleTextFieldChanged(text):
memo.title = text
return .none
}
}

struct VoiceMemoView: View {
// NB: We are using an explicit `ObservedObject` for the view store here instead of
// `WithViewStore` due to a SwiftUI bug where `GeometryReader`s inside `WithViewStore` will
// not properly update.
//
// Feedback filed: https://gist.github.com/mbrandonw/cc5da3d487bcf7c4f21c27019a440d18
@ObservedObject var viewStore: ViewStore<VoiceMemo, VoiceMemoAction>

init(store: Store<VoiceMemo, VoiceMemoAction>) {
self.viewStore = ViewStore(store)
}

var body: some View {
GeometryReader { proxy in
ZStack(alignment: .leading) {
if self.viewStore.mode.isPlaying {
Rectangle()
.foregroundColor(Color(white: 0.9))
.frame(width: proxy.size.width * CGFloat(self.viewStore.mode.progress ?? 0))
.animation(.linear(duration: 0.5))
}

HStack {
TextField(
"Untitled, \(dateFormatter.string(from: self.viewStore.date))",
text: self.viewStore.binding(
get: { $0.title }, send: VoiceMemoAction.titleTextFieldChanged)
)

Spacer()

dateComponentsFormatter.string(from: self.currentTime).map {
Text($0)
.font(Font.footnote.monospacedDigit())
.foregroundColor(.gray)
}

Button(action: { self.viewStore.send(.playButtonTapped) }) {
Image(systemName: self.viewStore.mode.isPlaying ? "stop.circle" : "play.circle")
.font(Font.system(size: 22))
}
}
.padding([.leading, .trailing])
}
}
.buttonStyle(BorderlessButtonStyle())
.listRowBackground(self.viewStore.mode.isPlaying ? Color(white: 0.97) : .clear)
.listRowInsets(EdgeInsets())
}

var currentTime: TimeInterval {
self.viewStore.mode.progress.map { $0 * self.viewStore.duration } ?? self.viewStore.duration
}
}
Loading