From 30d6fd9d33e17110726a299749b058416ed77ecf Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 23 Nov 2021 11:46:10 -0800 Subject: Convert fragment channel to flow --- .../pitchedapps/frost/fragments/BaseFragment.kt | 25 ++++++++-------------- .../frost/fragments/FragmentContract.kt | 10 +-------- 2 files changed, 10 insertions(+), 25 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/fragments') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt index 79495b2a..5c97de65 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt @@ -21,6 +21,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import androidx.lifecycle.flowWithLifecycle import ca.allanwang.kau.utils.ContextHelper import ca.allanwang.kau.utils.fadeScaleTransition import ca.allanwang.kau.utils.setIcon @@ -47,8 +48,9 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.channels.ReceiveChannel +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.isActive -import kotlinx.coroutines.launch import javax.inject.Inject import kotlin.coroutines.CoroutineContext @@ -121,7 +123,6 @@ abstract class BaseFragment : } override var firstLoad: Boolean = true - private var activityReceiver: ReceiveChannel? = null private var onCreateRunnable: ((FragmentContract) -> Unit)? = null override var content: FrostContentParent? = null @@ -152,8 +153,7 @@ abstract class BaseFragment : onCreateRunnable?.invoke(this) onCreateRunnable = null firstLoadRequest() - detachMainObservable() - activityReceiver = attachMainObservable(mainContract) + attach(mainContract) } override fun setUserVisibleHint(isVisibleToUser: Boolean) { @@ -177,10 +177,10 @@ abstract class BaseFragment : mainContract.setTitle(title) } - override fun attachMainObservable(contract: MainActivityContract): ReceiveChannel { - val receiver = contract.fragmentChannel.openSubscription() - launch { - for (flag in receiver) { + override fun attach(contract: MainActivityContract) { + contract.fragmentFlow + .flowWithLifecycle(viewLifecycleOwner.lifecycle) + .onEach { flag -> when (flag) { REQUEST_REFRESH -> { core?.apply { @@ -201,9 +201,7 @@ abstract class BaseFragment : reloadTextSize() } } - } - } - return receiver + }.launchIn(this) } override fun updateFab(contract: MainFabContract) { @@ -222,16 +220,11 @@ abstract class BaseFragment : setOnClickListener { click() } } - override fun detachMainObservable() { - activityReceiver?.cancel() - } - override fun onDestroyView() { super.onDestroyView() L.i { "Fragment on destroy $position ${hashCode()}" } content?.destroy() content = null - detachMainObservable() } override fun onDestroy() { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentContract.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentContract.kt index 10c612c5..beac7494 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentContract.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentContract.kt @@ -22,7 +22,6 @@ import com.pitchedapps.frost.contracts.FrostContentParent import com.pitchedapps.frost.contracts.MainActivityContract import com.pitchedapps.frost.contracts.MainFabContract import com.pitchedapps.frost.views.FrostRecyclerView -import kotlinx.coroutines.channels.ReceiveChannel /** * Created by Allan Wang on 2017-11-07. @@ -77,15 +76,8 @@ interface FragmentContract : FrostContentContainer { /** * Call whenever a fragment is attached so that it may listen * to activity emissions. - * Returns a means of closing the listener, which can be called from [detachMainObservable] */ - fun attachMainObservable(contract: MainActivityContract): ReceiveChannel - - /** - * Call when fragment is detached so that any existing - * observable is disposed - */ - fun detachMainObservable() + fun attach(contract: MainActivityContract) /* * ----------------------------------------- -- cgit v1.2.3