diff --git a/app-ios/Sources/KMPClient/Client.swift b/app-ios/Sources/KMPClient/Client.swift index 95b7fa564..7b608fe7e 100644 --- a/app-ios/Sources/KMPClient/Client.swift +++ b/app-ios/Sources/KMPClient/Client.swift @@ -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 + 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 +public struct SponsorsClient: Sendable { + public var streamSponsors: @Sendable () throws -> AsyncThrowingStream<[Sponsor], any Error> } diff --git a/app-ios/Sources/KMPClient/LiveKey.swift b/app-ios/Sources/KMPClient/LiveKey.swift index b388b102c..711182663 100644 --- a/app-ios/Sources/KMPClient/LiveKey.swift +++ b/app-ios/Sources/KMPClient/LiveKey.swift @@ -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() } ) } diff --git a/app-ios/Sources/KMPClient/TestKey.swift b/app-ios/Sources/KMPClient/TestKey.swift index e6e3fc295..8a2553afb 100644 --- a/app-ios/Sources/KMPClient/TestKey.swift +++ b/app-ios/Sources/KMPClient/TestKey.swift @@ -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() }