From 8710dab252ebff2bd726ba48b36bc8a2ddce9c3a Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 28 Jul 2020 14:46:04 -0500 Subject: [PATCH] Use non-blocking daos for collapsing filter groups --- .../todoroo/astrid/adapter/FilterAdapter.kt | 11 ++++---- .../astrid/adapter/NavigationDrawerAdapter.kt | 16 +++++++---- .../astrid/adapter/SubheaderViewHolder.kt | 28 +++++++++++-------- .../java/org/tasks/data/CaldavDaoBlocking.kt | 4 --- .../org/tasks/data/GoogleTaskDaoBlocking.kt | 10 ------- 5 files changed, 33 insertions(+), 36 deletions(-) delete mode 100644 app/src/main/java/org/tasks/data/GoogleTaskDaoBlocking.kt diff --git a/app/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.kt b/app/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.kt index 3b44231d4..6ba5e98bd 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.kt +++ b/app/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.kt @@ -11,6 +11,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter +import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.RecyclerView import com.todoroo.andlib.utility.AndroidUtilities import com.todoroo.astrid.api.Filter @@ -18,8 +19,8 @@ import com.todoroo.astrid.api.FilterListItem import com.todoroo.astrid.api.FilterListItem.Type.* import org.tasks.LocalBroadcastManager import org.tasks.billing.Inventory -import org.tasks.data.CaldavDaoBlocking -import org.tasks.data.GoogleTaskDaoBlocking +import org.tasks.data.CaldavDao +import org.tasks.data.GoogleTaskDao import org.tasks.filters.NavigationDrawerSubheader import org.tasks.locale.Locale import org.tasks.preferences.Preferences @@ -33,8 +34,8 @@ class FilterAdapter @Inject constructor( private val inventory: Inventory, private val colorProvider: ColorProvider, private val preferences: Preferences, - private val googleTaskDao: GoogleTaskDaoBlocking, - private val caldavDao: CaldavDaoBlocking, + private val googleTaskDao: GoogleTaskDao, + private val caldavDao: CaldavDao, private val localBroadcastManager: LocalBroadcastManager) : BaseAdapter() { private var selected: Filter? = null private var items: List = ArrayList() @@ -81,7 +82,7 @@ class FilterAdapter @Inject constructor( SEPARATOR -> SeparatorViewHolder(newView) SUBHEADER -> SubheaderViewHolder( newView, - activity, + activity as AppCompatActivity, preferences, googleTaskDao, caldavDao, diff --git a/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.kt b/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.kt index e47f7e488..9788041a1 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.kt +++ b/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.kt @@ -9,6 +9,7 @@ import android.app.Activity import android.os.Bundle import android.view.LayoutInflater import android.view.ViewGroup +import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import com.todoroo.astrid.api.Filter @@ -18,8 +19,8 @@ import io.reactivex.subjects.PublishSubject import org.tasks.LocalBroadcastManager import org.tasks.activities.DragAndDropDiffer import org.tasks.billing.Inventory -import org.tasks.data.CaldavDaoBlocking -import org.tasks.data.GoogleTaskDaoBlocking +import org.tasks.data.CaldavDao +import org.tasks.data.GoogleTaskDao import org.tasks.filters.NavigationDrawerSubheader import org.tasks.locale.Locale import org.tasks.preferences.Preferences @@ -34,8 +35,8 @@ class NavigationDrawerAdapter @Inject constructor( private val inventory: Inventory, private val colorProvider: ColorProvider, private val preferences: Preferences, - private val googleTaskDao: GoogleTaskDaoBlocking, - private val caldavDao: CaldavDaoBlocking, + private val googleTaskDao: GoogleTaskDao, + private val caldavDao: CaldavDao, private val localBroadcastManager: LocalBroadcastManager) : RecyclerView.Adapter(), DragAndDropDiffer> { @@ -75,7 +76,12 @@ class NavigationDrawerAdapter @Inject constructor( FilterListItem.Type.ITEM -> FilterViewHolder( view, true, locale, activity, inventory, colorProvider) { onClickFilter(it) } FilterListItem.Type.SUBHEADER -> SubheaderViewHolder( - view, activity, preferences, googleTaskDao, caldavDao, localBroadcastManager) + view, + activity as AppCompatActivity, + preferences, + googleTaskDao, + caldavDao, + localBroadcastManager) FilterListItem.Type.ACTION -> ActionViewHolder(activity, view) { onClickFilter(it) } else -> SeparatorViewHolder(view) } diff --git a/app/src/main/java/com/todoroo/astrid/adapter/SubheaderViewHolder.kt b/app/src/main/java/com/todoroo/astrid/adapter/SubheaderViewHolder.kt index 3a36107aa..bc0f3f299 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/SubheaderViewHolder.kt +++ b/app/src/main/java/com/todoroo/astrid/adapter/SubheaderViewHolder.kt @@ -1,18 +1,20 @@ package com.todoroo.astrid.adapter -import android.app.Activity import android.content.Intent import android.view.View import android.widget.ImageView import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.RecyclerView import butterknife.BindView import butterknife.ButterKnife import butterknife.OnClick +import kotlinx.coroutines.launch import org.tasks.LocalBroadcastManager import org.tasks.R -import org.tasks.data.CaldavDaoBlocking -import org.tasks.data.GoogleTaskDaoBlocking +import org.tasks.data.CaldavDao +import org.tasks.data.GoogleTaskDao import org.tasks.filters.NavigationDrawerSubheader import org.tasks.filters.NavigationDrawerSubheader.SubheaderType import org.tasks.preferences.Preferences @@ -21,10 +23,10 @@ import org.tasks.themes.DrawableUtil internal class SubheaderViewHolder( itemView: View, - activity: Activity, + private val activity: AppCompatActivity, private val preferences: Preferences, - private val googleTaskDao: GoogleTaskDaoBlocking, - private val caldavDao: CaldavDaoBlocking, + private val googleTaskDao: GoogleTaskDao, + private val caldavDao: CaldavDao, private val localBroadcastManager: LocalBroadcastManager) : RecyclerView.ViewHolder(itemView) { @@ -38,13 +40,15 @@ internal class SubheaderViewHolder( @OnClick(R.id.subheader_row) fun onClick() { - val collapsed = !subheader.isCollapsed - when (subheader.subheaderType) { - SubheaderType.PREFERENCE -> preferences.setBoolean(subheader.id.toInt(), collapsed) - SubheaderType.GOOGLE_TASKS -> googleTaskDao.setCollapsed(subheader.id, collapsed) - SubheaderType.CALDAV -> caldavDao.setCollapsed(subheader.id, collapsed) + activity.lifecycleScope.launch { + val collapsed = !subheader.isCollapsed + when (subheader.subheaderType) { + SubheaderType.PREFERENCE -> preferences.setBoolean(subheader.id.toInt(), collapsed) + SubheaderType.GOOGLE_TASKS -> googleTaskDao.setCollapsed(subheader.id, collapsed) + SubheaderType.CALDAV -> caldavDao.setCollapsed(subheader.id, collapsed) + } + localBroadcastManager.broadcastRefreshList() } - localBroadcastManager.broadcastRefreshList() } fun bind(subheader: NavigationDrawerSubheader) { diff --git a/app/src/main/java/org/tasks/data/CaldavDaoBlocking.kt b/app/src/main/java/org/tasks/data/CaldavDaoBlocking.kt index 3a7df335e..b3736f433 100644 --- a/app/src/main/java/org/tasks/data/CaldavDaoBlocking.kt +++ b/app/src/main/java/org/tasks/data/CaldavDaoBlocking.kt @@ -9,10 +9,6 @@ class CaldavDaoBlocking @Inject constructor(private val dao: CaldavDao) { return dao.subscribeToCalendars() } - fun setCollapsed(id: Long, collapsed: Boolean) = runBlocking { - dao.setCollapsed(id, collapsed) - } - fun getCalendars(): List = runBlocking { dao.getCalendars() } diff --git a/app/src/main/java/org/tasks/data/GoogleTaskDaoBlocking.kt b/app/src/main/java/org/tasks/data/GoogleTaskDaoBlocking.kt deleted file mode 100644 index f4aaec049..000000000 --- a/app/src/main/java/org/tasks/data/GoogleTaskDaoBlocking.kt +++ /dev/null @@ -1,10 +0,0 @@ -package org.tasks.data - -import javax.inject.Inject - -@Deprecated("use coroutines") -class GoogleTaskDaoBlocking @Inject constructor(private val dao: GoogleTaskDao) { - fun setCollapsed(id: Long, collapsed: Boolean) = runBlocking { - dao.setCollapsed(id, collapsed) - } -} \ No newline at end of file