Skip to content

Commit

Permalink
0.8.0 (#15)
Browse files Browse the repository at this point in the history
* Add LabelDisplayable protocol, add Button+SFSymbol extension, Update & reorganize constants extensions

* Bump minimum version for iOS, macOS, tvOS & watchOS, add support for visionOS

* Update README content

* fix Button+SFSymbol extension encapsulation

* fix LabelDisplayable encapsulation

* Add Setting protocol
  • Loading branch information
jjotaum authored Jan 30, 2025
1 parent 8bd3693 commit b64a286
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 52 deletions.
11 changes: 6 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// swift-tools-version: 5.8
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "Caffeine",
platforms: [
.iOS(.v14),
.macOS(.v11),
.tvOS(.v14),
.watchOS(.v7)
.iOS(.v15),
.macOS(.v12),
.tvOS(.v15),
.visionOS(.v1),
.watchOS(.v8)
],
products: [
.library(
Expand Down
51 changes: 29 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,33 @@ A collection of reusable logic to boost Apple platform apps development process.
## Contents

```
├── Controllers
│ └── CacheController.swift
├── Extensions
│ ├── AdditiveArithmetic+Constants.swift
│ ├── Array+Operations.swift
│ ├── CGFloat+Spacing.swift
│ ├── Image+SFSymbol.swift
│ ├── Image+SystemImage.swift
│ ├── Label+SFSymbol.swift
│ ├── LazyGrids+Inits.swift
│ └── String+Constants.swift
├── Network
│ ├── RemoteAPI.swift
│ ├── RemoteEndpoint.swift
│ ├── RemoteService.swift
│ └── RequestCoordinator.swift
├── Presentation
│ ├── AlertStateModifier.swift
│ ├── SFSymbol.swift
│ ├── SystemImage.swift
│ ├── ViewFrameSizeModifier.swift
│ └── ViewModel.swift
├── Caffeine
│ ├── Commons
│ │ └── Setting.swift
│ ├── Constants
│ │ ├── AdditiveArithmetic+Constants.swift
│ │ ├── CGFloat+Constants.swift
│ │ ├── CGSize+Constants.swift
│ │ └── String+Constants.swift
│ ├── Controllers
│ │ └── CacheController.swift
│ ├── Extensions
│ │ ├── Array+Operations.swift
│ │ ├── Button+SFSymbol.swift
│ │ ├── Image+SFSymbol.swift
│ │ ├── Image+SystemImage.swift
│ │ ├── Label+SFSymbol.swift
│ │ └── LazyGrids+Inits.swift
│ ├── Network
│ │ ├── RemoteAPI.swift
│ │ ├── RemoteEndpoint.swift
│ │ ├── RemoteService.swift
│ │ └── RequestCoordinator.swift
│ ├── Presentation
│ │ ├── AlertStateModifier.swift
│ │ ├── LabelDisplayable.swift
│ │ ├── SFSymbol.swift
│ │ ├── SystemImage.swift
│ │ ├── ViewFrameSizeModifier.swift
│ │ └── ViewModel.swift
```
17 changes: 17 additions & 0 deletions Sources/Caffeine/Commons/Setting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Setting.swift
// Caffeine
//
// Created by Jota Uribe on 30/01/25.
//

import Foundation

@available(iOS 16, *)
@available(macOS 13, *)
@available(tvOS 16, *)
@available(watchOS 9, *)
public protocol Setting: Hashable, LabelDisplayable {
static var key: String { get }
static var localizedDescription: LocalizedStringResource { get }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public extension AdditiveArithmetic where Self : ExpressibleByIntegerLiteral {
@inlinable static var one: Self { 1 }
@inlinable static var two: Self { 2 }
@inlinable static var three: Self { 3 }
@inlinable static var four: Self { 4 }
@inlinable static var five: Self { 5 }
@inlinable static var six: Self { 6 }
@inlinable static var seven: Self { 7 }
@inlinable static var eight: Self { 8 }
@inlinable static var nine: Self { 9 }
@inlinable static var ten: Self { 10 }
}

public extension AdditiveArithmetic where Self : ExpressibleByFloatLiteral {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//
// CGFloat+Spacing.swift
// CGFloat+Constants.swift
// Caffeine
//
// Created by Jota Uribe on 11/04/24.
//

import struct CoreGraphics.CGFloat

/// Mainly intended for layout spacing
public extension CGFloat {
/// 4
static var xSmall: Self = 4
Expand Down
23 changes: 23 additions & 0 deletions Sources/Caffeine/Constants/CGSize+Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// CGSize+Constants.swift
// Caffeine
//
// Created by Jota Uribe on 1/11/24.
//

import struct CoreGraphics.CGFloat
import struct CoreGraphics.CGSize

/// Mainly intended for icons size usage
public extension CGSize {
/// 16x16
static let small: CGSize = .init(width: 16, height: 16)
/// 24x24
static let regular: CGSize = .init(width: 24, height: 24)
/// 32x32
static let large: CGSize = .init(width: 32, height: 32)
}

public extension CGSize {
static let infinity = CGSize(width: CGFloat.infinity, height: CGFloat.infinity)
}
18 changes: 18 additions & 0 deletions Sources/Caffeine/Extensions/Button+SFSymbol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Button+SFSymbol.swift
// Caffeine
//
// Created by Jota Uribe on 1/11/24.
//

import SwiftUI

@available(iOS 16, *)
@available(macOS 13, *)
@available(tvOS 16, *)
@available(watchOS 9, *)
public extension Button where Label == SwiftUI.Label<Text, Image> {
init(role: ButtonRole? = .none, action: @escaping () -> Void, title: LocalizedStringResource, symbol: SFSymbol) {
self.init(role: role, action: action, label: { Label(title, symbol: symbol) })
}
}
4 changes: 0 additions & 4 deletions Sources/Caffeine/Network/RemoteService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import Foundation

public protocol RemoteService {
func load<T: Decodable>(endpoint: RemoteEndpoint, completion: @escaping (RequestResult<T>) -> Void) throws
@available(iOS 15.0, *)
@available(macOS 12.0, *)
func load<T: Decodable>(endpoint: RemoteEndpoint) async throws -> T
func load<T: Decodable>(endpoint: RemoteEndpoint) throws -> AnyPublisher<T, Error>
}
Expand All @@ -32,8 +30,6 @@ public class BaseRemoteService: RemoteService {
coordinator.dataTask(request: request, decoder: decoder, completion: completion)
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
public func load<T>(endpoint: RemoteEndpoint) async throws -> T where T : Decodable {
let request = try remoteAPI.urlRequest(endpoint)
return try await coordinator.data(request: request, decoder: decoder)
Expand Down
4 changes: 0 additions & 4 deletions Sources/Caffeine/Network/RequestCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ extension JSONDecoder: RequestDecoder { }

public protocol RequestCoordinator {
func dataTask<T: Decodable>(request: URLRequest, decoder: RequestDecoder, completion: @escaping (RequestResult<T>) -> Void)
@available(iOS 15.0, *)
@available(macOS 12.0, *)
func data<T: Decodable>(request: URLRequest, decoder: RequestDecoder) async throws -> T
func dataTaskPublisher<T: Decodable>(for request: URLRequest, decoder: RequestDecoder) throws -> AnyPublisher<T, Error>
}
Expand All @@ -45,8 +43,6 @@ extension URLSession: RequestCoordinator {
}.resume()
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
public func data<T>(request: URLRequest, decoder: RequestDecoder) async throws -> T where T : Decodable {
let data: (Data, URLResponse) = try await data(for: request)
return try decoder.decode(T.self, from: data.0)
Expand Down
16 changes: 0 additions & 16 deletions Sources/Caffeine/Presentation/AlertStateModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public struct ErrorAlertState<Error: LocalizedError>: AlertState {
}
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
@available(tvOS 15.0, *)
@available(watchOS 8.0, *)
public struct LocalizedAlertState: AlertState {
public var id: String { "\(title))" }
let title: LocalizedStringKey
Expand Down Expand Up @@ -56,10 +52,6 @@ public struct LocalizedAlertState: AlertState {
}
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
@available(tvOS 15.0, *)
@available(watchOS 8.0, *)
struct LocalizedAlertStateModifier: ViewModifier {
@Binding var state: LocalizedAlertState?
func body(content: Content) -> some View {
Expand All @@ -76,10 +68,6 @@ struct LocalizedAlertStateModifier: ViewModifier {
}
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
@available(tvOS 15.0, *)
@available(watchOS 8.0, *)
struct ErrorAlertStateModifier<Error: LocalizedError>: ViewModifier {
@Binding var state: ErrorAlertState<Error>?

Expand All @@ -90,10 +78,6 @@ struct ErrorAlertStateModifier<Error: LocalizedError>: ViewModifier {
}
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
@available(tvOS 15.0, *)
@available(watchOS 8.0, *)
public extension View {
func localizedAlert(for state: Binding<LocalizedAlertState?>) -> some View {
modifier(LocalizedAlertStateModifier(state: state))
Expand Down
36 changes: 36 additions & 0 deletions Sources/Caffeine/Presentation/LabelDisplayable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// LabelDisplayable.swift
// Caffeine
//
// Created by Jota Uribe on 1/11/24.
//

import Foundation
import SwiftUI

@available(iOS 16, *)
@available(macOS 13, *)
@available(tvOS 16, *)
@available(watchOS 9, *)
public protocol LabelDisplayable {
var localizedValue: LocalizedStringResource { get }
var symbol: SFSymbol { get }
}

@available(iOS 16, *)
@available(macOS 13, *)
@available(tvOS 16, *)
@available(watchOS 9, *)
public extension LabelDisplayable {
func button(role: ButtonRole? = .none, action: @escaping () -> Void) -> Button<Label<Text, Image>> {
Button(role: role, action: action, title: localizedValue, symbol: symbol)
}

func image() -> Image {
Image(symbol)
}

func label() -> Label<Text, Image> {
Label(localizedValue, symbol: symbol)
}
}

0 comments on commit b64a286

Please sign in to comment.