From 9b4ccd51372a1cebf68b8c465973631ee5c31f80 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Wed, 8 Jul 2020 08:36:24 -0500 Subject: [PATCH] Convert BuiltInFilterExposer to Kotlin --- .../astrid/core/BuiltInFilterExposer.java | 113 ------------------ .../astrid/core/BuiltInFilterExposer.kt | 101 ++++++++++++++++ .../java/org/tasks/filters/FilterProvider.kt | 4 +- .../preferences/DefaultFilterProvider.kt | 2 +- 4 files changed, 104 insertions(+), 116 deletions(-) delete mode 100644 app/src/main/java/com/todoroo/astrid/core/BuiltInFilterExposer.java create mode 100644 app/src/main/java/com/todoroo/astrid/core/BuiltInFilterExposer.kt diff --git a/app/src/main/java/com/todoroo/astrid/core/BuiltInFilterExposer.java b/app/src/main/java/com/todoroo/astrid/core/BuiltInFilterExposer.java deleted file mode 100644 index e848e01c3..000000000 --- a/app/src/main/java/com/todoroo/astrid/core/BuiltInFilterExposer.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2012 Todoroo Inc - * - * See the file "LICENSE" for the full license governing this code. - */ - -package com.todoroo.astrid.core; - -import android.content.Context; -import android.content.res.Resources; -import com.todoroo.andlib.sql.Criterion; -import com.todoroo.andlib.sql.QueryTemplate; -import com.todoroo.andlib.utility.AndroidUtilities; -import com.todoroo.astrid.api.Filter; -import com.todoroo.astrid.api.PermaSql; -import com.todoroo.astrid.dao.TaskDaoBlocking; -import com.todoroo.astrid.dao.TaskDao.TaskCriteria; -import com.todoroo.astrid.data.Task; -import com.todoroo.astrid.timers.TimerPlugin; -import dagger.hilt.android.qualifiers.ApplicationContext; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.inject.Inject; -import org.tasks.R; -import org.tasks.filters.RecentlyModifiedFilter; -import org.tasks.filters.SortableFilter; -import org.tasks.preferences.Preferences; -import org.tasks.themes.CustomIcons; - -/** - * Exposes Astrid's built in filters to the NavigationDrawerFragment - * - * @author Tim Su - */ -public final class BuiltInFilterExposer { - - private final Preferences preferences; - private final TaskDaoBlocking taskDao; - private final Context context; - - @Inject - public BuiltInFilterExposer( - @ApplicationContext Context context, Preferences preferences, TaskDaoBlocking taskDao) { - this.context = context; - this.preferences = preferences; - this.taskDao = taskDao; - } - - /** Build inbox filter */ - public static Filter getMyTasksFilter(Resources r) { - return new SortableFilter( - r.getString(R.string.BFE_Active), - new QueryTemplate().where(TaskCriteria.activeAndVisible())); - } - - public static Filter getTodayFilter(Resources r) { - String todayTitle = AndroidUtilities.capitalize(r.getString(R.string.today)); - Map todayValues = new HashMap<>(); - todayValues.put(Task.DUE_DATE.name, PermaSql.VALUE_NOON); - return new SortableFilter( - todayTitle, - new QueryTemplate() - .where( - Criterion.and( - TaskCriteria.activeAndVisible(), - Task.DUE_DATE.gt(0), - Task.DUE_DATE.lte(PermaSql.VALUE_EOD))), - todayValues); - } - - public static Filter getRecentlyModifiedFilter(Resources r) { - return new RecentlyModifiedFilter(r.getString(R.string.BFE_Recent)); - } - - public static boolean isInbox(Context context, Filter filter) { - return filter != null && filter.equals(getMyTasksFilter(context.getResources())); - } - - public static boolean isTodayFilter(Context context, Filter filter) { - return filter != null && filter.equals(getTodayFilter(context.getResources())); - } - - public static boolean isRecentlyModifiedFilter(Context context, Filter filter) { - return filter != null && filter.equals(getRecentlyModifiedFilter(context.getResources())); - } - - public Filter getMyTasksFilter() { - Filter myTasksFilter = getMyTasksFilter(context.getResources()); - myTasksFilter.icon = CustomIcons.ALL_INBOX; - return myTasksFilter; - } - - public List getFilters() { - Resources r = context.getResources(); - List filters = new ArrayList<>(); - if (preferences.getBoolean(R.string.p_show_today_filter, true)) { - Filter todayFilter = getTodayFilter(r); - todayFilter.icon = CustomIcons.TODAY; - filters.add(todayFilter); - } - if (preferences.getBoolean(R.string.p_show_recently_modified_filter, true)) { - Filter recentlyModifiedFilter = getRecentlyModifiedFilter(r); - recentlyModifiedFilter.icon = CustomIcons.HISTORY; - filters.add(recentlyModifiedFilter); - } - if (taskDao.activeTimers() > 0) { - filters.add(TimerPlugin.createFilter(context)); - } - return filters; - } -} diff --git a/app/src/main/java/com/todoroo/astrid/core/BuiltInFilterExposer.kt b/app/src/main/java/com/todoroo/astrid/core/BuiltInFilterExposer.kt new file mode 100644 index 000000000..71249a213 --- /dev/null +++ b/app/src/main/java/com/todoroo/astrid/core/BuiltInFilterExposer.kt @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2012 Todoroo Inc + * + * See the file "LICENSE" for the full license governing this code. + */ +package com.todoroo.astrid.core + +import android.content.Context +import android.content.res.Resources +import com.todoroo.andlib.sql.Criterion.Companion.and +import com.todoroo.andlib.sql.QueryTemplate +import com.todoroo.andlib.utility.AndroidUtilities +import com.todoroo.astrid.api.Filter +import com.todoroo.astrid.api.PermaSql +import com.todoroo.astrid.dao.TaskDao +import com.todoroo.astrid.dao.TaskDao.TaskCriteria.activeAndVisible +import com.todoroo.astrid.data.Task +import com.todoroo.astrid.timers.TimerPlugin +import dagger.hilt.android.qualifiers.ApplicationContext +import org.tasks.R +import org.tasks.filters.RecentlyModifiedFilter +import org.tasks.filters.SortableFilter +import org.tasks.preferences.Preferences +import org.tasks.themes.CustomIcons +import java.util.* +import javax.inject.Inject + +/** + * Exposes Astrid's built in filters to the NavigationDrawerFragment + * + * @author Tim Su @todoroo.com> + */ +class BuiltInFilterExposer @Inject constructor( + @param:ApplicationContext private val context: Context, + private val preferences: Preferences, + private val taskDao: TaskDao) { + + val myTasksFilter: Filter + get() { + val myTasksFilter = getMyTasksFilter(context.resources) + myTasksFilter.icon = CustomIcons.ALL_INBOX + return myTasksFilter + } + + suspend fun filters(): List { + val r = context.resources + val filters: MutableList = ArrayList() + if (preferences.getBoolean(R.string.p_show_today_filter, true)) { + val todayFilter = getTodayFilter(r) + todayFilter.icon = CustomIcons.TODAY + filters.add(todayFilter) + } + if (preferences.getBoolean(R.string.p_show_recently_modified_filter, true)) { + val recentlyModifiedFilter = getRecentlyModifiedFilter(r) + recentlyModifiedFilter.icon = CustomIcons.HISTORY + filters.add(recentlyModifiedFilter) + } + if (taskDao.activeTimers() > 0) { + filters.add(TimerPlugin.createFilter(context)) + } + return filters + } + + companion object { + /** Build inbox filter */ + fun getMyTasksFilter(r: Resources): Filter { + return SortableFilter( + r.getString(R.string.BFE_Active), + QueryTemplate().where(activeAndVisible())) + } + + fun getTodayFilter(r: Resources): Filter { + val todayTitle = AndroidUtilities.capitalize(r.getString(R.string.today)) + val todayValues: MutableMap = HashMap() + todayValues[Task.DUE_DATE.name] = PermaSql.VALUE_NOON + return SortableFilter( + todayTitle, + QueryTemplate() + .where( + and( + activeAndVisible(), + Task.DUE_DATE.gt(0), + Task.DUE_DATE.lte(PermaSql.VALUE_EOD))), + todayValues) + } + + fun getRecentlyModifiedFilter(r: Resources) = + RecentlyModifiedFilter(r.getString(R.string.BFE_Recent)) + + @JvmStatic + fun isInbox(context: Context, filter: Filter?) = + filter != null && filter == getMyTasksFilter(context.resources) + + @JvmStatic + fun isTodayFilter(context: Context, filter: Filter?) = + filter != null && filter == getTodayFilter(context.resources) + + fun isRecentlyModifiedFilter(context: Context, filter: Filter?) = + filter != null && filter == getRecentlyModifiedFilter(context.resources) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/filters/FilterProvider.kt b/app/src/main/java/org/tasks/filters/FilterProvider.kt index 37ae41991..8545202bf 100644 --- a/app/src/main/java/org/tasks/filters/FilterProvider.kt +++ b/app/src/main/java/org/tasks/filters/FilterProvider.kt @@ -62,7 +62,7 @@ class FilterProvider @Inject constructor( R.string.p_collapse_filters.toLong())) .apply { if (collapsed) return this } .plusAllIf(showBuiltIn) { - builtInFilterExposer.filters + builtInFilterExposer.filters() } .plus(filterDao.getFilters().map(::CustomFilter).sort()) .plusIf(showCreate) { @@ -250,7 +250,7 @@ class FilterProvider @Inject constructor( sortedWith(COMPARATOR) } - private fun Collection.plusAllIf(predicate: Boolean, item: () -> Iterable): List = + private suspend fun Collection.plusAllIf(predicate: Boolean, item: suspend () -> Iterable): List = plus(if (predicate) item.invoke() else emptyList()) private fun Iterable.plusIf(predicate: Boolean, item: () -> T): List = diff --git a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt index 116e16fba..f221f33c0 100644 --- a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt +++ b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt @@ -4,7 +4,7 @@ import android.content.Context import com.todoroo.astrid.api.* import com.todoroo.astrid.api.Filter import com.todoroo.astrid.core.BuiltInFilterExposer -import com.todoroo.astrid.core.BuiltInFilterExposer.getMyTasksFilter +import com.todoroo.astrid.core.BuiltInFilterExposer.Companion.getMyTasksFilter import com.todoroo.astrid.data.Task import dagger.hilt.android.qualifiers.ApplicationContext import org.tasks.R