Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy-and-edit functionality #183

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
}

dependencies {
implementation(libs.appcompat)
implementation(libs.activity.compose)
implementation(libs.activity.ktx)
implementation(platform(libs.compose.bom))
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/java/be/chvp/nanoledger/ui/edit/EditActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ 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
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()
Expand All @@ -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
Expand All @@ -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() },
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 2 additions & 19 deletions app/src/main/java/be/chvp/nanoledger/ui/edit/EditViewModel.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,24 +22,9 @@ class EditViewModel
) : TransactionFormViewModel(application, preferencesDataSource, ledgerRepository) {
private lateinit var sourceTransaction: Transaction

private val _loading = MutableLiveData<Boolean>(true)
val loading: LiveData<Boolean> = _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) {
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/be/chvp/nanoledger/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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!!)
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/baseline_difference_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M18,23H4c-1.1,0 -2,-0.9 -2,-2V7h2v14h14V23zM15,1H8C6.9,1 6.01,1.9 6.01,3L6,17c0,1.1 0.89,2 1.99,2H19c1.1,0 2,-0.9 2,-2V7L15,1zM16.5,15h-6v-2h6V15zM16.5,9h-2v2h-2V9h-2V7h2V5h2v2h2V9z"/>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<string name="cancel">Cancel</string>
<string name="change_default_currency">Change default currency</string>
<string name="copy">Copy</string>
<string name="copy_and_edit">Copy and edit</string>
<string name="currency_amount_order">Order of currency and amount</string>
<string name="currency_order_after">Currency after amount</string>
<string name="currency_order_before">Currency before amount</string>
Expand Down
16 changes: 9 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
Loading