From 43fd1c8f059d9565a0b4dc9b097ee7afe465e2e1 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Sun, 6 Oct 2024 01:07:02 +0200 Subject: [PATCH] Add copy-and-edit functionality --- app/build.gradle.kts | 1 + .../be/chvp/nanoledger/ui/add/AddActivity.kt | 7 +++++++ .../be/chvp/nanoledger/ui/add/AddViewModel.kt | 7 +++++++ .../ui/common/TransactionFormComponents.kt | 2 ++ .../ui/common/TransactionFormViewModel.kt | 18 ++++++++++++++++ .../chvp/nanoledger/ui/edit/EditActivity.kt | 10 ++++----- .../chvp/nanoledger/ui/edit/EditViewModel.kt | 21 ++----------------- .../chvp/nanoledger/ui/main/MainActivity.kt | 11 +++++++++- .../res/drawable/baseline_difference_24.xml | 10 +++++++++ app/src/main/res/values/strings.xml | 1 + gradle/libs.versions.toml | 16 +++++++------- 11 files changed, 71 insertions(+), 33 deletions(-) create mode 100644 app/src/main/res/drawable/baseline_difference_24.xml diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 70e513d..35d4538 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -81,6 +81,7 @@ configure { } dependencies { + implementation(libs.appcompat) implementation(libs.activity.compose) implementation(libs.activity.ktx) implementation(platform(libs.compose.bom)) diff --git a/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt b/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt index f97fd09..98d5f5f 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt @@ -32,6 +32,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import be.chvp.nanoledger.R +import be.chvp.nanoledger.ui.common.TRANSACTION_INDEX_KEY import be.chvp.nanoledger.ui.common.TransactionForm import be.chvp.nanoledger.ui.main.MainActivity import be.chvp.nanoledger.ui.theme.NanoLedgerTheme @@ -45,6 +46,12 @@ class AddActivity() : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + + if (getIntent().hasExtra(TRANSACTION_INDEX_KEY)) { + val transactionIndex = getIntent().getIntExtra(TRANSACTION_INDEX_KEY, 0) + addViewModel.loadTransactionFromIndex(transactionIndex) + } + setContent { val context = LocalContext.current val scope = rememberCoroutineScope() diff --git a/app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt b/app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt index ad470b6..c450a43 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt @@ -9,6 +9,7 @@ import be.chvp.nanoledger.ui.util.Event import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch +import java.util.Date import javax.inject.Inject @HiltViewModel @@ -19,6 +20,12 @@ class AddViewModel preferencesDataSource: PreferencesDataSource, ledgerRepository: LedgerRepository, ) : TransactionFormViewModel(application, preferencesDataSource, ledgerRepository) { + fun loadTransactionFromIndex(index: Int) { + setFromTransaction(ledgerRepository.transactions.value!![index]) + // When copying, set the date to today + setDate(Date()) + } + override fun save(onFinish: suspend () -> Unit) { val uri = preferencesDataSource.getFileUri() if (uri != null) { diff --git a/app/src/main/java/be/chvp/nanoledger/ui/common/TransactionFormComponents.kt b/app/src/main/java/be/chvp/nanoledger/ui/common/TransactionFormComponents.kt index c64ead8..addf71e 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/common/TransactionFormComponents.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/common/TransactionFormComponents.kt @@ -54,6 +54,8 @@ import androidx.lifecycle.viewmodel.compose.viewModel import be.chvp.nanoledger.R import kotlinx.coroutines.launch +val TRANSACTION_INDEX_KEY = "transaction_index" + @Composable fun TransactionForm( viewModel: TransactionFormViewModel, diff --git a/app/src/main/java/be/chvp/nanoledger/ui/common/TransactionFormViewModel.kt b/app/src/main/java/be/chvp/nanoledger/ui/common/TransactionFormViewModel.kt index 7ef228d..efd4de5 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/common/TransactionFormViewModel.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/common/TransactionFormViewModel.kt @@ -8,6 +8,7 @@ import androidx.lifecycle.map import androidx.lifecycle.switchMap import be.chvp.nanoledger.data.LedgerRepository import be.chvp.nanoledger.data.PreferencesDataSource +import be.chvp.nanoledger.data.Transaction import be.chvp.nanoledger.ui.util.Event import java.io.IOException import java.math.BigDecimal @@ -155,10 +156,27 @@ abstract class TransactionFormViewModel abstract fun save(onFinish: suspend () -> Unit) + fun setFromTransaction(transaction: Transaction) { + setDate(transaction.date) + setStatus(transaction.status ?: "") + setPayee(transaction.payee) + setNote(transaction.note ?: "") + + transaction.postings.forEachIndexed { i, posting -> + setAccount(i, posting.account) + setCurrency(i, posting.amount?.currency ?: "") + setAmount(i, posting.amount?.quantity ?: "") + } + } + fun setDate(dateMillis: Long) { _date.value = Date(dateMillis) } + fun setDate(newDate: Date) { + _date.value = newDate + } + fun setDate(newDate: String) { val parsed = dateFormat.parse(newDate, ParsePosition(0)) if (parsed != null) { diff --git a/app/src/main/java/be/chvp/nanoledger/ui/edit/EditActivity.kt b/app/src/main/java/be/chvp/nanoledger/ui/edit/EditActivity.kt index 282a9ef..ae20b15 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/edit/EditActivity.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/edit/EditActivity.kt @@ -32,6 +32,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import be.chvp.nanoledger.R +import be.chvp.nanoledger.ui.common.TRANSACTION_INDEX_KEY import be.chvp.nanoledger.ui.common.TransactionForm import be.chvp.nanoledger.ui.main.MainActivity import be.chvp.nanoledger.ui.theme.NanoLedgerTheme @@ -39,8 +40,6 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch -val TRANSACTION_INDEX_KEY = "transaction_index" - @AndroidEntryPoint class EditActivity() : ComponentActivity() { private val editViewModel: EditViewModel by viewModels() @@ -53,7 +52,7 @@ class EditActivity() : ComponentActivity() { finish() } val transactionIndex = getIntent().getIntExtra(TRANSACTION_INDEX_KEY, 0) - editViewModel.setFromIndex(transactionIndex) + editViewModel.loadTransactionFromIndex(transactionIndex) setContent { val context = LocalContext.current @@ -65,10 +64,9 @@ class EditActivity() : ComponentActivity() { finish() startActivity(Intent(context, MainActivity::class.java).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) } - val loading by editViewModel.loading.observeAsState() val saving by editViewModel.saving.observeAsState() val valid by editViewModel.valid.observeAsState() - val enabled = !(saving ?: true) && (valid ?: false) && !(loading ?: true) + val enabled = !(saving ?: true) && (valid ?: false) NanoLedgerTheme { Scaffold( topBar = { Bar() }, @@ -96,7 +94,7 @@ class EditActivity() : ComponentActivity() { MaterialTheme.colorScheme.surface }, ) { - if (saving ?: true || loading ?: true) { + if (saving ?: true) { CircularProgressIndicator( color = MaterialTheme.colorScheme.secondary, trackColor = MaterialTheme.colorScheme.surfaceVariant, diff --git a/app/src/main/java/be/chvp/nanoledger/ui/edit/EditViewModel.kt b/app/src/main/java/be/chvp/nanoledger/ui/edit/EditViewModel.kt index 0648fa8..419c63f 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/edit/EditViewModel.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/edit/EditViewModel.kt @@ -1,8 +1,6 @@ package be.chvp.nanoledger.ui.edit import android.app.Application -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import be.chvp.nanoledger.data.LedgerRepository import be.chvp.nanoledger.data.PreferencesDataSource @@ -24,24 +22,9 @@ class EditViewModel ) : TransactionFormViewModel(application, preferencesDataSource, ledgerRepository) { private lateinit var sourceTransaction: Transaction - private val _loading = MutableLiveData(true) - val loading: LiveData = _loading - - fun setFromIndex(index: Int) { + fun loadTransactionFromIndex(index: Int) { sourceTransaction = ledgerRepository.transactions.value!![index] - - setDate(sourceTransaction.date) - setStatus(sourceTransaction.status ?: "") - setPayee(sourceTransaction.payee) - setNote(sourceTransaction.note ?: "") - - sourceTransaction.postings.forEachIndexed { i, posting -> - setAccount(i, posting.account) - setCurrency(i, posting.amount?.currency ?: "") - setAmount(i, posting.amount?.quantity ?: "") - } - - _loading.value = false + setFromTransaction(sourceTransaction) } override fun save(onFinish: suspend () -> Unit) { diff --git a/app/src/main/java/be/chvp/nanoledger/ui/main/MainActivity.kt b/app/src/main/java/be/chvp/nanoledger/ui/main/MainActivity.kt index c99522e..32a0fac 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/main/MainActivity.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/main/MainActivity.kt @@ -49,6 +49,7 @@ import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalSoftwareKeyboardController +import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign @@ -57,8 +58,8 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import be.chvp.nanoledger.R import be.chvp.nanoledger.ui.add.AddActivity +import be.chvp.nanoledger.ui.common.TRANSACTION_INDEX_KEY import be.chvp.nanoledger.ui.edit.EditActivity -import be.chvp.nanoledger.ui.edit.TRANSACTION_INDEX_KEY import be.chvp.nanoledger.ui.preferences.PreferencesActivity import be.chvp.nanoledger.ui.theme.NanoLedgerTheme import dagger.hilt.android.AndroidEntryPoint @@ -303,6 +304,14 @@ fun SelectionBar(mainViewModel: MainViewModel = viewModel()) { }, title = { }, actions = { + IconButton(onClick = { + val intent = Intent(context, AddActivity::class.java) + intent.putExtra(TRANSACTION_INDEX_KEY, selected!!) + mainViewModel.toggleSelect(selected!!) + context.startActivity(intent) + }) { + Icon(painterResource(R.drawable.baseline_difference_24), contentDescription = stringResource(R.string.copy_and_edit)) + } IconButton(onClick = { val intent = Intent(context, EditActivity::class.java) intent.putExtra(TRANSACTION_INDEX_KEY, selected!!) diff --git a/app/src/main/res/drawable/baseline_difference_24.xml b/app/src/main/res/drawable/baseline_difference_24.xml new file mode 100644 index 0000000..6d3869f --- /dev/null +++ b/app/src/main/res/drawable/baseline_difference_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b8199e9..175a9a8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,6 +7,7 @@ Cancel Change default currency Copy + Copy and edit Order of currency and amount Currency after amount Currency before amount diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 57f5435..747b322 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,22 +1,24 @@ [versions] +activity = "1.9.2" agp = "8.7.0" -kotlin = "2.0.20" -ksp = "2.0.20-1.0.25" -hilt = "2.52" +androidx-test-ext-junit = "1.2.1" +appcompat = "1.7.0" +compose-bom = "2024.09.03" core-ktx = "1.13.1" +espresso-core = "3.6.1" +hilt = "2.52" junit = "4.13.2" +kotlin = "2.0.20" +ksp = "2.0.20-1.0.25" ktlint = "12.1.1" -androidx-test-ext-junit = "1.2.1" -espresso-core = "3.6.1" lifecycle = "2.8.6" -activity = "1.9.2" -compose-bom = "2024.09.03" material = "1.12.0" [libraries] activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activity" } activity-ktx = { group = "androidx.activity", name = "activity-ktx", version.ref = "activity" } androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" } +appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose-bom" } compose-material-icons-extended = { group = "androidx.compose.material", name = "material-icons-extended" } compose-material3 = { group = "androidx.compose.material3", name = "material3" }