Skip to content

Commit

Permalink
Cleanup the code
Browse files Browse the repository at this point in the history
  • Loading branch information
bpedryc committed Jun 16, 2023
1 parent aea43f6 commit 088eafd
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package co.touchlab.kampkit.android
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import co.touchlab.kampkit.android.ui.MainNavigation
import co.touchlab.kampkit.android.ui.MainNavCoordinator
import co.touchlab.kampkit.android.ui.theme.KaMPKitTheme
import org.koin.core.component.KoinComponent

Expand All @@ -13,7 +13,7 @@ class MainActivity : ComponentActivity(), KoinComponent {
super.onCreate(savedInstanceState)
setContent {
KaMPKitTheme {
MainNavigation()
MainNavCoordinator()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ import co.touchlab.kampkit.android.R
import co.touchlab.kampkit.db.Breed
import co.touchlab.kampkit.ui.breeds.BreedViewState
import co.touchlab.kampkit.ui.breeds.BreedsViewModel
import co.touchlab.kampkit.ui.breeds.NavigationIntent
import co.touchlab.kampkit.ui.breeds.BreedsNavRequest
import co.touchlab.kermit.Logger
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState

@Composable
fun BreedsScreen(
viewModel: BreedsViewModel,
onNavigateToDetails: (breedId: Long) -> Unit,
onBreedDetailsNavRequest: (breedId: Long) -> Unit,
log: Logger
) {
val dogsState by viewModel.breedState.collectAsStateWithLifecycle()

dogsState.navigationIntent?.let { navIntent ->
LaunchedEffect(navIntent) {
if (navIntent is NavigationIntent.ToDetails) {
onNavigateToDetails(navIntent.breedId)
viewModel.onNavigationCompleted()
dogsState.breedsNavRequest?.let { navRequest ->
LaunchedEffect(navRequest) {
if (navRequest is BreedsNavRequest.ToDetails) {
onBreedDetailsNavRequest(navRequest.breedId)
viewModel.onBreedDetailsNavRequestCompleted()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.touchlab.kampkit.android.ui

import androidx.compose.runtime.Composable
import androidx.navigation.NavController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -10,28 +11,37 @@ import org.koin.androidx.compose.get
import org.koin.androidx.compose.koinViewModel
import org.koin.core.parameter.parametersOf

private const val BREEDS = "breeds"

private const val BREED_DETAILS = "breedDetails"
private const val BREED_ID_ARG = "breedId"

@Composable
fun MainNavigation() {
fun MainNavCoordinator() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "breeds") {
composable("breeds") {
composable(BREEDS) {
BreedsScreen(
viewModel = koinViewModel(),
onNavigateToDetails = { breedId ->
navController.navigate("breedDetails/$breedId")
onBreedDetailsNavRequest = { breedId ->
navController.navigateToBreedDetails(breedId)
},
log = get { parametersOf("BreedsScreen") }
)
}
composable(
route = "breedDetails/{breedId}",
arguments = listOf(navArgument("breedId") { type = NavType.LongType })
route = "$BREED_DETAILS/{$BREED_ID_ARG}",
arguments = listOf(navArgument(BREED_ID_ARG) { type = NavType.LongType })
) {
val breedId = it.arguments?.getLong("breedId")
val breedId = it.arguments?.getLong(BREED_ID_ARG)
BreedDetailsScreen(
viewModel = koinViewModel { parametersOf(breedId) },
log = get { parametersOf("BreedDetailsScreen") }
)
}
}
}

private fun NavController.navigateToBreedDetails(breedId: Long) {
navigate("$BREED_DETAILS/$breedId")
}
18 changes: 9 additions & 9 deletions ios/KaMPKitiOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

/* 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 */; };
3C6AEC092A30C17A0003F34A /* MainNavCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6AEC082A30C17A0003F34A /* MainNavCoordinator.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 */; };
46A5B5EF26AF54F7002EFEAA /* BreedListScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */; };
46A5B5EF26AF54F7002EFEAA /* BreedsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A5B5EE26AF54F7002EFEAA /* BreedsScreen.swift */; };
46A5B60826B04921002EFEAA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 46A5B60626B04920002EFEAA /* Main.storyboard */; };
46B5284D249C5CF400A7725D /* Koin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46B5284C249C5CF400A7725D /* Koin.swift */; };
F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */; };
Expand Down Expand Up @@ -43,8 +43,8 @@
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>"; };
3C6AEC082A30C17A0003F34A /* MainNavCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainNavCoordinator.swift; sourceTree = "<group>"; };
46A5B5EE26AF54F7002EFEAA /* BreedsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedsScreen.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>"; };
B859F3FB23133D22AB9DD835 /* Pods_KaMPKitiOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_KaMPKitiOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -135,15 +135,15 @@
F1465EFF23AA94BF0055F7C3 /* KaMPKitiOS */ = {
isa = PBXGroup;
children = (
3C6AEC062A30881B0003F34A /* BreedDetailsScreen.swift */,
46A5B5EE26AF54F7002EFEAA /* BreedsScreen.swift */,
F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */,
46A5B60626B04920002EFEAA /* Main.storyboard */,
46B5284C249C5CF400A7725D /* Koin.swift */,
F1465F0923AA94BF0055F7C3 /* Assets.xcassets */,
F1465F0B23AA94BF0055F7C3 /* LaunchScreen.storyboard */,
F1465F0E23AA94BF0055F7C3 /* Info.plist */,
46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */,
3C6AEC062A30881B0003F34A /* BreedDetailsScreen.swift */,
3C6AEC082A30C17A0003F34A /* AppNavigationController.swift */,
3C6AEC082A30C17A0003F34A /* MainNavCoordinator.swift */,
);
path = KaMPKitiOS;
sourceTree = "<group>";
Expand Down Expand Up @@ -368,10 +368,10 @@
buildActionMask = 2147483647;
files = (
46B5284D249C5CF400A7725D /* Koin.swift in Sources */,
46A5B5EF26AF54F7002EFEAA /* BreedListScreen.swift in Sources */,
46A5B5EF26AF54F7002EFEAA /* BreedsScreen.swift in Sources */,
F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */,
3C6AEC072A30881B0003F34A /* BreedDetailsScreen.swift in Sources */,
3C6AEC092A30C17A0003F34A /* AppNavigationController.swift in Sources */,
3C6AEC092A30C17A0003F34A /* MainNavCoordinator.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
5 changes: 3 additions & 2 deletions ios/KaMPKitiOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

startKoin()

let navController = AppNavigationController()
navController.toBreeds()
let navController = UINavigationController()
let mainCoordinator = MainNavCoordinator(navController: navController)
mainCoordinator.start()

self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navController
Expand Down
29 changes: 0 additions & 29 deletions ios/KaMPKitiOS/AppNavigationController.swift

This file was deleted.

9 changes: 9 additions & 0 deletions ios/KaMPKitiOS/BreedDetails/BreedDetailsNavController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// BreedDetailsNavController.swift
// KaMPKitiOS
//
// Created by Bartłomiej Pedryc on 16/06/2023.
// Copyright © 2023 Touchlab. All rights reserved.
//

import Foundation
16 changes: 8 additions & 8 deletions ios/KaMPKitiOS/BreedDetailsScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import shared
import KMPNativeCoroutinesCombine
import Foundation

class ObservableBreedDetailsModel: ObservableObject {
private var viewModel: BreedDetailsViewModel
class BreedDetailsViewModel: ObservableObject {
private var viewModel: BreedDetailsViewModelDelegate

init(breedId: Int64) {
self.viewModel = KotlinDependencies.shared.getBreedDetailsViewModel(breedId: breedId)
}

@Published
var breed: Breed?
var detailsState: BreedDetailsViewState = BreedDetailsViewState.companion.default()

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 }
self?.detailsState = detailsState
}
.store(in: &cancellables)
}
Expand All @@ -44,17 +44,17 @@ class ObservableBreedDetailsModel: ObservableObject {

struct BreedDetailsScreen: View {
@StateObject
var observableModel: ObservableBreedDetailsModel
var viewModel: BreedDetailsViewModel

var body: some View {
BreedDetailsContent(
breedName: observableModel.breed?.name ?? ""
breedName: viewModel.detailsState.breed.name
)
.onAppear(perform: {
observableModel.activate()
viewModel.activate()
})
.onDisappear(perform: {
observableModel.deactivate()
viewModel.deactivate()
})
}
}
Expand Down
13 changes: 13 additions & 0 deletions ios/KaMPKitiOS/Breeds/BreedsNavController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// BreedsNavController.swift
// KaMPKitiOS
//
// Created by Bartłomiej Pedryc on 16/06/2023.
// Copyright © 2023 Touchlab. All rights reserved.
//

import Foundation

func buildBreedsNavController(navController: BreedsNavController) -> UIViewController {
let viewModel = Kotlin
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import KMPNativeCoroutinesCombine

private let log = koin.loggerWithTag(tag: "ViewController")

class ObservableBreedModel: ObservableObject {
private var navigationController: AppNavigationController
private var viewModel: BreedsViewModel = KotlinDependencies.shared.getBreedsViewModel()
init(navigationController: AppNavigationController) {
self.navigationController = navigationController
class BreedsViewModel: ObservableObject {
private var navCoordinator: BreedsNavCoordinator
private var viewModel: BreedsViewModelDelegate = KotlinDependencies.shared.getBreedsViewModel()
init(navCoordinator: BreedsNavCoordinator) {
self.navCoordinator = navCoordinator
}

@Published
Expand All @@ -42,11 +42,8 @@ class ObservableBreedModel: ObservableObject {
self?.breeds = breedState.breeds
self?.error = breedState.error

if let navIntent = breedState.navigationIntent as? NavigationIntent.ToDetails {
self?.navigationController.toBreedDetails(breedId: navIntent.breedId)
self?.viewModel.onNavigationCompleted()
}

self?.handleNavRequests(breedsState: breedState)

if let breeds = breedState.breeds {
log.d(message: {"View updating with \(breeds.count) breeds"})
}
Expand All @@ -57,6 +54,13 @@ class ObservableBreedModel: ObservableObject {
.store(in: &cancellables)
}

private func handleNavRequests(breedsState: BreedViewState) {
if let navRequest = breedsState.breedsNavRequest as? BreedsNavRequest.ToDetails {
self.navCoordinator.onBreedDetailsRequest(breedId: navRequest.breedId)
self.viewModel.onBreedDetailsNavRequestCompleted()
}
}

func deactivate() {
cancellables.forEach { $0.cancel() }
cancellables.removeAll()
Expand All @@ -73,21 +77,21 @@ class ObservableBreedModel: ObservableObject {

struct BreedListScreen: View {
@StateObject
var observableModel: ObservableBreedModel
var viewModel: BreedsViewModel

var body: some View {
BreedListContent(
loading: observableModel.loading,
breeds: observableModel.breeds,
error: observableModel.error,
onBreedFavorite: { observableModel.onBreedFavorite($0) },
refresh: { observableModel.refresh() }
loading: viewModel.loading,
breeds: viewModel.breeds,
error: viewModel.error,
onBreedFavorite: { viewModel.onBreedFavorite($0) },
refresh: { viewModel.refresh() }
)
.onAppear(perform: {
observableModel.activate()
viewModel.activate()
})
.onDisappear(perform: {
observableModel.deactivate()
viewModel.deactivate()
})
}
}
Expand Down
42 changes: 42 additions & 0 deletions ios/KaMPKitiOS/MainNavCoordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// AppNavigationController.swift
// KaMPKitiOS
//
// Created by Bartłomiej Pedryc on 07/06/2023.
// Copyright © 2023 Touchlab. All rights reserved.
//

import SwiftUI
import Foundation

class MainNavCoordinator: BreedsNavCoordinator {
private let navController: UINavigationController

init(navController: UINavigationController) {
self.navController = navController
}

func start() {
let controller = buildBreedsController(navCoordinator: self)
navController.pushViewController(controller, animated: false)
}

func onBreedDetailsRequest(breedId: Int64) {
let controller = buildBreedDetailsController(breedId: breedId)
navController.pushViewController(controller, animated: true)
}
}

private func buildBreedsController(navCoordinator: BreedsNavCoordinator) -> UIHostingController<BreedListScreen> {
let viewModel = BreedsViewModel(navCoordinator: navCoordinator)
return UIHostingController(rootView: BreedListScreen(viewModel: viewModel))
}

private func buildBreedDetailsController(breedId: Int64) -> UIHostingController<BreedDetailsScreen> {
let viewModel = BreedDetailsViewModel(breedId: breedId)
return UIHostingController(rootView: BreedDetailsScreen(viewModel: viewModel))
}

protocol BreedsNavCoordinator {
func onBreedDetailsRequest(breedId: Int64)
}
Loading

0 comments on commit 088eafd

Please sign in to comment.