Convert DashClockExtension to Kotlin

pull/1055/head
Alex Baker 4 years ago
parent bfe97e77de
commit 02d99d9996

@ -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<Task> 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);
}
}
}

@ -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)
}
}
}

@ -63,11 +63,6 @@ class DefaultFilterProvider @Inject constructor(
} }
} }
@Deprecated("use coroutines")
fun getFilterFromPreferenceBlocking(resId: Int) = runBlocking {
getFilterFromPreference(resId)
}
@Deprecated("use coroutines") @Deprecated("use coroutines")
fun getFilterFromPreferenceBlocking(prefString: String?) = runBlocking { fun getFilterFromPreferenceBlocking(prefString: String?) = runBlocking {
getFilterFromPreference(prefString) getFilterFromPreference(prefString)

@ -3,8 +3,10 @@ package org.tasks.preferences.fragments
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.todoroo.astrid.api.Filter import com.todoroo.astrid.api.Filter
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.tasks.LocalBroadcastManager import org.tasks.LocalBroadcastManager
import org.tasks.R import org.tasks.R
import org.tasks.activities.FilterSelectionActivity import org.tasks.activities.FilterSelectionActivity
@ -43,7 +45,9 @@ class DashClock : InjectingPreferenceFragment() {
val filter: Filter = val filter: Filter =
data!!.getParcelableExtra(FilterSelectionActivity.EXTRA_FILTER)!! data!!.getParcelableExtra(FilterSelectionActivity.EXTRA_FILTER)!!
defaultFilterProvider.dashclockFilter = filter defaultFilterProvider.dashclockFilter = filter
refreshPreferences() lifecycleScope.launch {
refreshPreferences()
}
localBroadcastManager.broadcastRefresh() localBroadcastManager.broadcastRefresh()
} }
} else { } else {
@ -51,8 +55,8 @@ class DashClock : InjectingPreferenceFragment() {
} }
} }
private fun refreshPreferences() { private suspend fun refreshPreferences() {
val filter = defaultFilterProvider.getFilterFromPreferenceBlocking(R.string.p_dashclock_filter) val filter = defaultFilterProvider.getFilterFromPreference(R.string.p_dashclock_filter)
findPreference(R.string.p_dashclock_filter).summary = filter?.listingTitle findPreference(R.string.p_dashclock_filter).summary = filter.listingTitle
} }
} }
Loading…
Cancel
Save