From 8627559a5d44c16f6ce956220e55b4e35da1964d Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 2 Jun 2020 16:27:41 -0500 Subject: [PATCH] Convert DefaultFilterProvider to Kotlin --- .../todoroo/astrid/activity/MainActivity.kt | 2 +- .../preferences/DefaultFilterProvider.java | 231 ------------------ .../preferences/DefaultFilterProvider.kt | 144 +++++++++++ .../tasks/preferences/fragments/DashClock.kt | 2 +- .../preferences/fragments/LookAndFeel.kt | 4 +- .../preferences/fragments/Notifications.kt | 2 +- .../tasks/preferences/fragments/Widgets.kt | 2 +- 7 files changed, 150 insertions(+), 237 deletions(-) delete mode 100644 app/src/main/java/org/tasks/preferences/DefaultFilterProvider.java create mode 100644 app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt diff --git a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt index 5bcc9ad32..42c14e032 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt @@ -248,7 +248,7 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl } filter = newFilter navigationDrawer.setSelected(filter) - defaultFilterProvider.setLastViewedFilter(filter) + defaultFilterProvider.lastViewedFilter = filter applyTheme() val fragmentManager = supportFragmentManager fragmentManager diff --git a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.java b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.java deleted file mode 100644 index e801fd2a1..000000000 --- a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.java +++ /dev/null @@ -1,231 +0,0 @@ -package org.tasks.preferences; - -import static com.todoroo.astrid.core.BuiltInFilterExposer.getMyTasksFilter; -import static com.todoroo.astrid.core.BuiltInFilterExposer.getRecentlyModifiedFilter; -import static com.todoroo.astrid.core.BuiltInFilterExposer.getTodayFilter; -import static com.todoroo.astrid.core.BuiltInFilterExposer.isRecentlyModifiedFilter; -import static com.todoroo.astrid.core.BuiltInFilterExposer.isTodayFilter; -import static org.tasks.Strings.isNullOrEmpty; - -import android.content.Context; -import android.content.res.Resources; -import com.todoroo.astrid.api.CaldavFilter; -import com.todoroo.astrid.api.CustomFilter; -import com.todoroo.astrid.api.Filter; -import com.todoroo.astrid.api.GtasksFilter; -import com.todoroo.astrid.api.TagFilter; -import com.todoroo.astrid.core.BuiltInFilterExposer; -import com.todoroo.astrid.core.CustomFilterExposer; -import javax.inject.Inject; -import org.tasks.R; -import org.tasks.data.CaldavCalendar; -import org.tasks.data.CaldavDao; -import org.tasks.data.GoogleTaskList; -import org.tasks.data.GoogleTaskListDao; -import org.tasks.data.LocationDao; -import org.tasks.data.Place; -import org.tasks.data.TagData; -import org.tasks.data.TagDataDao; -import org.tasks.filters.PlaceFilter; -import org.tasks.injection.ForApplication; -import timber.log.Timber; - -public class DefaultFilterProvider { - - private static final int TYPE_FILTER = 0; - private static final int TYPE_CUSTOM_FILTER = 1; - private static final int TYPE_TAG = 2; - private static final int TYPE_GOOGLE_TASKS = 3; - private static final int TYPE_CALDAV = 4; - private static final int TYPE_LOCATION = 5; - - private static final int FILTER_MY_TASKS = 0; - private static final int FILTER_TODAY = 1; - private static final int FILTER_UNCATEGORIZED = 2; - private static final int FILTER_RECENTLY_MODIFIED = 3; - - private final Context context; - private final Preferences preferences; - private final CustomFilterExposer customFilterExposer; - private final TagDataDao tagDataDao; - private final GoogleTaskListDao googleTaskListDao; - private final CaldavDao caldavDao; - private final LocationDao locationDao; - - @Inject - public DefaultFilterProvider( - @ForApplication Context context, - Preferences preferences, - CustomFilterExposer customFilterExposer, - TagDataDao tagDataDao, - GoogleTaskListDao googleTaskListDao, - CaldavDao caldavDao, - LocationDao locationDao) { - this.context = context; - this.preferences = preferences; - this.customFilterExposer = customFilterExposer; - this.tagDataDao = tagDataDao; - this.googleTaskListDao = googleTaskListDao; - this.caldavDao = caldavDao; - this.locationDao = locationDao; - } - - public Filter getDashclockFilter() { - return getFilterFromPreference(R.string.p_dashclock_filter); - } - - public void setDashclockFilter(Filter filter) { - setFilterPreference(filter, R.string.p_dashclock_filter); - } - - public Filter getBadgeFilter() { - return getFilterFromPreference(R.string.p_badge_list); - } - - public void setBadgeFilter(Filter filter) { - setFilterPreference(filter, R.string.p_badge_list); - } - - public Filter getDefaultFilter() { - return getFilterFromPreference(R.string.p_default_list); - } - - public void setDefaultFilter(Filter filter) { - setFilterPreference(filter, R.string.p_default_list); - } - - public void setLastViewedFilter(Filter filter) { - setFilterPreference(filter, R.string.p_last_viewed_list); - } - - public Filter getDefaultRemoteList() { - return getFilterFromPreference( - preferences.getStringValue(R.string.p_default_remote_list), null); - } - - public void setDefaultRemoteList(Filter filter) { - setFilterPreference(filter, R.string.p_default_remote_list); - } - - public Filter getStartupFilter() { - return getFilterFromPreference( - preferences.getBoolean(R.string.p_open_last_viewed_list, true) - ? R.string.p_last_viewed_list - : R.string.p_default_list); - } - - public Filter getFilterFromPreference(int resId) { - return getFilterFromPreference(preferences.getStringValue(resId)); - } - - public Filter getFilterFromPreference(String preferenceValue) { - return getFilterFromPreference( - preferenceValue, BuiltInFilterExposer.getMyTasksFilter(context.getResources())); - } - - private Filter getFilterFromPreference(String preferenceValue, Filter def) { - if (!isNullOrEmpty(preferenceValue)) { - try { - Filter filter = loadFilter(preferenceValue); - if (filter != null) { - return filter; - } - } catch (Exception e) { - Timber.e(e); - } - } - return def; - } - - private Filter loadFilter(String preferenceValue) { - String[] split = preferenceValue.split(":"); - switch (Integer.parseInt(split[0])) { - case TYPE_FILTER: - return getBuiltInFilter(Integer.parseInt(split[1])); - case TYPE_CUSTOM_FILTER: - return customFilterExposer.getFilter(Long.parseLong(split[1])); - case TYPE_TAG: - TagData tag = tagDataDao.getByUuid(split[1]); - return tag == null || isNullOrEmpty(tag.getName()) ? null : new TagFilter(tag); - case TYPE_GOOGLE_TASKS: - GoogleTaskList list = googleTaskListDao.getById(Long.parseLong(split[1])); - return list == null ? null : new GtasksFilter(list); - case TYPE_CALDAV: - CaldavCalendar caldavCalendar = caldavDao.getCalendarByUuid(split[1]); - return caldavCalendar == null ? null : new CaldavFilter(caldavCalendar); - case TYPE_LOCATION: - Place place = locationDao.getPlace(split[1]); - return place == null ? null : new PlaceFilter(place); - default: - return null; - } - } - - private void setFilterPreference(Filter filter, int prefId) { - String filterPreferenceValue = getFilterPreferenceValue(filter); - if (!isNullOrEmpty(filterPreferenceValue)) { - preferences.setString(prefId, filterPreferenceValue); - } - } - - public String getFilterPreferenceValue(Filter filter) { - int filterType = getFilterType(filter); - switch (filterType) { - case TYPE_FILTER: - return getFilterPreference(filterType, getBuiltInFilterId(filter)); - case TYPE_CUSTOM_FILTER: - return getFilterPreference(filterType, ((CustomFilter) filter).getId()); - case TYPE_TAG: - return getFilterPreference(filterType, ((TagFilter) filter).getUuid()); - case TYPE_GOOGLE_TASKS: - return getFilterPreference(filterType, ((GtasksFilter) filter).getStoreId()); - case TYPE_CALDAV: - return getFilterPreference(filterType, ((CaldavFilter) filter).getUuid()); - case TYPE_LOCATION: - return getFilterPreference(filterType, ((PlaceFilter) filter).getUid()); - } - return null; - } - - private String getFilterPreference(int type, T value) { - return String.format("%s:%s", type, value); - } - - private int getFilterType(Filter filter) { - if (filter instanceof TagFilter) { - return TYPE_TAG; - } else if (filter instanceof GtasksFilter) { - return TYPE_GOOGLE_TASKS; - } else if (filter instanceof CustomFilter) { - return TYPE_CUSTOM_FILTER; - } else if (filter instanceof CaldavFilter) { - return TYPE_CALDAV; - } else if (filter instanceof PlaceFilter) { - return TYPE_LOCATION; - } - return TYPE_FILTER; - } - - private Filter getBuiltInFilter(int id) { - Resources resources = context.getResources(); - switch (id) { - case FILTER_TODAY: - return getTodayFilter(resources); - case FILTER_RECENTLY_MODIFIED: - return getRecentlyModifiedFilter(resources); - case FILTER_UNCATEGORIZED: - break; - } - return getMyTasksFilter(resources); - } - - private int getBuiltInFilterId(Filter filter) { - if (isTodayFilter(context, filter)) { - return FILTER_TODAY; - } else if (isRecentlyModifiedFilter(context, filter)) { - return FILTER_RECENTLY_MODIFIED; - } - - return FILTER_MY_TASKS; - } -} diff --git a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt new file mode 100644 index 000000000..0e8b3ffc1 --- /dev/null +++ b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt @@ -0,0 +1,144 @@ +package org.tasks.preferences + +import android.content.Context +import com.todoroo.astrid.api.* +import com.todoroo.astrid.core.BuiltInFilterExposer +import com.todoroo.astrid.core.BuiltInFilterExposer.getMyTasksFilter +import com.todoroo.astrid.core.CustomFilterExposer +import org.tasks.R +import org.tasks.Strings.isNullOrEmpty +import org.tasks.data.CaldavDao +import org.tasks.data.GoogleTaskListDao +import org.tasks.data.LocationDao +import org.tasks.data.TagDataDao +import org.tasks.filters.PlaceFilter +import org.tasks.injection.ForApplication +import timber.log.Timber +import javax.inject.Inject + +class DefaultFilterProvider @Inject constructor( + @param:ForApplication private val context: Context, + private val preferences: Preferences, + private val customFilterExposer: CustomFilterExposer, + private val tagDataDao: TagDataDao, + private val googleTaskListDao: GoogleTaskListDao, + private val caldavDao: CaldavDao, + private val locationDao: LocationDao) { + + var dashclockFilter: Filter? + get() = getFilterFromPreference(R.string.p_dashclock_filter) + set(filter) = setFilterPreference(filter, R.string.p_dashclock_filter) + + var badgeFilter: Filter? + get() = getFilterFromPreference(R.string.p_badge_list) + set(filter) = setFilterPreference(filter, R.string.p_badge_list) + + var defaultFilter: Filter? + get() = getFilterFromPreference(R.string.p_default_list) + set(filter) = setFilterPreference(filter, R.string.p_default_list) + + var lastViewedFilter: Filter? + get() = getFilterFromPreference(R.string.p_last_viewed_list) + set(filter) = setFilterPreference(filter, R.string.p_last_viewed_list) + + var defaultRemoteList: Filter? + get() = getFilterFromPreference(R.string.p_default_remote_list) + set(filter) = setFilterPreference(filter, R.string.p_default_remote_list) + + val startupFilter: Filter? + get() { + return if (preferences.getBoolean(R.string.p_open_last_viewed_list, true)) { + lastViewedFilter + } else { + defaultFilter + } + } + + fun getFilterFromPreference(resId: Int): Filter? = + getFilterFromPreference(preferences.getStringValue(resId)) + + fun getFilterFromPreference(prefString: String?): Filter? = + getFilterFromPreference(prefString, getMyTasksFilter(context.resources)) + + private fun getFilterFromPreference(preferenceValue: String?, def: Filter?): Filter? { + if (!isNullOrEmpty(preferenceValue)) { + try { + val filter = loadFilter(preferenceValue) + if (filter != null) { + return filter + } + } catch (e: Exception) { + Timber.e(e) + } + } + return def + } + + private fun loadFilter(preferenceValue: String?): Filter? { + val split = preferenceValue!!.split(":".toRegex()).toTypedArray() + return when (split[0].toInt()) { + TYPE_FILTER -> getBuiltInFilter(split[1].toInt()) + TYPE_CUSTOM_FILTER -> customFilterExposer.getFilter(split[1].toLong()) + TYPE_TAG -> { + val tag = tagDataDao.getByUuid(split[1]) + if (tag == null || isNullOrEmpty(tag.name)) null else TagFilter(tag) + } + TYPE_GOOGLE_TASKS -> googleTaskListDao.getById(split[1].toLong())?.let { GtasksFilter(it) } + TYPE_CALDAV -> caldavDao.getCalendarByUuid(split[1])?.let { CaldavFilter(it) } + TYPE_LOCATION -> locationDao.getPlace(split[1])?.let { PlaceFilter(it) } + else -> null + } + } + + private fun setFilterPreference(filter: Filter?, prefId: Int) = + getFilterPreferenceValue(filter).let { preferences.setString(prefId, it) } + + fun getFilterPreferenceValue(filter: Filter?): String? = when (val filterType = getFilterType(filter)) { + TYPE_FILTER -> getFilterPreference(filterType, getBuiltInFilterId(filter)) + TYPE_CUSTOM_FILTER -> getFilterPreference(filterType, (filter as CustomFilter).id) + TYPE_TAG -> getFilterPreference(filterType, (filter as TagFilter).uuid) + TYPE_GOOGLE_TASKS -> getFilterPreference(filterType, (filter as GtasksFilter).storeId) + TYPE_CALDAV -> getFilterPreference(filterType, (filter as CaldavFilter).uuid) + TYPE_LOCATION -> getFilterPreference(filterType, (filter as PlaceFilter).uid) + else -> null + } + + private fun getFilterPreference(type: Int, value: T) = "$type:$value" + + private fun getFilterType(filter: Filter?) = when (filter) { + is TagFilter -> TYPE_TAG + is GtasksFilter -> TYPE_GOOGLE_TASKS + is CustomFilter -> TYPE_CUSTOM_FILTER + is CaldavFilter -> TYPE_CALDAV + is PlaceFilter -> TYPE_LOCATION + else -> TYPE_FILTER + } + + private fun getBuiltInFilter(id: Int): Filter = when (id) { + FILTER_TODAY -> BuiltInFilterExposer.getTodayFilter(context.resources) + FILTER_RECENTLY_MODIFIED -> BuiltInFilterExposer.getRecentlyModifiedFilter(context.resources) + else -> getMyTasksFilter(context.resources) + } + + private fun getBuiltInFilterId(filter: Filter?): Int { + if (BuiltInFilterExposer.isTodayFilter(context, filter)) { + return FILTER_TODAY + } else if (BuiltInFilterExposer.isRecentlyModifiedFilter(context, filter)) { + return FILTER_RECENTLY_MODIFIED + } + return FILTER_MY_TASKS + } + + companion object { + private const val TYPE_FILTER = 0 + private const val TYPE_CUSTOM_FILTER = 1 + private const val TYPE_TAG = 2 + private const val TYPE_GOOGLE_TASKS = 3 + private const val TYPE_CALDAV = 4 + private const val TYPE_LOCATION = 5 + private const val FILTER_MY_TASKS = 0 + private const val FILTER_TODAY = 1 + @Suppress("unused") private const val FILTER_UNCATEGORIZED = 2 + private const val FILTER_RECENTLY_MODIFIED = 3 + } +} \ No newline at end of file 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 a320ef657..e0d836bcc 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt @@ -52,7 +52,7 @@ class DashClock : InjectingPreferenceFragment() { private fun refreshPreferences() { 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 } override fun inject(component: FragmentComponent) = component.inject(this) diff --git a/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt b/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt index 65af6f83e..76efab76c 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt @@ -97,8 +97,8 @@ class LookAndFeel : InjectingPreferenceFragment() { } val defaultList = findPreference(R.string.p_default_list) - val filter: Filter = defaultFilterProvider.defaultFilter - defaultList.summary = filter.listingTitle + val filter: Filter? = defaultFilterProvider.defaultFilter + defaultList.summary = filter?.listingTitle defaultList.onPreferenceClickListener = Preference.OnPreferenceClickListener { val intent = Intent(context, FilterSelectionActivity::class.java) intent.putExtra( diff --git a/app/src/main/java/org/tasks/preferences/fragments/Notifications.kt b/app/src/main/java/org/tasks/preferences/fragments/Notifications.kt index 82fd9a1a6..1c79ba8f1 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/Notifications.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/Notifications.kt @@ -76,7 +76,7 @@ class Notifications : InjectingPreferenceFragment() { val badgePreference: Preference = findPreference(R.string.p_badge_list) val filter = defaultFilterProvider.badgeFilter - badgePreference.summary = filter.listingTitle + badgePreference.summary = filter?.listingTitle badgePreference.onPreferenceClickListener = Preference.OnPreferenceClickListener { val intent = Intent(context, FilterSelectionActivity::class.java) intent.putExtra( diff --git a/app/src/main/java/org/tasks/preferences/fragments/Widgets.kt b/app/src/main/java/org/tasks/preferences/fragments/Widgets.kt index 54370230d..600a2cb14 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/Widgets.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/Widgets.kt @@ -33,7 +33,7 @@ class Widgets : InjectingPreferenceFragment() { val widgetPrefs = WidgetPreferences(context, preferences, it) val pref = Preference(context) val filter = defaultFilterProvider.getFilterFromPreference(widgetPrefs.filterId) - pref.title = filter.listingTitle + pref.title = filter?.listingTitle pref.summary = getString(R.string.widget_id, it) val intent = Intent(context, WidgetConfigActivity::class.java) intent.putExtra(android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID, it)