Skip to content

Commit

Permalink
✨ 여정 기록 중 앱 종료 시 다음 실행 때 이어서 실행되도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
SwiftyJunnos committed Jan 10, 2024
1 parent 2b824dd commit 5e5c715
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ extension MapViewController {
#endif
}

public func recordingShouldResume(_ recordedJourney: RecordingJourney) {
let userRepository = UserRepositoryImplementation()
let journeyRepository = JourneyRepositoryImplementation()
let recordJourneyViewModel = RecordJourneyViewModel(startedJourney: recordedJourney,
userRepository: userRepository,
journeyRepository: journeyRepository)
self.swapViewModel(to: recordJourneyViewModel)

self.locationManager.startUpdatingLocation()
self.locationManager.allowsBackgroundLocationUpdates = true

#if DEBUG
MSLogger.make(category: .home).debug("여정 기록이 재개되었습니다.")
#endif
}

public func recordingShouldStop(isCancelling: Bool) {
guard let viewModel = self.viewModel as? RecordJourneyViewModel else {
MSLogger.make(category: .home).error("여정이 종료되어야 하지만 이미 Map에서 NavigateMapViewModel을 사용하고 있습니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public final class MapViewController: UIViewController {
#if DEBUG
MSLogger.make(category: .home).debug("Map에 NavigateMapViewModel을 바인딩 했습니다.")
#endif

navigateMapViewModel.trigger(.viewNeedsLoaded)
}

if let recordJourneyViewModel = viewModel as? RecordJourneyViewModel {
Expand All @@ -157,6 +159,17 @@ public final class MapViewController: UIViewController {
self?.drawPolyLinesToMap(with: journeys)
}
.store(in: &self.cancellables)

viewModel.state.recordingJourneyShouldResume
.sink { [weak self] recordingJourney in
self?.recordingShouldResume(recordingJourney)

let coordinates = recordingJourney.coordinates.map {
CLLocationCoordinate2D(latitude: $0.latitude, longitude: $0.longitude)
}
self?.drawPolylineToMap(using: coordinates)
}
.store(in: &self.cancellables)
}

private func bind(_ viewModel: RecordJourneyViewModel) {
Expand Down Expand Up @@ -222,24 +235,6 @@ public final class MapViewController: UIViewController {
self.mapView.addAnnotation(annotation)
}

public func addSpotInRecording(spot: Spot) {
Task {

let imageFetcher = MSImageFetcher.shared
guard let photoData = await imageFetcher.fetchImage(from: spot.photoURL,
forKey: spot.photoURL.paath()) else {
throw ImageFetchError.imageFetchFailed
}

let coordinate = CLLocationCoordinate2D(latitude: spot.coordinate.latitude,
longitude: spot.coordinate.longitude)

self.addAnnotation(title: "",
coordinate: coordinate,
photoData: photoData)
}
}

// MARK: - Functions: Polyline

private func drawPolyLinesToMap(with journeys: [Journey]) {
Expand Down Expand Up @@ -328,15 +323,6 @@ extension MapViewController: CLLocationManagerDelegate {
return
}

let previousCoordinate = (self.viewModel as? RecordJourneyViewModel)?.state.previousCoordinate.value

if let previousCoordinate = previousCoordinate {
if !self.isDistanceOver5AndUnder50(coordinate1: previousCoordinate,
coordinate2: newCurrentLocation.coordinate) {
return
}
}

let coordinate2D = CLLocationCoordinate2D(latitude: newCurrentLocation.coordinate.latitude,
longitude: newCurrentLocation.coordinate.longitude)

Expand Down Expand Up @@ -369,14 +355,6 @@ extension MapViewController: CLLocationManagerDelegate {
}
}

private func isDistanceOver5AndUnder50(coordinate1: CLLocationCoordinate2D,
coordinate2: CLLocationCoordinate2D) -> Bool {
let location1 = CLLocation(latitude: coordinate1.latitude, longitude: coordinate1.longitude)
let location2 = CLLocation(latitude: coordinate2.latitude, longitude: coordinate2.longitude)
MSLogger.make(category: .navigateMap).log("이동한 거리: \(location1.distance(from: location2))")
return 5 <= location1.distance(from: location2) && location1.distance(from: location2) <= 50
}

private func presentLocationAuthorizationAlert() {
let sheet = UIAlertController(title: Typo.locationAlertTitle,
message: Typo.locationAlertMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class NavigateMapViewModel: MapViewModel {

public struct State {
// Passthrough
public var locationShouldAuthorized = PassthroughSubject<Bool, Never>()
public var recordingJourneyShouldResume = PassthroughSubject<RecordingJourney, Never>()

// CurrentValue
public var visibleJourneys = CurrentValueSubject<[Journey], Never>([])
Expand All @@ -47,10 +47,31 @@ public final class NavigateMapViewModel: MapViewModel {
public func trigger(_ action: Action) {
switch action {
case .viewNeedsLoaded:
self.state.locationShouldAuthorized.send(true)
if let recordingJourney = self.fetchRecordingJourneyIfNeeded() {
#if DEBUG
MSLogger.make(category: .navigateMap)
.debug("기록중이던 여정이 발견되었습니다: \(recordingJourney)")
#endif
self.state.recordingJourneyShouldResume.send(recordingJourney)
}
case .visibleJourneysDidUpdated(let visibleJourneys):
self.state.visibleJourneys.send(visibleJourneys)
}
}

}

// MARK: - Private Functions

private extension NavigateMapViewModel {

/// 앱 종료 전 진행중이던 여정 기록이 남아있는지 확인합니다.
/// 진행 중이던 여정 기록이 있다면 해당 데이터를 불러옵니다.
/// - Returns: 진행 중이던 여정 기록. 없다면 `nil`을 반환합니다.
func fetchRecordingJourneyIfNeeded() -> RecordingJourney? {
guard self.journeyRepository.isRecording else { return nil }

return self.journeyRepository.fetchRecordingJourney()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ public struct JourneyRepositoryImplementation: JourneyRepository {
}

public func fetchRecordingJourney() -> RecordingJourney? {
guard let recordingJourneyID = self.fetchRecordingJourneyID(),
let recordingJourney = self.fetchRecordingJourney(forID: recordingJourneyID) else {
return nil
}
return recordingJourney
return self.recordingJourney.currentState
}

public mutating func startJourney(at coordinate: Coordinate,
Expand Down Expand Up @@ -171,21 +167,3 @@ public struct JourneyRepositoryImplementation: JourneyRepository {
}

}

// MARK: - Private Functions

private extension JourneyRepositoryImplementation {

func fetchRecordingJourneyID() -> String? {
guard let recordingJourneyID = self.recordingJourney.id else {
MSLogger.make(category: .recordingJourneyStorage).error("기록 중인 여정 정보를 가져오는데 실패했습니다.")
return nil
}
return recordingJourneyID
}

func fetchRecordingJourney(forID id: String) -> RecordingJourney? {
return self.storage.get(RecordingJourneyDTO.self, forKey: id)?.toDomain()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public struct RecordingJourneyStorage {
// MARK: - Functions

public mutating func start(initialData recordingJourney: RecordingJourney) {
defer { self.isRecording = true }

// 삭제되지 않은 이전 여정 기록이 남아있다면 삭제
if self.recordingJourneyID != nil {
do {
Expand Down

0 comments on commit 5e5c715

Please sign in to comment.