From eab2be04566056870745a48c3c413572fe5456ed Mon Sep 17 00:00:00 2001 From: ryoya ito Date: Sun, 25 Sep 2022 21:34:49 +0900 Subject: [PATCH] show map --- app-ios/Package.swift | 1 + app-ios/Sources/MapFeature/MapView.swift | 2 +- .../Sources/SessionFeature/SessionView.swift | 151 +++++++++++------- 3 files changed, 91 insertions(+), 63 deletions(-) diff --git a/app-ios/Package.swift b/app-ios/Package.swift index 61cfcef93..d639cc2d4 100644 --- a/app-ios/Package.swift +++ b/app-ios/Package.swift @@ -172,6 +172,7 @@ var package = Package( .target(name: "Assets"), .target(name: "CommonComponents"), .target(name: "Event"), + .target(name: "MapFeature"), .target(name: "Model"), .target(name: "Theme"), .product(name: "ComposableArchitecture", package: "swift-composable-architecture"), diff --git a/app-ios/Sources/MapFeature/MapView.swift b/app-ios/Sources/MapFeature/MapView.swift index f5a95b51b..b155a6577 100644 --- a/app-ios/Sources/MapFeature/MapView.swift +++ b/app-ios/Sources/MapFeature/MapView.swift @@ -9,7 +9,7 @@ public struct MapState: Equatable { public init() {} } -public enum MapAction { +public enum MapAction: Equatable { case tapPin } diff --git a/app-ios/Sources/SessionFeature/SessionView.swift b/app-ios/Sources/SessionFeature/SessionView.swift index 854eecd35..78b5d3207 100644 --- a/app-ios/Sources/SessionFeature/SessionView.swift +++ b/app-ios/Sources/SessionFeature/SessionView.swift @@ -2,6 +2,7 @@ import appioscombined import Assets import ComposableArchitecture import Event +import MapFeature import Model import SwiftUI import Theme @@ -9,6 +10,7 @@ import Theme public struct SessionState: Equatable { public var timetableItemWithFavorite: TimetableItemWithFavorite public var isShareSheetShown: Bool = false + public var mapState: MapState? public var eventAddConfirmAlert: AlertState? public init(timetableItemWithFavorite: TimetableItemWithFavorite) { @@ -16,7 +18,7 @@ public struct SessionState: Equatable { } } -public enum SessionAction { +public enum SessionAction: Equatable { case tapCalendar case tapMap case tapFavorite @@ -25,6 +27,8 @@ public enum SessionAction { case showEventAddConfirmAlert case hideEventAddConfirmAlert case addEvent + case map(MapAction) + case hideMap } public struct SessionEnvironment { @@ -40,71 +44,84 @@ public struct SessionEnvironment { } } -public let sessionReducer = Reducer { state, action, environment in - - switch action { - case .tapCalendar: - let timetableItem = state.timetableItemWithFavorite.timetableItem - return .run { @MainActor subscriber in - if try await environment.eventKitClient.requestAccessIfNeeded() { - subscriber.send(.showEventAddConfirmAlert) - } - } - .receive(on: DispatchQueue.main.eraseToAnyScheduler()) - .eraseToEffect() - case .tapMap: - // TODO: Open Session Map Location - return .none - case .tapFavorite: - let timetableItem = state.timetableItemWithFavorite.timetableItem - let isFavorite = state.timetableItemWithFavorite.isFavorited - state.timetableItemWithFavorite = TimetableItemWithFavorite( - timetableItem: timetableItem, - isFavorited: !isFavorite - ) - return .run { @MainActor _ in - try await environment.sessionsRepository.setFavorite(sessionId: timetableItem.id, favorite: !isFavorite) +public let sessionReducer = Reducer.combine( + mapReducer.optional().pullback( + state: \.mapState, + action: /SessionAction.map, + environment: { _ in + .init() } - .receive(on: DispatchQueue.main.eraseToAnyScheduler()) - .eraseToEffect() - case .tapShare: - state.isShareSheetShown = true - return .none - case .hideShareSheet: - state.isShareSheetShown = false - return .none - case .showEventAddConfirmAlert: - state.eventAddConfirmAlert = .init( - title: .init(StringsKt.shared.session_event_add_confirm.localized()), - buttons: [ - .cancel( - .init(StringsKt.shared.session_event_add_confirm_cancel.localized()), - action: .send(.hideEventAddConfirmAlert) - ), - .default( - .init(StringsKt.shared.session_event_add_confirm_ok.localized()), - action: .send(.addEvent) - ), - ] - ) - return .none - case .hideEventAddConfirmAlert: - state.eventAddConfirmAlert = nil - return .none - case .addEvent: - let timetableItem = state.timetableItemWithFavorite.timetableItem - do { - try environment.eventKitClient.addEvent( - title: timetableItem.title.jaTitle, - startDate: timetableItem.startsAt.toDate(), - endDate: timetableItem.endsAt.toDate() + ), + .init { state, action, environment in + switch action { + case .tapCalendar: + let timetableItem = state.timetableItemWithFavorite.timetableItem + return .run { @MainActor subscriber in + if try await environment.eventKitClient.requestAccessIfNeeded() { + subscriber.send(.showEventAddConfirmAlert) + } + } + .receive(on: DispatchQueue.main.eraseToAnyScheduler()) + .eraseToEffect() + case .tapMap: + state.mapState = .init() + return .none + case .tapFavorite: + let timetableItem = state.timetableItemWithFavorite.timetableItem + let isFavorite = state.timetableItemWithFavorite.isFavorited + state.timetableItemWithFavorite = TimetableItemWithFavorite( + timetableItem: timetableItem, + isFavorited: !isFavorite ) - } catch let e { - print(e) + return .run { @MainActor _ in + try await environment.sessionsRepository.setFavorite(sessionId: timetableItem.id, favorite: !isFavorite) + } + .receive(on: DispatchQueue.main.eraseToAnyScheduler()) + .eraseToEffect() + case .tapShare: + state.isShareSheetShown = true + return .none + case .hideShareSheet: + state.isShareSheetShown = false + return .none + case .showEventAddConfirmAlert: + state.eventAddConfirmAlert = .init( + title: .init(StringsKt.shared.session_event_add_confirm.localized()), + buttons: [ + .cancel( + .init(StringsKt.shared.session_event_add_confirm_cancel.localized()), + action: .send(.hideEventAddConfirmAlert) + ), + .default( + .init(StringsKt.shared.session_event_add_confirm_ok.localized()), + action: .send(.addEvent) + ), + ] + ) + return .none + case .hideEventAddConfirmAlert: + state.eventAddConfirmAlert = nil + return .none + case .addEvent: + let timetableItem = state.timetableItemWithFavorite.timetableItem + do { + try environment.eventKitClient.addEvent( + title: timetableItem.title.jaTitle, + startDate: timetableItem.startsAt.toDate(), + endDate: timetableItem.endsAt.toDate() + ) + } catch let error { + print(error) + } + return .none + case .map: + return .none + case .hideMap: + state.mapState = nil + return .none } - return .none } -} +) public struct SessionView: View { @Environment(\.presentationMode) var presentationMode @@ -205,6 +222,16 @@ public struct SessionView: View { .sheet(isPresented: viewStore.binding(get: { $0.isShareSheetShown }, send: .hideShareSheet)) { ShareTextView(text: timetableItem.shareText) } + .sheet(isPresented: viewStore.binding(get: { $0.mapState != nil }, send: .hideMap)) { + IfLetStore( + store.scope( + state: \.mapState, + action: SessionAction.map + ) + ) { mapStore in + MapView(store: mapStore) + } + } .alert(store.scope(state: \.eventAddConfirmAlert), dismiss: .hideEventAddConfirmAlert) } }