From 99379a3aaa4bfb708f2951fc5a4fc7e29d5b8572 Mon Sep 17 00:00:00 2001 From: bpedryc Date: Wed, 5 Jul 2023 12:56:39 +0200 Subject: [PATCH] Fix rebase changes --- .../kampkit/android/ui/BreedsScreen.kt | 3 +- ios/KaMPKitiOS/Breeds/BreedsViewModel.swift | 2 +- .../kotlin/co/touchlab/kampkit/core/Koin.kt | 4 -- .../kampkit/data/dog/DogDatabaseHelper.kt | 2 - .../kampkit/data/dog/DogRepository.kt | 65 ------------------- .../data/dog/NetworkBreedRepository.kt | 19 ++++-- .../kampkit/domain/breed/BreedRepository.kt | 1 + .../ui/breedDetails/BreedDetailsViewModel.kt | 8 +-- .../ui/breedDetails/BreedDisplayable.kt | 2 +- .../kampkit/ui/breeds/BreedsViewModel.kt | 15 ----- .../kampkit/ui/breeds/BreedsViewState.kt | 3 +- .../touchlab/kampkit/BreedsViewModelTest.kt | 2 +- ...yTest.kt => NetworkBreedRepositoryTest.kt} | 2 +- .../co/touchlab/kampkit/core/KoinIOS.kt | 1 - 14 files changed, 26 insertions(+), 103 deletions(-) delete mode 100644 shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/DogRepository.kt rename shared/src/commonTest/kotlin/co/touchlab/kampkit/{NetworkDogRepositoryTest.kt => NetworkBreedRepositoryTest.kt} (99%) diff --git a/app/src/main/kotlin/co/touchlab/kampkit/android/ui/BreedsScreen.kt b/app/src/main/kotlin/co/touchlab/kampkit/android/ui/BreedsScreen.kt index dccd8440..b0f0fc9c 100644 --- a/app/src/main/kotlin/co/touchlab/kampkit/android/ui/BreedsScreen.kt +++ b/app/src/main/kotlin/co/touchlab/kampkit/android/ui/BreedsScreen.kt @@ -27,6 +27,7 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import co.touchlab.kampkit.android.R import co.touchlab.kampkit.domain.breed.Breed +import co.touchlab.kampkit.ui.breeds.BreedsNavRequest import co.touchlab.kampkit.ui.breeds.BreedsViewModel import co.touchlab.kampkit.ui.breeds.BreedsViewState import co.touchlab.kermit.Logger @@ -83,7 +84,7 @@ fun BreedsScreenContent( LaunchedEffect(breeds) { onSuccess(breeds) } - Success(successData = breeds, favoriteBreed = onFavorite) + Success(successData = breeds, onBreedClick = onBreedClick) } } diff --git a/ios/KaMPKitiOS/Breeds/BreedsViewModel.swift b/ios/KaMPKitiOS/Breeds/BreedsViewModel.swift index 49a2b7aa..f7375eb3 100644 --- a/ios/KaMPKitiOS/Breeds/BreedsViewModel.swift +++ b/ios/KaMPKitiOS/Breeds/BreedsViewModel.swift @@ -35,7 +35,7 @@ class BreedsViewModel: ObservableObject { .store(in: &cancellables) } - private func handleNavRequests(breedsState: BreedViewState) { + private func handleNavRequests(breedsState: BreedsViewState) { if let navRequest = breedsState.breedsNavRequest as? BreedsNavRequest.ToDetails { self.navCoordinator.onBreedDetailsRequest(breedId: navRequest.breedId) self.viewModelDelegate.onBreedDetailsNavRequestCompleted() diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/core/Koin.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/core/Koin.kt index 7296ac48..5bd590b6 100644 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/core/Koin.kt +++ b/shared/src/commonMain/kotlin/co/touchlab/kampkit/core/Koin.kt @@ -5,10 +5,6 @@ import co.touchlab.kampkit.data.dog.DogApiImpl import co.touchlab.kampkit.data.dog.DogDatabaseHelper import co.touchlab.kampkit.data.dog.NetworkBreedRepository import co.touchlab.kampkit.domain.breed.BreedRepository -import co.touchlab.kampkit.data.dog.DogApi -import co.touchlab.kampkit.data.dog.DogApiImpl -import co.touchlab.kampkit.data.dog.DogDatabaseHelper -import co.touchlab.kampkit.data.dog.DogRepository import co.touchlab.kermit.Logger import co.touchlab.kermit.StaticConfig import co.touchlab.kermit.platformLogWriter diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/DogDatabaseHelper.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/DogDatabaseHelper.kt index ca111eef..85dea97d 100644 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/DogDatabaseHelper.kt +++ b/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/DogDatabaseHelper.kt @@ -1,7 +1,5 @@ package co.touchlab.kampkit.data.dog -import co.touchlab.kampkit.core.transactionWithContext -import co.touchlab.kampkit.db.Breed import co.touchlab.kampkit.core.transactionWithContext import co.touchlab.kampkit.db.DbBreed import co.touchlab.kampkit.db.KaMPKitDb diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/DogRepository.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/DogRepository.kt deleted file mode 100644 index 8bfa52fe..00000000 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/DogRepository.kt +++ /dev/null @@ -1,65 +0,0 @@ -package co.touchlab.kampkit.data.dog - -import co.touchlab.kampkit.db.Breed -import co.touchlab.kermit.Logger -import co.touchlab.stately.ensureNeverFrozen -import com.russhwolf.settings.Settings -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.first -import kotlinx.datetime.Clock - -class DogRepository( - private val dbHelper: DogDatabaseHelper, - private val settings: Settings, - private val dogApi: DogApi, - log: Logger, - private val clock: Clock -) { - - private val log = log.withTag("BreedModel") - - companion object { - internal const val DB_TIMESTAMP_KEY = "DbTimestampKey" - } - - init { - ensureNeverFrozen() - } - - fun getBreed(id: Long): Flow = dbHelper.selectById(id) - - fun getBreeds(): Flow> = dbHelper.selectAllItems() - - suspend fun refreshBreedsIfStale() { - if (isBreedListStale()) { - refreshBreeds() - } - } - - suspend fun refreshBreeds() { - val breedResult = dogApi.getJsonFromApi() - log.v { "Breed network result: ${breedResult.status}" } - val breedList = breedResult.message.keys.sorted().toList() - log.v { "Fetched ${breedList.size} breeds from network" } - settings.putLong(DB_TIMESTAMP_KEY, clock.now().toEpochMilliseconds()) - - if (breedList.isNotEmpty()) { - dbHelper.insertBreeds(breedList) - } - } - - suspend fun updateBreedFavorite(breedId: Long) { - val breed = dbHelper.selectById(breedId).first() ?: throw Exception("Breed not found") - dbHelper.updateFavorite(breed.id, !breed.favorite) - } - - private fun isBreedListStale(): Boolean { - val lastDownloadTimeMS = settings.getLong(DB_TIMESTAMP_KEY, 0) - val oneHourMS = 60 * 60 * 1000 - val stale = lastDownloadTimeMS + oneHourMS < clock.now().toEpochMilliseconds() - if (!stale) { - log.i { "Breeds not fetched from network. Recently updated" } - } - return stale - } -} diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/NetworkBreedRepository.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/NetworkBreedRepository.kt index df74d05f..e14f45db 100644 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/NetworkBreedRepository.kt +++ b/shared/src/commonMain/kotlin/co/touchlab/kampkit/data/dog/NetworkBreedRepository.kt @@ -18,7 +18,7 @@ class NetworkBreedRepository( private val clock: Clock ) : BreedRepository { - private val log = log.withTag("DogRepository") + private val log = log.withTag("NetworkBreedRepository") companion object { internal const val DB_TIMESTAMP_KEY = "DbTimestampKey" @@ -28,10 +28,15 @@ class NetworkBreedRepository( ensureNeverFrozen() } + override fun getBreed(id: Long): Flow { + return dbHelper + .selectById(id) + .map { dbBreed -> dbBreed?.toDomain() } + } override fun getBreeds(): Flow> { - return dbHelper.selectAllItems().map { list -> - list.map { dbBreed -> dbBreed.toDomain() } - } + return dbHelper + .selectAllItems() + .map { list -> list.map { dbBreed -> dbBreed.toDomain() } } } override suspend fun refreshBreedsIfStale() { @@ -53,8 +58,10 @@ class NetworkBreedRepository( } override suspend fun updateBreedFavorite(breedId: Long) { - val foundBreedsWithId = dbHelper.selectById(breedId).first() - foundBreedsWithId.firstOrNull()?.let { breed -> + dbHelper + .selectById(breedId) + .first() + ?.let { breed -> dbHelper.updateFavorite(breed.id, !breed.favorite) } } diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/domain/breed/BreedRepository.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/domain/breed/BreedRepository.kt index b5cbb2e2..626bd672 100644 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/domain/breed/BreedRepository.kt +++ b/shared/src/commonMain/kotlin/co/touchlab/kampkit/domain/breed/BreedRepository.kt @@ -3,6 +3,7 @@ package co.touchlab.kampkit.domain.breed import kotlinx.coroutines.flow.Flow interface BreedRepository { + fun getBreed(id: Long): Flow fun getBreeds(): Flow> suspend fun refreshBreedsIfStale() suspend fun refreshBreeds() diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breedDetails/BreedDetailsViewModel.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breedDetails/BreedDetailsViewModel.kt index f279354a..349e6c35 100644 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breedDetails/BreedDetailsViewModel.kt +++ b/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breedDetails/BreedDetailsViewModel.kt @@ -1,7 +1,7 @@ package co.touchlab.kampkit.ui.breedDetails import co.touchlab.kampkit.core.ViewModel -import co.touchlab.kampkit.data.dog.DogRepository +import co.touchlab.kampkit.domain.breed.BreedRepository import co.touchlab.kermit.Logger import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState import kotlinx.coroutines.flow.MutableStateFlow @@ -13,7 +13,7 @@ import kotlin.native.ObjCName @ObjCName("BreedDetailsViewModelDelegate") class BreedDetailsViewModel( private val breedId: Long, - private val dogRepository: DogRepository, + private val breedRepository: BreedRepository, log: Logger ) : ViewModel() { private val log = log.withTag("BreedDetailsViewModel") @@ -30,7 +30,7 @@ class BreedDetailsViewModel( private fun loadDetails() { viewModelScope.launch { - dogRepository.getBreed(breedId).collect { breed -> + breedRepository.getBreed(breedId).collect { breed -> mutableDetailsState.update { previousState -> val error = if (breed == null) "Couldn't load the breed details" else null val newBreed = breed?.toDisplayable() ?: previousState.breed @@ -46,7 +46,7 @@ class BreedDetailsViewModel( fun onFavoriteClick() { viewModelScope.launch { - dogRepository.updateBreedFavorite(breedId) + breedRepository.updateBreedFavorite(breedId) } } } diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breedDetails/BreedDisplayable.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breedDetails/BreedDisplayable.kt index 58767ae1..e49b4b9c 100644 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breedDetails/BreedDisplayable.kt +++ b/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breedDetails/BreedDisplayable.kt @@ -1,6 +1,6 @@ package co.touchlab.kampkit.ui.breedDetails -import co.touchlab.kampkit.db.Breed +import co.touchlab.kampkit.domain.breed.Breed data class BreedDisplayable( val id: Long = 0, diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breeds/BreedsViewModel.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breeds/BreedsViewModel.kt index 52ada5f8..af861b5a 100644 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breeds/BreedsViewModel.kt +++ b/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breeds/BreedsViewModel.kt @@ -1,7 +1,6 @@ package co.touchlab.kampkit.ui.breeds import co.touchlab.kampkit.core.ViewModel -import co.touchlab.kampkit.domain.breed.Breed import co.touchlab.kampkit.domain.breed.BreedRepository import co.touchlab.kermit.Logger import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState @@ -105,17 +104,3 @@ class BreedsViewModel( } } } - -data class BreedViewState( - val breeds: List = emptyList(), - val error: String? = null, - val isLoading: Boolean = false, - val isEmpty: Boolean = false, - val breedsNavRequest: BreedsNavRequest? = null -) { - companion object { - // This method lets you use the default constructor values in Swift. When accessing the - // constructor directly, they will not work there and would need to be provided explicitly. - fun default() = BreedViewState() - } -} diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breeds/BreedsViewState.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breeds/BreedsViewState.kt index 0a908bad..75633c56 100644 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breeds/BreedsViewState.kt +++ b/shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/breeds/BreedsViewState.kt @@ -6,7 +6,8 @@ data class BreedsViewState( val breeds: List = emptyList(), val error: String? = null, val isLoading: Boolean = false, - val isEmpty: Boolean = false + val isEmpty: Boolean = false, + val breedsNavRequest: BreedsNavRequest? = null ) { companion object { // This method lets you use the default constructor values in Swift. When accessing the diff --git a/shared/src/commonTest/kotlin/co/touchlab/kampkit/BreedsViewModelTest.kt b/shared/src/commonTest/kotlin/co/touchlab/kampkit/BreedsViewModelTest.kt index 9f44f729..8f22573f 100644 --- a/shared/src/commonTest/kotlin/co/touchlab/kampkit/BreedsViewModelTest.kt +++ b/shared/src/commonTest/kotlin/co/touchlab/kampkit/BreedsViewModelTest.kt @@ -142,7 +142,7 @@ class BreedsViewModelTest { dbHelper.insertBreeds(breedNames) viewModel.onBreedClick(1).join() - viewModel.breedState.test { + viewModel.breedsState.test { val state = awaitItem() assertEquals(BreedsNavRequest.ToDetails(1), state.breedsNavRequest) } diff --git a/shared/src/commonTest/kotlin/co/touchlab/kampkit/NetworkDogRepositoryTest.kt b/shared/src/commonTest/kotlin/co/touchlab/kampkit/NetworkBreedRepositoryTest.kt similarity index 99% rename from shared/src/commonTest/kotlin/co/touchlab/kampkit/NetworkDogRepositoryTest.kt rename to shared/src/commonTest/kotlin/co/touchlab/kampkit/NetworkBreedRepositoryTest.kt index e7850239..102564fb 100644 --- a/shared/src/commonTest/kotlin/co/touchlab/kampkit/NetworkDogRepositoryTest.kt +++ b/shared/src/commonTest/kotlin/co/touchlab/kampkit/NetworkBreedRepositoryTest.kt @@ -18,7 +18,7 @@ import kotlin.test.assertEquals import kotlin.test.assertFails import kotlin.time.Duration.Companion.hours -class NetworkDogRepositoryTest { +class NetworkBreedRepositoryTest { private var kermit = Logger(StaticConfig()) private var testDbConnection = testDbConnection() diff --git a/shared/src/iosMain/kotlin/co/touchlab/kampkit/core/KoinIOS.kt b/shared/src/iosMain/kotlin/co/touchlab/kampkit/core/KoinIOS.kt index dfc125ac..faf9243f 100644 --- a/shared/src/iosMain/kotlin/co/touchlab/kampkit/core/KoinIOS.kt +++ b/shared/src/iosMain/kotlin/co/touchlab/kampkit/core/KoinIOS.kt @@ -1,7 +1,6 @@ package co.touchlab.kampkit.core import co.touchlab.kampkit.db.KaMPKitDb -import co.touchlab.kampkit.ui.breeds.BreedsViewModel import co.touchlab.kampkit.ui.breedDetails.BreedDetailsViewModel import co.touchlab.kampkit.ui.breeds.BreedsViewModel import co.touchlab.kermit.Logger