diff --git a/app/src/main/java/org/tasks/dashclock/DashClockExtension.java b/app/src/main/java/org/tasks/dashclock/DashClockExtension.java deleted file mode 100644 index 7139ecb51..000000000 --- a/app/src/main/java/org/tasks/dashclock/DashClockExtension.java +++ /dev/null @@ -1,90 +0,0 @@ -package org.tasks.dashclock; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import com.google.android.apps.dashclock.api.ExtensionData; -import com.todoroo.astrid.api.Filter; -import com.todoroo.astrid.dao.TaskDaoBlocking; -import com.todoroo.astrid.data.Task; -import dagger.hilt.android.AndroidEntryPoint; -import java.util.List; -import javax.inject.Inject; -import org.tasks.LocalBroadcastManager; -import org.tasks.R; -import org.tasks.intents.TaskIntents; -import org.tasks.preferences.DefaultFilterProvider; -import org.tasks.preferences.Preferences; -import timber.log.Timber; - -@AndroidEntryPoint -public class DashClockExtension extends com.google.android.apps.dashclock.api.DashClockExtension { - - @Inject DefaultFilterProvider defaultFilterProvider; - @Inject TaskDaoBlocking taskDao; - @Inject Preferences preferences; - @Inject LocalBroadcastManager localBroadcastManager; - - private final BroadcastReceiver refreshReceiver = - new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - refresh(); - } - }; - - @Override - public void onCreate() { - super.onCreate(); - - localBroadcastManager.registerRefreshReceiver(refreshReceiver); - } - - @Override - public void onDestroy() { - super.onDestroy(); - - localBroadcastManager.unregisterReceiver(refreshReceiver); - } - - @Override - protected void onUpdateData(int i) { - refresh(); - } - - private void refresh() { - final String filterPreference = preferences.getStringValue(R.string.p_dashclock_filter); - Filter filter = defaultFilterProvider.getFilterFromPreferenceBlocking(filterPreference); - - int count = taskDao.count(filter); - - if (count == 0) { - publish(null); - } else { - Intent clickIntent = TaskIntents.getTaskListIntent(this, filter); - ExtensionData extensionData = - new ExtensionData() - .visible(true) - .icon(R.drawable.ic_check_white_24dp) - .status(Integer.toString(count)) - .expandedTitle(getResources().getQuantityString(R.plurals.task_count, count, count)) - .expandedBody(filter.listingTitle) - .clickIntent(clickIntent); - if (count == 1) { - List tasks = taskDao.fetchFiltered(filter); - if (!tasks.isEmpty()) { - extensionData.expandedTitle(tasks.get(0).getTitle()); - } - } - publish(extensionData); - } - } - - private void publish(ExtensionData data) { - try { - publishUpdate(data); - } catch (Exception e) { - Timber.e(e); - } - } -} diff --git a/app/src/main/java/org/tasks/dashclock/DashClockExtension.kt b/app/src/main/java/org/tasks/dashclock/DashClockExtension.kt new file mode 100644 index 000000000..9dcfee628 --- /dev/null +++ b/app/src/main/java/org/tasks/dashclock/DashClockExtension.kt @@ -0,0 +1,84 @@ +package org.tasks.dashclock + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import com.google.android.apps.dashclock.api.DashClockExtension +import com.google.android.apps.dashclock.api.ExtensionData +import com.todoroo.astrid.dao.TaskDao +import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch +import org.tasks.LocalBroadcastManager +import org.tasks.R +import org.tasks.intents.TaskIntents +import org.tasks.preferences.DefaultFilterProvider +import org.tasks.preferences.Preferences +import timber.log.Timber +import javax.inject.Inject + +@AndroidEntryPoint +class DashClockExtension : DashClockExtension() { + private val job = SupervisorJob() + private val scope = CoroutineScope(Dispatchers.IO + job) + + @Inject lateinit var defaultFilterProvider: DefaultFilterProvider + @Inject lateinit var taskDao: TaskDao + @Inject lateinit var preferences: Preferences + @Inject lateinit var localBroadcastManager: LocalBroadcastManager + + private val refreshReceiver: BroadcastReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + refresh() + } + } + + override fun onCreate() { + super.onCreate() + localBroadcastManager.registerRefreshReceiver(refreshReceiver) + } + + override fun onDestroy() { + super.onDestroy() + localBroadcastManager.unregisterReceiver(refreshReceiver) + } + + override fun onUpdateData(i: Int) { + refresh() + } + + private fun refresh() = scope.launch { + val filterPreference = preferences.getStringValue(R.string.p_dashclock_filter) + val filter = defaultFilterProvider.getFilterFromPreference(filterPreference) + val count = taskDao.count(filter) + if (count == 0) { + publish(null) + } else { + val clickIntent = TaskIntents.getTaskListIntent(this@DashClockExtension, filter) + val extensionData = ExtensionData() + .visible(true) + .icon(R.drawable.ic_check_white_24dp) + .status(count.toString()) + .expandedTitle(resources.getQuantityString(R.plurals.task_count, count, count)) + .expandedBody(filter.listingTitle) + .clickIntent(clickIntent) + if (count == 1) { + val tasks = taskDao.fetchFiltered(filter) + if (tasks.isNotEmpty()) { + extensionData.expandedTitle(tasks[0].title) + } + } + publish(extensionData) + } + } + + private fun publish(data: ExtensionData?) { + try { + publishUpdate(data) + } catch (e: Exception) { + Timber.e(e) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt index 0f194b52f..612013431 100644 --- a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt +++ b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt @@ -63,11 +63,6 @@ class DefaultFilterProvider @Inject constructor( } } - @Deprecated("use coroutines") - fun getFilterFromPreferenceBlocking(resId: Int) = runBlocking { - getFilterFromPreference(resId) - } - @Deprecated("use coroutines") fun getFilterFromPreferenceBlocking(prefString: String?) = runBlocking { getFilterFromPreference(prefString) diff --git a/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt b/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt index f4a1b4723..a71b0d5ac 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt @@ -3,8 +3,10 @@ package org.tasks.preferences.fragments import android.app.Activity import android.content.Intent import android.os.Bundle +import androidx.lifecycle.lifecycleScope import com.todoroo.astrid.api.Filter import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import org.tasks.LocalBroadcastManager import org.tasks.R import org.tasks.activities.FilterSelectionActivity @@ -43,7 +45,9 @@ class DashClock : InjectingPreferenceFragment() { val filter: Filter = data!!.getParcelableExtra(FilterSelectionActivity.EXTRA_FILTER)!! defaultFilterProvider.dashclockFilter = filter - refreshPreferences() + lifecycleScope.launch { + refreshPreferences() + } localBroadcastManager.broadcastRefresh() } } else { @@ -51,8 +55,8 @@ class DashClock : InjectingPreferenceFragment() { } } - private fun refreshPreferences() { - val filter = defaultFilterProvider.getFilterFromPreferenceBlocking(R.string.p_dashclock_filter) - findPreference(R.string.p_dashclock_filter).summary = filter?.listingTitle + private suspend fun refreshPreferences() { + val filter = defaultFilterProvider.getFilterFromPreference(R.string.p_dashclock_filter) + findPreference(R.string.p_dashclock_filter).summary = filter.listingTitle } } \ No newline at end of file