Skip to content

Commit

Permalink
Implement the dive log list (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevYeom authored Nov 2, 2024
1 parent c907c00 commit 4f6602a
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 52 deletions.
13 changes: 11 additions & 2 deletions Sources/BadaApp/Logbook/LogbookDiveSiteSearchReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,24 @@ struct LogbookDiveSiteSearchReducer: Reducer {
state.searchResult = searchResult
return .none
case let .setSearchCompletions(searchCompletions):
if searchCompletions.isEmpty && !state.searchText.isEmpty {
guard !state.searchText.isEmpty else {
state.searchCompletions = []
return .none
}
if searchCompletions.isEmpty {
let manualCompletion = LocalSearchCompletion(
title: state.searchText,
subtitle: "No matching results",
rawValue: nil
)
state.searchCompletions = [manualCompletion]
} else {
state.searchCompletions = searchCompletions
let manualCompletion = LocalSearchCompletion(
title: state.searchText,
subtitle: "",
rawValue: nil
)
state.searchCompletions = [manualCompletion] + searchCompletions
}
return .none
case let .setIsSearching(isSearching):
Expand Down
80 changes: 80 additions & 0 deletions Sources/BadaApp/Logbook/LogbookListReducer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// BadaBook
// Apache License, Version 2.0
//
// Copyright (c) 2024 Seungyeop Yeom ( https://github.com/OceanPositive ).
//

import BadaCore
import BadaDomain

struct LogbookListReducer: Reducer {
enum Action: Sendable {
case load
case setItems([LogbookListRowItem])
case setIsAddSheetPresenting(Bool)
}

struct State: Sendable, Equatable {
var items: [LogbookListRowItem] = []
var isAddSheetPresenting: Bool = false
}

@UseCase private var getDiveLogsUseCase: GetDiveLogsUseCase

func reduce(state: inout State, action: Action) -> AnyEffect<Action> {
switch action {
case .load:
return .single { await executeGetDiveLogsUseCase() }
case let .setItems(items):
state.items = items
return .none
case let .setIsAddSheetPresenting(isAddSheetPresenting):
state.isAddSheetPresenting = isAddSheetPresenting
return .none
}
}

private func executeGetDiveLogsUseCase() async -> Action {
let result = await getDiveLogsUseCase.execute()
switch result {
case let .success(diveLogs):
let items = diveLogs.map { diveLog in
LogbookListRowItem(
id: diveLog.id,
leadingPrimaryText: "#\(diveLog.logNumber)",
leadingSecondaryImage: .location,
leadingSecondaryText: diveLog.diveSite?.title,
trailingPrimaryText: formatted(depth: diveLog.maximumDepth),
trailingSecondaryText: formattedTotalTime(diveLog.entryTime, diveLog.exitTime),
trailingTertiarytext: formatted(date: diveLog.logDate)
)
}
return .setItems(items)
case .failure:
return .setItems([])
}
}

private func formatted(depth: UnitValue.Distance?) -> String? {
guard let depth else { return nil }
switch depth {
case let .km(double):
return "\((double * 1_000).formatted(.number.precision(.fractionLength(0))))m"
case let .m(double):
return "\(double.formatted(.number.precision(.fractionLength(0))))m"
}
}

private func formattedTotalTime(_ entryTime: Date?, _ exitTime: Date?) -> String? {
guard let entryTime else { return nil }
guard let exitTime else { return nil }
let interval = exitTime.timeIntervalSince(entryTime)
return "\(interval.formatted(.number.precision(.fractionLength(0))))min"
}

private func formatted(date: Date?) -> String? {
guard let date else { return nil }
return date.formatted(date: .abbreviated, time: .omitted)
}
}
63 changes: 55 additions & 8 deletions Sources/BadaApp/Logbook/LogbookListRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,66 @@ import BadaCore
import BadaUI

struct LogbookListRow: View {
let item: LogbookRowItem
let item: LogbookListRowItem

var body: some View {
HStack(spacing: 0) {
Text("\(item.logNumber)")
Spacer()
Text(item.siteName)
VStack(spacing: 8) {
HStack(spacing: 0) {
Text(item.leadingPrimaryText)
.font(.headline)
.foregroundStyle(.primary)
Spacer()
VStack(alignment: .trailing, spacing: 2) {
Text(item.trailingPrimaryText ?? "")
.font(.callout)
.foregroundStyle(.primary)
Text(item.trailingSecondaryText ?? "")
.font(.callout)
.foregroundStyle(.primary)
}
}
HStack(spacing: 0) {
if let leadingSecondaryText = item.leadingSecondaryText,
let leadingSecondaryImage = item.leadingSecondaryImage {
HStack(spacing: 2) {
Image(systemImage: leadingSecondaryImage)
.font(.caption)
.foregroundStyle(.secondary)
Text(leadingSecondaryText)
.font(.footnote)
.foregroundStyle(.secondary)
}
}
Spacer()
Text(item.trailingTertiarytext ?? "")
.font(.footnote)
.foregroundStyle(.secondary)
}
}
.padding([.vertical, .trailing], 4)
}
}

struct LogbookRowItem: Identifiable, Equatable {
struct LogbookListRowItem: Identifiable, Equatable {
let id: UUID
let logNumber: Int
let siteName: String
let leadingPrimaryText: String
let leadingSecondaryImage: SystemImage?
let leadingSecondaryText: String?
let trailingPrimaryText: String?
let trailingSecondaryText: String?
let trailingTertiarytext: String?
}

#Preview(traits: .sizeThatFitsLayout) {
LogbookListRow(
item: LogbookListRowItem(
id: UUID(),
leadingPrimaryText: "#12",
leadingSecondaryImage: .location,
leadingSecondaryText: "Doljo beach",
trailingPrimaryText: "24m",
trailingSecondaryText: "32min",
trailingTertiarytext: "Oct 31, 2024"
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
import BadaCore
import BadaUI

struct LogbookView: View {
struct LogbookListView: View {
@StateObject private var store = ViewStore(
reducer: LogbookReducer(),
state: LogbookReducer.State()
reducer: LogbookListReducer(),
state: LogbookListReducer.State()
)

var body: some View {
NavigationStack {
List {
ForEach(store.state.items) { item in
LogbookListRow(item: item)
Button(action: { tapRowItem(item) }) {
LogbookListRow(item: item)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
}
}
.navigationTitle(L10n.Logbook.title)
Expand Down Expand Up @@ -54,4 +58,8 @@ struct LogbookView: View {
private func tapAddButton() {
store.send(.setIsAddSheetPresenting(true))
}

private func tapRowItem(_ item: LogbookListRowItem) {
// TODO: WIP
}
}
37 changes: 0 additions & 37 deletions Sources/BadaApp/Logbook/LogbookReducer.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/BadaApp/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private struct MainTabView: View {
systemImage: SystemImage.bookPages.rawValue,
value: .logbook
) {
LogbookView()
LogbookListView()
}
}
.tabViewStyle(.sidebarAdaptable)
Expand Down
1 change: 1 addition & 0 deletions Sources/BadaUI/SystemImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package enum SystemImage: String {
case wind = "wind"
case cloudRain = "cloud.rain"
case cloudSnow = "cloud.snow"
case location = "location"
}

extension Image {
Expand Down

0 comments on commit 4f6602a

Please sign in to comment.