Skip to content

Write out aligned postings according to a user preference #218

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

Merged
merged 1 commit into from
Dec 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const val FILE_URI_KEY = "file_uri"
const val DEFAULT_CURRENCY_KEY = "default_currency"
const val DEFAULT_STATUS_KEY = "default_status"
const val CURRENCY_BEFORE_AMOUNT_KEY = "currency_before_amount"
const val POSTING_WIDTH_KEY = "posting_width"

class PreferencesDataSource
@Inject
Expand Down Expand Up @@ -80,4 +81,10 @@ class PreferencesDataSource
CURRENCY_BEFORE_AMOUNT_KEY,
currencyBeforeAmount,
).apply()

val postingWidth: LiveData<Int> = sharedPreferences.intLiveData(POSTING_WIDTH_KEY, 72).map { it!! }

fun getPostingWidth(): Int = sharedPreferences.getInt(POSTING_WIDTH_KEY, 72)!!

fun setPostingWidth(width: Int) = sharedPreferences.edit().putInt(POSTING_WIDTH_KEY, width).apply()
}
20 changes: 20 additions & 0 deletions app/src/main/java/be/chvp/nanoledger/data/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,23 @@ fun SharedPreferences.booleanLiveData(
): SharedPreferenceLiveData<Boolean> {
return SharedPreferenceBooleanLiveData(this, key, default)
}

class SharedPreferenceIntLiveData(
sharedPrefs: SharedPreferences,
key: String,
private val default: Int,
) :
SharedPreferenceLiveData<Int>(sharedPrefs, key) {
init {
value = this.getValueFromPreferences(key)
}

override fun getValueFromPreferences(key: String): Int = if (sharedPrefs.contains(key)) sharedPrefs.getInt(key, default) else default
}

fun SharedPreferences.intLiveData(
key: String,
default: Int = 0,
): SharedPreferenceLiveData<Int> {
return SharedPreferenceIntLiveData(this, key, default)
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,22 @@ abstract class TransactionFormViewModel
transaction.append(" | ${note.value}")
}
transaction.append('\n')
// Drop last element, it should always be an empty posting
// Drop last element, it should always be an empty posting (and the only empty posting)
for (posting in postings.value!!.dropLast(1)) {
val usedLength = 7 + posting.first.length + posting.second.length + posting.third.length
val numberOfSpaces = preferencesDataSource.getPostingWidth() - usedLength
val spaces = " ".repeat(maxOf(0, numberOfSpaces))
if (posting.third == "") {
transaction.append(
" ${posting.first}\n",
)
} else if (preferencesDataSource.getCurrencyBeforeAmount()) {
transaction.append(
" ${posting.first} ${posting.second} ${posting.third}\n",
" ${posting.first} ${spaces}${posting.second} ${posting.third}\n",
)
} else {
transaction.append(
" ${posting.first} ${posting.third} ${posting.second}\n",
" ${posting.first} ${spaces}${posting.third} ${posting.second}\n",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
Expand Down Expand Up @@ -43,6 +44,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import be.chvp.nanoledger.R
import be.chvp.nanoledger.ui.theme.NanoLedgerTheme
Expand Down Expand Up @@ -112,6 +114,26 @@ class PreferencesActivity() : ComponentActivity() {
OutlinedTextField(newDefaultCurrency, { newDefaultCurrency = it })
}
HorizontalDivider()
val postingWidth by preferencesViewModel.postingWidth.observeAsState()
var newPostingWidth by remember { mutableStateOf("${postingWidth ?: 72}") }
var postingWidthOpen by remember { mutableStateOf(false) }
Setting(stringResource(R.string.posting_width), "${postingWidth ?: 72}") {
postingWidthOpen = true
}
SettingDialog(postingWidthOpen, stringResource(R.string.change_posting_width), true, {
preferencesViewModel.storePostingWidth(Integer.parseInt(newPostingWidth))
}, { postingWidthOpen = false }) {
OutlinedTextField(
newPostingWidth,
{
if (it.isEmpty() || it.matches(Regex("^\\d+$"))) {
newPostingWidth = it
}
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
)
}
HorizontalDivider()
val defaultStatus by preferencesViewModel.defaultStatus.observeAsState()
var expandedStatus by rememberSaveable { mutableStateOf(false) }
ExposedDropdownMenuBox(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PreferencesViewModel
val defaultCurrency: LiveData<String> = preferencesDataSource.defaultCurrency
val defaultStatus: LiveData<String> = preferencesDataSource.defaultStatus
val currencyBeforeAmount: LiveData<Boolean> = preferencesDataSource.currencyBeforeAmount
val postingWidth: LiveData<Int> = preferencesDataSource.postingWidth

fun storeFileUri(uri: Uri) = preferencesDataSource.setFileUri(uri)

Expand All @@ -30,4 +31,6 @@ class PreferencesViewModel
preferencesDataSource.setCurrencyBeforeAmount(
enable,
)

fun storePostingWidth(width: Int) = preferencesDataSource.setPostingWidth(width)
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="back">Back</string>
<string name="cancel">Cancel</string>
<string name="change_default_currency">Change default currency</string>
<string name="change_posting_width">Change posting width</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>
Expand All @@ -27,6 +28,7 @@
<string name="note">Note</string>
<string name="ok">OK</string>
<string name="payee">Payee</string>
<string name="posting_width">Width of postings written to file</string>
<string name="save">Save</string>
<string name="select_file">Select file…</string>
<string name="settings">Settings</string>
Expand Down
Loading