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

[iOS] UserID 관련 버그 수정 #301

Merged
merged 8 commits into from
Dec 11, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public final class HomeViewController: HomeBottomSheetViewController {
self.viewModel.state.startedJourney
.receive(on: DispatchQueue.main)
.sink { [weak self] startedJourney in
self?.contentViewController.journeyDidStarted(startedJourney)
self?.contentViewController.journeyShouldStarted(startedJourney)
}
.store(in: &self.cancellables)

Expand All @@ -140,6 +140,7 @@ public final class HomeViewController: HomeBottomSheetViewController {
.store(in: &self.cancellables)

self.viewModel.state.isRecording
.removeDuplicates()
.receive(on: DispatchQueue.main)
.sink { [weak self] isRecording in
if isRecording {
Expand Down Expand Up @@ -226,7 +227,7 @@ extension HomeViewController: RecordJourneyButtonViewDelegate {
guard self.viewModel.state.isRecording.value == true else { return }

self.viewModel.trigger(.backButtonDidTap)
self.contentViewController.journeyDidCancelled()
self.contentViewController.journeyShouldStopped()
}

public func spotButtonDidTap(_ button: MSRectButton) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation

import MSData
import MSDomain
import MSLogger

// MARK: - NavigateMap

Expand All @@ -26,17 +27,25 @@ extension MapViewController {

extension MapViewController {

public func journeyDidStarted(_ startedJourney: RecordingJourney) {
public func journeyShouldStarted(_ startedJourney: RecordingJourney) {
guard let viewModel = self.viewModel as? NavigateMapViewModel else {
MSLogger.make(category: .home).error("여정이 시작되어야 하지만 이미 Map에서 RecordJourneyViewModel을 사용하고 있습니다.")
return
}

let userRepository = UserRepositoryImplementation()
let journeyRepository = JourneyRepositoryImplementation()
let viewModel = RecordJourneyViewModel(startedJourney: startedJourney,
userRepository: userRepository,
journeyRepository: journeyRepository)
self.swapViewModel(to: viewModel)
let recordJourneyViewModel = RecordJourneyViewModel(startedJourney: startedJourney,
userRepository: userRepository,
journeyRepository: journeyRepository)
self.swapViewModel(to: recordJourneyViewModel)
}

public func journeyDidCancelled() {
guard let viewModel = self.viewModel as? RecordJourneyViewModel else { return }
public func journeyShouldStopped() {
guard let viewModel = self.viewModel as? RecordJourneyViewModel else {
MSLogger.make(category: .home).error("여정이 종료되어야 하지만 이미 Map에서 NavigateMapViewModel을 사용하고 있습니다.")
return
}
viewModel.trigger(.recordingDidCancelled)

let journeyRepository = JourneyRepositoryImplementation()
Expand Down
3 changes: 2 additions & 1 deletion iOS/MSData/Sources/MSData/Repository/JourneyRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ public struct JourneyRepositoryImplementation: JourneyRepository {

self.saveToLocal(value: recordingJourney.id)
self.saveToLocal(value: recordingJourney.startTimestamp)
self.isRecording = true

self.recordingJourneyID = recordingJourney.id
self.isRecording = true

#if DEBUG
if let recordingJourneyID = self.recordingJourneyID {
MSLogger.make(category: .userDefaults).debug("기록중인 여정 정보가 저장되었습니다: \(recordingJourneyID)")
Expand Down
10 changes: 0 additions & 10 deletions iOS/MSData/Sources/MSData/Repository/SpotRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,5 @@ public struct SpotRepositoryImplementation: SpotRepository {
return .failure(error)
}
}

// MARK: - Persistable

public func saveToLocal(value: Codable) {

}

public func loadJourneyFromLocal() -> RecordingJourney? {
return nil
}

}
23 changes: 23 additions & 0 deletions iOS/MSData/Sources/MSData/Repository/UserRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import MSNetworking
public protocol UserRepository {

func createUser() async -> Result<UUID, Error>
func storeUUID(_ userID: UUID) throws -> UUID
func fetchUUID() -> UUID?

}
Expand All @@ -40,8 +41,14 @@ public struct UserRepositoryImplementation: UserRepository {
let userID: UUID
if let existingUserID = self.fetchUUID() {
userID = existingUserID
#if DEBUG
MSLogger.make(category: .keychain).debug("Keychain에 유저가 존재해 해당 유저 ID를 사용합니다: \(userID)")
#endif
} else {
userID = UUID()
#if DEBUG
MSLogger.make(category: .keychain).debug("Keychain에 유저가 존재하지 않아 새로운 유저 ID를 사용합니다: \(userID)")
#endif
}

let requestDTO = UserRequestDTO(userID: userID)
Expand All @@ -50,12 +57,28 @@ public struct UserRepositoryImplementation: UserRepository {
let result = await self.networking.request(UserResponseDTO.self, router: router)
switch result {
case .success(let userResponse):
if let storedUserID = try? self.storeUUID(userID) {
return .success(storedUserID)
}
MSLogger.make(category: .keychain).warning("서버에 새로운 유저를 생성했지만, Keychain에 저장하지 못했습니다.")
return .success(userResponse.userID)
case .failure(let error):
return .failure(error)
}
}

@discardableResult
public func storeUUID(_ userID: UUID) throws -> UUID {
let account = MSKeychainStorage.Accounts.userID.rawValue

do {
try self.keychain.set(value: userID, account: account)
return userID
} catch {
throw MSKeychainStorage.KeychainError.creationError
}
}

/// UUID가 이미 키체인에 등록되어 있다면 가져옵니다.
public func fetchUUID() -> UUID? {
let account = MSKeychainStorage.Accounts.userID.rawValue
Expand Down
8 changes: 4 additions & 4 deletions iOS/MusicSpot/MusicSpot/MSCoordinator/HomeCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ extension HomeCoordinator: HomeCoordinatorDelegate {
}

func popToHome(from coordinator: Coordinator, with endedJourney: Journey) {
guard let viewController = self.navigationController.viewControllers.first(where: {
$0 is HomeViewController
}) else {
let viewControllers = self.navigationController.viewControllers
guard let viewController = viewControllers.first(where: { $0 is HomeViewController }),
let homeViewController = viewController as? HomeViewController else {
return
}

self.navigationController.popToViewController(viewController, animated: true)
self.navigationController.popToViewController(homeViewController, animated: true)
}

}
14 changes: 11 additions & 3 deletions iOS/MusicSpot/MusicSpot/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import JourneyList
import MSConstants
import MSData
import MSDesignSystem
import MSLogger

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

Expand All @@ -24,6 +25,9 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var recordingJourneyID: String?
@UserDefaultsWrapped(UserDefaultsKey.isFirstLaunch, defaultValue: false)
var isFirstLaunch: Bool
@UserDefaultsWrapped(UserDefaultsKey.isRecording, defaultValue: false)
var isRecording: Bool

var keychain = MSKeychainStorage()
#endif

Expand All @@ -36,6 +40,13 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
let window = UIWindow(windowScene: windowScene)
defer { self.window = window }

MSLogger.make(category: .userDefaults).info("isFirstLaunch: \(self.isFirstLaunch.description)")
MSLogger.make(category: .userDefaults).info("isRecording: \(self.isRecording.description)")
MSLogger.make(category: .userDefaults).info("recordingJourneyID: \(self.recordingJourneyID ?? "No Recording")")

#if DEBUG
self.prepareToDebug()
#endif
MSFont.registerFonts()

let musicSpotNavigationController = self.makeNavigationController()
Expand Down Expand Up @@ -65,9 +76,7 @@ private extension SceneDelegate {
// MARK: - Debug

#if DEBUG

import MSKeychainStorage
import MSLogger
import MSUserDefaults

private extension SceneDelegate {
Expand All @@ -83,5 +92,4 @@ private extension SceneDelegate {
}

}

#endif
Loading