|
| 1 | +import ComposableArchitecture |
| 2 | +import Foundation |
| 3 | +import SwiftUI |
| 4 | + |
| 5 | +struct VoiceMemo: Equatable { |
| 6 | + var date: Date |
| 7 | + var duration: TimeInterval |
| 8 | + var mode = Mode.notPlaying |
| 9 | + var title = "" |
| 10 | + var url: URL |
| 11 | + |
| 12 | + enum Mode: Equatable { |
| 13 | + case notPlaying |
| 14 | + case playing(progress: Double) |
| 15 | + |
| 16 | + var isPlaying: Bool { |
| 17 | + if case .playing = self { return true } |
| 18 | + return false |
| 19 | + } |
| 20 | + |
| 21 | + var progress: Double? { |
| 22 | + if case let .playing(progress) = self { return progress } |
| 23 | + return nil |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +enum VoiceMemoAction: Equatable { |
| 29 | + case audioPlayerClient(Result<AudioPlayerClient.Action, AudioPlayerClient.Failure>) |
| 30 | + case playButtonTapped |
| 31 | + case delete |
| 32 | + case timerUpdated(TimeInterval) |
| 33 | + case titleTextFieldChanged(String) |
| 34 | +} |
| 35 | + |
| 36 | +struct VoiceMemoEnvironment { |
| 37 | + var audioPlayerClient: AudioPlayerClient |
| 38 | + var mainQueue: AnySchedulerOf<DispatchQueue> |
| 39 | +} |
| 40 | + |
| 41 | +let voiceMemoReducer = Reducer<VoiceMemo, VoiceMemoAction, VoiceMemoEnvironment> { |
| 42 | + memo, action, environment in |
| 43 | + struct PlayerId: Hashable {} |
| 44 | + struct TimerId: Hashable {} |
| 45 | + |
| 46 | + switch action { |
| 47 | + case .audioPlayerClient(.success(.didFinishPlaying)), .audioPlayerClient(.failure): |
| 48 | + memo.mode = .notPlaying |
| 49 | + return .cancel(id: TimerId()) |
| 50 | + |
| 51 | + case .delete: |
| 52 | + return .merge( |
| 53 | + .cancel(id: PlayerId()), |
| 54 | + .cancel(id: TimerId()) |
| 55 | + ) |
| 56 | + |
| 57 | + case .playButtonTapped: |
| 58 | + switch memo.mode { |
| 59 | + case .notPlaying: |
| 60 | + memo.mode = .playing(progress: 0) |
| 61 | + let start = environment.mainQueue.now |
| 62 | + return .merge( |
| 63 | + environment.audioPlayerClient |
| 64 | + .play(PlayerId(), memo.url) |
| 65 | + .catchToEffect() |
| 66 | + .map(VoiceMemoAction.audioPlayerClient) |
| 67 | + .cancellable(id: PlayerId()), |
| 68 | + |
| 69 | + Effect.timer(id: TimerId(), every: 0.5, on: environment.mainQueue) |
| 70 | + .map { |
| 71 | + .timerUpdated( |
| 72 | + TimeInterval($0.dispatchTime.uptimeNanoseconds - start.dispatchTime.uptimeNanoseconds) |
| 73 | + / TimeInterval(NSEC_PER_SEC) |
| 74 | + ) |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + case .playing: |
| 79 | + memo.mode = .notPlaying |
| 80 | + return .concatenate( |
| 81 | + .cancel(id: TimerId()), |
| 82 | + environment.audioPlayerClient |
| 83 | + .stop(PlayerId()) |
| 84 | + .fireAndForget() |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + case let .timerUpdated(time): |
| 89 | + switch memo.mode { |
| 90 | + case .notPlaying: |
| 91 | + break |
| 92 | + case let .playing(progress: progress): |
| 93 | + memo.mode = .playing(progress: time / memo.duration) |
| 94 | + } |
| 95 | + return .none |
| 96 | + |
| 97 | + case let .titleTextFieldChanged(text): |
| 98 | + memo.title = text |
| 99 | + return .none |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +struct VoiceMemoView: View { |
| 104 | + // NB: We are using an explicit `ObservedObject` for the view store here instead of |
| 105 | + // `WithViewStore` due to a SwiftUI bug where `GeometryReader`s inside `WithViewStore` will |
| 106 | + // not properly update. |
| 107 | + // |
| 108 | + // Feedback filed: https://gist.github.com/mbrandonw/cc5da3d487bcf7c4f21c27019a440d18 |
| 109 | + @ObservedObject var viewStore: ViewStore<VoiceMemo, VoiceMemoAction> |
| 110 | + |
| 111 | + init(store: Store<VoiceMemo, VoiceMemoAction>) { |
| 112 | + self.viewStore = ViewStore(store) |
| 113 | + } |
| 114 | + |
| 115 | + var body: some View { |
| 116 | + GeometryReader { proxy in |
| 117 | + ZStack(alignment: .leading) { |
| 118 | + if self.viewStore.mode.isPlaying { |
| 119 | + Rectangle() |
| 120 | + .foregroundColor(Color(white: 0.9)) |
| 121 | + .frame(width: proxy.size.width * CGFloat(self.viewStore.mode.progress ?? 0)) |
| 122 | + .animation(.linear(duration: 0.5)) |
| 123 | + } |
| 124 | + |
| 125 | + HStack { |
| 126 | + TextField( |
| 127 | + "Untitled, \(dateFormatter.string(from: self.viewStore.date))", |
| 128 | + text: self.viewStore.binding( |
| 129 | + get: { $0.title }, send: VoiceMemoAction.titleTextFieldChanged) |
| 130 | + ) |
| 131 | + |
| 132 | + Spacer() |
| 133 | + |
| 134 | + dateComponentsFormatter.string(from: self.currentTime).map { |
| 135 | + Text($0) |
| 136 | + .font(Font.footnote.monospacedDigit()) |
| 137 | + .foregroundColor(.gray) |
| 138 | + } |
| 139 | + |
| 140 | + Button(action: { self.viewStore.send(.playButtonTapped) }) { |
| 141 | + Image(systemName: self.viewStore.mode.isPlaying ? "stop.circle" : "play.circle") |
| 142 | + .font(Font.system(size: 22)) |
| 143 | + } |
| 144 | + } |
| 145 | + .padding([.leading, .trailing]) |
| 146 | + } |
| 147 | + } |
| 148 | + .buttonStyle(BorderlessButtonStyle()) |
| 149 | + .listRowBackground(self.viewStore.mode.isPlaying ? Color(white: 0.97) : .clear) |
| 150 | + .listRowInsets(EdgeInsets()) |
| 151 | + } |
| 152 | + |
| 153 | + var currentTime: TimeInterval { |
| 154 | + self.viewStore.mode.progress.map { $0 * self.viewStore.duration } ?? self.viewStore.duration |
| 155 | + } |
| 156 | +} |
0 commit comments