Skip to content

Commit

Permalink
Merge pull request #296 from boostcampwm2023/iOS/task/UserID-Fix
Browse files Browse the repository at this point in the history
[iOS] ์—ฌ์ • ๋ชฉ๋ก ํ—ค๋” & UserID ๊ด€๋ จ ๋กœ์ง ์ˆ˜์ •
  • Loading branch information
SwiftyJunnos authored Dec 11, 2023
2 parents 0be9d2c + 27e39b4 commit 7cfc5e1
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 31 deletions.
22 changes: 8 additions & 14 deletions iOS/Features/Home/Sources/Home/Presentation/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,12 @@ public final class HomeViewModel {
func trigger(_ action: Action) {
switch action {
case .viewNeedsLoaded:
// #if DEBUG
// self.isFirstLaunch = true
// try? self.keychain.deleteAll()
// #endif
#if DEBUG
let firstLaunchMessage = self.isFirstLaunch ? "์•ฑ์ด ์ฒ˜์Œ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค." : "์•ฑ ์ฒซ ์‹คํ–‰์ด ์•„๋‹™๋‹ˆ๋‹ค."
MSLogger.make(category: .userDefaults).log("\(firstLaunchMessage)")
MSLogger.make(category: .userDefaults).debug("\(firstLaunchMessage)")
#endif

if self.isFirstLaunch {
self.createNewUser()
}
self.createNewUserWhenFirstLaunch()
case .viewNeedsReloaded:
let isRecording = self.journeyRepository.fetchIsRecording()
self.state.isRecording.send(isRecording)
Expand All @@ -103,7 +99,7 @@ public final class HomeViewModel {

private extension HomeViewModel {

func createNewUser() {
func createNewUserWhenFirstLaunch() {
guard self.isFirstLaunch else { return }

Task {
Expand All @@ -125,10 +121,8 @@ private extension HomeViewModel {
self.state.isStartButtonLoading.send(true)
defer { self.state.isStartButtonLoading.send(false) }

let userID = try self.userRepository.fetchUUID()
#if DEBUG
MSLogger.make(category: .home).debug("์œ ์ € ID ์กฐํšŒ ์„ฑ๊ณต: \(userID)")
#endif
guard let userID = self.userRepository.fetchUUID() else { return }

let result = await self.journeyRepository.startJourney(at: coordinate, userID: userID)
switch result {
case .success(let recordingJourney):
Expand All @@ -141,7 +135,7 @@ private extension HomeViewModel {
}

func fetchJourneys(minCoordinate: Coordinate, maxCoordinate: Coordinate) {
guard let userID = try? self.userRepository.fetchUUID() else { return }
guard let userID = self.userRepository.fetchUUID() else { return }

Task {
let result = await self.journeyRepository.fetchJourneyList(userID: userID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ extension MapViewController: CLLocationManagerDelegate {

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

recordJourneyViewModel.trigger(.locationDidUpdated(coordinate2D))
recordJourneyViewModel.trigger(.locationsShouldRecorded([coordinate2D]))
}

private func handleAuthorizationChange(_ manager: CLLocationManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public final class RecordJourneyViewModel: MapViewModel {
}
case .recordingDidCancelled:
Task {
let userID = try self.userRepository.fetchUUID()
guard let userID = self.userRepository.fetchUUID() else { return }

let recordingJourney = self.state.recordingJourney.value
let result = await self.journeyRepository.deleteJourney(recordingJourney, userID: userID)
switch result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ final class MSCollectionView: UICollectionView {

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
// Cell
let headers = self.visibleSupplementaryViews(ofKind: UICollectionView.elementKindSectionHeader)
let cells = self.visibleCells
for cell in cells where cell.frame.contains(point) {
return super.hitTest(point, with: event)
for header in headers where !header.frame.contains(point) {
return super.hitTest(point, with: event)
}
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public struct MSKeychainStorage {

/// Keychain์— ์ €์žฅ๋œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
public func deleteAll() throws {
for account in Accounts.allCases {
for account in Accounts.allCases where try self.exists(account: account.rawValue) {
try self.delete(account: account.rawValue)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// RecordSpotResponseDTO.swift
// RecordJourneyResponseDTO.swift
// MSData
//
// Created by ์ด์ฐฝ์ค€ on 2023.12.06.
//

import Foundation

public struct RecordSpotResponseDTO: Decodable {
public struct RecordJourneyResponseDTO: Decodable {

// MARK: - Properties

Expand Down
4 changes: 2 additions & 2 deletions iOS/MSData/Sources/MSData/Repository/JourneyRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ public struct JourneyRepositoryImplementation: JourneyRepository {
let coordinatesDTO = coordinates.map { CoordinateDTO($0) }
let requestDTO = RecordCoordinateRequestDTO(journeyID: journeyID, coordinates: coordinatesDTO)
let router = JourneyRouter.recordCoordinate(dto: requestDTO)
let result = await self.networking.request(RecordCoordinateRequestDTO.self, router: router)
let result = await self.networking.request(RecordJourneyResponseDTO.self, router: router)
switch result {
case .success(let responseDTO):
let coordinates = responseDTO.coordinates.map { $0.toDomain() }
let recordingJourney = RecordingJourney(id: responseDTO.journeyID,
let recordingJourney = RecordingJourney(id: journeyID,
startTimestamp: Date(),
spots: [],
coordinates: coordinates)
Expand Down
26 changes: 16 additions & 10 deletions iOS/MSData/Sources/MSData/Repository/UserRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import Foundation

import MSKeychainStorage
import MSLogger
import MSNetworking

public protocol UserRepository {

func createUser() async -> Result<UUID, Error>
func fetchUUID() throws -> UUID
func fetchUUID() -> UUID?

}

Expand All @@ -35,8 +36,12 @@ public struct UserRepositoryImplementation: UserRepository {
// MARK: - Functions

public func createUser() async -> Result<UUID, Error> {
guard let userID = try? self.fetchUUID() else {
return .failure(MSKeychainStorage.KeychainError.transactionError)
// Keychain์— UserID๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๊ณ  ์•„๋‹ˆ๋ผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑ
let userID: UUID
if let existingUserID = self.fetchUUID() {
userID = existingUserID
} else {
userID = UUID()
}

let requestDTO = UserRequestDTO(userID: userID)
Expand All @@ -52,16 +57,17 @@ public struct UserRepositoryImplementation: UserRepository {
}

/// UUID๊ฐ€ ์ด๋ฏธ ํ‚ค์ฒด์ธ์— ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค๋ฉด ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
/// ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ณ , ํ‚ค์ฒด์ธ์— ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.
public func fetchUUID() throws -> UUID {
public func fetchUUID() -> UUID? {
let account = MSKeychainStorage.Accounts.userID.rawValue
if let userID = try? self.keychain.get(UUID.self, account: account) {
return userID
guard let userID = try? self.keychain.get(UUID.self, account: account) else {
MSLogger.make(category: .keychain).error("Keychain์—์„œ UserID๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ฒƒ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.")
return nil
}

let newUserID = UUID()
try self.keychain.set(value: newUserID, account: account)
return newUserID
#if DEBUG
MSLogger.make(category: .keychain).debug("Keychain์—์„œ UserID๋ฅผ ์กฐํšŒํ–ˆ์Šต๋‹ˆ๋‹ค: \(userID)")
#endif
return userID
}

}
34 changes: 34 additions & 0 deletions iOS/MusicSpot/MusicSpot/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import UIKit

import JourneyList
import MSConstants
import MSData
import MSDesignSystem

Expand All @@ -18,6 +19,14 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
private var appCoordinator: Coordinator!

#if DEBUG
@UserDefaultsWrapped(UserDefaultsKey.recordingJourneyID, defaultValue: nil)
var recordingJourneyID: String?
@UserDefaultsWrapped(UserDefaultsKey.isFirstLaunch, defaultValue: false)
var isFirstLaunch: Bool
var keychain = MSKeychainStorage()
#endif

// MARK: - Functions

func scene(_ scene: UIScene,
Expand Down Expand Up @@ -51,3 +60,28 @@ private extension SceneDelegate {
}

}


// MARK: - Debug

#if DEBUG

import MSKeychainStorage
import MSLogger
import MSUserDefaults

private extension SceneDelegate {

func prepareToDebug() {
self.isFirstLaunch = true
self.recordingJourneyID = nil
do {
try self.keychain.deleteAll()
} catch {
MSLogger.make(category: .keychain).error("ํ‚ค์ฒด์ธ ์ดˆ๊ธฐํ™” ์‹คํŒจ")
}
}

}

#endif

0 comments on commit 7cfc5e1

Please sign in to comment.