forked from touchlab/KaMPKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement sample navigation for both platforms
- Loading branch information
Showing
36 changed files
with
462 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/src/main/kotlin/co/touchlab/kampkit/android/ui/BreedDetailsScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package co.touchlab.kampkit.android.ui | ||
|
||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import co.touchlab.kampkit.ui.breedDetails.BreedDetailsViewModel | ||
import co.touchlab.kermit.Logger | ||
|
||
@Composable | ||
fun BreedDetailsScreen( | ||
viewModel: BreedDetailsViewModel, | ||
log: Logger | ||
) { | ||
val state by viewModel.detailsState.collectAsStateWithLifecycle() | ||
|
||
Text("${state.breed?.name }") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/kotlin/co/touchlab/kampkit/android/ui/MainNavigation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package co.touchlab.kampkit.android.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.navigation.NavType | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
import androidx.navigation.navArgument | ||
import org.koin.androidx.compose.get | ||
import org.koin.androidx.compose.koinViewModel | ||
import org.koin.core.parameter.parametersOf | ||
|
||
@Composable | ||
fun MainNavigation() { | ||
val navController = rememberNavController() | ||
NavHost(navController = navController, startDestination = "breeds") { | ||
composable("breeds") { | ||
BreedsScreen( | ||
viewModel = koinViewModel(), | ||
onNavigateToDetails = { breedId -> | ||
navController.navigate("breedDetails/$breedId") | ||
}, | ||
log = get { parametersOf("BreedsScreen") } | ||
) | ||
} | ||
composable( | ||
route = "breedDetails/{breedId}", | ||
arguments = listOf(navArgument("breedId") { type = NavType.LongType }) | ||
) { | ||
val breedId = it.arguments?.getLong("breedId") | ||
BreedDetailsScreen( | ||
viewModel = koinViewModel { parametersOf(breedId) }, | ||
log = get { parametersOf("BreedDetailsScreen") } | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
objects = { | ||
|
||
/* Begin PBXBuildFile section */ | ||
3C6AEC072A30881B0003F34A /* BreedDetailsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6AEC062A30881B0003F34A /* BreedDetailsScreen.swift */; }; | ||
3C6AEC092A30C17A0003F34A /* AppNavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6AEC082A30C17A0003F34A /* AppNavigationController.swift */; }; | ||
3CD290EC2A251417004C7AD1 /* KMPNativeCoroutinesCombine in Frameworks */ = {isa = PBXBuildFile; productRef = 3CD290EB2A251417004C7AD1 /* KMPNativeCoroutinesCombine */; }; | ||
3CD290EE2A251417004C7AD1 /* KMPNativeCoroutinesCore in Frameworks */ = {isa = PBXBuildFile; productRef = 3CD290ED2A251417004C7AD1 /* KMPNativeCoroutinesCore */; }; | ||
3DFF917C64A18A83DA010EE1 /* Pods_KaMPKitiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B859F3FB23133D22AB9DD835 /* Pods_KaMPKitiOS.framework */; }; | ||
|
@@ -40,6 +42,8 @@ | |
/* Begin PBXFileReference section */ | ||
1DFCC00C8DAA719770A18D1A /* Pods-KaMPKitiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KaMPKitiOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS.release.xcconfig"; sourceTree = "<group>"; }; | ||
2A1ED6A4A2A53F5F75C58E5F /* Pods-KaMPKitiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KaMPKitiOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS.release.xcconfig"; sourceTree = "<group>"; }; | ||
3C6AEC062A30881B0003F34A /* BreedDetailsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedDetailsScreen.swift; sourceTree = "<group>"; }; | ||
3C6AEC082A30C17A0003F34A /* AppNavigationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppNavigationController.swift; sourceTree = "<group>"; }; | ||
46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedListScreen.swift; sourceTree = "<group>"; }; | ||
46A5B60726B04920002EFEAA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; }; | ||
46B5284C249C5CF400A7725D /* Koin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Koin.swift; sourceTree = "<group>"; }; | ||
|
@@ -138,6 +142,8 @@ | |
F1465F0B23AA94BF0055F7C3 /* LaunchScreen.storyboard */, | ||
F1465F0E23AA94BF0055F7C3 /* Info.plist */, | ||
46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */, | ||
3C6AEC062A30881B0003F34A /* BreedDetailsScreen.swift */, | ||
3C6AEC082A30C17A0003F34A /* AppNavigationController.swift */, | ||
); | ||
path = KaMPKitiOS; | ||
sourceTree = "<group>"; | ||
|
@@ -364,6 +370,8 @@ | |
46B5284D249C5CF400A7725D /* Koin.swift in Sources */, | ||
46A5B5EF26AF54F7002EFEAA /* BreedListScreen.swift in Sources */, | ||
F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */, | ||
3C6AEC072A30881B0003F34A /* BreedDetailsScreen.swift in Sources */, | ||
3C6AEC092A30C17A0003F34A /* AppNavigationController.swift in Sources */, | ||
); | ||
runOnlyForDeploymentPostprocessing = 0; | ||
}; | ||
|
@@ -453,7 +461,7 @@ | |
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; | ||
CODE_SIGN_IDENTITY = "Apple Development: [email protected] (94U525PPDD)"; | ||
COPY_PHASE_STRIP = NO; | ||
DEBUG_INFORMATION_FORMAT = dwarf; | ||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; | ||
DEVELOPMENT_TEAM = 6A5MWU525T; | ||
ENABLE_STRICT_OBJC_MSGSEND = YES; | ||
ENABLE_TESTABILITY = YES; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// AppNavigationController.swift | ||
// KaMPKitiOS | ||
// | ||
// Created by Bartłomiej Pedryc on 07/06/2023. | ||
// Copyright © 2023 Touchlab. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import Foundation | ||
|
||
class AppNavigationController: UINavigationController { | ||
func toBreeds() { | ||
pushViewController( | ||
UIHostingController( | ||
rootView: BreedListScreen(observableModel: ObservableBreedModel(navigationController: self)) | ||
), | ||
animated: false | ||
) | ||
} | ||
func toBreedDetails(breedId: Int64) { | ||
pushViewController( | ||
UIHostingController( | ||
rootView: BreedDetailsScreen(observableModel: ObservableBreedDetailsModel(breedId: breedId)) | ||
), | ||
animated: false | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// BreedDetailsScreen.swift | ||
// KaMPKitiOS | ||
// | ||
// Created by Bartłomiej Pedryc on 07/06/2023. | ||
// Copyright © 2023 Touchlab. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import SwiftUI | ||
import shared | ||
import KMPNativeCoroutinesCombine | ||
import Foundation | ||
|
||
class ObservableBreedDetailsModel: ObservableObject { | ||
private var viewModel: BreedDetailsViewModel | ||
|
||
init(breedId: Int64) { | ||
self.viewModel = KotlinDependencies.shared.getBreedDetailsViewModel(breedId: breedId) | ||
} | ||
|
||
@Published | ||
var breed: Breed? | ||
|
||
private var cancellables = [AnyCancellable]() | ||
|
||
func activate() { | ||
createPublisher(for: viewModel.detailsStateFlow) | ||
.sink { _ in } receiveValue: { [weak self] (detailsState: BreedDetailsViewState) in | ||
if let breed = detailsState.breed { self?.breed = breed } | ||
} | ||
.store(in: &cancellables) | ||
} | ||
|
||
func deactivate() { | ||
cancellables.forEach { $0.cancel() } | ||
cancellables.removeAll() | ||
} | ||
|
||
deinit { | ||
viewModel.clear() | ||
} | ||
} | ||
|
||
struct BreedDetailsScreen: View { | ||
@StateObject | ||
var observableModel: ObservableBreedDetailsModel | ||
|
||
var body: some View { | ||
BreedDetailsContent( | ||
breedName: observableModel.breed?.name ?? "" | ||
) | ||
.onAppear(perform: { | ||
observableModel.activate() | ||
}) | ||
.onDisappear(perform: { | ||
observableModel.deactivate() | ||
}) | ||
} | ||
} | ||
|
||
struct BreedDetailsContent: View { | ||
var breedName: String | ||
var body: some View { | ||
Text(breedName) | ||
} | ||
} |
Oops, something went wrong.