Break up caldav and google task filters

pull/1012/head
Alex Baker 4 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, it), .putExtra(GoogleTaskListSettingsActivity.EXTRA_ACCOUNT, account),
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) }
private fun caldavFilter(account: CaldavAccount, showCreate: Boolean): List<FilterListItem> =
listOf( listOf(
NavigationDrawerSubheader( NavigationDrawerSubheader(
if (it.accountType == TYPE_LOCAL) { if (account.accountType == TYPE_LOCAL) {
context.getString(R.string.lists) context.getString(R.string.lists)
} else { } else {
it.name account.name
}, },
it.error?.isNotBlank() ?: false, account.error?.isNotBlank() ?: false,
it.isCollapsed, account.isCollapsed,
SubheaderType.CALDAV, SubheaderType.CALDAV,
it.id)) account.id))
.plusAllIf(!it.isCollapsed) { .apply { if (account.isCollapsed) return this }
caldavDao .plus(caldavDao
.getCaldavFilters(it.uuid!!) .getCaldavFilters(account.uuid!!)
.map(CaldavFilters::toCaldavFilter) .map(CaldavFilters::toCaldavFilter)
.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, it.listSettingsClass()) Intent(context, account.listSettingsClass())
.putExtra(BaseCaldavCalendarSettingsActivity.EXTRA_CALDAV_ACCOUNT, it), .putExtra(BaseCaldavCalendarSettingsActivity.EXTRA_CALDAV_ACCOUNT, account),
NavigationDrawerFragment.REQUEST_NEW_LIST) 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