-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix [#80] 소식 탭 오류 수정 및 배지 구현, 알림 탭 프로필 이동 구현
- Loading branch information
Showing
29 changed files
with
968 additions
and
482 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
34 changes: 34 additions & 0 deletions
34
Wable-iOS/Global/Shared/UserDefaults/CodableSerializable.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,34 @@ | ||
// | ||
// CodableSerializable.swift | ||
// Wable-iOS | ||
// | ||
// Created by 김진웅 on 1/7/25. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol Serializable { | ||
func serialize(_ value: Encodable) throws -> Data | ||
} | ||
|
||
protocol Deserializable { | ||
func deserialize<T: Decodable>(_ type: T.Type, from data: Data) throws -> T | ||
} | ||
|
||
typealias CodableSerializable = Serializable & Deserializable | ||
|
||
|
||
// MARK: - JSONSerializer | ||
|
||
struct JSONSerializer: CodableSerializable { | ||
private let encoder = JSONEncoder() | ||
private let decoder = JSONDecoder() | ||
|
||
func serialize(_ value: any Encodable) throws -> Data { | ||
try encoder.encode(value) | ||
} | ||
|
||
func deserialize<T: Decodable>(_ type: T.Type, from data: Data) throws -> T { | ||
try decoder.decode(type, from: data) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Wable-iOS/Global/Shared/UserDefaults/UserDefaultsKey.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,28 @@ | ||
// | ||
// UserDefaultsKey.swift | ||
// Wable-iOS | ||
// | ||
// Created by 김진웅 on 1/7/25. | ||
// | ||
|
||
import Foundation | ||
|
||
/// UserDefaults에 접근하기 위한 Key의 타입을 정의하는 프로토콜 | ||
/// | ||
/// 아래와 같이, 열거형으로 구현한다. | ||
/// | ||
/// ```swift | ||
/// enum UserDefaultsKeys: UserDefaultsKey { | ||
/// case userInfo | ||
/// | ||
/// var value: String { | ||
/// switch self{ | ||
/// case .userInfo: | ||
/// return "userInfo" | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
protocol UserDefaultsKey: Hashable, CaseIterable { | ||
var value: String { get } | ||
} |
71 changes: 71 additions & 0 deletions
71
Wable-iOS/Global/Shared/UserDefaults/UserDefaultsManager.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,71 @@ | ||
// | ||
// UserDefaultsManager.swift | ||
// Wable-iOS | ||
// | ||
// Created by 김진웅 on 1/7/25. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol UserDefaultsManager { | ||
func save<T: Codable>(_ value: T, forKey key: any UserDefaultsKey) | ||
func load<T: Codable>(forKey key: any UserDefaultsKey, as type: T.Type) -> T? | ||
func remove(forKey keys: any UserDefaultsKey...) | ||
func removeAll() | ||
} | ||
|
||
final class UserDefaultsManagerImpl: UserDefaultsManager { | ||
private let serializer: CodableSerializable | ||
private let userDefaults = UserDefaults.standard | ||
|
||
init(serializer: CodableSerializable = JSONSerializer()) { | ||
self.serializer = serializer | ||
} | ||
|
||
func save<T: Codable>(_ value: T, forKey key: any UserDefaultsKey) { | ||
if isPrimitiveType(T.self) { | ||
userDefaults.set(value, forKey: key.value) | ||
return | ||
} | ||
|
||
do { | ||
let data = try serializer.serialize(value) | ||
userDefaults.set(data, forKey: key.value) | ||
} catch { | ||
print("Failed to encode data for key \(key.value): \(error)") | ||
} | ||
} | ||
|
||
func load<T: Codable>(forKey key: any UserDefaultsKey, as type: T.Type) -> T? { | ||
if isPrimitiveType(T.self) { | ||
return userDefaults.object(forKey: key.value) as? T | ||
} | ||
|
||
guard let data = userDefaults.data(forKey: key.value) else { | ||
return nil | ||
} | ||
|
||
do { | ||
return try serializer.deserialize(type, from: data) | ||
} catch { | ||
print("Failed to decode data for key \(key.value): \(error)") | ||
return nil | ||
} | ||
} | ||
|
||
/// 특정 키 삭제 | ||
func remove(forKey keys: any UserDefaultsKey...) { | ||
keys.forEach { self.userDefaults.removeObject(forKey: $0.value) } | ||
} | ||
|
||
/// 모든 데이터 삭제 | ||
func removeAll() { | ||
userDefaults.dictionaryRepresentation().forEach { (key, _) in | ||
userDefaults.removeObject(forKey: key) | ||
} | ||
} | ||
|
||
private func isPrimitiveType<T>(_ type: T.Type) -> Bool { | ||
return type is Int.Type || type is Double.Type || type is Float.Type || type is Bool.Type || type is String.Type | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// InfoCountDTO.swift | ||
// Wable-iOS | ||
// | ||
// Created by 김진웅 on 1/7/25. | ||
// | ||
|
||
import Foundation | ||
|
||
struct InfoCountDTO: Codable { | ||
let newsCount: Int | ||
let noticeCount: Int | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case newsCount = "newsNumber" | ||
case noticeCount = "noticeNumber" | ||
} | ||
} |
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
Oops, something went wrong.