Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement saving dive logs #40

Merged
merged 1 commit into from
Nov 2, 2024
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
57 changes: 57 additions & 0 deletions Sources/BadaApp/Logbook/LogbookAddReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import BadaDomain

struct LogbookAddReducer: Reducer {
enum Action: Sendable {
case save
case none
case setLogNumber(Int?)
case setLogDate(Date)
case setDiveSite(LocalSearchResult)
Expand All @@ -30,6 +32,7 @@ struct LogbookAddReducer: Reducer {
case setFeeling(Feeling)
case setNotes(String)
case setIsDiveSiteSearchSheetPresenting(Bool)
case setShouldDismiss(Bool)
}

struct State: Sendable, Equatable {
Expand All @@ -53,10 +56,19 @@ struct LogbookAddReducer: Reducer {
var feeling: Feeling = .good
var notes: String = ""
var isDiveSiteSearchSheetPresenting: Bool = false
var shouldDismiss: Bool = false
}

@UseCase private var postDiveLogsUseCase: PostDiveLogUseCase

func reduce(state: inout State, action: Action) -> AnyEffect<Action> {
switch action {
case .save:
return .single { [state] in
await executePostDiveLogUseCase(state: state)
}
case .none:
return .none
case let .setLogNumber(logNumber):
state.logNumber = logNumber
return .none
Expand Down Expand Up @@ -172,6 +184,51 @@ struct LogbookAddReducer: Reducer {
case let .setIsDiveSiteSearchSheetPresenting(isDiveSiteSearchSheetPresenting):
state.isDiveSiteSearchSheetPresenting = isDiveSiteSearchSheetPresenting
return .none
case let .setShouldDismiss(shouldDismiss):
state.shouldDismiss = shouldDismiss
return .none
}
}

private func executePostDiveLogUseCase(state: State) async -> Action {
guard let logNumber = state.logNumber else { return .none }
let request = DiveLogInsertRequest(
logNumber: logNumber,
logDate: state.logDate,
diveSite: state.diveSite,
diveStyle: state.diveStyle,
entryTime: state.entryTime,
exitTime: state.exitTime,
surfaceInterval: state.surfaceInterval,
entryAir: state.entryAir,
exitAir: state.exitAir,
gasType: nil,
equipment: nil,
maximumDepth: state.maximumDepth,
averageDepth: state.averageDepth,
airTemperature: state.airTemperature,
surfaceTemperature: state.surfaceTemperature,
bottomTemperature: state.bottomTemperature,
weather: state.weather,
surge: nil,
visibility: nil,
visibilityDistance: nil,
feeling: state.feeling,
companions: [],
notes: state.notes
)
let result = await postDiveLogsUseCase.execute(for: request)
switch result {
case .success:
return .setShouldDismiss(true)
case .failure:
return .none
}
}
}

extension LogbookAddReducer.State {
var saveButtonDisabled: Bool {
logNumber == nil
}
}
17 changes: 12 additions & 5 deletions Sources/BadaApp/Logbook/LogbookAddSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ struct LogbookAddSheet: View {
#endif
}
}
#if os(macOS)
.padding()
#endif
.navigationTitle("New log")
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
Expand Down Expand Up @@ -236,16 +239,15 @@ struct LogbookAddSheet: View {
),
content: { LogbookDiveSiteSearchSheet(action: selectDiveSite) }
)
#if os(macOS)
.padding()
#endif
.onChange(of: store.state.shouldDismiss, shouldDismissChanged)
}
}

private var saveButton: some View {
Button(action: tapSaveButton) {
Text("Save")
}
.disabled(store.state.saveButtonDisabled)
}

private var cancelButton: some View {
Expand Down Expand Up @@ -275,12 +277,17 @@ struct LogbookAddSheet: View {
.disabled(focusedField?.next == nil)
}

private func tapSaveButton() {
private func shouldDismissChanged() {
guard store.state.shouldDismiss else { return }
dismiss()
}

private func tapSaveButton() {
store.send(.save)
}

private func tapCancelButton() {
dismiss()
store.send(.setShouldDismiss(true))
}

private func selectDiveSite(_ searchResult: LocalSearchResult) {
Expand Down
7 changes: 7 additions & 0 deletions Sources/BadaApp/Logbook/LogbookListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct LogbookListView: View {
),
content: { LogbookAddSheet() }
)
.onChange(of: store.state.isAddSheetPresenting, isAddSheetPresentingChanged)
.onAppear(perform: onAppear)
}
}
Expand All @@ -51,6 +52,12 @@ struct LogbookListView: View {
}
}

private func isAddSheetPresentingChanged(oldValue: Bool, newValue: Bool) {
if oldValue && !newValue {
store.send(.load)
}
}

private func onAppear() {
store.send(.load)
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/BadaAppTests/LogbookAddReducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import BadaTesting
@testable import BadaApp

struct LogbookAddReducerTests {
init() {
UseCaseContainer.instance.register {
PostDiveLogUseCase { _ in .success(()) }
}
}

@Test
func setLogNumber() async {
let sut = Store(
Expand Down
Loading