From c1364ff95435029e68b395d33a602593f3227e74 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Wed, 10 Jun 2020 11:56:47 -0500 Subject: [PATCH] Break up caldav and google task filters --- .../java/org/tasks/filters/FilterProvider.kt | 101 +++++++++--------- 1 file changed, 48 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/org/tasks/filters/FilterProvider.kt b/app/src/main/java/org/tasks/filters/FilterProvider.kt index 4c02827db..07bb59280 100644 --- a/app/src/main/java/org/tasks/filters/FilterProvider.kt +++ b/app/src/main/java/org/tasks/filters/FilterProvider.kt @@ -171,68 +171,63 @@ class FilterProvider @Inject constructor( 0)) private fun googleTaskFilters(showCreate: Boolean = true): List = - googleTaskListDao - .getAccounts() - .flatMap { - listOf( - NavigationDrawerSubheader( - it.account, - it.error?.isNotBlank() ?: false, - it.isCollapsed, - SubheaderType.GOOGLE_TASKS, - it.id)) - .plusAllIf(!it.isCollapsed) { - googleTaskListDao - .getGoogleTaskFilters(it.account!!) - .map(GoogleTaskFilters::toGtasksFilter) - .sortedWith(AlphanumComparator.FILTER) - } - .plusIf(showCreate && !it.isCollapsed) { - NavigationDrawerAction( - context.getString(R.string.new_list), - R.drawable.ic_outline_add_24px, - Intent(context, GoogleTaskListSettingsActivity::class.java) - .putExtra(GoogleTaskListSettingsActivity.EXTRA_ACCOUNT, it), - NavigationDrawerFragment.REQUEST_NEW_LIST) - } + googleTaskListDao.getAccounts().flatMap { googleTaskFilter(it, showCreate) } + + private fun googleTaskFilter(account: GoogleTaskAccount, showCreate: Boolean): List = + listOf( + NavigationDrawerSubheader( + account.account, + account.error?.isNotBlank() ?: false, + account.isCollapsed, + SubheaderType.GOOGLE_TASKS, + account.id)) + .apply { if (account.isCollapsed) return this } + .plus(googleTaskListDao + .getGoogleTaskFilters(account.account!!) + .map(GoogleTaskFilters::toGtasksFilter) + .sortedWith(AlphanumComparator.FILTER)) + .plusIf(showCreate) { + NavigationDrawerAction( + context.getString(R.string.new_list), + R.drawable.ic_outline_add_24px, + Intent(context, GoogleTaskListSettingsActivity::class.java) + .putExtra(GoogleTaskListSettingsActivity.EXTRA_ACCOUNT, account), + NavigationDrawerFragment.REQUEST_NEW_LIST) } private fun caldavFilters(showCreate: Boolean = true): List = caldavDao.getAccounts() .ifEmpty { listOf(caldavDao.setupLocalAccount(context)) } .filter { it.accountType != TYPE_LOCAL || preferences.getBoolean(R.string.p_lists_enabled, true) } - .flatMap { - listOf( - NavigationDrawerSubheader( - if (it.accountType == TYPE_LOCAL) { - context.getString(R.string.lists) - } else { - it.name - }, - it.error?.isNotBlank() ?: false, - it.isCollapsed, - SubheaderType.CALDAV, - it.id)) - .plusAllIf(!it.isCollapsed) { - caldavDao - .getCaldavFilters(it.uuid!!) - .map(CaldavFilters::toCaldavFilter) - .sortedWith(AlphanumComparator.FILTER) - } - .plusIf(showCreate && !it.isCollapsed) { - NavigationDrawerAction( - context.getString(R.string.new_list), - R.drawable.ic_outline_add_24px, - Intent(context, it.listSettingsClass()) - .putExtra(BaseCaldavCalendarSettingsActivity.EXTRA_CALDAV_ACCOUNT, it), - NavigationDrawerFragment.REQUEST_NEW_LIST) - } + .flatMap { caldavFilter(it, showCreate) } + + private fun caldavFilter(account: CaldavAccount, showCreate: Boolean): List = + listOf( + NavigationDrawerSubheader( + if (account.accountType == TYPE_LOCAL) { + context.getString(R.string.lists) + } else { + account.name + }, + account.error?.isNotBlank() ?: false, + account.isCollapsed, + SubheaderType.CALDAV, + account.id)) + .apply { if (account.isCollapsed) return this } + .plus(caldavDao + .getCaldavFilters(account.uuid!!) + .map(CaldavFilters::toCaldavFilter) + .sortedWith(AlphanumComparator.FILTER)) + .plusIf(showCreate) { + NavigationDrawerAction( + context.getString(R.string.new_list), + R.drawable.ic_outline_add_24px, + Intent(context, account.listSettingsClass()) + .putExtra(BaseCaldavCalendarSettingsActivity.EXTRA_CALDAV_ACCOUNT, account), + NavigationDrawerFragment.REQUEST_NEW_LIST) } companion object { - private fun Collection.plusAllIf(predicate: Boolean, item: () -> Iterable): List = - plus(if (predicate) item.invoke() else emptyList()) - private fun Collection.plusIf(predicate: Boolean, item: () -> T): List = if (predicate) plus(item.invoke()) else this.toList()