Skip to content

Commit

Permalink
Merge pull request #77 from uhooi/feature/fix_showing_error
Browse files Browse the repository at this point in the history
Fix Error alert appears at first launch
  • Loading branch information
uhooi authored Nov 20, 2022
2 parents 32e4f4e + 4365b55 commit 29fb26a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ public typealias UserDefaultsGettable = Decodable
public typealias UserDefaultsSettable = Encodable
public typealias UserDefaultsPersistable = UserDefaultsGettable & UserDefaultsSettable

private enum UserDefaultsError: LocalizedError {
case gettingFailed(key: String)
public enum UserDefaultsError: LocalizedError {
case missingValue(key: String)

var errorDescription: String? {
public var errorDescription: String? {
switch self {
case .gettingFailed:
return "オブジェクトの取得に失敗しました"
case .missingValue:
return "対象のキーに値が存在しません"
}
}
}
Expand All @@ -26,7 +26,7 @@ public struct UserDefaultsClient {
let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
guard let data = userDefaults.data(forKey: defaultName) else {
throw UserDefaultsError.gettingFailed(key: defaultName)
throw UserDefaultsError.missingValue(key: defaultName)
}
return try jsonDecoder.decode(V.self, from: data)
}
Expand Down
8 changes: 7 additions & 1 deletion TotonoiPackage/Sources/Data/Sakatsu/SakatsuRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ public struct SakatsuUserDefaultsClient {

extension SakatsuUserDefaultsClient: SakatsuRepository {
public func sakatsus() throws -> [Sakatsu] {
try userDefaultsClient.object(forKey: Self.sakatsusKey)
do {
return try userDefaultsClient.object(forKey: Self.sakatsusKey)
} catch UserDefaultsError.missingValue {
return []
} catch {
throw error
}
}

public func saveSakatsus(_ sakatsus: [Sakatsu]) throws {
Expand Down

0 comments on commit 29fb26a

Please sign in to comment.