From f7a065e7a8bcb9f195cd02c20fb1c091a63d686a Mon Sep 17 00:00:00 2001 From: Jota Uribe Date: Tue, 12 Mar 2024 15:04:21 -0500 Subject: [PATCH] Release 0.5.0 (#7) * Introduce Parent & Child View Models definitions * Update StorageController definitions, remove Storable protocol * Add two & three constants * Update README to update contents --- README.md | 2 -- .../Controllers/StorageController.swift | 20 ++++++++++++---- .../AdditiveArithmetic+Caffeine.swift | 2 ++ Sources/Caffeine/Presentation/ViewModel.swift | 21 +++++++++++++++++ Sources/Caffeine/Protocols/Storable.swift | 23 ------------------- 5 files changed, 39 insertions(+), 29 deletions(-) delete mode 100644 Sources/Caffeine/Protocols/Storable.swift diff --git a/README.md b/README.md index 1cbaa6a..02d6bb5 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,4 @@ A collection of reusable logic to boost Apple platform apps development process. │ │ └── AlertStateModifier.swift │ ├── SystemImage.swift │ └── ViewModel.swift -├── Protocols -│ └── Storable.swift ``` diff --git a/Sources/Caffeine/Controllers/StorageController.swift b/Sources/Caffeine/Controllers/StorageController.swift index 3c1ef5a..202bfc2 100644 --- a/Sources/Caffeine/Controllers/StorageController.swift +++ b/Sources/Caffeine/Controllers/StorageController.swift @@ -9,18 +9,30 @@ import Foundation // Defines convenience methods for ´Codable´ types storage. public protocol StorageController { - func read(with key: String) throws -> T? where T: Codable - func write(with key: String, value: T) throws where T: Codable + func read(with key: String) throws -> T? where T: Decodable + func write(with key: String, value: T) throws where T: Encodable } extension UserDefaults: StorageController { - public func read(with key: String) throws -> T? where T : Codable { + public func read(with key: String) throws -> T? where T : Decodable { guard let data = data(forKey: key) else { return nil } return try JSONDecoder().decode(T.self, from: data) } - public func write(with key: String, value: T) throws where T : Codable { + public func write(with key: String, value: T) throws where T : Encodable { let data = try JSONEncoder().encode(value) setValue(data, forKey: key) } } + +public extension Decodable { + static func read(_ controller: StorageController = UserDefaults.standard, cacheKey: String) throws -> Self? { + return try controller.read(with: cacheKey) + } +} + +public extension Encodable { + func write(_ controller: StorageController = UserDefaults.standard, cacheKey: String) throws { + try controller.write(with: cacheKey, value: self) + } +} diff --git a/Sources/Caffeine/Extensions/AdditiveArithmetic+Caffeine.swift b/Sources/Caffeine/Extensions/AdditiveArithmetic+Caffeine.swift index dc3648b..0d3330e 100644 --- a/Sources/Caffeine/Extensions/AdditiveArithmetic+Caffeine.swift +++ b/Sources/Caffeine/Extensions/AdditiveArithmetic+Caffeine.swift @@ -9,6 +9,8 @@ import Foundation public extension AdditiveArithmetic where Self : ExpressibleByIntegerLiteral { @inlinable static var one: Self { 1 } + @inlinable static var two: Self { 2 } + @inlinable static var three: Self { 3 } } public extension AdditiveArithmetic where Self : ExpressibleByFloatLiteral { diff --git a/Sources/Caffeine/Presentation/ViewModel.swift b/Sources/Caffeine/Presentation/ViewModel.swift index c7461a6..5c8b909 100644 --- a/Sources/Caffeine/Presentation/ViewModel.swift +++ b/Sources/Caffeine/Presentation/ViewModel.swift @@ -20,3 +20,24 @@ public extension ViewModel { } } } + +/// A ´ViewModel´ which can be delegated to handle ´ChildViewModel´ actions. +public protocol ParentViewModel: Caffeine.ViewModel { + associatedtype ChildAction + func handle(childAction: ChildAction) async +} + +/// A ´ViewModel´ that can delegate its actions to a ´ParentViewModel´. +public protocol ChildViewModel: ViewModel where Parent: ParentViewModel, Parent.ChildAction == Self.Action { + associatedtype Parent + var parent: Parent { get set } +} + +public extension ParentViewModel { + func handle(childAction: ChildAction) { + Task { + await handle(childAction: childAction) + } + } +} + diff --git a/Sources/Caffeine/Protocols/Storable.swift b/Sources/Caffeine/Protocols/Storable.swift deleted file mode 100644 index 79b6c48..0000000 --- a/Sources/Caffeine/Protocols/Storable.swift +++ /dev/null @@ -1,23 +0,0 @@ -// -// Storable.swift -// Caffeine -// -// Created by Jota Uribe on 4/02/24. -// - -import Foundation - -// Defines convenience methods to make use of ´StorageController´. -public protocol Storable: Codable { - var cacheKey: String { get } -} - -public extension Storable { - static func read(_ controller: StorageController = UserDefaults.standard, cacheKey: String) throws -> Self? { - return try controller.read(with: cacheKey) - } - - func write(_ controller: StorageController = UserDefaults.standard) throws { - try controller.write(with: cacheKey, value: self) - } -}