-
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
Showing
38 changed files
with
518 additions
and
390 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
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
11 changes: 0 additions & 11 deletions
11
core/network/src/commonMain/kotlin/com/stslex/core/network/utils/PagingWorker.kt
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
core/network/src/commonMain/kotlin/com/stslex/core/network/utils/PagingWorkerImpl.kt
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
core/ui/src/commonMain/kotlin/com/stslex/core/ui/di/CoreUiModule.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.stslex.core.ui.di | ||
|
||
import com.stslex.core.ui.pager.pager.StorePagerFactory | ||
import com.stslex.core.ui.pager.pager.StorePagerFactoryImpl | ||
import org.koin.dsl.module | ||
|
||
val coreUiModule = module { | ||
factory<StorePagerFactory> { | ||
StorePagerFactoryImpl() | ||
} | ||
} |
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
core/ui/src/commonMain/kotlin/com/stslex/core/ui/pager/pager/StorePagerFactory.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.stslex.core.ui.pager.pager | ||
|
||
import com.stslex.core.core.coroutine.AppCoroutineScope | ||
import com.stslex.core.core.paging.PagingCoreData | ||
import com.stslex.core.core.paging.PagingCoreItem | ||
import com.stslex.core.core.paging.PagingResponse | ||
import com.stslex.core.ui.base.paging.PagingItem | ||
import com.stslex.core.ui.pager.utils.PagingMapper | ||
|
||
interface StorePagerFactory { | ||
|
||
fun <T : PagingItem, R : PagingCoreItem> create( | ||
scope: AppCoroutineScope, | ||
request: suspend (page: Int, pageSize: Int) -> PagingResponse<R>, | ||
mapper: PagingMapper<R, T>, | ||
pageSize: Int = PagingCoreData.DEFAULT_PAGE_SIZE, | ||
): StorePager<T> | ||
} |
25 changes: 25 additions & 0 deletions
25
core/ui/src/commonMain/kotlin/com/stslex/core/ui/pager/pager/StorePagerFactoryImpl.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,25 @@ | ||
package com.stslex.core.ui.pager.pager | ||
|
||
import com.stslex.core.core.coroutine.AppCoroutineScope | ||
import com.stslex.core.core.paging.PagingCoreItem | ||
import com.stslex.core.core.paging.PagingResponse | ||
import com.stslex.core.ui.base.paging.PagingItem | ||
import com.stslex.core.ui.pager.utils.PagingMapper | ||
import com.stslex.core.ui.pager.utils.PagingWorkerImpl | ||
|
||
class StorePagerFactoryImpl : StorePagerFactory { | ||
|
||
override fun <T : PagingItem, R : PagingCoreItem> create( | ||
scope: AppCoroutineScope, | ||
request: suspend (page: Int, pageSize: Int) -> PagingResponse<R>, | ||
mapper: PagingMapper<R, T>, | ||
pageSize: Int | ||
): StorePager<T> { | ||
return StorePagerImpl( | ||
pagingWorker = PagingWorkerImpl(scope = scope), | ||
request = request, | ||
mapper = mapper, | ||
pageSize = pageSize | ||
) | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...m/stslex/core/ui/pager/PagerLoadEvents.kt → ...x/core/ui/pager/states/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
2 changes: 1 addition & 1 deletion
2
...om/stslex/core/ui/pager/PagerLoadState.kt → ...ex/core/ui/pager/states/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
2 changes: 1 addition & 1 deletion
2
.../com/stslex/core/ui/pager/PagingMapper.kt → ...tslex/core/ui/pager/utils/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
18 changes: 18 additions & 0 deletions
18
core/ui/src/commonMain/kotlin/com/stslex/core/ui/pager/utils/PagingWorker.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.stslex.core.ui.pager.utils | ||
|
||
import com.stslex.core.core.paging.PagingCoreItem | ||
import com.stslex.core.core.paging.PagingResponse | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
|
||
interface PagingWorker { | ||
|
||
fun <T : PagingCoreItem> launch( | ||
onError: suspend (Throwable) -> Unit = {}, | ||
onSuccess: suspend CoroutineScope.(PagingResponse<T>) -> Unit = {}, | ||
action: suspend CoroutineScope.() -> PagingResponse<T>, | ||
): Job | ||
|
||
fun cancel() | ||
} | ||
|
69 changes: 69 additions & 0 deletions
69
core/ui/src/commonMain/kotlin/com/stslex/core/ui/pager/utils/PagingWorkerImpl.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,69 @@ | ||
package com.stslex.core.ui.pager.utils | ||
|
||
import com.stslex.core.core.coroutine.AppCoroutineScope | ||
import com.stslex.core.core.paging.PagingCoreItem | ||
import com.stslex.core.core.paging.PagingResponse | ||
import com.stslex.core.network.utils.currentTimeMs | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.CoroutineStart | ||
import kotlinx.coroutines.Job | ||
|
||
class PagingWorkerImpl( | ||
private val scope: AppCoroutineScope | ||
) : PagingWorker { | ||
|
||
private var job: Job? = null | ||
private var nextPageJob: Job? = null | ||
private var lastRequestTime = 0L | ||
|
||
override fun <T : PagingCoreItem> launch( | ||
onError: suspend (Throwable) -> Unit, | ||
onSuccess: suspend CoroutineScope.(PagingResponse<T>) -> Unit, | ||
action: suspend CoroutineScope.() -> PagingResponse<T> | ||
): Job = if (lastRequestTime + REQUEST_DELAY > currentTimeMs) { | ||
startRequest( | ||
onError = onError, | ||
onSuccess = onSuccess, | ||
action = action, | ||
start = CoroutineStart.LAZY | ||
).apply { | ||
nextPageJob = this | ||
} | ||
} else { | ||
startRequest( | ||
onError = onError, | ||
onSuccess = onSuccess, | ||
action = action, | ||
) | ||
} | ||
|
||
override fun cancel() { | ||
job?.cancel() | ||
nextPageJob?.cancel() | ||
} | ||
|
||
private fun <T : PagingCoreItem> startRequest( | ||
onError: suspend (Throwable) -> Unit, | ||
onSuccess: suspend CoroutineScope.(PagingResponse<T>) -> Unit, | ||
action: suspend CoroutineScope.() -> PagingResponse<T>, | ||
start: CoroutineStart = CoroutineStart.DEFAULT, | ||
): Job = scope | ||
.launch( | ||
start = start, | ||
onError = onError, | ||
onSuccess = onSuccess, | ||
action = { | ||
job = nextPageJob | ||
nextPageJob = null | ||
action() | ||
} | ||
).apply { | ||
invokeOnCompletion { | ||
nextPageJob?.start() | ||
} | ||
} | ||
|
||
companion object { | ||
private const val REQUEST_DELAY = 500L | ||
} | ||
} |
Oops, something went wrong.