mirror of https://github.com/tasks/tasks
Convert DashClockExtension to Kotlin
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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue