Skip to content

Commit

Permalink
Merge pull request #947 from yimajo/add_kmpclient_live
Browse files Browse the repository at this point in the history
Extract `KMPClientLive` target from `KMPClient` target
  • Loading branch information
ry-itto authored Sep 5, 2024
2 parents 9390851 + c03af9b commit c08832c
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 57 deletions.
1 change: 1 addition & 0 deletions app-ios/App/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import App
import ComposableArchitecture
import SwiftUI
import Theme
import KMPClientLive

final class AppDelegate: NSObject, UIApplicationDelegate {
let store = Store(
Expand Down
16 changes: 12 additions & 4 deletions app-ios/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ let package = Package(
.eventMapFeature,
.tca,
.kmpClient,
.KMPClientLive,
.licenseList,
]
),
Expand All @@ -85,14 +86,20 @@ let package = Package(
name: "KMPClient",
dependencies: [
.kmpModule,
.firebaseAuth,
.firebaseRemoteConfig,
.tca,
.model,
.licenseList,
]
),


.target(
name: "KMPClientLive",
dependencies: [
.kmpClient,
.firebaseAuth,
.firebaseRemoteConfig,
]
),

.target(
name: "EventKitClient",
dependencies: [
Expand Down Expand Up @@ -289,6 +296,7 @@ extension Target.Dependency {
static let profileCardFeature: Target.Dependency = "ProfileCardFeature"
static let kmpModule: Target.Dependency = "KmpModule"
static let kmpClient: Target.Dependency = "KMPClient"
static let KMPClientLive: Target.Dependency = "KMPClientLive"
static let eventKitClient: Target.Dependency = "EventKitClient"
static let theme: Target.Dependency = "Theme"
static let commonComponents: Target.Dependency = "CommonComponents"
Expand Down
3 changes: 2 additions & 1 deletion app-ios/Sources/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import KMPClient

@Reducer
public struct AppDelegateReducer {
@Dependency(\.firebaseAppClient) var firebaseAppClient
public struct State: Equatable {
public init() {}
}
Expand All @@ -15,7 +16,7 @@ public struct AppDelegateReducer {
Reduce { state, action in
switch action {
case .didFinishLaunching:
prepareFirebase()
firebaseAppClient.prepareFirebase()

return .none
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import SwiftUI
import LicenseList
@preconcurrency import shared
import shared
import ComposableArchitecture

public struct KmpAppComposeViewControllerWrapper: UIViewControllerRepresentable {
public init() {}
@MainActor
struct KmpAppComposeViewControllerWrapper: UIViewControllerRepresentable {
@Dependency(\.containerClient) var containerClient

public func makeUIViewController(context: Context) -> UIViewController {
let container = Container.shared
let repositories: any Repositories = container.get(type: (any Repositories).self)
return IosComposeKaigiAppKt.kaigiAppController(
repositories: repositories,
init() {}

func makeUIViewController(context: Context) -> UIViewController {
IosComposeKaigiAppKt.kaigiAppController(
repositories: containerClient.repositories(),
onLicenseScreenRequest: {
openLicenseScreen()
}
)
}

public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}

private func openLicenseScreen() {
if let windowScene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene {
if let rootViewController = windowScene.windows.first?.rootViewController {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import SwiftUI
import shared
import ComposableArchitecture

public struct KmpProfileCardComposeViewControllerWrapper: UIViewControllerRepresentable {
public init() {}

public func makeUIViewController(context: Context) -> UIViewController {
struct KmpProfileCardComposeViewControllerWrapper: UIViewControllerRepresentable {
@Dependency(\.containerClient) var containerClient

init() {}

func makeUIViewController(context: Context) -> UIViewController {
profileCardViewController(
repositories: Container.shared.get(type: (any Repositories).self),
repositories: containerClient.repositories(),
onClickShareProfileCard: { image, text in
let activityViewController = UIActivityViewController(activityItems: [text, image], applicationActivities: nil)
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let keyWindow = windowScene.windows.first(where: { $0.isKeyWindow }),
let rootViewController = keyWindow.rootViewController {
let keyWindow = windowScene.windows.first(where: { $0.isKeyWindow }),
let rootViewController = keyWindow.rootViewController {
rootViewController.present(activityViewController, animated: true, completion: nil)
} else {
print("Unable to find the root view controller.")
}
}
)
}
public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import SwiftUI
import shared
import ComposableArchitecture

struct KmpContributorComposeViewControllerWrapper: UIViewControllerRepresentable {
public typealias URLString = String

let repositories: any Repositories
private let onContributorsItemClick: (URLString) -> Void

init(onContributorsItemClick: @escaping (URLString) -> Void) {
@Dependency(\.containerClient) var containerClient

self.repositories = containerClient.repositories()
self.onContributorsItemClick = onContributorsItemClick
}

func makeUIViewController(context: Context) -> UIViewController {
return contributorsViewController(
repositories: repositories,
onContributorsItemClick: onContributorsItemClick
)
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Model
import SwiftUI
@preconcurrency import shared
import Theme
import ComposableArchitecture

struct KmpPresenterContributorView: View {
private let repositories: any Repositories
Expand All @@ -12,8 +13,9 @@ struct KmpPresenterContributorView: View {
@State private var currentState: ContributorsUiState? = nil

init(onContributorButtonTapped: @escaping (URL) -> Void) {
self.repositories = Container.shared.get(type: (any Repositories).self)
@Dependency(\.containerClient) var containerClient

self.repositories = containerClient.repositories()
self.events = SkieKotlinSharedFlowFactory<any ContributorsScreenEvent>()
.createSkieKotlinSharedFlow(replay: 0, extraBufferCapacity: 0)
self.onContributorButtonTapped = onContributorButtonTapped
Expand Down
26 changes: 26 additions & 0 deletions app-ios/Sources/KMPClient/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import shared
import Model

extension DependencyValues {
public var firebaseAppClient: FirebaseAppClient {
get { self[FirebaseAppClient.self] }
set { self[FirebaseAppClient.self] = newValue }
}

public var containerClient: ContainerClient {
get { self[ContainerClient.self] }
set { self[ContainerClient.self] = newValue }
}

public var timetableClient: TimetableClient {
get { self[TimetableClient.self] }
set { self[TimetableClient.self] = newValue }
Expand Down Expand Up @@ -35,6 +45,22 @@ extension DependencyValues {
}
}

@DependencyClient
public struct FirebaseAppClient: Sendable {
public var prepareFirebase: @Sendable () -> ()
}

@DependencyClient
public struct ContainerClient: Sendable {
private class DefaultRepositories: Repositories {
var map = [AnyHashable: Any]()
}

public var repositories: @Sendable () -> any Repositories = {
DefaultRepositories()
}
}

@DependencyClient
public struct TimetableClient: Sendable {
public var streamTimetable: @Sendable () throws -> AsyncThrowingStream<Timetable, any Error>
Expand Down
10 changes: 10 additions & 0 deletions app-ios/Sources/KMPClient/TestKey.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import Dependencies

extension FirebaseAppClient: TestDependencyKey {
public static let previewValue: Self = Self()
public static let testValue: Self = Self()
}

extension ContainerClient: TestDependencyKey {
public static let previewValue: Self = Self()
public static let testValue: Self = Self()
}

extension TimetableClient: TestDependencyKey {
public static let previewValue: Self = Self()

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import Firebase
import ObjectiveC
@preconcurrency import shared

public func prepareFirebase() {
FirebaseApp.configure()
}

public struct Container: Sendable {
public static let shared: Container = .init()

Expand All @@ -19,9 +15,9 @@ public struct Container: Sendable {
}

public func get<TypeProtocol, ReturnType>(type: TypeProtocol) -> ReturnType where TypeProtocol: Protocol {
guard let object = entryPoint.get(objCProtocol: type) as? ReturnType else {
fatalError("Not found instance for \(type)")
}
return object
guard let object = entryPoint.get(objCProtocol: type) as? ReturnType else {
fatalError("Not found instance for \(type)")
}
return object
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Dependencies
import shared
import Model
import Foundation
import Firebase
import KMPClient

private var sessionsRepository: any SessionsRepository {
Container.shared.get(type: (any SessionsRepository).self)
Expand All @@ -27,6 +29,26 @@ private var profileCardRepository: any ProfileCardRepository {
Container.shared.get(type: (any ProfileCardRepository).self)
}

extension FirebaseAppClient: DependencyKey {
public static var liveValue: Self {
Self(
prepareFirebase: {
FirebaseApp.configure()
}
)
}
}

extension ContainerClient: DependencyKey {
public static var liveValue: Self {
Self(
repositories: {
Container.shared.get(type: (any Repositories).self)
}
)
}
}

extension TimetableClient: DependencyKey {
public static let liveValue: TimetableClient = .init(
streamTimetable: {
Expand Down

0 comments on commit c08832c

Please sign in to comment.