Skip to content

Commit

Permalink
fixed date in backup and improved cc addition
Browse files Browse the repository at this point in the history
  • Loading branch information
suyash01 committed May 11, 2024
1 parent 9f954a1 commit fa904ed
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId = "com.suyash.creditmanager"
minSdk = 26
targetSdk = 34
versionCode = 14
versionName = "1.0.13"
versionCode = 15
versionName = "1.0.14"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:supportsRtl="false"
android:theme="@style/Theme.CreditManager"
tools:targetApi="34">
<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import androidx.work.WorkInfo
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.annotations.SerializedName
import com.google.gson.reflect.TypeToken
import com.suyash.creditmanager.R
import com.suyash.creditmanager.domain.model.CreditCard
import com.suyash.creditmanager.domain.model.EMI
Expand All @@ -26,13 +27,15 @@ import com.suyash.creditmanager.domain.use_case.CreditCardUseCases
import com.suyash.creditmanager.domain.use_case.EMIUseCases
import com.suyash.creditmanager.domain.use_case.TransactionUseCase
import com.suyash.creditmanager.domain.use_case.TxnCategoryUseCase
import com.suyash.creditmanager.domain.util.LocalDateJSONConverter
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.time.LocalDate

@HiltWorker
class BackupWorker @AssistedInject constructor(
Expand All @@ -47,6 +50,11 @@ class BackupWorker @AssistedInject constructor(
private val notificationManager =
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager

private val gson = GsonBuilder().registerTypeAdapter(
object : TypeToken<LocalDate>() {}.type,
LocalDateJSONConverter()
).create()

override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
val fileUri = inputData.getString(LOCATION_URI_KEY)?.toUri()
?: return@withContext Result.failure()
Expand Down Expand Up @@ -123,7 +131,6 @@ class BackupWorker @AssistedInject constructor(

private fun writeBackupDataToJsonFile(uri: Uri, backupData: BackupData) {
applicationContext.contentResolver.openOutputStream(uri)?.use { outputStream ->
val gson = Gson()
val jsonString = gson.toJson(backupData)
val writer = OutputStreamWriter(outputStream)
writer.write(jsonString)
Expand All @@ -135,7 +142,6 @@ class BackupWorker @AssistedInject constructor(
try {
applicationContext.contentResolver.openInputStream(uri)?.use { inputStream ->
val reader = InputStreamReader(inputStream)
val gson = Gson()
return gson.fromJson(reader, BackupData::class.java)
}
return BackupData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ data class EMI(
@PrimaryKey(autoGenerate = true)
var id: Int = 0
)

class InvalidEMIException(message: String): Exception(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.suyash.creditmanager.domain.util

import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import java.lang.reflect.Type
import java.time.LocalDate
import java.time.format.DateTimeFormatter

class LocalDateJSONConverter: JsonSerializer<LocalDate>, JsonDeserializer<LocalDate> {
override fun serialize(src: LocalDate, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
return JsonPrimitive(DateTimeFormatter.ISO_LOCAL_DATE.format(src))
}

override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): LocalDate {
return DateTimeFormatter.ISO_LOCAL_DATE.parse(json.asString, LocalDate::from)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardActions
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
import androidx.compose.material.icons.filled.Done
Expand All @@ -29,6 +32,9 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
Expand All @@ -43,16 +49,18 @@ fun AddEditCCScreen(
navController: NavController,
viewModel: AddEditCCViewModel = hiltViewModel()
) {
val focusManager = LocalFocusManager.current
val snackbarHostState = remember { SnackbarHostState() }

LaunchedEffect(key1 = true) {
viewModel.eventFlow.collectLatest { event ->
when(event) {
when (event) {
is AddEditCCViewModel.UiEvent.ShowSnackbar -> {
snackbarHostState.showSnackbar(
message = event.message
)
}

is AddEditCCViewModel.UiEvent.NavigateUp -> {
navController.navigateUp()
}
Expand All @@ -70,7 +78,7 @@ fun AddEditCCScreen(
Text(text = if (viewModel.currentCCId.value == 0) "Add Credit Card" else "Edit Credit Card")
},
navigationIcon = {
IconButton(onClick = {viewModel.onEvent(AddEditCCEvent.BackPressed)}) {
IconButton(onClick = { viewModel.onEvent(AddEditCCEvent.BackPressed) }) {
Icon(
Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = "Go Back"
Expand All @@ -92,18 +100,10 @@ fun AddEditCCScreen(
.fillMaxWidth()
.padding(contentPadding)
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
var ccTypeDropdownExpanded by remember { mutableStateOf(false) }

OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = viewModel.cardName.value,
onValueChange = { newText ->
viewModel.onEvent(AddEditCCEvent.EnteredCardName(newText))
},
label = { Text("Card Name") }
)
Spacer(modifier = Modifier.height(16.dp))
ExposedDropdownMenuBox(
modifier = Modifier.fillMaxWidth(),
expanded = ccTypeDropdownExpanded,
Expand Down Expand Up @@ -143,6 +143,18 @@ fun AddEditCCScreen(
}
}
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = viewModel.cardName.value,
singleLine = true,
onValueChange = { newText ->
viewModel.onEvent(AddEditCCEvent.EnteredCardName(newText))
},
label = { Text("Card Name") },
keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(onNext = { focusManager.moveFocus(FocusDirection.Down) })
)
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
singleLine = true,
Expand All @@ -157,8 +169,12 @@ fun AddEditCCScreen(
Text("XXXX-XXXX-XXXX-")
}
},
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
label = { Text("Card Number") }
label = { Text("Card Number") },
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(onNext = { focusManager.moveFocus(FocusDirection.Down) })
)
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
Expand All @@ -168,8 +184,12 @@ fun AddEditCCScreen(
viewModel.onEvent(AddEditCCEvent.EnteredExpiry(newText))
},
visualTransformation = CMDateMask(),
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
label = { Text("Expiry Date") }
label = { Text("Expiry Date") },
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(onNext = { focusManager.moveFocus(FocusDirection.Down) })
)
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
Expand All @@ -178,8 +198,12 @@ fun AddEditCCScreen(
onValueChange = { newText ->
viewModel.onEvent(AddEditCCEvent.EnteredDueDate(newText))
},
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
label = { Text("Due Date") }
label = { Text("Due Date") },
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(onNext = { focusManager.moveFocus(FocusDirection.Down) })
)
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
Expand All @@ -188,8 +212,12 @@ fun AddEditCCScreen(
onValueChange = { newText ->
viewModel.onEvent(AddEditCCEvent.EnteredBillDate(newText))
},
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
label = { Text("Bill Date") }
label = { Text("Bill Date") },
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(onNext = { focusManager.moveFocus(FocusDirection.Down) })
)
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
Expand All @@ -198,17 +226,24 @@ fun AddEditCCScreen(
onValueChange = { newText ->
viewModel.onEvent(AddEditCCEvent.EnteredLimit(newText))
},
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
label = { Text("Limit") }
label = { Text("Limit") },
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(onNext = { focusManager.moveFocus(FocusDirection.Down) })
)
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = viewModel.bankName.value,
singleLine = true,
onValueChange = { newText ->
viewModel.onEvent(AddEditCCEvent.EnteredBankName(newText))
},
label = { Text("Bank Name") }
label = { Text("Bank Name") },
keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() })
)
}
}
Expand Down

0 comments on commit fa904ed

Please sign in to comment.