Skip to content

Commit

Permalink
refactor: guarantor cleanup (#2654)
Browse files Browse the repository at this point in the history
  • Loading branch information
AvneetSingh2001 committed Jul 10, 2024
1 parent 246eab9 commit e80d7cc
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 32 deletions.
17 changes: 15 additions & 2 deletions app/src/main/java/org/mifos/mobile/navigation/MifosNavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,38 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import org.mifos.mobile.feature.auth.navigation.AuthenticationNavigation
import org.mifos.mobile.feature.auth.navigation.authenticationNavGraph
import org.mifos.mobile.feature.guarantor.navigation.GuarantorNavigation
import org.mifos.mobile.feature.guarantor.navigation.GuarantorRoute
import org.mifos.mobile.feature.guarantor.navigation.guarantorNavGraph
import org.mifos.mobile.ui.activities.PassCodeActivity

@Composable
fun RootNavGraph(
navController: NavHostController,
startDestination: String,
nestedStartDestination: String,
loanId: Long = -1, // we will remove this and pass this argument when its caller function is called in this NavGraph,
navigateBack: () -> Unit = {} // remove this as well in future
) {
val context = LocalContext.current

NavHost(
navController = navController,
startDestination = startDestination
startDestination = startDestination,
) {
authenticationNavGraph(
navController = navController,
navigateBack = { navController.popBackStack() },
startDestination = AuthenticationNavigation.Login.route,
startDestination = nestedStartDestination,
navigateToPasscodeScreen = { startPassCodeActivity(context = context) }
)

guarantorNavGraph(
startDestination = nestedStartDestination,
navController = navController,
navigateBack = navigateBack,
loanId = loanId
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.navigation.compose.rememberNavController
import org.mifos.mobile.feature.guarantor.navigation.GuarantorScreen
import org.mifos.mobile.feature.guarantor.navigation.SetUpGuarantorNavGraph
import org.mifos.mobile.feature.guarantor.navigation.GuarantorNavigation
import dagger.hilt.android.AndroidEntryPoint
import org.mifos.mobile.core.common.Constants.LOAN_ID
import org.mifos.mobile.core.ui.theme.MifosMobileTheme
import org.mifos.mobile.feature.guarantor.navigation.GuarantorRoute
import org.mifos.mobile.navigation.RootNavGraph

@AndroidEntryPoint
class GuarantorActivity : ComponentActivity() {
Expand All @@ -20,9 +21,10 @@ class GuarantorActivity : ComponentActivity() {
setContent {
MifosMobileTheme {
val navController = rememberNavController()
SetUpGuarantorNavGraph(
startDestination = GuarantorScreen.GuarantorList.route,
RootNavGraph(
startDestination = GuarantorRoute.GUARANTOR_NAVIGATION_ROUTE,
navController = navController,
nestedStartDestination = GuarantorNavigation.GuarantorList.route,
loanId = loanId,
navigateBack = { finish() }
)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/mifos/mobile/ui/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.activity.enableEdgeToEdge
import androidx.navigation.compose.rememberNavController
import dagger.hilt.android.AndroidEntryPoint
import org.mifos.mobile.core.ui.theme.MifosMobileTheme
import org.mifos.mobile.feature.auth.navigation.AuthenticationNavigation
import org.mifos.mobile.feature.auth.navigation.AuthenticationRoute
import org.mifos.mobile.navigation.RootNavGraph
import org.mifos.mobile.ui.activities.base.BaseActivity
Expand All @@ -27,6 +28,7 @@ class LoginActivity : BaseActivity() {
RootNavGraph(
startDestination = AuthenticationRoute.AUTH_NAVIGATION_ROUTE,
navController = navController,
nestedStartDestination = AuthenticationNavigation.Login.route,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package org.mifos.mobile.feature.guarantor.navigation

import androidx.compose.runtime.Composable
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
import androidx.navigation.navArgument
import org.mifos.mobile.feature.guarantor.screens.guarantor_add.AddGuarantorScreen
import org.mifos.mobile.feature.guarantor.screens.guarantor_details.GuarantorDetailScreen
import org.mifos.mobile.feature.guarantor.screens.guarantor_list.GuarantorListScreen
import org.mifos.mobile.core.common.Constants.INDEX
import org.mifos.mobile.core.common.Constants.LOAN_ID

@Composable
fun SetUpGuarantorNavGraph(
startDestination: String, navController: NavHostController, loanId: Long, navigateBack: () -> Unit
fun NavGraphBuilder.guarantorNavGraph(
startDestination: String,
navController: NavHostController,
navigateBack: () -> Unit,
loanId: Long
) {
NavHost(navController = navController, startDestination = startDestination) {

navigation(
startDestination = startDestination,
route = GuarantorRoute.GUARANTOR_NAVIGATION_ROUTE
) {
addGuarantorRoute(
navigateBack = { navController.popBackStack() }
)
Expand All @@ -27,11 +30,11 @@ fun SetUpGuarantorNavGraph(
loanId = loanId,
navigateBack = navigateBack,
addGuarantor = {
navController.navigate(GuarantorScreen.GuarantorAdd.passArguments(-1, loanId))
navController.navigate(GuarantorNavigation.GuarantorAdd.passArguments(-1, loanId))
},
onGuarantorClicked = { index->
onGuarantorClicked = { index, loanId ->
navController.navigate(
GuarantorScreen.GuarantorDetails.passArguments(
GuarantorNavigation.GuarantorDetails.passArguments(
index = index,
loanId = loanId
)
Expand All @@ -43,7 +46,7 @@ fun SetUpGuarantorNavGraph(
navigateBack = { navController.popBackStack() },
updateGuarantor = { index, loanId ->
navController.navigate(
GuarantorScreen.GuarantorAdd.passArguments(
GuarantorNavigation.GuarantorAdd.passArguments(
index = index,
loanId = loanId
)
Expand All @@ -57,17 +60,17 @@ fun NavGraphBuilder.listGuarantorRoute(
loanId: Long,
navigateBack: () -> Unit,
addGuarantor: (Long) -> Unit,
onGuarantorClicked: (Int) -> Unit
onGuarantorClicked: (Int, Long) -> Unit
) {
composable(
route = GuarantorScreen.GuarantorList.route,
route = GuarantorNavigation.GuarantorList.route,
arguments = listOf(
navArgument(name = LOAN_ID) { type = NavType.LongType; defaultValue = loanId }
)
) {
GuarantorListScreen(
navigateBack = navigateBack,
addGuarantor = { addGuarantor(loanId) },
addGuarantor = addGuarantor,
onGuarantorClicked = onGuarantorClicked
)
}
Expand All @@ -78,7 +81,7 @@ fun NavGraphBuilder.detailGuarantorRoute(
updateGuarantor: (index: Int, loanId: Long) -> Unit
) {
composable(
route = GuarantorScreen.GuarantorDetails.route,
route = GuarantorNavigation.GuarantorDetails.route,
arguments = listOf(
navArgument(name = INDEX) { type = NavType.IntType },
navArgument(name = LOAN_ID) { type = NavType.LongType }
Expand All @@ -95,7 +98,7 @@ fun NavGraphBuilder.addGuarantorRoute(
navigateBack: () -> Unit
) {
composable(
route = GuarantorScreen.GuarantorAdd.route,
route = GuarantorNavigation.GuarantorAdd.route,
arguments = listOf(
navArgument(name = INDEX) { type = NavType.IntType; defaultValue = -1 },
navArgument(name = LOAN_ID) { type = NavType.LongType }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import org.mifos.mobile.feature.guarantor.navigation.GuarantorRoute.GUARANTOR_AD
import org.mifos.mobile.feature.guarantor.navigation.GuarantorRoute.GUARANTOR_DETAIL_SCREEN_ROUTE_BASE
import org.mifos.mobile.feature.guarantor.navigation.GuarantorRoute.GUARANTOR_LIST_SCREEN_ROUTE_BASE

sealed class GuarantorScreen(val route: String) {
data object GuarantorList : GuarantorScreen(
sealed class GuarantorNavigation(val route: String) {
data object GuarantorList : GuarantorNavigation(
route = "$GUARANTOR_LIST_SCREEN_ROUTE_BASE/{$LOAN_ID}"
)

data object GuarantorDetails : GuarantorScreen(
data object GuarantorDetails : GuarantorNavigation(
route = "$GUARANTOR_DETAIL_SCREEN_ROUTE_BASE/{$LOAN_ID}/{$INDEX}"
) {
fun passArguments(index: Int, loanId: Long) = "$GUARANTOR_DETAIL_SCREEN_ROUTE_BASE/$loanId/$index"
}

data object GuarantorAdd : GuarantorScreen(
data object GuarantorAdd : GuarantorNavigation(
route = "$GUARANTOR_ADD_SCREEN_ROUTE_BASE/{$LOAN_ID}/{$INDEX}"
) {
fun passArguments(index: Int, loanId: Long) = "$GUARANTOR_ADD_SCREEN_ROUTE_BASE/$loanId/$index"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.mifos.mobile.feature.guarantor.navigation

object GuarantorRoute {
const val GUARANTOR_NAVIGATION_ROUTE = "guarantor_route"
const val GUARANTOR_LIST_SCREEN_ROUTE_BASE = "guarantor_list_screen_route"
const val GUARANTOR_DETAIL_SCREEN_ROUTE_BASE = "guarantor_detail_screen_route"
const val GUARANTOR_ADD_SCREEN_ROUTE_BASE = "guarantor_add_screen_route"
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ import org.mifos.mobile.feature.guarantor.R
fun GuarantorListScreen(
viewModel: GuarantorListViewModel = hiltViewModel(),
navigateBack: () -> Unit,
addGuarantor: () -> Unit,
onGuarantorClicked: (Int) -> Unit
addGuarantor: (Long) -> Unit,
onGuarantorClicked: (Int, Long) -> Unit
) {
val uiState = viewModel.guarantorUiState.collectAsStateWithLifecycle()
GuarantorListScreen(
uiState = uiState.value,
navigateBack = navigateBack,
addGuarantor = addGuarantor,
onGuarantorClicked = onGuarantorClicked
addGuarantor = { addGuarantor(viewModel.loanId.value) },
onGuarantorClicked = { onGuarantorClicked(it, viewModel.loanId.value) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import org.mifos.mobile.core.common.Constants
import org.mifos.mobile.core.common.Constants.LOAN_ID
import org.mifos.mobile.core.data.repositories.GuarantorRepository
import org.mifos.mobile.core.model.entity.guarantor.GuarantorPayload
Expand All @@ -26,8 +27,9 @@ class GuarantorListViewModel @Inject constructor(
private val guarantorRepositoryImp: GuarantorRepository
) : ViewModel() {

val guarantorUiState =
savedStateHandle.getStateFlow<Long>(key = LOAN_ID, initialValue = -1)
val loanId = savedStateHandle.getStateFlow<Long>(key = Constants.LOAN_ID, initialValue = -1)

val guarantorUiState = loanId
.flatMapLatest { loanId ->
guarantorRepositoryImp.getGuarantorList(loanId = loanId)
}
Expand Down

0 comments on commit e80d7cc

Please sign in to comment.