From 65247cf608cae7934161a8d077e1a0fd03e3aa8e Mon Sep 17 00:00:00 2001 From: Maxwell Mapako Date: Sat, 8 Apr 2023 20:06:19 +0200 Subject: [PATCH] Remove unnecessary receiver type enforcement (#145) --- .../arch/extension/ext/LifecycleExt.kt | 68 ------------------- gradle/release.properties | 4 +- .../ui/fragment/list/SupportFragmentList.kt | 58 +++++++++------- 3 files changed, 34 insertions(+), 96 deletions(-) delete mode 100644 extension/src/main/kotlin/co/anitrend/arch/extension/ext/LifecycleExt.kt diff --git a/extension/src/main/kotlin/co/anitrend/arch/extension/ext/LifecycleExt.kt b/extension/src/main/kotlin/co/anitrend/arch/extension/ext/LifecycleExt.kt deleted file mode 100644 index 4db4be95..00000000 --- a/extension/src/main/kotlin/co/anitrend/arch/extension/ext/LifecycleExt.kt +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright 2021 AniTrend - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package co.anitrend.arch.extension.ext - -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.LifecycleCoroutineScope -import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.repeatOnLifecycle -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Job -import kotlinx.coroutines.launch - -/** - * Launches a coroutine for [block] when the [LifecycleOwner] matches [state] - * - * @see Lifecycle.repeatOn - */ -suspend fun LifecycleOwner.repeatOn( - state: Lifecycle.State, - block: - suspend CoroutineScope.() -> Unit, -) = lifecycle.repeatOn(state, block) - -/** - * Launches a coroutine for [block] when the [Lifecycle] matches [state] - * - * @see LifecycleCoroutineScope.repeatOn - */ -suspend fun Lifecycle.repeatOn( - state: Lifecycle.State, - block: suspend CoroutineScope.() -> Unit, -) = repeatOnLifecycle(state, block) - -/** - * Launches and runs the given block when the [Lifecycle] controlling this - * [LifecycleCoroutineScope] is goes into the the matching state. - * - * @param state Lifecycle state to repeat on - * @param block The block of suspend code to invoke - * - * @return [Job] of the launching scope - * - * @see Lifecycle.repeatOn - */ -fun LifecycleCoroutineScope.repeatOn( - state: Lifecycle.State, - block: suspend CoroutineScope.() -> Unit, -): Job = launch { - if (this is LifecycleOwner) { - repeatOn(state, block) - } else { - error("${javaClass.simpleName} is not a LifecycleOwner") - } -} diff --git a/gradle/release.properties b/gradle/release.properties index ea0148c6..15b81e1c 100644 --- a/gradle/release.properties +++ b/gradle/release.properties @@ -1,2 +1,2 @@ -version=1.6.0 -code=1000600000 +version=1.6.1 +code=1000600001 diff --git a/ui/src/main/kotlin/co/anitrend/arch/ui/fragment/list/SupportFragmentList.kt b/ui/src/main/kotlin/co/anitrend/arch/ui/fragment/list/SupportFragmentList.kt index 3b8a69fb..38bbe728 100644 --- a/ui/src/main/kotlin/co/anitrend/arch/ui/fragment/list/SupportFragmentList.kt +++ b/ui/src/main/kotlin/co/anitrend/arch/ui/fragment/list/SupportFragmentList.kt @@ -24,6 +24,7 @@ import androidx.annotation.IntegerRes import androidx.lifecycle.Lifecycle import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import androidx.paging.PagedList import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.Adapter.StateRestorationPolicy @@ -31,7 +32,6 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager import co.anitrend.arch.domain.entities.LoadState import co.anitrend.arch.extension.ext.attachComponent import co.anitrend.arch.extension.ext.detachComponent -import co.anitrend.arch.extension.ext.repeatOn import co.anitrend.arch.recycler.SupportRecyclerView import co.anitrend.arch.recycler.adapter.SupportListAdapter import co.anitrend.arch.recycler.adapter.contract.ISupportAdapter @@ -88,17 +88,19 @@ abstract class SupportFragmentList : SupportFragment(), ISupportFragmentList< } protected open fun ISupportAdapter<*>.registerFlowListener() { - lifecycleScope.repeatOn(Lifecycle.State.RESUMED) { - clickableFlow - .debounce(16) - .filterIsInstance() - .onEach { - if (it.state !is LoadState.Loading) { - viewModelState()?.retry() - } else { - Timber.d("retry -> state is loading? current state: ${it.state}") - } - }.collect() + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.RESUMED) { + clickableFlow + .debounce(16) + .filterIsInstance() + .onEach { + if (it.state !is LoadState.Loading) { + viewModelState()?.retry() + } else { + Timber.d("retry -> state is loading? current state: ${it.state}") + } + }.collect() + } } } @@ -154,22 +156,26 @@ abstract class SupportFragmentList : SupportFragment(), ISupportFragmentList< override fun initializeComponents(savedInstanceState: Bundle?) { attachComponent(supportViewAdapter) attachComponent(listPresenter) - lifecycleScope.repeatOn(Lifecycle.State.RESUMED) { - listPresenter.stateLayout.interactionFlow - .debounce(16) - .filterIsInstance() - .onEach { - if (it.state !is LoadState.Loading) { - viewModelState()?.retry() - } else { - Timber.d("state is loading? current state: ${it.state}") - } - }.collect() + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.RESUMED) { + listPresenter.stateLayout.interactionFlow + .debounce(16) + .filterIsInstance() + .onEach { + if (it.state !is LoadState.Loading) { + viewModelState()?.retry() + } else { + Timber.d("state is loading? current state: ${it.state}") + } + }.collect() + } } - lifecycleScope.repeatOn(Lifecycle.State.RESUMED) { - if (supportViewAdapter.isEmpty()) { - onFetchDataInitialize() + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.RESUMED) { + if (supportViewAdapter.isEmpty()) { + onFetchDataInitialize() + } } } }