Skip to content

Commit ed3704e

Browse files
committed
Add back view model in DI for iOS
1 parent f1bdcee commit ed3704e

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

common/src/iosMain/kotlin/dev/johnoreilly/common/di/IosApplicationComponent.kt

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import androidx.room.Room
44
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
55
import dev.johnoreilly.common.database.AppDatabase
66
import dev.johnoreilly.common.database.dbFileName
7+
import dev.johnoreilly.common.viewmodel.CountriesViewModelShared
8+
import dev.johnoreilly.common.viewmodel.NetworksViewModelShared
9+
import dev.johnoreilly.common.viewmodel.StationsViewModelShared
710
import io.ktor.client.engine.darwin.Darwin
811
import kotlinx.coroutines.Dispatchers
912
import kotlinx.coroutines.IO
@@ -20,6 +23,10 @@ import software.amazon.lastmile.kotlin.inject.anvil.SingleIn
2023
@SingleIn(AppScope::class)
2124
abstract class IosApplicationComponent: SharedApplicationComponent {
2225

26+
abstract val countriesViewModel: CountriesViewModelShared
27+
abstract val networksViewModel: NetworksViewModelShared
28+
abstract val stationsViewModel: StationsViewModelShared
29+
2330
override fun httpClientEngine() = Darwin.create()
2431

2532
override fun appDatabase() = createRoomDatabase()

ios/BikeShare/BikeShare/ContentView.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import KMPObservableViewModelSwiftUI
66

77

88
struct ContentView : View {
9-
let applicationCompoonent: IosApplicationComponent
9+
let applicationComponent: IosApplicationComponent
1010

1111
@ObservedViewModel var viewModel: CountriesViewModelShared
1212
@State var query: String = ""
1313

14-
init(applicationCompoonent: IosApplicationComponent) {
15-
self.applicationCompoonent = applicationCompoonent
16-
self.viewModel = applicationCompoonent.countriesViewModel
14+
init(applicationComponent: IosApplicationComponent) {
15+
self.applicationComponent = applicationComponent
16+
self.viewModel = applicationComponent.countriesViewModel
1717
}
1818

1919
var body: some View {
2020
NavigationView {
2121
List {
2222
ForEach(viewModel.countryList.filter { query.isEmpty || $0.displayName.contains(query)}, id: \.self) { country in
23-
NavigationLink(destination: NetworkListView(applicationCompoonent: applicationCompoonent, countryCode: country.code)) {
23+
NavigationLink(destination: NetworkListView(applicationComponent: applicationComponent, countryCode: country.code)) {
2424
HStack {
2525
Text(countryFlag(from: country.code))
2626
Text(country.displayName).font(.headline)
@@ -37,20 +37,20 @@ struct ContentView : View {
3737

3838

3939
struct NetworkListView: View {
40-
let applicationCompoonent: IosApplicationComponent
40+
let applicationComponent: IosApplicationComponent
4141
let countryCode: String
4242

4343
@ObservedViewModel var viewModel: NetworksViewModelShared
4444

45-
init(applicationCompoonent: IosApplicationComponent, countryCode: String) {
46-
self.applicationCompoonent = applicationCompoonent
45+
init(applicationComponent: IosApplicationComponent, countryCode: String) {
46+
self.applicationComponent = applicationComponent
4747
self.countryCode = countryCode
48-
self.viewModel = applicationCompoonent.networksViewModel
48+
self.viewModel = applicationComponent.networksViewModel
4949
}
5050

5151
var body: some View {
5252
List(viewModel.networkList) { network in
53-
NavigationLink(destination: StationListTabView(applicationCompoonent: applicationCompoonent, network: network)) {
53+
NavigationLink(destination: StationListTabView(applicationComponent: applicationComponent, network: network)) {
5454
Text("\(network.name) (\(network.city))").font(.subheadline)
5555
}
5656
}
@@ -63,15 +63,15 @@ struct NetworkListView: View {
6363

6464

6565
struct StationListTabView: View {
66-
let applicationCompoonent: IosApplicationComponent
66+
let applicationComponent: IosApplicationComponent
6767
var network: Network
6868

6969
@ObservedViewModel var viewModel: StationsViewModelShared
7070

71-
init(applicationCompoonent: IosApplicationComponent, network: Network) {
72-
self.applicationCompoonent = applicationCompoonent
71+
init(applicationComponent: IosApplicationComponent, network: Network) {
72+
self.applicationComponent = applicationComponent
7373
self.network = network
74-
self.viewModel = applicationCompoonent.stationsViewModel
74+
self.viewModel = applicationComponent.stationsViewModel
7575
}
7676

7777
var body: some View {

ios/BikeShare/BikeShare/SceneDelegate.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
2020
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
2121
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
2222

23-
let applicationCompoonent = IosApplicationComponentCreateComponentKt.create()
23+
let applicationComponent = IosApplicationComponentCreateComponentKt.create()
2424

2525
// Create the SwiftUI view that provides the window contents.
26-
let contentView = ContentView(applicationCompoonent: applicationCompoonent)
26+
let contentView = ContentView(applicationComponent: applicationComponent)
2727

2828
// Use a UIHostingController as window root view controller.
2929
if let windowScene = scene as? UIWindowScene {

0 commit comments

Comments
 (0)