Skip to content

Commit

Permalink
prototype for filter card
Browse files Browse the repository at this point in the history
  • Loading branch information
mtotschnig committed Sep 30, 2022
1 parent 982026f commit 72d6209
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 53 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ ext {
desugarVersion = "1.1.5"
mlkitTextRecognition = "16.0.0-beta5"
truthVersion = "1.1.3"
accompanist = "0.25.1"
}
7 changes: 5 additions & 2 deletions myExpenses/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ dependencies {
implementation 'com.squareup.phrase:phrase:1.1.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation("androidx.compose.material3:material3:1.0.0-beta03")
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.activity:activity-compose:1.5.1'
implementation "com.google.android.material:compose-theme-adapter:1.1.18"
Expand All @@ -176,8 +177,10 @@ dependencies {
implementation "androidx.compose.material:material-icons-extended:$compose_version"
implementation "androidx.paging:paging-runtime-ktx:3.1.1"
implementation "androidx.paging:paging-compose:1.0.0-alpha16"
implementation "com.google.accompanist:accompanist-drawablepainter:0.23.1"
implementation "com.google.accompanist:accompanist-pager:0.25.1"
implementation "com.google.accompanist:accompanist-drawablepainter:$accompanist"
implementation "com.google.accompanist:accompanist-pager:$accompanist"
implementation "com.google.accompanist:accompanist-flowlayout:$accompanist"

kapt "com.jakewharton:butterknife-compiler:$butterknifeVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "com.google.dagger:dagger:$daggerVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@ import androidx.appcompat.view.ActionMode
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.Toolbar
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.RestoreFromTrash
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.SuggestionChip
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.accompanist.flowlayout.FlowRow
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.android.material.navigation.NavigationView
Expand Down Expand Up @@ -64,6 +76,7 @@ import org.totschnig.myexpenses.provider.DatabaseConstants.*
import org.totschnig.myexpenses.provider.TransactionProvider
import org.totschnig.myexpenses.provider.filter.CommentCriterion
import org.totschnig.myexpenses.provider.filter.Criterion
import org.totschnig.myexpenses.provider.filter.WhereFilter
import org.totschnig.myexpenses.sync.GenericAccountService
import org.totschnig.myexpenses.sync.GenericAccountService.Companion.requestSync
import org.totschnig.myexpenses.task.TaskExecutionFragment
Expand Down Expand Up @@ -105,16 +118,17 @@ abstract class BaseMyExpenses : LaunchActivity(), OcrHost, OnDialogResultListene
}

private fun moveToAccount(): Boolean {
return viewModel.accountData.value.indexOfFirst { it.id == accountId }.takeIf { it > -1 }?.let {
if (viewModel.pagerState.currentPage != it) {
lifecycleScope.launch {
viewModel.pagerState.scrollToPage(it)
return viewModel.accountData.value.indexOfFirst { it.id == accountId }.takeIf { it > -1 }
?.let {
if (viewModel.pagerState.currentPage != it) {
lifecycleScope.launch {
viewModel.pagerState.scrollToPage(it)
}
} else {
setCurrentAccount(it)
}
} else {
setCurrentAccount(it)
}
true
} ?: false
true
} ?: false
}

val currentAccount: FullAccount
Expand Down Expand Up @@ -259,6 +273,7 @@ abstract class BaseMyExpenses : LaunchActivity(), OcrHost, OnDialogResultListene
}
}

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
readAccountGroupingFromPref()
Expand Down Expand Up @@ -312,53 +327,83 @@ abstract class BaseMyExpenses : LaunchActivity(), OcrHost, OnDialogResultListene
}
}
}
ComposeTransactionList(
pagingSourceFactory = data,
headerData = headerData.collectAsState(HeaderData.EMPTY).value,
accountId = account.value.id,
selectionHandler = object : SelectionHandler {
override fun toggle(transaction: Transaction2) {
if (viewModel.selectionState.toggle(transaction)) {
viewModel.selectedTransactionSum += transaction.amount.amountMinor
} else {
viewModel.selectedTransactionSum -= transaction.amount.amountMinor
Column {
val filter = viewModel.filterPersistence.getValue(account.value.id)
.whereFilterAsFlow
.collectAsState(WhereFilter.empty())
.value
.takeIf { !it.isEmpty }?.let {
Row {
androidx.compose.material.Icon(
imageVector = Icons.Filled.Search,
contentDescription = stringResource(R.string.menu_search),
tint = Color.Green
)
FlowRow(modifier = Modifier.weight(1f)) {
it.criteria.forEach {
SuggestionChip(
onClick = { },
label = {
Text(it.prettyPrint(LocalContext.current))
}

)
}
}
androidx.compose.material.Icon(
imageVector = Icons.Filled.Close,
contentDescription = stringResource(R.string.clear_all_filters)
)
}
}
ComposeTransactionList(
pagingSourceFactory = data,
headerData = headerData.collectAsState(HeaderData.EMPTY).value,
accountId = account.value.id,
selectionHandler = object : SelectionHandler {
override fun toggle(transaction: Transaction2) {
if (viewModel.selectionState.toggle(transaction)) {
viewModel.selectedTransactionSum += transaction.amount.amountMinor
} else {
viewModel.selectedTransactionSum -= transaction.amount.amountMinor
}
}

override fun isSelected(transaction: Transaction2) =
selectionState.contains(transaction)
override fun isSelected(transaction: Transaction2) =
selectionState.contains(transaction)

override val selectionCount: Int
get() = selectionState.size
override val selectionCount: Int
get() = selectionState.size

},
menuGenerator = remember {
{ transaction ->
if (viewModel.accountData.value.first { it.id == transaction.accountId }.sealed) null else Menu(
listOfNotNull(
if (transaction.crStatus != CrStatus.VOID)
edit { edit(transaction) } else null,
MenuEntry(
icon = Icons.Filled.ContentCopy,
label = R.string.menu_clone_transaction
) {
edit(transaction, true)
},
delete { delete(listOf(transaction)) },
MenuEntry(
icon = myiconpack.IcActionTemplateAdd,
label = R.string.menu_create_template_from_transaction
) { createTemplate(transaction) },
if (transaction.crStatus == CrStatus.VOID)
},
menuGenerator = remember {
{ transaction ->
if (viewModel.accountData.value.first { it.id == transaction.accountId }.sealed) null else Menu(
listOfNotNull(
if (transaction.crStatus != CrStatus.VOID)
edit { edit(transaction) } else null,
MenuEntry(
icon = Icons.Filled.ContentCopy,
label = R.string.menu_clone_transaction
) {
edit(transaction, true)
},
delete { delete(listOf(transaction)) },
MenuEntry(
icon = Icons.Filled.RestoreFromTrash,
label = R.string.menu_undelete_transaction
) { undelete(transaction) } else null
icon = myiconpack.IcActionTemplateAdd,
label = R.string.menu_create_template_from_transaction
) { createTemplate(transaction) },
if (transaction.crStatus == CrStatus.VOID)
MenuEntry(
icon = Icons.Filled.RestoreFromTrash,
label = R.string.menu_undelete_transaction
) { undelete(transaction) } else null
)
)
)
}
}
}
)
)
}
}
}
}
Expand Down Expand Up @@ -968,7 +1013,8 @@ abstract class BaseMyExpenses : LaunchActivity(), OcrHost, OnDialogResultListene
enabled = sumInfo.mappedCategories
}
R.id.FILTER_STATUS_COMMAND -> {
enabled = currentAccount.isAggregate || currentAccount.type != AccountType.CASH
enabled =
currentAccount.isAggregate || currentAccount.type != AccountType.CASH
}
R.id.FILTER_PAYEE_COMMAND -> {
enabled = sumInfo.mappedPayees
Expand Down Expand Up @@ -1534,7 +1580,7 @@ abstract class BaseMyExpenses : LaunchActivity(), OcrHost, OnDialogResultListene
viewModel.addFilterCriteria(c, currentAccount.id)
}

fun removeFilter(id: Int) = if(viewModel.removeFilter(id, currentAccount.id)) {
fun removeFilter(id: Int) = if (viewModel.removeFilter(id, currentAccount.id)) {
invalidateOptionsMenu()
true
} else false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data class WhereFilter(val criteria: List<Criterion<*>>) {
if (queryParts || it.shouldApplyToParts()) {
listOf(*(it.selectionArgs + it.selectionArgs))
} else listOf(*it.selectionArgs)
}.toTypedArray().takeIf { it.isNotEmpty() }
}.toTypedArray()

operator fun get(id: Int): Criterion<*>? = criteria.find { it.id == id }

Expand Down

0 comments on commit 72d6209

Please sign in to comment.