Break up caldav and google task filters

pull/1012/head
Alex Baker 6 years ago
parent 864bc73a8c
commit c1364ff954

@ -171,68 +171,63 @@ class FilterProvider @Inject constructor(
0)) 0))
private fun googleTaskFilters(showCreate: Boolean = true): List<FilterListItem> = private fun googleTaskFilters(showCreate: Boolean = true): List<FilterListItem> =
googleTaskListDao googleTaskListDao.getAccounts().flatMap { googleTaskFilter(it, showCreate) }
.getAccounts()
.flatMap { private fun googleTaskFilter(account: GoogleTaskAccount, showCreate: Boolean): List<FilterListItem> =
listOf( listOf(
NavigationDrawerSubheader( NavigationDrawerSubheader(
it.account, account.account,
it.error?.isNotBlank() ?: false, account.error?.isNotBlank() ?: false,
it.isCollapsed, account.isCollapsed,
SubheaderType.GOOGLE_TASKS, SubheaderType.GOOGLE_TASKS,
it.id)) account.id))
.plusAllIf(!it.isCollapsed) { .apply { if (account.isCollapsed) return this }
googleTaskListDao .plus(googleTaskListDao
.getGoogleTaskFilters(it.account!!) .getGoogleTaskFilters(account.account!!)
.map(GoogleTaskFilters::toGtasksFilter) .map(GoogleTaskFilters::toGtasksFilter)
.sortedWith(AlphanumComparator.FILTER) .sortedWith(AlphanumComparator.FILTER))
} .plusIf(showCreate) {
.plusIf(showCreate && !it.isCollapsed) { NavigationDrawerAction(
NavigationDrawerAction( context.getString(R.string.new_list),
context.getString(R.string.new_list), R.drawable.ic_outline_add_24px,
R.drawable.ic_outline_add_24px, Intent(context, GoogleTaskListSettingsActivity::class.java)
Intent(context, GoogleTaskListSettingsActivity::class.java) .putExtra(GoogleTaskListSettingsActivity.EXTRA_ACCOUNT, account),
.putExtra(GoogleTaskListSettingsActivity.EXTRA_ACCOUNT, it), NavigationDrawerFragment.REQUEST_NEW_LIST)
NavigationDrawerFragment.REQUEST_NEW_LIST)
}
} }
private fun caldavFilters(showCreate: Boolean = true): List<FilterListItem> = private fun caldavFilters(showCreate: Boolean = true): List<FilterListItem> =
caldavDao.getAccounts() caldavDao.getAccounts()
.ifEmpty { listOf(caldavDao.setupLocalAccount(context)) } .ifEmpty { listOf(caldavDao.setupLocalAccount(context)) }
.filter { it.accountType != TYPE_LOCAL || preferences.getBoolean(R.string.p_lists_enabled, true) } .filter { it.accountType != TYPE_LOCAL || preferences.getBoolean(R.string.p_lists_enabled, true) }
.flatMap { .flatMap { caldavFilter(it, showCreate) }
listOf(
NavigationDrawerSubheader( private fun caldavFilter(account: CaldavAccount, showCreate: Boolean): List<FilterListItem> =
if (it.accountType == TYPE_LOCAL) { listOf(
context.getString(R.string.lists) NavigationDrawerSubheader(
} else { if (account.accountType == TYPE_LOCAL) {
it.name context.getString(R.string.lists)
}, } else {
it.error?.isNotBlank() ?: false, account.name
it.isCollapsed, },
SubheaderType.CALDAV, account.error?.isNotBlank() ?: false,
it.id)) account.isCollapsed,
.plusAllIf(!it.isCollapsed) { SubheaderType.CALDAV,
caldavDao account.id))
.getCaldavFilters(it.uuid!!) .apply { if (account.isCollapsed) return this }
.map(CaldavFilters::toCaldavFilter) .plus(caldavDao
.sortedWith(AlphanumComparator.FILTER) .getCaldavFilters(account.uuid!!)
} .map(CaldavFilters::toCaldavFilter)
.plusIf(showCreate && !it.isCollapsed) { .sortedWith(AlphanumComparator.FILTER))
NavigationDrawerAction( .plusIf(showCreate) {
context.getString(R.string.new_list), NavigationDrawerAction(
R.drawable.ic_outline_add_24px, context.getString(R.string.new_list),
Intent(context, it.listSettingsClass()) R.drawable.ic_outline_add_24px,
.putExtra(BaseCaldavCalendarSettingsActivity.EXTRA_CALDAV_ACCOUNT, it), Intent(context, account.listSettingsClass())
NavigationDrawerFragment.REQUEST_NEW_LIST) .putExtra(BaseCaldavCalendarSettingsActivity.EXTRA_CALDAV_ACCOUNT, account),
} NavigationDrawerFragment.REQUEST_NEW_LIST)
} }
companion object { companion object {
private fun <T> Collection<T>.plusAllIf(predicate: Boolean, item: () -> Iterable<T>): List<T> =
plus(if (predicate) item.invoke() else emptyList())
private fun <T> Collection<T>.plusIf(predicate: Boolean, item: () -> T): List<T> = private fun <T> Collection<T>.plusIf(predicate: Boolean, item: () -> T): List<T> =
if (predicate) plus(item.invoke()) else this.toList() if (predicate) plus(item.invoke()) else this.toList()

Loading…
Cancel
Save