Skip to content

Commit 3c89cd9

Browse files
committed
refact :: [#118] SchoolMeal Combine refact
1 parent fce4331 commit 3c89cd9

7 files changed

Lines changed: 29 additions & 45 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
2+
import Combine
23

34
public protocol SchoolMealRepository {
4-
func fetchSchoolMeal(date: String) async throws -> SchoolMealEntity
5+
func fetchSchoolMeal(date: String) -> AnyPublisher<SchoolMealEntity, Error>
56
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
2+
import Combine
23

34
public protocol FetchSchoolMealUseCaseProtocol {
4-
func execute(date: String) async throws -> SchoolMealEntity
5+
func execute(date: String) -> AnyPublisher<SchoolMealEntity, Error>
56
}

Projects/Domain/SchoolMealDomain/Sources/DataSource/SchoolMealDataSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import Foundation
22
import Combine
33

44
public protocol SchoolMealRemoteDataSource {
5-
func fetchSchoolMeal(date: String) async throws -> SchoolMealDTO
5+
func fetchSchoolMeal(date: String) -> AnyPublisher<SchoolMealDTO, Error>
66
}
Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
11
import Foundation
2-
import Moya
3-
import CombineMoya
42
import Combine
53
import BaseDomain
64
import Core
75

8-
public final class SchoolMealRemoteDataSourceImpl: SchoolMealRemoteDataSource {
9-
private let keychain: any Keychain
10-
private let provider: MoyaProvider<SchoolMealAPI>
11-
12-
public init(keychain: any Keychain) {
13-
self.keychain = keychain
14-
self.provider = MoyaProvider<SchoolMealAPI>(plugins: [MoyaLoggingPlugin()])
15-
}
16-
17-
public func fetchSchoolMeal(date: String) async throws -> SchoolMealDTO {
18-
return try await withCheckedThrowingContinuation { continuation in
19-
provider.request(.fetchSchoolMeal(date: date)) { result in
20-
switch result {
21-
case .success(let response):
22-
do {
23-
let dto = try response.map(SchoolMealDTO.self)
24-
continuation.resume(returning: dto)
25-
} catch {
26-
continuation.resume(throwing: error)
27-
}
28-
case .failure(let error):
29-
continuation.resume(throwing: error)
30-
}
6+
public final class SchoolMealRemoteDataSourceImpl: BaseRemoteDataSource<SchoolMealAPI>, SchoolMealRemoteDataSource {
7+
8+
public func fetchSchoolMeal(date: String) -> AnyPublisher<SchoolMealDTO, Error> {
9+
request(.fetchSchoolMeal(date: date))
10+
.tryMap { response in
11+
try response.map(SchoolMealDTO.self)
3112
}
32-
}
13+
.eraseToAnyPublisher()
3314
}
3415
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import Foundation
2+
import Combine
23
import SchoolMealDomainInterface
34

45
public final class SchoolMealRepositoryImpl: SchoolMealRepository {
56
private let remoteDataSource: SchoolMealRemoteDataSource
6-
7+
78
public init(remoteDataSource: SchoolMealRemoteDataSource) {
89
self.remoteDataSource = remoteDataSource
910
}
10-
11-
public func fetchSchoolMeal(date: String) async throws -> SchoolMealEntity {
12-
let dto = try await remoteDataSource.fetchSchoolMeal(date: date)
13-
return dto.toDomain()
11+
12+
public func fetchSchoolMeal(date: String) -> AnyPublisher<SchoolMealEntity, Error> {
13+
remoteDataSource.fetchSchoolMeal(date: date)
14+
.map { $0.toDomain() }
15+
.eraseToAnyPublisher()
1416
}
1517
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import Foundation
2+
import Combine
23
import SchoolMealDomainInterface
34

45
public struct FetchSchoolMealUseCase: FetchSchoolMealUseCaseProtocol {
56
private let repository: SchoolMealRepository
6-
7+
78
public init(repository: SchoolMealRepository) {
89
self.repository = repository
910
}
10-
11-
public func execute(date: String) async throws -> SchoolMealEntity {
12-
return try await repository.fetchSchoolMeal(date: date)
11+
12+
public func execute(date: String) -> AnyPublisher<SchoolMealEntity, Error> {
13+
repository.fetchSchoolMeal(date: date)
1314
}
1415
}

Projects/Feature/SchoolMealFeature/Sources/SchoolMealReducer.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ public struct SchoolMealReducer: Reducer {
4242
case .fetchMeal(let dateString):
4343
state.isLoading = true
4444
state.errorMessage = nil
45-
return .run { send in
46-
do {
47-
let mealEntity = try await fetchSchoolMealsUseCase.execute(date: dateString)
48-
await send(.mealFetched(mealEntity))
49-
} catch {
50-
await send(.fetchFailed(error.localizedDescription))
51-
}
45+
return .publisher {
46+
fetchSchoolMealsUseCase.execute(date: dateString)
47+
.mapError { $0 as Error }
48+
.map { Action.mealFetched($0) }
49+
.catch { Just(Action.fetchFailed($0.localizedDescription)) }
5250
}
5351

5452
case .mealFetched(let mealEntity):

0 commit comments

Comments
 (0)