diff --git a/app/build.gradle b/app/build.gradle index 866de452..4166cb07 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -80,6 +80,7 @@ dependencies { implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_common_java8_version" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_extensions_version" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_viewmodel_ktx_version" + implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_runtime_version" implementation "androidx.constraintlayout:constraintlayout:$constraint_layout_version" implementation "androidx.room:room-runtime:$room_version" implementation "androidx.documentfile:documentfile:$documentfile_version" diff --git a/app/src/main/java/com/dp/logcatapp/fragments/base/BaseFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/base/BaseFragment.kt index 7ecf5a6a..5121d0fd 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/base/BaseFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/base/BaseFragment.kt @@ -4,8 +4,13 @@ import android.os.Bundle import android.os.Handler import android.os.Looper import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope open class BaseFragment : Fragment() { + + protected val scope + get() = viewLifecycleOwner.lifecycleScope + protected lateinit var handler: Handler private set diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt index fd7f314c..c457c698 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt @@ -76,8 +76,6 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen private var lastSearchRunnable: Runnable? = null private var searchTask: Job? = null - private val scope = LifecycleScope() - private val hideFabUpRunnable: Runnable = Runnable { fabUp.hide() } @@ -198,9 +196,6 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - - viewLifecycleOwner.lifecycle.addObserver(scope) - recyclerView = view.findViewById(R.id.recyclerView) linearLayoutManager = LinearLayoutManager(activity) recyclerView.layoutManager = linearLayoutManager @@ -560,11 +555,6 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen lastSearchRunnable = null } - override fun onDestroyView() { - viewLifecycleOwner.lifecycle.removeObserver(scope) - super.onDestroyView() - } - override fun onDestroy() { super.onDestroy() activity!!.getDefaultSharedPreferences().unregisterOnSharedPreferenceChangeListener(adapter) diff --git a/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt index 1c6e1413..215a3bca 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt @@ -59,8 +59,6 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick private var exportFormat: ExportFormat? = null - private val scope = LifecycleScope() - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewModel = getAndroidViewModel() @@ -75,9 +73,6 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - - viewLifecycleOwner.lifecycle.addObserver(scope) - emptyView = view.findViewById(R.id.textViewEmpty) progressBar = view.findViewById(R.id.progressBar) @@ -376,11 +371,6 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick (activity as SavedLogsActivity).closeCabToolbar() } - override fun onDestroyView() { - viewLifecycleOwner.lifecycle.removeObserver(scope) - super.onDestroyView() - } - class ChooseExportFormatTypeDialogFragment : BaseDialogFragment() { private var exportFormat = ExportFormat.DEFAULT diff --git a/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt index a1b997de..10212a6d 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt @@ -15,7 +15,10 @@ import com.dp.logcatapp.R import com.dp.logcatapp.activities.BaseActivityWithToolbar import com.dp.logcatapp.fragments.base.BaseFragment import com.dp.logcatapp.fragments.shared.dialogs.CopyToClipboardDialogFragment -import com.dp.logcatapp.util.* +import com.dp.logcatapp.util.containsIgnoreCase +import com.dp.logcatapp.util.getAndroidViewModel +import com.dp.logcatapp.util.inflateLayout +import com.dp.logcatapp.util.showToast import com.google.android.material.floatingactionbutton.FloatingActionButton import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Job @@ -37,8 +40,6 @@ class SavedLogsViewerFragment : BaseFragment() { } } - private val scope = LifecycleScope() - private lateinit var recyclerView: RecyclerView private lateinit var linearLayoutManager: LinearLayoutManager private lateinit var viewModel: SavedLogsViewerViewModel @@ -168,8 +169,6 @@ class SavedLogsViewerFragment : BaseFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - viewLifecycleOwner.lifecycle.addObserver(scope) - progressBar = view.findViewById(R.id.progressBar) textViewEmpty = view.findViewById(R.id.textViewEmpty) @@ -322,11 +321,6 @@ class SavedLogsViewerFragment : BaseFragment() { lastSearchRunnable = null } - override fun onDestroyView() { - viewLifecycleOwner.lifecycle.removeObserver(scope) - super.onDestroyView() - } - override fun onDestroy() { super.onDestroy() (activity as BaseActivityWithToolbar).toolbar.subtitle = null diff --git a/app/src/main/java/com/dp/logcatapp/util/LifecycleScope.kt b/app/src/main/java/com/dp/logcatapp/util/LifecycleScope.kt deleted file mode 100644 index 267e28e1..00000000 --- a/app/src/main/java/com/dp/logcatapp/util/LifecycleScope.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.dp.logcatapp.util - -import androidx.lifecycle.DefaultLifecycleObserver -import androidx.lifecycle.LifecycleOwner -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers.Main -import kotlinx.coroutines.Job -import kotlin.coroutines.CoroutineContext - -class LifecycleScope : DefaultLifecycleObserver, CoroutineScope { - private val job = Job() - - override val coroutineContext: CoroutineContext - get() = job + Main - - override fun onDestroy(owner: LifecycleOwner) { - job.cancel() - } -} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 02062268..ed85e6c2 100644 --- a/build.gradle +++ b/build.gradle @@ -30,6 +30,7 @@ ext { preference_version = "1.1.0" legacy_preference_v14_version = "1.0.0" lifecycle_common_java8_version = "2.2.0" + lifecycle_runtime_version = "2.2.0" lifecycle_extensions_version = "2.2.0" lifecycle_viewmodel_ktx_version = "2.2.0" constraint_layout_version = "1.1.3"