Skip to content

Commit

Permalink
Release 0.5.0 (#7)
Browse files Browse the repository at this point in the history
* Introduce Parent & Child View Models definitions

* Update StorageController definitions, remove Storable protocol

* Add two & three constants

* Update README to update contents
  • Loading branch information
jjotaum authored Mar 12, 2024
1 parent 6f731c2 commit f7a065e
Showing 5 changed files with 39 additions and 29 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
20 changes: 16 additions & 4 deletions Sources/Caffeine/Controllers/StorageController.swift
Original file line number Diff line number Diff line change
@@ -9,18 +9,30 @@ import Foundation

// Defines convenience methods for ´Codable´ types storage.
public protocol StorageController {
func read<T>(with key: String) throws -> T? where T: Codable
func write<T>(with key: String, value: T) throws where T: Codable
func read<T>(with key: String) throws -> T? where T: Decodable
func write<T>(with key: String, value: T) throws where T: Encodable
}

extension UserDefaults: StorageController {
public func read<T>(with key: String) throws -> T? where T : Codable {
public func read<T>(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<T>(with key: String, value: T) throws where T : Codable {
public func write<T>(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)
}
}
2 changes: 2 additions & 0 deletions Sources/Caffeine/Extensions/AdditiveArithmetic+Caffeine.swift
Original file line number Diff line number Diff line change
@@ -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 {
21 changes: 21 additions & 0 deletions Sources/Caffeine/Presentation/ViewModel.swift
Original file line number Diff line number Diff line change
@@ -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<Parent>: 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)
}
}
}

23 changes: 0 additions & 23 deletions Sources/Caffeine/Protocols/Storable.swift

This file was deleted.

0 comments on commit f7a065e

Please sign in to comment.