Save progress on UI for expense entry.

This commit is contained in:
Hyperling 2024-07-20 11:56:39 -07:00
parent dcca201a91
commit 628f858a1d

View File

@ -1,38 +1,37 @@
package com.hyperling.expensetracker package com.hyperling.expensetracker
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.preference.PreferenceManager
import android.widget.EditText
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold import androidx.compose.material3.RadioButton
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextField import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hyperling.expensetracker.ui.theme.ExpenseTrackerTheme import com.hyperling.expensetracker.ui.theme.ExpenseTrackerTheme
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.core.text.isDigitsOnly
// Activity which allows data to be entered and saved to Shared Preferences. // Activity which allows data to be entered and saved to Shared Preferences.
@ -57,6 +56,9 @@ class NewExpenseActivity : ComponentActivity() {
@Composable @Composable
fun CreateExpense() { fun CreateExpense() {
/*** Activity Setup ***/
val context = LocalContext.current val context = LocalContext.current
val intent = Intent(context, MainActivity::class.java) val intent = Intent(context, MainActivity::class.java)
fun returnToMain() { fun returnToMain() {
@ -65,53 +67,101 @@ fun CreateExpense() {
context.startActivity(intent) context.startActivity(intent)
} }
val sharedPref : SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) // https://medium.com/jetpack-composers/jetpack-compose-room-db-b7b23bd6b189
//val sharedPref : SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
fun saveAndReturn() { fun saveAndReturn() {
val editor = sharedPref.edit() //val editor = sharedPref.edit()
editor.putString("Test", "Test") //editor.putString("Test", "Test")
returnToMain() returnToMain()
} }
/*** UI Variables ***/
var text by remember { mutableStateOf("") }
var cost by remember { mutableStateOf("") }
// https://jetpackcomposeworld.com/radio-buttons-in-jetpack-compose/
val freqOptions = listOf("Daily", "Weekly", "Monthly", "Yearly")
var freqSelected by remember { mutableStateOf(freqOptions[2]) }
/*** UI ***/
Column ( Column (
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center, verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize() modifier = Modifier.fillMaxSize()
) { ) {
Row ( // Header //
verticalAlignment = Alignment.CenterVertically, Text(
horizontalArrangement = Arrangement.Center, text = "Add New Expense",
) { fontSize = 32.sp,
Text( fontWeight = FontWeight.Bold,
text = "Add New Expense", modifier = Modifier.padding(8.dp),
fontSize = 32.sp, )
fontWeight = FontWeight.Bold
)
}
Row ( // Input //
verticalAlignment = Alignment.CenterVertically, Column (
horizontalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) { ) {
var text by remember { mutableStateOf("") } TextField (
TextField(
value = text, value = text,
onValueChange = { text = it }, onValueChange = { text = it },
label = { Text("Label") } label = { Text("Name") },
modifier = Modifier.padding(4.dp)
) )
TextField (
value = cost,
onValueChange = {
val lastChar = it.substring(it.length-1)
if (lastChar.isDigitsOnly())
cost = it
if (lastChar == ".")
cost = it.replace(".", "") + "."
},
label = { Text("Cost") },
modifier = Modifier.padding(4.dp),
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
)
Row (
verticalAlignment = Alignment.CenterVertically
){
Text (
text = "Frequency: "
)
Column (
horizontalAlignment = Alignment.Start
) {
freqOptions.forEach { freq ->
Row(
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(
selected = (freq == freqSelected),
onClick = { freqSelected = freq }
)
Text(
text = freq
)
}
}
}
}
} }
// Action Buttons //
Row ( Row (
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center horizontalArrangement = Arrangement.Center,
) { ) {
Spacer(modifier = Modifier.weight(1f)) Spacer(modifier = Modifier.weight(1f))
Button( Button(
onClick = { saveAndReturn() }, onClick = { saveAndReturn() },
Modifier.weight(3f)) Modifier.weight(3f).padding(4.dp),
{ ) {
Text(text = "Save") Text(text = "Save")
} }
@ -119,15 +169,14 @@ fun CreateExpense() {
Button( Button(
onClick = { returnToMain() }, onClick = { returnToMain() },
Modifier.weight(3f)) Modifier.weight(3f).padding(4.dp),
{ ) {
Text(text = "Cancel") Text(text = "Cancel")
} }
Spacer(modifier = Modifier.weight(1f)) Spacer(modifier = Modifier.weight(1f))
} }
} }
} }
@Preview(showBackground = true) @Preview(showBackground = true)