Skip to content

Commit

Permalink
add dependency clients for kmp (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry-itto authored Jun 22, 2024
1 parent 8320da6 commit 9db8a08
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 18 deletions.
34 changes: 32 additions & 2 deletions app-ios/Sources/KMPClient/Client.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
import DependenciesMacros
import Dependencies
import shared

extension DependencyValues {
public var timetableClient: TimetableClient {
get { self[TimetableClient.self] }
set { self[TimetableClient.self] = newValue }
}

public var staffClient: StaffClient {
get { self[StaffClient.self] }
set { self[StaffClient.self] = newValue }
}

public var sponsorsClient: SponsorsClient {
get { self[SponsorsClient.self] }
set { self[SponsorsClient.self] = newValue }
}
}

@DependencyClient
public struct TimetableClient: Sendable {
public var streamTimetable: @Sendable () throws -> AsyncThrowingStream<Timetable, any Error>
public var streamTimetableItemWithFavorite: @Sendable (_ id: TimetableItemId) throws -> AsyncThrowingStream<(TimetableItem, Bool), any Error>
public var toggleBookmark: @Sendable (_ id: TimetableItemId) async throws -> Void
}

@DependencyClient
public struct StaffClient: Sendable {
public var streamStaffs: @Sendable () throws -> AsyncThrowingStream<[Staff], any Error>
}

@DependencyClient
public struct KMPClient: Sendable {
public let fetchTimetable: @Sendable () -> AsyncThrowingStream<Timetable, any Error>
public struct SponsorsClient: Sendable {
public var streamSponsors: @Sendable () throws -> AsyncThrowingStream<[Sponsor], any Error>
}
42 changes: 39 additions & 3 deletions app-ios/Sources/KMPClient/LiveKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,46 @@ private var sessionsRepository: any SessionsRepository {
Container.shared.get(type: (any SessionsRepository).self)
}

extension KMPClient: DependencyKey {
public static let liveValue: KMPClient = .init(
fetchTimetable: {
private var staffRepository: any StaffRepository {
Container.shared.get(type: (any StaffRepository).self)
}

private var sponsorsRepository: any SponsorsRepository {
Container.shared.get(type: (any SponsorsRepository).self)
}

extension TimetableClient: DependencyKey {
public static let liveValue: TimetableClient = .init(
streamTimetable: {
sessionsRepository.getTimetableStream().eraseToThrowingStream()
},
streamTimetableItemWithFavorite: { id in
sessionsRepository
.getTimetableItemWithBookmarkStream(id: id)
.compactMap { pair in
guard let item = pair.first, let isFavorited = pair.second?.boolValue else { return nil }
return (item, isFavorited)
}
.eraseToThrowingStream()
},
toggleBookmark: { id in
try await sessionsRepository.toggleBookmark(id: id)
}
)
}

extension StaffClient: DependencyKey {
public static let liveValue: StaffClient = .init(
streamStaffs: {
staffRepository.staffs().eraseToThrowingStream()
}
)
}

extension SponsorsClient: DependencyKey {
public static let liveValue: SponsorsClient = .init(
streamSponsors: {
sponsorsRepository.sponsors().eraseToThrowingStream()
}
)
}
22 changes: 9 additions & 13 deletions app-ios/Sources/KMPClient/TestKey.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import Dependencies

extension DependencyValues {
public var kmpClient: KMPClient {
get { self[KMPClient.self] }
set { self[KMPClient.self] = newValue }
}
extension TimetableClient: TestDependencyKey {
public static let previewValue: Self = Self()
public static let testValue: Self = Self()
}

extension KMPClient: TestDependencyKey {
public static let previewValue: KMPClient = .noop
extension StaffClient: TestDependencyKey {
public static let previewValue: Self = Self()
public static let testValue: Self = Self()
}

extension KMPClient {
public static let noop = Self(
fetchTimetable: {
.never
}
)
extension SponsorsClient: TestDependencyKey {
public static let previewValue: Self = Self()
public static let testValue: Self = Self()
}

0 comments on commit 9db8a08

Please sign in to comment.