-
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.
- Loading branch information
1 parent
3ad5103
commit e9fda80
Showing
22 changed files
with
285 additions
and
52 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
5 changes: 0 additions & 5 deletions
5
common/src/commonMain/kotlin/com/vn/chungha/pet_kmm/data/Result.kt
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
common/src/commonMain/kotlin/com/vn/chungha/pet_kmm/data/mapper/CatMapper.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,11 @@ | ||
package com.vn.chungha.pet_kmm.data.mapper | ||
|
||
import com.vn.chungha.pet_kmm.data.remote.response.PetCatResponse | ||
import com.vn.chungha.pet_kmm.domain.model.PetModel | ||
|
||
fun PetCatResponse.toPetCat() = PetModel( | ||
id = id, | ||
name = name, | ||
description = description, | ||
url = image.url | ||
) |
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
32 changes: 25 additions & 7 deletions
32
common/src/commonMain/kotlin/com/vn/chungha/pet_kmm/data/repository/PetHomeRepositoryIml.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 |
---|---|---|
@@ -1,14 +1,32 @@ | ||
package com.vn.chungha.pet_kmm.data.repository | ||
|
||
import androidx.compose.animation.core.rememberTransition | ||
import com.github.michaelbull.result.coroutines.runSuspendCatching | ||
import com.github.michaelbull.result.Result | ||
import com.vn.chungha.pet_kmm.data.remote.PetApi | ||
import com.vn.chungha.pet_kmm.data.remote.response.PetCatResponse | ||
import com.vn.chungha.pet_kmm.domain.PetCatRepository | ||
import com.vn.chungha.pet_kmm.domain.mapper.toPetModel | ||
import com.vn.chungha.pet_kmm.domain.model.PetModel | ||
import com.vn.chungha.pet_kmm.utils.AppCoroutineDispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class PetHomeRepositoryIml(private val petApi: PetApi) : PetCatRepository { | ||
override suspend fun getPetCatByPage( | ||
query: String, | ||
page: Int, | ||
perPage: Int | ||
): List<PetCatResponse> = | ||
petApi.fetchPetHomeByBreedPage(0, 20) | ||
class PetHomeRepositoryIml( | ||
private val petApi: PetApi, | ||
private val appCoroutineDispatchers: AppCoroutineDispatchers, | ||
) : | ||
PetCatRepository { | ||
override suspend fun getPetCatByPage( | ||
query: String, | ||
page: Int, | ||
perPage: Int, | ||
): Result<List<PetModel>, Throwable> { | ||
return withContext(appCoroutineDispatchers.io) { | ||
runSuspendCatching { | ||
petApi.fetchPetHomeByBreedPage(page, perPage).map { | ||
it.toPetModel() | ||
} | ||
} | ||
} | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
common/src/commonMain/kotlin/com/vn/chungha/pet_kmm/di/viewModel.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,15 @@ | ||
@file:Suppress("PackageDirectoryMismatch") | ||
|
||
package com.vn.chungha.pet_kmm.di | ||
|
||
import androidx.lifecycle.ViewModel | ||
import org.koin.core.definition.Definition | ||
import org.koin.core.definition.KoinDefinition | ||
import org.koin.core.module.Module | ||
import org.koin.core.qualifier.Qualifier | ||
|
||
// https://github.com/InsertKoinIO/koin-annotations/issues/130#issuecomment-2189079092 | ||
public inline fun <reified T : ViewModel> Module.viewModel( | ||
qualifier: Qualifier? = null, | ||
noinline definition: Definition<T>, | ||
): KoinDefinition<T> = factory(qualifier, definition) |
5 changes: 3 additions & 2 deletions
5
common/src/commonMain/kotlin/com/vn/chungha/pet_kmm/domain/PetCatRepository.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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package com.vn.chungha.pet_kmm.domain | ||
|
||
import com.vn.chungha.pet_kmm.data.remote.response.PetCatResponse | ||
import com.github.michaelbull.result.Result | ||
import com.vn.chungha.pet_kmm.domain.model.PetModel | ||
|
||
interface PetCatRepository { | ||
suspend fun getPetCatByPage(query: String, page: Int, perPage: Int): List<PetCatResponse> | ||
suspend fun getPetCatByPage(query: String, page: Int, perPage: Int): Result<List<PetModel>, Throwable> | ||
} |
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
14 changes: 14 additions & 0 deletions
14
common/src/commonMain/kotlin/com/vn/chungha/pet_kmm/utils/AppCoroutineDispatcherImpl.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,14 @@ | ||
package com.vn.chungha.pet_kmm.utils | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.IO | ||
import kotlinx.coroutines.MainCoroutineDispatcher | ||
|
||
internal class AppCoroutineDispatcherImpl : AppCoroutineDispatchers { | ||
override val main: CoroutineDispatcher get() = Dispatchers.Main | ||
override val io: CoroutineDispatcher get() = Dispatchers.IO | ||
override val default: CoroutineDispatcher get() = Dispatchers.Default | ||
override val unconfined: CoroutineDispatcher get() = Dispatchers.Unconfined | ||
override val immediateMain: MainCoroutineDispatcher get() = Dispatchers.Main.immediate | ||
} |
102 changes: 102 additions & 0 deletions
102
common/src/commonMain/kotlin/com/vn/chungha/pet_kmm/utils/AppCoroutineDispatchers.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,102 @@ | ||
package com.vn.chungha.pet_kmm.utils | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
/** | ||
* An interface that provides properties for accessing commonly used [CoroutineDispatcher]s. This differs from the | ||
* [Dispatchers] object in that it has consistent properties across all platforms and since [AppCoroutineDispatchers] is | ||
* an interface, it can easily be mocked and tested, and different implementations can easily be made to adapt to | ||
* different scenarios. | ||
* | ||
* Each supported platform contains an implementation of this [AppCoroutineDispatchers] interface. | ||
* | ||
* Note that not all platforms natively support all of the [CoroutineDispatcher] types (ex: only JVM supports | ||
* Dispatchers.IO), so fallbacks are provided when they aren't available for the default implementations. | ||
*/ | ||
interface AppCoroutineDispatchers { | ||
/** | ||
* The companion object for the [CoroutineDispatchers] interface. This is provided so that it's possible to create | ||
* extension functions and properties on the companion object. | ||
*/ | ||
companion object | ||
|
||
/** | ||
* The main [CoroutineDispatcher] that is usually used for UI work. Default implementations of this interface, | ||
* refer to [Dispatchers.Main] for this value when it is available. | ||
* | ||
* Note that this isn't available on all platforms, so when one isn't present, this falls back to the [default] | ||
* [CoroutineDispatcher] in the default implementation. | ||
* | ||
* Default implementation [CoroutineDispatcher]: | ||
* Android - Main | ||
* iOS - Custom Main implementation or Default | ||
*/ | ||
val main: CoroutineDispatcher | ||
|
||
/** | ||
* The [CoroutineDispatcher] that is usually used for input/output, or intensive operations. Default | ||
* implementations of this interface, refer to Dispatchers.IO for this value when it is available. | ||
* | ||
* Note that this isn't available on all platforms, so when one isn't present, this falls back to the [default] | ||
* [CoroutineDispatcher] in the default implementation. | ||
* | ||
* Default implementation [CoroutineDispatcher]: | ||
* Android - IO | ||
* iOS - Default | ||
*/ | ||
val io: CoroutineDispatcher | ||
|
||
/** | ||
* The [CoroutineDispatcher] that is the default that is used by all standard builders like launch and async if no | ||
* other [CoroutineDispatcher] is provided or in their context. Default implementations of this interface, refer to | ||
* [Dispatchers.Default] for this value. | ||
* | ||
* Default implementation [CoroutineDispatcher]: | ||
* Android - Default | ||
* iOS - Default | ||
*/ | ||
val default: CoroutineDispatcher | ||
|
||
/** | ||
* The [CoroutineDispatcher] that is not confined to any specific thread. Default implementations of this | ||
* interface refer to [Dispatchers.Unconfined] for this value. | ||
* | ||
* Default implementation [CoroutineDispatcher]: | ||
* Android - Unconfined | ||
* iOS - Unconfined | ||
*/ | ||
val unconfined: CoroutineDispatcher | ||
|
||
/** | ||
* Returns dispatcher that executes coroutines immediately when it is already in the right context | ||
* (e.g. current looper is the same as this handler's looper) without an additional [re-dispatch][CoroutineDispatcher.dispatch]. | ||
* | ||
* Immediate dispatcher is safe from stack overflows and in case of nested invocations forms event-loop similar to [Dispatchers.Unconfined]. | ||
* The event loop is an advanced topic and its implications can be found in [Dispatchers.Unconfined] documentation. | ||
* The formed event-loop is shared with [Unconfined] and other immediate dispatchers, potentially overlapping tasks between them. | ||
* | ||
* Example of usage: | ||
* ``` | ||
* suspend fun updateUiElement(val text: String) { | ||
* /* | ||
* * If it is known that updateUiElement can be invoked both from the Main thread and from other threads, | ||
* * `immediate` dispatcher is used as a performance optimization to avoid unnecessary dispatch. | ||
* * | ||
* * In that case, when `updateUiElement` is invoked from the Main thread, `uiElement.text` will be | ||
* * invoked immediately without any dispatching, otherwise, the `Dispatchers.Main` dispatch cycle will be triggered. | ||
* */ | ||
* withContext(immediateMain) { | ||
* uiElement.text = text | ||
* } | ||
* // Do context-independent logic such as logging | ||
* } | ||
* ``` | ||
* | ||
* Method may throw [UnsupportedOperationException] if immediate dispatching is not supported by current dispatcher, | ||
* please refer to specific dispatcher documentation. | ||
* | ||
* [Dispatchers.Main] supports immediate execution for Android, JavaFx and Swing platforms. | ||
*/ | ||
val immediateMain: CoroutineDispatcher | ||
} |
18 changes: 18 additions & 0 deletions
18
common/src/commonMain/kotlin/com/vn/chungha/pet_kmm/utils/AppCoroutineScope.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 com.vn.chungha.pet_kmm.utils | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.SupervisorJob | ||
|
||
interface AppCoroutineScope : CoroutineScope | ||
|
||
internal open class IoAppCoroutineScopeImpl(appCoroutineDispatchers: AppCoroutineDispatchers) : AppCoroutineScope { | ||
override val coroutineContext = SupervisorJob() + appCoroutineDispatchers.io | ||
|
||
override fun toString() = "DefaultAppCoroutineScope(coroutineContext=$coroutineContext)" | ||
} | ||
|
||
internal open class MainAppCoroutineScopeImpl(appCoroutineDispatchers: AppCoroutineDispatchers) : AppCoroutineScope { | ||
override val coroutineContext = SupervisorJob() + appCoroutineDispatchers.main | ||
|
||
override fun toString() = "DefaultAppCoroutineScope(coroutineContext=$coroutineContext)" | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.