Convert BuiltInFilterExposer to Kotlin

pull/1051/head
Alex Baker 4 years ago
parent a2776b960e
commit 9b4ccd5137

@ -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 <tim@todoroo.com>
*/
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<String, Object> 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<Filter> getFilters() {
Resources r = context.getResources();
List<Filter> 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;
}
}

@ -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 <tim></tim>@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<Filter> {
val r = context.resources
val filters: MutableList<Filter> = 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<String?, Any> = 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)
}
}

@ -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 <T> Collection<T>.plusAllIf(predicate: Boolean, item: () -> Iterable<T>): List<T> =
private suspend fun <T> Collection<T>.plusAllIf(predicate: Boolean, item: suspend () -> Iterable<T>): List<T> =
plus(if (predicate) item.invoke() else emptyList())
private fun <T> Iterable<T>.plusIf(predicate: Boolean, item: () -> T): List<T> =

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

Loading…
Cancel
Save