diff --git a/iOS/Features/SaveJourney/Sources/SaveJourney/Presentation/SaveJourneyViewModel.swift b/iOS/Features/SaveJourney/Sources/SaveJourney/Presentation/SaveJourneyViewModel.swift index 3c7d1ba..536b432 100644 --- a/iOS/Features/SaveJourney/Sources/SaveJourney/Presentation/SaveJourneyViewModel.swift +++ b/iOS/Features/SaveJourney/Sources/SaveJourney/Presentation/SaveJourneyViewModel.swift @@ -9,7 +9,6 @@ import Combine import Foundation import MusicKit -import MSData import MSDomain import MSLogger diff --git a/iOS/MSData/Sources/MSData/Repository/Error/Repository+Error.swift b/iOS/MSData/Sources/MSData/RepositoryImplementation/Error/Repository+Error.swift similarity index 100% rename from iOS/MSData/Sources/MSData/Repository/Error/Repository+Error.swift rename to iOS/MSData/Sources/MSData/RepositoryImplementation/Error/Repository+Error.swift diff --git a/iOS/MSData/Sources/MSData/Repository/JourneyRepository.swift b/iOS/MSData/Sources/MSData/RepositoryImplementation/JourneyRepository.swift similarity index 88% rename from iOS/MSData/Sources/MSData/Repository/JourneyRepository.swift rename to iOS/MSData/Sources/MSData/RepositoryImplementation/JourneyRepository.swift index 2a5c26e..25ef722 100644 --- a/iOS/MSData/Sources/MSData/Repository/JourneyRepository.swift +++ b/iOS/MSData/Sources/MSData/RepositoryImplementation/JourneyRepository.swift @@ -15,23 +15,7 @@ import MSNetworking import MSPersistentStorage import MSUserDefaults -public protocol JourneyRepository: Persistable { - - func fetchIsRecording() -> Bool - mutating func updateIsRecording(_ isRecording: Bool) -> Bool - func fetchRecordingJourneyID() -> String? - func fetchRecordingJourney(forID id: String) -> RecordingJourney? - func fetchJourneyList(userID: UUID, - minCoordinate: Coordinate, - maxCoordinate: Coordinate) async -> Result<[Journey], Error> - mutating func startJourney(at coordinate: Coordinate, userID: UUID) async -> Result - mutating func endJourney(_ journey: Journey) async -> Result - func recordJourney(journeyID: String, at coordinates: [Coordinate]) async -> Result - mutating func deleteJourney(_ journey: RecordingJourney, userID: UUID) async -> Result - -} - -public struct JourneyRepositoryImplementation: JourneyRepository { +public struct JourneyRepositoryImplementation: JourneyRepository, Persistable { // MARK: - Properties @@ -131,8 +115,8 @@ public struct JourneyRepositoryImplementation: JourneyRepository { spots: [], coordinates: [responseDTO.coordinate.toDomain()]) - self.saveToLocal(value: recordingJourney.id) - self.saveToLocal(value: recordingJourney.startTimestamp) + self.saveToLocal(recordingJourney.id, at: self.storage) + self.saveToLocal(recordingJourney.startTimestamp, at: self.storage) self.recordingJourneyID = recordingJourney.id self.isRecording = true @@ -166,7 +150,9 @@ public struct JourneyRepositoryImplementation: JourneyRepository { spots: [], coordinates: coordinates) - responseDTO.coordinates.forEach { self.saveToLocal(value: $0) } + responseDTO.coordinates.forEach { + self.saveToLocal($0, at: self.storage) + } return .success(recordingJourney) case .failure(let error): diff --git a/iOS/MSData/Sources/MSData/Repository/Persistable.swift b/iOS/MSData/Sources/MSData/RepositoryImplementation/Protocol/Persistable.swift similarity index 68% rename from iOS/MSData/Sources/MSData/Repository/Persistable.swift rename to iOS/MSData/Sources/MSData/RepositoryImplementation/Protocol/Persistable.swift index f3d53d5..a986d33 100644 --- a/iOS/MSData/Sources/MSData/Repository/Persistable.swift +++ b/iOS/MSData/Sources/MSData/RepositoryImplementation/Protocol/Persistable.swift @@ -11,16 +11,6 @@ import MSDomain import MSLogger import MSPersistentStorage -public protocol Persistable { - - var storage: MSPersistentStorage { get } - - @discardableResult - func saveToLocal(value: Codable) -> Bool - func loadJourneyFromLocal() -> RecordingJourney? - -} - // MARK: - KeyStorage private struct KeyStorage { @@ -37,9 +27,9 @@ private struct KeyStorage { extension Persistable { @discardableResult - public func saveToLocal(value: Codable) -> Bool { + public func saveToLocal(_ value: Codable, at storage: MSPersistentStorage) -> Bool { let key = UUID().uuidString - self.storage.set(value: value, forKey: key) + storage.set(value: value, forKey: key) switch value { case is String: @@ -53,7 +43,7 @@ extension Persistable { if KeyStorage.startTimestamp == nil { KeyStorage.startTimestamp = key } else { - MSLogger.make(category: .persistable).debug("start tamp는 하나의 값만 저장할 수 있습니다.") + MSLogger.make(category: .persistable).debug("start timestamp는 하나의 값만 저장할 수 있습니다.") return false } case is SpotDTO: @@ -67,20 +57,20 @@ extension Persistable { return true } - public func loadJourneyFromLocal() -> RecordingJourney? { - guard let id = self.loadID(), - let startTimestamp = self.loadStartTimeStamp() else { + public func loadJourney(from storage: MSPersistentStorage) -> RecordingJourney? { + guard let id = self.loadID(from: storage), + let startTimestamp = self.loadStartTimeStamp(from: storage) else { return nil } return RecordingJourney(id: id, startTimestamp: startTimestamp, - spots: self.loadSpots(), - coordinates: self.loadCoordinates()) + spots: self.loadSpots(from: storage), + coordinates: self.loadCoordinates(from: storage)) } - func loadStartTimeStamp() -> Date? { + func loadStartTimeStamp(from storage: MSPersistentStorage) -> Date? { guard let startTimestampKey = KeyStorage.startTimestamp, - let startTimestamp = self.storage.get(Date.self, forKey: startTimestampKey) + let startTimestamp = storage.get(Date.self, forKey: startTimestampKey) else { MSLogger.make(category: .persistable).debug("id 또는 startTimestamp가 저장되지 않았습니다.") return nil @@ -88,25 +78,25 @@ extension Persistable { return startTimestamp } - func loadID() -> String? { + func loadID(from storage: MSPersistentStorage) -> String? { guard let idKey = KeyStorage.id, - let id = self.storage.get(String.self, forKey: idKey) else { + let id = storage.get(String.self, forKey: idKey) else { MSLogger.make(category: .persistable).debug("id 또는 startTimestamp가 저장되지 않았습니다.") return nil } return id } - func loadSpots() -> [Spot] { + func loadSpots(from storage: MSPersistentStorage) -> [Spot] { return KeyStorage.spots.compactMap { spotKey in - let spotDTO = self.storage.get(SpotDTO.self, forKey: spotKey) + let spotDTO = storage.get(SpotDTO.self, forKey: spotKey) return spotDTO?.toDomain() } } - func loadCoordinates() -> [Coordinate] { + func loadCoordinates(from storage: MSPersistentStorage) -> [Coordinate] { return KeyStorage.coordinates.compactMap { coordinateKey in - let coordinateDTO = self.storage.get(CoordinateDTO.self, forKey: coordinateKey) + let coordinateDTO = storage.get(CoordinateDTO.self, forKey: coordinateKey) return coordinateDTO?.toDomain() } } diff --git a/iOS/MSData/Sources/MSData/Repository/SongRepository.swift b/iOS/MSData/Sources/MSData/RepositoryImplementation/SongRepository.swift similarity index 88% rename from iOS/MSData/Sources/MSData/Repository/SongRepository.swift rename to iOS/MSData/Sources/MSData/RepositoryImplementation/SongRepository.swift index 843687e..5cf1f6b 100644 --- a/iOS/MSData/Sources/MSData/Repository/SongRepository.swift +++ b/iOS/MSData/Sources/MSData/RepositoryImplementation/SongRepository.swift @@ -8,18 +8,10 @@ import Foundation import MusicKit +import MSDomain import MSLogger import MSNetworking -public protocol SongRepository { - - func fetchSong(withID id: String) async -> Result - func fetchSongList(with term: String) async -> Result, Error> - @available(iOS 16.0, *) - func fetchSongListByRank() async -> Result, Error> - -} - public struct SongRepositoryImplementation: SongRepository { // MARK: - Properties diff --git a/iOS/MSData/Sources/MSData/Repository/SpotRepository.swift b/iOS/MSData/Sources/MSData/RepositoryImplementation/SpotRepository.swift similarity index 89% rename from iOS/MSData/Sources/MSData/Repository/SpotRepository.swift rename to iOS/MSData/Sources/MSData/RepositoryImplementation/SpotRepository.swift index 336cb94..fab0bc7 100644 --- a/iOS/MSData/Sources/MSData/Repository/SpotRepository.swift +++ b/iOS/MSData/Sources/MSData/RepositoryImplementation/SpotRepository.swift @@ -12,14 +12,7 @@ import MSNetworking import MSLogger import MSPersistentStorage -public protocol SpotRepository: Persistable { - - func fetchRecordingSpots() async -> Result<[Spot], Error> - func upload(spot: CreateSpotRequestDTO) async -> Result - -} - -public struct SpotRepositoryImplementation: SpotRepository { +public struct SpotRepositoryImplementation: SpotRepository, Persistable { // MARK: - Properties @@ -74,7 +67,7 @@ public struct SpotRepositoryImplementation: SpotRepository { #if DEBUG MSLogger.make(category: .network).debug("성공적으로 업로드하였습니다.") #endif - self.saveToLocal(value: spot) + self.saveToLocal(spot, at: self.storage) return .success(spot.toDomain()) case .failure(let error): MSLogger.make(category: .network).error("\(error): 업로드에 실패하였습니다.") diff --git a/iOS/MSData/Sources/MSData/Repository/UserRepository.swift b/iOS/MSData/Sources/MSData/RepositoryImplementation/UserRepository.swift similarity index 94% rename from iOS/MSData/Sources/MSData/Repository/UserRepository.swift rename to iOS/MSData/Sources/MSData/RepositoryImplementation/UserRepository.swift index 24954a7..d18b04a 100644 --- a/iOS/MSData/Sources/MSData/Repository/UserRepository.swift +++ b/iOS/MSData/Sources/MSData/RepositoryImplementation/UserRepository.swift @@ -7,18 +7,11 @@ import Foundation +import MSDomain import MSKeychainStorage import MSLogger import MSNetworking -public protocol UserRepository { - - func createUser() async -> Result - func storeUUID(_ userID: UUID) throws -> UUID - func fetchUUID() -> UUID? - -} - public struct UserRepositoryImplementation: UserRepository { // MARK: - Properties diff --git a/iOS/MSDomain/Sources/MSDomain/Repository/JourneyRepository.swift b/iOS/MSDomain/Sources/MSDomain/Repository/JourneyRepository.swift new file mode 100644 index 0000000..46af232 --- /dev/null +++ b/iOS/MSDomain/Sources/MSDomain/Repository/JourneyRepository.swift @@ -0,0 +1,24 @@ +// +// JourneyRepository.swift +// MSDomain +// +// Created by 이창준 on 2023.12.24. +// + +import Foundation + +public protocol JourneyRepository { + + func fetchIsRecording() -> Bool + mutating func updateIsRecording(_ isRecording: Bool) -> Bool + func fetchRecordingJourneyID() -> String? + func fetchRecordingJourney(forID id: String) -> RecordingJourney? + func fetchJourneyList(userID: UUID, + minCoordinate: Coordinate, + maxCoordinate: Coordinate) async -> Result<[Journey], Error> + mutating func startJourney(at coordinate: Coordinate, userID: UUID) async -> Result + mutating func endJourney(_ journey: Journey) async -> Result + func recordJourney(journeyID: String, at coordinates: [Coordinate]) async -> Result + mutating func deleteJourney(_ journey: RecordingJourney, userID: UUID) async -> Result + +} diff --git a/iOS/MSDomain/Sources/MSDomain/Repository/Protocol/Persistable.swift b/iOS/MSDomain/Sources/MSDomain/Repository/Protocol/Persistable.swift new file mode 100644 index 0000000..594f6a7 --- /dev/null +++ b/iOS/MSDomain/Sources/MSDomain/Repository/Protocol/Persistable.swift @@ -0,0 +1,16 @@ +// +// Persistable.swift +// MSDomain +// +// Created by 이창준 on 2023.12.26. +// + +import Foundation + +public protocol Persistable { + + @discardableResult + func saveToLocal(_ value: Codable, at storage: MSPersistentStorage) -> Bool + func loadJourney() -> RecordingJourney? + +} diff --git a/iOS/MSDomain/Sources/MSDomain/Repository/SongRepository.swift b/iOS/MSDomain/Sources/MSDomain/Repository/SongRepository.swift new file mode 100644 index 0000000..7ef0039 --- /dev/null +++ b/iOS/MSDomain/Sources/MSDomain/Repository/SongRepository.swift @@ -0,0 +1,18 @@ +// +// SongRepository.swift +// MSDomain +// +// Created by 이창준 on 2023.12.24. +// + +import Foundation +import MusicKit + +public protocol SongRepository { + + func fetchSong(withID id: String) async -> Result + func fetchSongList(with term: String) async -> Result, Error> + @available(iOS 16.0, *) + func fetchSongListByRank() async -> Result, Error> + +} diff --git a/iOS/MSDomain/Sources/MSDomain/Repository/SpotRepository.swift b/iOS/MSDomain/Sources/MSDomain/Repository/SpotRepository.swift new file mode 100644 index 0000000..5abeb1e --- /dev/null +++ b/iOS/MSDomain/Sources/MSDomain/Repository/SpotRepository.swift @@ -0,0 +1,15 @@ +// +// SpotRepository.swift +// MSDomain +// +// Created by 이창준 on 2023.12.24. +// + +import Foundation + +public protocol SpotRepository { + + func fetchRecordingSpots() async -> Result<[Spot], Error> + func upload(spot: Spot) async -> Result + +} diff --git a/iOS/MSDomain/Sources/MSDomain/Repository/UserRepository.swift b/iOS/MSDomain/Sources/MSDomain/Repository/UserRepository.swift new file mode 100644 index 0000000..68a4d44 --- /dev/null +++ b/iOS/MSDomain/Sources/MSDomain/Repository/UserRepository.swift @@ -0,0 +1,16 @@ +// +// UserRepository.swift +// MSDomain +// +// Created by 이창준 on 2023.12.24. +// + +import Foundation + +public protocol UserRepository { + + func createUser() async -> Result + func storeUUID(_ userID: UUID) throws -> UUID + func fetchUUID() -> UUID? + +}