-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #278 from boostcampwm2023/iOS/task/upgradeLocalSto…
…rage [iOS] Local Storage coordinate 외 정보들도 저장할 수 있게끔 수정
- Loading branch information
Showing
8 changed files
with
246 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
iOS/MSData/Sources/MSData/Repository/JourneyRepository+Persistable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// | ||
// JourneyRepository+Persistable.swift | ||
// MSData | ||
// | ||
// Created by 전민건 on 12/10/23. | ||
// | ||
|
||
import Foundation | ||
|
||
import MSDomain | ||
import MSLogger | ||
import MSPersistentStorage | ||
|
||
public protocol Persistable { | ||
|
||
func saveToLocal(value: Codable) -> Bool | ||
func loadJourneyFromLocal() -> RecordingJourney? | ||
|
||
} | ||
|
||
// MARK: - Interface | ||
|
||
extension JourneyRepositoryImplementation: Persistable { | ||
|
||
private struct KeyStorage { | ||
|
||
static var id: String? = nil | ||
static var startTimestamp: String? = nil | ||
static var spots = [String]() | ||
static var coordinates = [String]() | ||
|
||
} | ||
|
||
@discardableResult | ||
public func saveToLocal(value: Codable) -> Bool { | ||
let key = UUID().uuidString | ||
self.storage.set(value: value, forKey: key) | ||
|
||
switch value { | ||
case is String: | ||
if KeyStorage.id == nil { | ||
KeyStorage.id = key | ||
} else { | ||
MSLogger.make(category: .persistable).debug("journey ID는 하나의 값만 저장할 수 있습니다.") | ||
return false | ||
} | ||
case is Date: | ||
if KeyStorage.startTimestamp == nil { | ||
KeyStorage.startTimestamp = key | ||
} else { | ||
MSLogger.make(category: .persistable).debug("start tamp는 하나의 값만 저장할 수 있습니다.") | ||
return false | ||
} | ||
case is SpotDTO: | ||
KeyStorage.spots.append(key) | ||
case is CoordinateDTO: | ||
KeyStorage.coordinates.append(key) | ||
default: | ||
MSLogger.make(category: .persistable).debug("RecordingJourney 타입의 요소들만 넣을 수 있습니다.") | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
public func loadJourneyFromLocal() -> RecordingJourney? { | ||
guard let id = self.loadID(), | ||
let startTimestamp = self.loadStartTimeStamp() else { | ||
return nil | ||
} | ||
return RecordingJourney(id: id, | ||
startTimestamp: startTimestamp, | ||
spots: self.loadSpots(), | ||
coordinates: self.loadCoordinates()) | ||
} | ||
|
||
} | ||
|
||
// MARK: - load Functions | ||
|
||
private extension JourneyRepositoryImplementation { | ||
|
||
func loadStartTimeStamp() -> Date? { | ||
guard let startTimestampKey = KeyStorage.startTimestamp, | ||
let startTimestamp = self.storage.get(Date.self, forKey: startTimestampKey) | ||
else { | ||
MSLogger.make(category: .persistable).debug("id 또는 startTimestamp가 저장되지 않았습니다.") | ||
return nil | ||
} | ||
return startTimestamp | ||
} | ||
|
||
func loadID() -> String? { | ||
guard let idKey = KeyStorage.id, | ||
let id = self.storage.get(String.self, forKey: idKey) else { | ||
MSLogger.make(category: .persistable).debug("id 또는 startTimestamp가 저장되지 않았습니다.") | ||
return nil | ||
} | ||
return id | ||
} | ||
|
||
func loadSpots() -> [Spot] { | ||
return KeyStorage.spots.compactMap { spotKey in | ||
let spotDTO = self.storage.get(SpotDTO.self, forKey: spotKey) | ||
return spotDTO?.toDomain() | ||
} | ||
} | ||
|
||
func loadCoordinates() -> [Coordinate] { | ||
return KeyStorage.coordinates.compactMap { coordinateKey in | ||
let coordinateDTO = self.storage.get(CoordinateDTO.self, forKey: coordinateKey) | ||
return coordinateDTO?.toDomain() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
iOS/MSData/Sources/MSData/Repository/LocalRepository.swift
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
iOS/MSData/Tests/RepositoryTests/PersistableRepositoryTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// PersistableRepositoryTests.swift | ||
// | ||
// | ||
// Created by 전민건 on 12/11/23. | ||
// | ||
|
||
import XCTest | ||
@testable import MSData | ||
@testable import MSDomain | ||
|
||
final class PersistableRepositoryTests: XCTestCase { | ||
|
||
// MARK: - Properties | ||
|
||
private let journeyRepository = JourneyRepositoryImplementation() | ||
|
||
// MARK: - Tests | ||
|
||
func test_Spot저장_성공() { | ||
let coordinate = Coordinate(latitude: 10, longitude: 10) | ||
let url = URL(string: "/../")! | ||
|
||
let spot = Spot(coordinate: coordinate, timestamp: .now, photoURL: url) | ||
|
||
XCTAssertTrue(self.journeyRepository.saveToLocal(value: SpotDTO(spot))) | ||
} | ||
|
||
func test_RecordingJourney_하위요소가_아닌_것들_저장_실패() { | ||
XCTAssertFalse(self.journeyRepository.saveToLocal(value: Int())) | ||
} | ||
|
||
func test_RecordingJourney_반환_성공() { | ||
let url = URL(string: "/../")! | ||
|
||
let id = "id" | ||
let startTimestamp = Date.now | ||
let coordinate = Coordinate(latitude: 5, longitude: 5) | ||
let spot = Spot(coordinate: coordinate, timestamp: .now, photoURL: url) | ||
|
||
self.journeyRepository.saveToLocal(value: id) | ||
self.journeyRepository.saveToLocal(value: Date.now) | ||
self.journeyRepository.saveToLocal(value: SpotDTO(spot)) | ||
self.journeyRepository.saveToLocal(value: CoordinateDTO(coordinate)) | ||
|
||
guard let loadedJourney = self.journeyRepository.loadJourneyFromLocal() else { | ||
XCTFail("load 실패") | ||
return | ||
} | ||
|
||
XCTAssertEqual(loadedJourney.id, id) | ||
XCTAssertEqual(loadedJourney.startTimestamp.description, startTimestamp.description) | ||
XCTAssertEqual(loadedJourney.spots.description, [spot].description) | ||
XCTAssertEqual(loadedJourney.coordinates, [coordinate]) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters