-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
282 additions
and
3 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
app/src/main/java/com/suyash/creditmanager/presentation/emi_details/EMIDetailEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.suyash.creditmanager.presentation.emi_details | ||
|
||
sealed class EMIDetailEvent { | ||
data object BackPressed: EMIDetailEvent() | ||
} |
148 changes: 148 additions & 0 deletions
148
app/src/main/java/com/suyash/creditmanager/presentation/emi_details/EMIDetailScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package com.suyash.creditmanager.presentation.emi_details | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
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.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.itemsIndexed | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ArrowBack | ||
import androidx.compose.material3.Divider | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.SnackbarHost | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.navigation.NavController | ||
import com.suyash.creditmanager.presentation.util.CMUtils | ||
import kotlinx.coroutines.flow.collectLatest | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun EMIDetailScreen( | ||
navController: NavController, | ||
viewModel: EMIDetailViewModel = hiltViewModel() | ||
) { | ||
val snackbarHostState = remember { SnackbarHostState() } | ||
|
||
LaunchedEffect(key1 = true) { | ||
viewModel.eventFlow.collectLatest { event -> | ||
when(event) { | ||
is EMIDetailViewModel.UiEvent.ShowSnackbar -> { | ||
snackbarHostState.showSnackbar( | ||
message = event.message | ||
) | ||
} | ||
is EMIDetailViewModel.UiEvent.NavigateUp -> { | ||
navController.navigateUp() | ||
} | ||
} | ||
} | ||
} | ||
|
||
Scaffold( | ||
snackbarHost = { | ||
SnackbarHost(hostState = snackbarHostState) | ||
}, | ||
topBar = { | ||
TopAppBar( | ||
title = { | ||
Text(text = "EMI Detail") | ||
}, | ||
navigationIcon = { | ||
IconButton(onClick = { | ||
viewModel.onEvent(EMIDetailEvent.BackPressed) | ||
}) { | ||
Icon( | ||
Icons.Filled.ArrowBack, | ||
contentDescription = "Go Back" | ||
) | ||
} | ||
} | ||
) | ||
} | ||
) { paddingValues -> | ||
Column( | ||
modifier = Modifier | ||
.padding(paddingValues) | ||
.fillMaxWidth() | ||
.padding(16.dp) | ||
) { | ||
Text( | ||
text = viewModel.emi?.name?:"", | ||
style = MaterialTheme.typography.titleLarge, | ||
color = MaterialTheme.colorScheme.primary | ||
) | ||
Spacer(modifier = Modifier.height(8.dp)) | ||
Row( | ||
modifier = Modifier.fillMaxWidth(), | ||
horizontalArrangement = Arrangement.SpaceBetween, | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Text( | ||
text = CMUtils.currencyMask(viewModel.emi?.amount?:0.0F, viewModel.countryCode), | ||
style = MaterialTheme.typography.bodySmall, | ||
color = MaterialTheme.colorScheme.primary | ||
) | ||
viewModel.creditCard?.let { | ||
Text( | ||
text = "${it.cardName} (${it.last4Digits})", | ||
style = MaterialTheme.typography.bodySmall, | ||
) | ||
} | ||
} | ||
Spacer(modifier = Modifier.height(8.dp)) | ||
Text( | ||
text = CMUtils.currencyMask(viewModel.emiAmount, viewModel.countryCode), | ||
style = MaterialTheme.typography.bodySmall, | ||
color = MaterialTheme.colorScheme.primary | ||
) | ||
Spacer(modifier = Modifier.height(16.dp)) | ||
Divider() | ||
LazyColumn { | ||
itemsIndexed(viewModel.schedule) {i, schedule -> | ||
Row( | ||
modifier = Modifier.fillMaxWidth().padding(top = 16.dp), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Text(text = (i+1).toString()) | ||
Column( | ||
modifier = Modifier | ||
.padding(start = 16.dp) | ||
.weight(1f) | ||
) { | ||
Text( | ||
text = CMUtils.currencyMask(schedule.principal, viewModel.countryCode), | ||
style = MaterialTheme.typography.bodySmall | ||
) | ||
Spacer(modifier = Modifier.height(8.dp)) | ||
Text( | ||
text = CMUtils.currencyMask(schedule.interest, viewModel.countryCode), | ||
style = MaterialTheme.typography.bodySmall | ||
) | ||
} | ||
Text( | ||
text = CMUtils.currencyMask(schedule.amount, viewModel.countryCode), | ||
color = MaterialTheme.colorScheme.primary | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
app/src/main/java/com/suyash/creditmanager/presentation/emi_details/EMIDetailViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.suyash.creditmanager.presentation.emi_details | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.suyash.creditmanager.data.settings.AppSettings | ||
import com.suyash.creditmanager.domain.model.CreditCard | ||
import com.suyash.creditmanager.domain.model.EMI | ||
import com.suyash.creditmanager.domain.use_case.CreditCardUseCases | ||
import com.suyash.creditmanager.domain.use_case.EMIUseCases | ||
import com.suyash.creditmanager.domain.util.DateFormat | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
import kotlin.math.pow | ||
|
||
@HiltViewModel | ||
class EMIDetailViewModel @Inject constructor( | ||
private val creditCardUseCases: CreditCardUseCases, | ||
private val emiUseCases: EMIUseCases, | ||
private val dataStore: DataStore<AppSettings>, | ||
savedStateHandle: SavedStateHandle | ||
): ViewModel() { | ||
|
||
var emi: EMI? = null | ||
var creditCard: CreditCard? = null | ||
var countryCode: String = "IN" | ||
var dateFormat: DateFormat = DateFormat.DDMMYYYY | ||
var emiAmount: Float = 0.0F | ||
var schedule: List<EMISchedule> = emptyList() | ||
|
||
private val _eventFlow = MutableSharedFlow<UiEvent>() | ||
val eventFlow = _eventFlow.asSharedFlow() | ||
|
||
init { | ||
savedStateHandle.get<Int>("emiId")?.let { | ||
viewModelScope.launch { | ||
emiUseCases.getEMI(it)?.also { e -> | ||
emi = e | ||
calculateEMISchedule(e.amount, e.rate, e.months) | ||
} | ||
emi?.card?.let { | ||
creditCardUseCases.getCreditCard(it)?.also { cc -> | ||
creditCard = cc | ||
} | ||
} | ||
dataStore.data.collect { | ||
countryCode = it.countryCode | ||
dateFormat = it.dateFormat | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun onEvent(event: EMIDetailEvent) { | ||
when (event) { | ||
is EMIDetailEvent.BackPressed -> { | ||
viewModelScope.launch { | ||
_eventFlow.emit(UiEvent.NavigateUp) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun calculateEMISchedule(startingAmount: Float, interestRate: Float, tenure: Int) { | ||
val schedule: MutableList<EMISchedule> = mutableListOf() | ||
var principal: Float = startingAmount | ||
val monthlyRate: Float = interestRate/1200 | ||
val emi: Float = startingAmount*monthlyRate*(1+monthlyRate).pow(tenure)/((1+monthlyRate).pow(tenure)-1) | ||
for (i in 1..tenure) { | ||
val interest = principal*(monthlyRate) | ||
val principalPaid: Float = emi - interest | ||
principal -= principalPaid | ||
schedule.add(EMISchedule(principalPaid, interest, principal)) | ||
} | ||
this.emiAmount = emi | ||
this.schedule = schedule.toList() | ||
} | ||
|
||
data class EMISchedule( | ||
val principal: Float, | ||
val interest: Float, | ||
val amount: Float | ||
) | ||
|
||
sealed class UiEvent { | ||
data class ShowSnackbar(val message: String): UiEvent() | ||
data object NavigateUp: UiEvent() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters