From 853059355cead729f033129cdf4359da9a0d220d Mon Sep 17 00:00:00 2001 From: Darshan Parajuli Date: Sun, 19 Aug 2018 12:22:37 -0700 Subject: [PATCH] Observe filter data on `onResume` and dispose it on `onPause` This prevents the case where the IllegalStateException is thrown as a result of fragment not being attached to the context at the time subscribe callback gets run. --- .../fragments/filters/FiltersFragment.kt | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt index 65e655e2..75f2a06f 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt @@ -1,5 +1,6 @@ package com.dp.logcatapp.fragments.filters +import android.annotation.SuppressLint import android.arch.lifecycle.ViewModelProviders import android.os.Bundle import android.support.v7.util.DiffUtil @@ -19,6 +20,7 @@ import com.dp.logcatapp.fragments.logcatlive.LogcatLiveViewModel import com.dp.logcatapp.util.inflateLayout import io.reactivex.Flowable import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers class FiltersFragment : BaseFragment() { @@ -40,6 +42,7 @@ class FiltersFragment : BaseFragment() { private lateinit var recyclerViewAdapter: MyRecyclerViewAdapter private lateinit var linearLayoutManager: LinearLayoutManager private lateinit var emptyMessage: TextView + private var filterObserver: Disposable? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -49,15 +52,16 @@ class FiltersFragment : BaseFragment() { recyclerViewAdapter = MyRecyclerViewAdapter { onRemoveClicked(it) } + } - val dao = MyDB.getInstance(activity!!).filterDao() + private fun startObservingFilters() { + val dao = MyDB.getInstance(context!!).filterDao() val flowable = if (isExclusions()) { dao.getExclusions() } else { dao.getFilters() } - - flowable.observeOn(AndroidSchedulers.mainThread()) + filterObserver = flowable.observeOn(AndroidSchedulers.mainThread()) .subscribe { list -> val data = list.map { val displayText: String @@ -102,8 +106,28 @@ class FiltersFragment : BaseFragment() { } } + private fun stopObservingFilters() { + filterObserver?.let { + if (!it.isDisposed) { + it.dispose() + } + } + filterObserver = null + } + + override fun onResume() { + super.onResume() + startObservingFilters() + } + + override fun onPause() { + super.onPause() + stopObservingFilters() + } + fun isExclusions() = arguments?.getBoolean(KEY_EXCLUSIONS) ?: false + @SuppressLint("CheckResult") private fun onRemoveClicked(v: View) { val pos = linearLayoutManager.getPosition(v) if (pos != RecyclerView.NO_POSITION) { @@ -158,6 +182,7 @@ class FiltersFragment : BaseFragment() { } } + @SuppressLint("CheckResult") fun addFilter(keyword: String, tag: String, pid: String, tid: String, logLevels: Set) { val list = mutableListOf() val exclude = isExclusions()