Skip to content

Commit

Permalink
Use lifecycleScope
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanparajuli committed Feb 18, 2020
1 parent 8cf8597 commit 3b02cbe
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 49 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
19 changes: 0 additions & 19 deletions app/src/main/java/com/dp/logcatapp/util/LifecycleScope.kt

This file was deleted.

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3b02cbe

Please sign in to comment.