From 1fa17583c08113d1d4f566bc5469e73082e4e15c Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 6 Jul 2020 17:12:10 -0500 Subject: [PATCH] Scope task list view model to task list fragment --- .../com/todoroo/astrid/activity/TaskListFragment.kt | 3 ++- app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt | 2 -- .../java/org/tasks/ui/NavigationDrawerFragment.kt | 3 --- app/src/main/java/org/tasks/ui/SubtaskControlSet.kt | 5 ++--- app/src/main/java/org/tasks/ui/TaskListViewModel.kt | 13 +++++-------- 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt index b608a9c37..53bb81092 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -21,6 +21,7 @@ import androidx.appcompat.widget.Toolbar import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels +import androidx.fragment.app.viewModels import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope import androidx.paging.PagedList @@ -126,7 +127,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL @BindView(R.id.recycler_view) lateinit var recyclerView: RecyclerView - private val listViewModel: TaskListViewModel by activityViewModels() + private val listViewModel: TaskListViewModel by viewModels() private lateinit var taskAdapter: TaskAdapter private var recyclerAdapter: TaskListRecyclerAdapter? = null private lateinit var filter: Filter diff --git a/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt b/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt index 3f2fa2d74..39480da58 100644 --- a/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt +++ b/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt @@ -120,8 +120,6 @@ abstract class TaskDao(private val database: Database) { @Transaction open suspend fun fetchTasks(callback: (SubtaskInfo) -> List, subtasks: SubtaskInfo): List { - assertNotMainThread() - val start = if (BuildConfig.DEBUG) DateUtilities.now() else 0 val queries = callback.invoke(subtasks) val db = database.openHelper.writableDatabase diff --git a/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.kt b/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.kt index 7d812a343..49e8ff4c2 100644 --- a/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.kt +++ b/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.kt @@ -90,9 +90,6 @@ class NavigationDrawerFragment : Fragment() { } } }) - if (item is Filter) { - ViewModelProvider(requireActivity()).get(TaskListViewModel::class.java).setFilter((item as Filter?)!!) - } close() } diff --git a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt index 2d29df5ae..24a076572 100644 --- a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt +++ b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt @@ -15,8 +15,8 @@ import android.view.inputmethod.InputMethodManager import android.widget.EditText import android.widget.LinearLayout import androidx.core.widget.addTextChangedListener +import androidx.fragment.app.viewModels import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.LinearLayoutManager @@ -69,14 +69,13 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks @Inject lateinit var checkBoxProvider: CheckBoxProvider @Inject lateinit var chipProvider: ChipProvider - private lateinit var listViewModel: TaskListViewModel + private val listViewModel: TaskListViewModel by viewModels() private val refreshReceiver = RefreshReceiver() private var remoteList: Filter? = null private var googleTask: GoogleTask? = null private lateinit var recyclerAdapter: SubtasksRecyclerAdapter override fun createView(savedInstanceState: Bundle?) { - listViewModel = ViewModelProvider(this).get(TaskListViewModel::class.java) viewModel.newSubtasks.forEach { addSubtask(it) } recyclerAdapter = SubtasksRecyclerAdapter(activity, chipProvider, checkBoxProvider, this) viewModel.task?.let { diff --git a/app/src/main/java/org/tasks/ui/TaskListViewModel.kt b/app/src/main/java/org/tasks/ui/TaskListViewModel.kt index 7ff9f8d4b..ae76b2397 100644 --- a/app/src/main/java/org/tasks/ui/TaskListViewModel.kt +++ b/app/src/main/java/org/tasks/ui/TaskListViewModel.kt @@ -8,10 +8,9 @@ import androidx.sqlite.db.SimpleSQLiteQuery import com.todoroo.andlib.utility.AndroidUtilities import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.api.Filter -import com.todoroo.astrid.dao.TaskDaoBlocking +import com.todoroo.astrid.dao.TaskDao import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import org.tasks.BuildConfig import org.tasks.data.SubtaskInfo import org.tasks.data.TaskContainer @@ -21,7 +20,7 @@ import timber.log.Timber class TaskListViewModel @ViewModelInject constructor( private val preferences: Preferences, - private val taskDao: TaskDaoBlocking) : ViewModel(), Observer> { + private val taskDao: TaskDao) : ViewModel(), Observer> { private var tasks = MutableLiveData>() private var filter: Filter? = null @@ -70,11 +69,9 @@ class TaskListViewModel @ViewModelInject constructor( } private suspend fun performNonPagedQuery(subtasks: SubtaskInfo) { - tasks.value = withContext(Dispatchers.IO) { - taskDao.fetchTasks( - { s: SubtaskInfo? -> getQuery(preferences, filter!!, s!!) }, - subtasks) - } + tasks.value = taskDao.fetchTasks( + { s: SubtaskInfo? -> getQuery(preferences, filter!!, s!!) }, + subtasks) } private fun performPagedListQuery() {