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] 전반적인 리팩토링 #355

Closed
wants to merge 6 commits into from
9 changes: 5 additions & 4 deletions iOS/MSCoreKit/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ private extension String {
private enum Target {

static let msImageFetcher = "MSImageFetcher"
static let keychainStorage = "KeychainStorage"
static let msPersistentStorage = "MSPersistentStorage"
static let msNetworking = "MSNetworking"
static let msCacheStorage = "MSCacheStorage"
static let msKeychainStorage = "MSKeychainStorage"
static let msMapKit = "MSMapKit"

}
Expand Down Expand Up @@ -54,8 +54,8 @@ let package = Package(
targets: [Target.msNetworking]),
.library(name: Target.msCacheStorage,
targets: [Target.msCacheStorage]),
.library(name: Target.msKeychainStorage,
targets: [Target.msKeychainStorage])
.library(name: Target.keychainStorage,
targets: [Target.keychainStorage])
],
dependencies: [
.package(name: Dependency.msFoundation,
Expand Down Expand Up @@ -86,8 +86,9 @@ let package = Package(
.product(name: Dependency.msConstants,
package: Dependency.msFoundation)
]),
.target(name: Target.msKeychainStorage,
.target(name: Target.keychainStorage,
dependencies: [
.target(name: Target.msPersistentStorage),
.product(name: Dependency.msLogger,
package: Dependency.msFoundation),
.product(name: Dependency.msConstants,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// MSKeychainStorage+Constants.swift
// KeychainStorage+Constants.swift
// MSCoreKit
//
// Created by 이창준 on 2023.12.06.
//

import MSConstants

extension MSKeychainStorage {
extension KeychainStorage {

enum KeychainConstants {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// MSKeychainStorage+Error.swift
// KeychainStorage+Error.swift
// MSCoreKit
//
// Created by 이창준 on 2023.12.06.
//

import Foundation

extension MSKeychainStorage {
extension KeychainStorage {

public enum KeychainError: Error {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MSKeychainStorage+Transaction.swift
// KeychainStorage+Transaction.swift
// MSCoreKit
//
// Created by 이창준 on 2023.12.06.
Expand All @@ -9,7 +9,7 @@ import Foundation

import MSLogger

extension MSKeychainStorage {
extension KeychainStorage {

/// Keychain으로부터 데이터를 불러옵니다.
/// - Parameters:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//
// MSKeychainStorage.swift
// KeychainStorage.swift
// MSCoreKit
//
// Created by 이창준 on 2023.12.06.
//

import Foundation

public struct MSKeychainStorage {
import MSPersistentStorage

public struct KeychainStorage: MSPersistentStorage {

// MARK: - Properties

Expand All @@ -27,12 +29,19 @@ public struct MSKeychainStorage {
/// - Parameters:
/// - value: Keychain에 저장할 데이터 (`Data`)
/// - account: Keychain에 저장할 데이터에 대응되는 Key (`String`)
public func set<T: Codable>(value: T, account: String) throws {
@discardableResult
public func set<T: Codable>(value: T, forKey key: String, subpath: String? = nil) throws -> T? {
let encodedValue = try self.encoder.encode(value)
if try self.exists(account: account) {
try self.update(value: encodedValue, account: account)
} else {
try self.add(value: encodedValue, account: account)

do {
if try self.exists(account: key) {
try self.update(value: encodedValue, account: key)
} else {
try self.add(value: encodedValue, account: key)
}
return value
} catch {
return nil
}
}

Expand All @@ -43,9 +52,9 @@ public struct MSKeychainStorage {
/// - Parameters:
/// - account: Keychain에서 조회할 데이터에 대응되는 Key (`String`)
/// - Returns: Keychain에 저장된 데이터
public func get<T: Codable>(_ type: T.Type, account: String) throws -> T? {
if try self.exists(account: account) {
guard let value = try self.fetch(account: account) else {
public func get<T: Codable>(_ type: T.Type, forKey key: String, subpath: String?) throws -> T? {
if try self.exists(account: key) {
guard let value = try self.fetch(account: key) else {
return nil
}
return try self.decoder.decode(T.self, from: value)
Expand All @@ -58,18 +67,18 @@ public struct MSKeychainStorage {
///
/// - Parameters:
/// - account: Keychain에서 삭제할 값에 대응되는 Key (`String`)
public func delete(account: String) throws {
if try self.exists(account: account) {
return try self.remove(account: account)
public func delete(forKey key: String, subpath: String? = nil) throws {
if try self.exists(account: key) {
return try self.remove(account: key)
} else {
throw KeychainError.transactionError
}
}

/// Keychain에 저장된 모든 데이터를 삭제합니다.
public func deleteAll() throws {
public func deleteAll(subpath: String? = nil) throws {
for account in Accounts.allCases where try self.exists(account: account.rawValue) {
try self.delete(account: account.rawValue)
try self.delete(forKey: account.rawValue)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ public final class FileManagerStorage: NSObject, MSPersistentStorage {
/// - type: 불러올 값의 타입
/// - key: 불러올 값에 대응되는 Key 값
/// - Returns: `FileManager`에서 불러온 뒤 지정된 타입으로 디코딩 된 값
public func get<T: Codable>(_ type: T.Type,
forKey key: String,
subpath: String? = nil) -> T? {
public func get<T: Codable>(_ type: T.Type, forKey key: String, subpath: String? = nil) throws -> T? {
guard let fileURL = self.fileURL(forKey: key, subpath: subpath),
let data = try? Data(contentsOf: fileURL) else {
return nil
Expand Down Expand Up @@ -106,9 +104,7 @@ public final class FileManagerStorage: NSObject, MSPersistentStorage {
/// `FileManager`를 사용한 저장에 성공했을 경우 요청한 데이터를 반환합니다. \
/// 저장에 실패했거나 이미 존재한다면 `nil`을 반환합니다.
@discardableResult
public func set<T: Codable>(value: T,
forKey key: String,
subpath: String? = nil) -> T? {
public func set<T: Codable>(value: T, forKey key: String, subpath: String? = nil) throws -> T? {
guard let fileURL = self.fileURL(forKey: key, subpath: subpath) else {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@

public protocol MSPersistentStorage {

func get<T: Codable>(_ type: T.Type, forKey key: String, subpath: String?) -> T?
func get<T: Codable>(_ type: T.Type, forKey key: String, subpath: String?) throws -> T?
@discardableResult
func set<T: Codable>(value: T, forKey key: String, subpath: String?) -> T?
func set<T: Codable>(value: T, forKey key: String, subpath: String?) throws -> T?
func delete(forKey key: String, subpath: String?) throws
func deleteAll(subpath: String?) throws

}

extension MSPersistentStorage {

public func get<T: Codable>(_ type: T.Type, forKey key: String, subpath: String? = nil) -> T? {
return self.get(type, forKey: key, subpath: nil)
public func get<T: Codable>(_ type: T.Type, forKey key: String, subpath: String? = nil) throws -> T? {
return try self.get(type, forKey: key, subpath: nil)
}

@discardableResult
public func set<T: Codable>(value: T, forKey key: String, subpath: String? = nil) -> T? {
return self.set(value: value, forKey: key, subpath: subpath)
public func set<T: Codable>(value: T, forKey key: String, subpath: String? = nil) throws -> T? {
return try self.set(value: value, forKey: key, subpath: subpath)
}

public func delete(forKey key: String, subpath: String? = nil) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ final class MSPersistentStorageTests: XCTestCase {
XCTAssertFalse(fileExists, "storageURL(create: true)는 디렉토리가 존재하지 않을 경우 새로 생성해야 합니다.")
}

func test_FileManagerStorage에_데이터저장_성공() {
func test_FileManagerStorage에_데이터저장_성공() throws {
let sut = MockCodableData(title: "boostcamp", content: "wm8")
let key = "S045"

let storedData = self.fileStorage.set(value: sut, forKey: key)
let storedData = try self.fileStorage.set(value: sut, forKey: key)
XCTAssertNotNil(storedData, "데이터가 저장되지 않았습니다.")
}

func test_FileManagerStorage에서_데이터로드_성공() {
func test_FileManagerStorage에서_데이터로드_성공() throws {
let sut = MockCodableData(title: "boostcamp", content: "wm8")
let key = "S045"
self.fileStorage.set(value: sut, forKey: key)
try self.fileStorage.set(value: sut, forKey: key)

guard let storedData = self.fileStorage.get(MockCodableData.self, forKey: key) else {
guard let storedData = try self.fileStorage.get(MockCodableData.self, forKey: key) else {
XCTFail("데이터 읽기에 실패했습니다.")
return
}
Expand All @@ -100,14 +100,14 @@ final class MSPersistentStorageTests: XCTestCase {
"목표 데이터와 불러온 값이 다릅니다.")
}

func test_FileManagerStorage에서_모든데이터저장불러오기_성공() {
func test_FileManagerStorage에서_모든데이터저장불러오기_성공() throws {
let sut1 = MockCodableData(title: "boostcamp", content: "wm8")
let sut2 = MockCodableData(title: "boostcamp", content: "wm8")
let key1 = "S045"
let key2 = "S034"

self.fileStorage.set(value: sut1, forKey: key1)
self.fileStorage.set(value: sut2, forKey: key2)
try self.fileStorage.set(value: sut1, forKey: key1)
try self.fileStorage.set(value: sut2, forKey: key2)

guard let allStoredData = self.fileStorage.getAllOf(MockCodableData.self) else {
XCTFail("데이터 읽기에 실패했습니다.")
Expand All @@ -118,14 +118,14 @@ final class MSPersistentStorageTests: XCTestCase {
XCTAssertTrue(allStoredData.allSatisfy { $0 == sut1 || $0 == sut2 })
}

func test_FileManagerStorage에서_모든데이터저장불러올때_폴더하위항목까지_읽을수있는지_실패() {
func test_FileManagerStorage에서_모든데이터저장불러올때_폴더하위항목까지_읽을수있는지_실패() throws {
let sut1 = MockCodableData(title: "boostcamp", content: "wm8")
let sut2 = MockCodableData(title: "boostcamp", content: "wm8")
let key1 = "S045"
let key2 = "/handsome/jeonmingun/S034"

self.fileStorage.set(value: sut1, forKey: key1)
self.fileStorage.set(value: sut2, forKey: key2)
try self.fileStorage.set(value: sut1, forKey: key1)
try self.fileStorage.set(value: sut2, forKey: key2)

guard let allStoredData = self.fileStorage.getAllOf(MockCodableData.self) else {
XCTFail("데이터 읽기에 실패했습니다.")
Expand All @@ -136,13 +136,13 @@ final class MSPersistentStorageTests: XCTestCase {
XCTAssertFalse(allStoredData.allSatisfy { $0 == sut1 || $0 == sut2 })
}

func test_Date형식_저장할_수_있는지_성공() {
func test_Date형식_저장할_수_있는지_성공() throws {
let sut = Date.now
let key = "S034"

self.fileStorage.set(value: sut, forKey: key)
try self.fileStorage.set(value: sut, forKey: key)

guard let storedData = self.fileStorage.get(Date.self, forKey: key) else {
guard let storedData = try? self.fileStorage.get(Date.self, forKey: key) else {
XCTFail("데이터 읽기에 실패했습니다.")
return
}
Expand Down
4 changes: 2 additions & 2 deletions iOS/MSData/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private enum Dependency {
static let msDomain = "MSDomain"
static let msImageFetcher = "MSImageFetcher"
static let msNetworking = "MSNetworking"
static let msKeychainStorage = "MSKeychainStorage"
static let keychainStorage = "KeychainStorage"
static let msPersistentStorage = "MSPersistentStorage"
static let msCoreKit = "MSCoreKit"
static let msExtension = "MSExtension"
Expand Down Expand Up @@ -69,7 +69,7 @@ let package = Package(
package: Dependency.msCoreKit),
.product(name: Dependency.msNetworking,
package: Dependency.msCoreKit),
.product(name: Dependency.msKeychainStorage,
.product(name: Dependency.keychainStorage,
package: Dependency.msCoreKit),
.product(name: Dependency.msPersistentStorage,
package: Dependency.msCoreKit),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct JourneyRepositoryImplementation: JourneyRepository {
private let networking: MSNetworking
private let storage: MSPersistentStorage

private var recordingJourney: RecordingJourneyStorage
private var recordingJourney: RecordingJourneyService

public var isRecording: Bool {
return self.recordingJourney.isRecording
Expand All @@ -36,7 +36,7 @@ public struct JourneyRepositoryImplementation: JourneyRepository {
persistentStorage: MSPersistentStorage = FileManagerStorage()) {
self.networking = MSNetworking(session: session)
self.storage = persistentStorage
self.recordingJourney = RecordingJourneyStorage.shared
self.recordingJourney = RecordingJourneyService.shared
}

// MARK: - Functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public struct SpotRepositoryImplementation: SpotRepository {
#if DEBUG
MSLogger.make(category: .network).debug("성공적으로 업로드하였습니다.")
#endif
RecordingJourneyStorage.shared.record([spot], keyPath: \.spots)
RecordingJourneyService.shared.record([spot], keyPath: \.spots)
return .success(spot.toDomain())
case .failure(let error):
MSLogger.make(category: .network).error("\(error): 업로드에 실패하였습니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@

import Foundation

import KeychainStorage
import MSDomain
import MSKeychainStorage
import MSLogger
import MSNetworking
import MSPersistentStorage

public struct UserRepositoryImplementation: UserRepository {

// MARK: - Properties

private let networking: MSNetworking
private let keychain: MSKeychainStorage
private let keychain: MSPersistentStorage

// MARK: - Initializer

public init(session: URLSession = URLSession(configuration: .default),
keychain: MSKeychainStorage = MSKeychainStorage()) {
keychain: KeychainStorage = KeychainStorage()) {
self.networking = MSNetworking(session: session)
self.keychain = keychain
}
Expand Down Expand Up @@ -66,23 +67,23 @@ public struct UserRepositoryImplementation: UserRepository {

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

do {
try self.keychain.set(value: userID, account: account)
try self.keychain.set(value: userID, forKey: account)
#if DEBUG
MSLogger.make(category: .keychain).debug("Keychain에 서버 정보를 저장했습니다.")
#endif
return userID
} catch {
throw MSKeychainStorage.KeychainError.creationError
throw KeychainStorage.KeychainError.creationError
}
}

/// UUID가 이미 키체인에 등록되어 있다면 가져옵니다.
public func fetchUUID() -> UUID? {
let account = MSKeychainStorage.Accounts.userID.rawValue
guard let userID = try? self.keychain.get(UUID.self, account: account) else {
let account = KeychainStorage.Accounts.userID.rawValue
guard let userID = try? self.keychain.get(UUID.self, forKey: account) else {
MSLogger.make(category: .keychain).error("Keychain에서 UserID를 조회하는 것에 실패했습니다.")
return nil
}
Expand Down
Loading
Loading