Skip to content

Commit

Permalink
feat: 🎸 root navigation show camera
Browse files Browse the repository at this point in the history
  • Loading branch information
tomokisun committed Dec 26, 2023
1 parent cbbb936 commit 30bd2d5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 55 deletions.
14 changes: 8 additions & 6 deletions Packages/FlyCam/Sources/CameraFeature/Camera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public struct CameraLogic {
case onAppear
case closeButtonTapped
case child(Child.Action)
case delegate(Delegate)

public enum Delegate: Equatable {
case dismiss
}
}

@Dependency(\.dismiss) var dismiss
@Dependency(\.feedbackGenerator) var feedbackGenerator

public var body: some Reducer<State, Action> {
Expand All @@ -34,9 +38,9 @@ public struct CameraLogic {
return .none

case .closeButtonTapped:
return .run { _ in
return .run { send in
await feedbackGenerator.impactOccurred()
await dismiss()
await send(.delegate(.dismiss), animation: .default)
}

case let .child(.record(.delegate(.result(altitude, videoURL)))):
Expand All @@ -46,9 +50,7 @@ public struct CameraLogic {
return .none

case .child(.result(.sendButtonTapped)):
return .run { _ in
await dismiss()
}
return .send(.delegate(.dismiss), animation: .default)

default:
return .none
Expand Down
88 changes: 40 additions & 48 deletions Packages/FlyCam/Sources/NavigationFeature/RootNavigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public struct RootNavigationLogic {
public struct State: Equatable {
var ranking = RankingLogic.State()
var setting = SettingLogic.State()

@PresentationState var destination: Destination.State?
var camera: CameraLogic.State?

public init() {}
}
Expand All @@ -23,10 +22,9 @@ public struct RootNavigationLogic {
case cameraButtonTapped
case ranking(RankingLogic.Action)
case setting(SettingLogic.Action)
case destination(PresentationAction<Destination.Action>)
case camera(CameraLogic.Action)
}

@Dependency(\.mainQueue) var mainQueue
@Dependency(\.feedbackGenerator) var feedbackGenerator

public var body: some Reducer<State, Action> {
Expand All @@ -38,36 +36,25 @@ public struct RootNavigationLogic {
return .none

case .cameraButtonTapped:
state.destination = .camera()
state.camera = .init()
return .run { _ in
await feedbackGenerator.impactOccurred()
}

case .ranking(.list(.empty(.delegate(.toCamera)))):
state.destination = .camera()
state.camera = .init()
return .none

case .camera(.delegate(.dismiss)):
state.camera = nil
return .none

default:
return .none
}
}
.ifLet(\.$destination, action: \.destination) {
Destination()
}
}

@Reducer
public struct Destination {
public enum State: Equatable {
case camera(CameraLogic.State = .init())
}

public enum Action {
case camera(CameraLogic.Action)
}

public var body: some Reducer<State, Action> {
Scope(state: \.camera, action: \.camera, child: CameraLogic.init)
.ifLet(\.camera, action: \.camera) {
CameraLogic()
}
}
}
Expand All @@ -80,33 +67,38 @@ public struct RootNavigationView: View {
}

public var body: some View {
NavigationStack {
RankingView(store: store.scope(state: \.ranking, action: \.ranking))
.task { await store.send(.onTask).finish() }
.overlay(alignment: .bottom) {
Button {
store.send(.cameraButtonTapped)
} label: {
RoundedRectangle(cornerRadius: 80 / 2)
.stroke(Color.white, lineWidth: 6.0)
.frame(width: 80, height: 80)
.background(Material.ultraThin)
.clipShape(RoundedRectangle(cornerRadius: 80 / 2))
}
.padding(.bottom, 24)
WithViewStore(store, observe: { $0 }) { viewStore in
ZStack {
NavigationStack {
RankingView(store: store.scope(state: \.ranking, action: \.ranking))
.task { await store.send(.onTask).finish() }
.overlay(alignment: .bottom) {
Button {
store.send(.cameraButtonTapped, animation: .default)
} label: {
RoundedRectangle(cornerRadius: 80 / 2)
.stroke(Color.white, lineWidth: 6.0)
.frame(width: 80, height: 80)
.background(Material.ultraThin)
.clipShape(RoundedRectangle(cornerRadius: 80 / 2))
}
.padding(.bottom, 24)
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
SettingView(store: store.scope(state: \.setting, action: \.setting))
}
}
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
SettingView(store: store.scope(state: \.setting, action: \.setting))
}
}
.fullScreenCover(
store: store.scope(state: \.$destination.camera, action: \.destination.camera)
) { store in
NavigationStack {
CameraView(store: store)
}

NavigationStack {
IfLetStore(
store.scope(state: \.camera, action: \.camera),
then: CameraView.init(store:)
)
}
.opacity(viewStore.camera == nil ? 0.0 : 1.0)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct RankingEmptyLogic {
Reduce<State, Action> { _, action in
switch action {
case .takeButtonTapped:
return .send(.delegate(.toCamera))
return .send(.delegate(.toCamera), animation: .default)

case .delegate:
return .none
Expand Down

0 comments on commit 30bd2d5

Please sign in to comment.