From d2bec1e4b8e72127d021d5dbfe977346737d1c79 Mon Sep 17 00:00:00 2001 From: LooKeR Date: Sat, 13 Nov 2021 11:47:10 +0530 Subject: [PATCH] Fix: Memory Usage after app been closed --- .../droidify/screen/EditRepositoryFragment.kt | 3 ++- .../droidify/screen/SettingsFragment.kt | 9 ++++++-- .../looker/droidify/screen/TabsFragment.kt | 14 ++++++----- .../droidify/ui/appsList/AppListFragment.kt | 23 +++++++++++-------- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/screen/EditRepositoryFragment.kt b/src/main/kotlin/com/looker/droidify/screen/EditRepositoryFragment.kt index c9e25b5c..60c9c390 100644 --- a/src/main/kotlin/com/looker/droidify/screen/EditRepositoryFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/EditRepositoryFragment.kt @@ -32,6 +32,7 @@ import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.core.Single import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.schedulers.Schedulers +import kotlinx.coroutines.launch import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.Request import java.net.URI @@ -239,7 +240,7 @@ class EditRepositoryFragment() : ScreenFragment() { } } - lifecycleScope.launchWhenCreated { + lifecycleScope.launch { val list = Database.RepositoryAdapter.getAll(null) takenAddresses = list.asSequence().filter { it.id != repositoryId } .flatMap { (it.mirrors + it.address).asSequence() } diff --git a/src/main/kotlin/com/looker/droidify/screen/SettingsFragment.kt b/src/main/kotlin/com/looker/droidify/screen/SettingsFragment.kt index c82d0446..6b99079e 100644 --- a/src/main/kotlin/com/looker/droidify/screen/SettingsFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/SettingsFragment.kt @@ -16,6 +16,8 @@ import androidx.core.net.toUri import androidx.core.widget.NestedScrollView import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope import com.google.android.material.circularreveal.CircularRevealFrameLayout import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -29,6 +31,7 @@ import com.looker.droidify.databinding.PreferenceItemBinding import com.looker.droidify.utility.extension.resources.* import com.topjohnwu.superuser.Shell import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch class SettingsFragment : ScreenFragment() { @@ -135,8 +138,10 @@ class SettingsFragment : ScreenFragment() { ) } - lifecycleScope.launchWhenStarted { - Preferences.subject.collect { updatePreference(it) } + lifecycleScope.launch { + Preferences.subject + .flowWithLifecycle(lifecycle, Lifecycle.State.STARTED) + .collect { updatePreference(it) } } updatePreference(null) } diff --git a/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt b/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt index d3b13538..fa1e55f2 100644 --- a/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt @@ -8,6 +8,8 @@ import android.view.animation.AccelerateInterpolator import android.view.animation.DecelerateInterpolator import androidx.appcompat.widget.SearchView import androidx.fragment.app.Fragment +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -35,6 +37,7 @@ import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.schedulers.Schedulers import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch import kotlin.math.* class TabsFragment : ScreenFragment() { @@ -201,12 +204,12 @@ class TabsFragment : ScreenFragment() { } updateOrder() - lifecycleScope.launchWhenStarted { - Preferences.subject.collect { - if (it == Preferences.Key.SortOrder) { - updateOrder() + lifecycleScope.launch { + Preferences.subject + .flowWithLifecycle(lifecycle, Lifecycle.State.STARTED) + .collect { + if (it == Preferences.Key.SortOrder) updateOrder() } - } } val content = fragmentBinding.fragmentContent @@ -531,7 +534,6 @@ class TabsFragment : ScreenFragment() { itemView as MaterialTextView itemView.gravity = Gravity.CENTER_VERTICAL itemView.resources.sizeScaled(16).let { itemView.setPadding(it, 0, it, 0) } - itemView.setTextColor(context.getColorFromAttr(android.R.attr.textColor)) itemView.setTextSizeScaled(16) itemView.background = context.getDrawableFromAttr(android.R.attr.selectableItemBackground) diff --git a/src/main/kotlin/com/looker/droidify/ui/appsList/AppListFragment.kt b/src/main/kotlin/com/looker/droidify/ui/appsList/AppListFragment.kt index 8f6a4d07..e37d0079 100644 --- a/src/main/kotlin/com/looker/droidify/ui/appsList/AppListFragment.kt +++ b/src/main/kotlin/com/looker/droidify/ui/appsList/AppListFragment.kt @@ -6,7 +6,9 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.viewModels +import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.looker.droidify.R @@ -21,6 +23,7 @@ import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.schedulers.Schedulers import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch class AppListFragment() : BaseFragment(), CursorOwner.Callback { @@ -94,15 +97,17 @@ class AppListFragment() : BaseFragment(), CursorOwner.Callback { override fun onCursorData(request: CursorOwner.Request, cursor: Cursor?) { (recyclerView?.adapter as? AppListAdapter)?.apply { this.cursor = cursor - viewLifecycleOwner.lifecycleScope.launchWhenCreated { - emptyText = when { - cursor == null -> "" - viewModel.searchQuery.first() - .isNotEmpty() -> getString(R.string.no_matching_applications_found) - else -> when (source) { - Source.AVAILABLE -> getString(R.string.no_applications_available) - Source.INSTALLED -> getString(R.string.no_applications_installed) - Source.UPDATES -> getString(R.string.all_applications_up_to_date) + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.CREATED) { + emptyText = when { + cursor == null -> "" + viewModel.searchQuery.first() + .isNotEmpty() -> getString(R.string.no_matching_applications_found) + else -> when (source) { + Source.AVAILABLE -> getString(R.string.no_applications_available) + Source.INSTALLED -> getString(R.string.no_applications_installed) + Source.UPDATES -> getString(R.string.all_applications_up_to_date) + } } } }