Skip to content

Commit 71f1544

Browse files
author
Mohammed Rokon Uddin
committed
improve ergonomics and readability
1 parent a4857e0 commit 71f1544

File tree

7 files changed

+41
-29
lines changed

7 files changed

+41
-29
lines changed

{{cookiecutter.app_name}}/Domain/Sources/Domain/Interface/UseCase.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88

99
import Foundation
1010

11-
public protocol UseCase {
11+
public protocol UseCase<Input, Output> {
1212
associatedtype Input
1313
associatedtype Output
1414

15-
func execute(input: Input) async throws -> Output
15+
func callAsFunction(input: Input) async throws -> Output
16+
}
17+
18+
extension UseCase where Input == Void {
19+
public func callAsFunction() async throws -> Output {
20+
try await callAsFunction(input: ())
21+
}
1622
}

{{cookiecutter.app_name}}/Domain/Sources/Domain/UseCases/PrepareCoreDataUseCase.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
import Foundation
1010

11-
public final class PrepareCoreDataUseCase: UseCase {
11+
public typealias PrepareCoreDataUseCaseType = UseCase<Void, Void>
12+
13+
public class PrepareCoreDataUseCase: UseCase {
1214
var prepare: () async throws -> Void
15+
1316
public init<R: PrepareRepository>(repository: R) {
1417
self.prepare = repository.prepare
1518
}
1619

17-
public func execute(input: Void) async {
20+
public func callAsFunction(input: Void) async {
1821
try? await prepare()
1922
}
2023
}

{{cookiecutter.app_name}}/Domain/Sources/Domain/UseCases/ProductUseCase.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import Foundation
1010

11+
public typealias ProductUseCaseType = UseCase<Int, Product?>
12+
1113
public final class ProductUseCase: UseCase {
1214

1315
var getProduct: (_ input: Int) async throws -> Product?
@@ -17,7 +19,7 @@ public final class ProductUseCase: UseCase {
1719
self.getProduct = repository.read(input:)
1820
}
1921

20-
@Sendable public func execute(input: Int) async throws -> Product? {
22+
public func callAsFunction(input: Int) async throws -> Product? {
2123
return try await getProduct(input)
2224
}
2325
}

{{cookiecutter.app_name}}/Domain/Sources/Domain/UseCases/SaveProductUseCase.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import Foundation
1010

11+
public typealias SaveProductUseCaseType = UseCase<Product, Void>
12+
1113
public final class SaveProductUseCase: UseCase {
1214
var saveProduct: (_ input: Product) async throws -> Void
1315

@@ -16,7 +18,7 @@ public final class SaveProductUseCase: UseCase {
1618
self.saveProduct = repository.create(input:)
1719
}
1820

19-
@Sendable public func execute(input: Product) async throws {
21+
public func callAsFunction(input: Product) async throws {
2022
return try await saveProduct(input)
2123
}
2224
}

{{cookiecutter.app_name}}/Features/Sources/App/AppClient.swift

+18-19
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,24 @@ import Foundation
1212
import NetworkPlatform
1313
import PersistentPlatform
1414

15-
public struct AppClient {
16-
public var prepare: (()) async -> Void
17-
public var save: @Sendable (_ request: Domain.Product) async throws -> Void
18-
public var product: @Sendable (_ request: Int) async throws -> Domain.Product?
19-
}
15+
struct AppClient {
16+
var getProduct: any ProductUseCaseType
17+
var saveProduct: any SaveProductUseCaseType
18+
var prepareCoreData: any PrepareCoreDataUseCaseType
2019

21-
extension AppClient {
22-
private init(
23-
_ prepare: PrepareCoreDataUseCase, productUseCase: ProductUseCase,
24-
saveProduct: SaveProductUseCase
20+
init(
21+
_ prepare: PrepareCoreDataUseCase,
22+
getProductUseCase: ProductUseCase,
23+
saveProductUseCase: SaveProductUseCase
2524
) {
26-
self.prepare = prepare.execute(input:)
27-
self.save = saveProduct.execute(input:)
28-
self.product = productUseCase.execute(input:)
25+
self.prepareCoreData = prepare
26+
self.getProduct = getProductUseCase
27+
self.saveProduct = saveProductUseCase
2928
}
3029
}
3130

3231
extension DependencyValues {
33-
public var appClient: AppClient {
32+
var appClient: AppClient {
3433
get { self[AppClient.self] }
3534
set { self[AppClient.self] = newValue }
3635
}
@@ -39,17 +38,17 @@ extension DependencyValues {
3938
extension AppClient: DependencyKey {
4039
public static var liveValue = AppClient(
4140
PrepareCoreDataUseCase(repository: PreparePersistentRepository.live),
42-
productUseCase: ProductUseCase(repository: RemoteProductRepository.live),
43-
saveProduct: SaveProductUseCase(
41+
getProductUseCase: ProductUseCase(repository: RemoteProductRepository.live),
42+
saveProductUseCase: SaveProductUseCase(
4443
repository: PersistentProductRepository.live))
4544
public static var testValue = AppClient(
4645
PrepareCoreDataUseCase(repository: PreparePersistentRepository.live),
47-
productUseCase: ProductUseCase(repository: RemoteProductRepository.stubbed),
48-
saveProduct: SaveProductUseCase(
46+
getProductUseCase: ProductUseCase(repository: RemoteProductRepository.stubbed),
47+
saveProductUseCase: SaveProductUseCase(
4948
repository: PersistentProductRepository.live))
5049
public static var previewValue = AppClient(
5150
PrepareCoreDataUseCase(repository: PreparePersistentRepository.live),
52-
productUseCase: ProductUseCase(repository: RemoteProductRepository.stubbed),
53-
saveProduct: SaveProductUseCase(
51+
getProductUseCase: ProductUseCase(repository: RemoteProductRepository.stubbed),
52+
saveProductUseCase: SaveProductUseCase(
5453
repository: PersistentProductRepository.live))
5554
}

{{cookiecutter.app_name}}/Features/Sources/App/AppFeature.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public struct AppFeature: FeatureReducer {
5050
switch viewAction {
5151
case .onAppear:
5252
return .run { send in
53-
await appClient.prepare(Void())
53+
try? await appClient.prepareCoreData()
5454
await send(
5555
.internal(
5656
.productResponse(
5757
Result {
58-
try await appClient.product(1)
58+
try await appClient.getProduct(input: 2)
5959
})))
6060
}
6161
case .showSheet:
@@ -69,7 +69,7 @@ public struct AppFeature: FeatureReducer {
6969
case .save:
7070
return .run { [product = state.product] send in
7171
do {
72-
try await appClient.save(product!)
72+
try await appClient.saveProduct(input: product!)
7373
} catch {
7474

7575
}

{{cookiecutter.app_name}}/{{cookiecutter.app_name}}.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
{
7676
"identity" : "swift-composable-architecture",
7777
"kind" : "remoteSourceControl",
78-
"location" : "https://github.com/pointfreeco/swift-composable-architecture",
78+
"location" : "git@github.com:pointfreeco/swift-composable-architecture.git",
7979
"state" : {
8080
"revision" : "cf967a28a8605629559533320d604168d733fc9c",
8181
"version" : "1.8.0"

0 commit comments

Comments
 (0)