-
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.
Merge pull request #33 from stslex/dev
dev
- Loading branch information
Showing
20 changed files
with
436 additions
and
161 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
core/core/src/commonMain/kotlin/com/stslex/core/core/coroutine/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,40 @@ | ||
package com.stslex.core.core.coroutine | ||
|
||
import com.stslex.core.core.AppDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface AppCoroutineScope { | ||
|
||
/** | ||
* Launches a coroutine and catches exceptions. The coroutine is launched on the default dispatcher of the AppDispatcher. | ||
* @param onError - error handler | ||
* @param onSuccess - success handler | ||
* @param action - action to be executed | ||
* @return Job | ||
* @see Job | ||
* @see AppDispatcher | ||
* */ | ||
fun <T> launch( | ||
onError: suspend (Throwable) -> Unit = {}, | ||
onSuccess: suspend CoroutineScope.(T) -> Unit = {}, | ||
action: suspend CoroutineScope.() -> T, | ||
): Job | ||
|
||
/** | ||
* Launches a flow and collects it in the screenModelScope. The flow is collected on the default dispatcher. of the AppDispatcher. | ||
* @param onError - error handler | ||
* @param each - action for each element of the flow | ||
* @return Job | ||
* @see Flow | ||
* @see Job | ||
* @see AppDispatcher | ||
* */ | ||
fun <T> launch( | ||
flow: Flow<T>, | ||
onError: suspend (cause: Throwable) -> Unit = {}, | ||
each: suspend (T) -> Unit | ||
): Job | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
core/core/src/commonMain/kotlin/com/stslex/core/core/coroutine/AppCoroutineScopeImpl.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,47 @@ | ||
package com.stslex.core.core.coroutine | ||
|
||
import com.stslex.core.core.AppDispatcher | ||
import com.stslex.core.core.Logger | ||
import com.stslex.core.core.coroutineExceptionHandler | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.launch | ||
|
||
class AppCoroutineScopeImpl( | ||
private val scope: CoroutineScope, | ||
private val appDispatcher: AppDispatcher | ||
) : AppCoroutineScope { | ||
|
||
private fun exceptionHandler( | ||
onError: suspend (cause: Throwable) -> Unit = {}, | ||
) = CoroutineExceptionHandler { _, throwable -> | ||
Logger.exception(throwable) | ||
scope.launch(appDispatcher.default + coroutineExceptionHandler) { | ||
onError(throwable) | ||
} | ||
} | ||
|
||
override fun <T> launch( | ||
onError: suspend (Throwable) -> Unit, | ||
onSuccess: suspend CoroutineScope.(T) -> Unit, | ||
action: suspend CoroutineScope.() -> T | ||
): Job = scope.launch( | ||
context = exceptionHandler(onError) + appDispatcher.default, | ||
block = { | ||
onSuccess(action()) | ||
} | ||
) | ||
|
||
override fun <T> launch( | ||
flow: Flow<T>, | ||
onError: suspend (cause: Throwable) -> Unit, | ||
each: suspend (T) -> Unit | ||
): Job = scope.launch( | ||
context = exceptionHandler(onError) + appDispatcher.default, | ||
block = { | ||
flow.collect(each) | ||
} | ||
) | ||
} |
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
8 changes: 8 additions & 0 deletions
8
core/ui/src/commonMain/kotlin/com/stslex/core/ui/pager/PagerLoadEvents.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,8 @@ | ||
package com.stslex.core.ui.pager | ||
|
||
import com.stslex.core.ui.base.AppError | ||
|
||
sealed interface PagerLoadEvents { | ||
|
||
data class Error(val error: AppError) : PagerLoadEvents | ||
} |
20 changes: 20 additions & 0 deletions
20
core/ui/src/commonMain/kotlin/com/stslex/core/ui/pager/PagerLoadState.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,20 @@ | ||
package com.stslex.core.ui.pager | ||
|
||
import com.stslex.core.ui.base.AppError | ||
|
||
sealed interface PagerLoadState { | ||
|
||
data object Data : PagerLoadState | ||
|
||
data object Initial : PagerLoadState | ||
|
||
data object Loading : PagerLoadState | ||
|
||
data object Refresh : PagerLoadState | ||
|
||
data object Retry : PagerLoadState | ||
|
||
data class Error(val error: AppError) : PagerLoadState | ||
|
||
data object Empty : PagerLoadState | ||
} |
9 changes: 9 additions & 0 deletions
9
core/ui/src/commonMain/kotlin/com/stslex/core/ui/pager/PagingMapper.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,9 @@ | ||
package com.stslex.core.ui.pager | ||
|
||
import com.stslex.core.core.paging.PagingCoreItem | ||
import com.stslex.core.ui.base.paging.PagingItem | ||
|
||
fun interface PagingMapper<T : PagingCoreItem, R : PagingItem> { | ||
|
||
suspend operator fun invoke(item: T): R | ||
} |
23 changes: 23 additions & 0 deletions
23
core/ui/src/commonMain/kotlin/com/stslex/core/ui/pager/StorePager.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,23 @@ | ||
package com.stslex.core.ui.pager | ||
|
||
import com.stslex.core.ui.base.paging.PagingItem | ||
import com.stslex.core.ui.base.paging.PagingState | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
interface StorePager<out Item : PagingItem> { | ||
|
||
val state: StateFlow<PagingState<Item>> | ||
|
||
val loadState: StateFlow<PagerLoadState> | ||
|
||
val loadEvents: SharedFlow<PagerLoadEvents> | ||
|
||
fun initialLoad() | ||
|
||
fun load() | ||
|
||
fun refresh() | ||
|
||
fun retry() | ||
} |
Oops, something went wrong.