Skip to content

Commit

Permalink
feat: implement navigation in report module
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-gupta99 committed Aug 15, 2024
1 parent 774ce74 commit e1feb06
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,8 @@ object Constants {
const val PASSCODE = "preferences_mifos_passcode_string"
const val THEME = "theme"
const val LANGUAGE = "language_type"

// Compose Navigation KEY
const val REPORT_TYPE_ITEM = "report_type_item"
const val REPORT_PARAMETER_RESPONSE = "report_parameter_response"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.mifos.feature.report.navigation

import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import androidx.navigation.navigation
import com.google.gson.Gson
import com.mifos.core.common.utils.Constants
import com.mifos.core.objects.runreports.FullParameterListResponse
import com.mifos.core.objects.runreports.client.ClientReportTypeItem
import com.mifos.feature.report.report.ReportScreen
import com.mifos.feature.report.report_detail.ReportDetailScreen
import com.mifos.feature.report.run_report.RunReportScreen

fun NavGraphBuilder.reportNavGraph(
navController: NavController
) {
navigation(
startDestination = ReportScreens.RunReportScreen.route,
route = "run_report_route"
) {
runReportScreenRoute(
onBackPressed = navController::popBackStack,
onReportSelected = navController::navigateReportDetailsScreen
)
reportDetailsScreenRoute(
onBackPressed = navController::popBackStack,
runReport = navController::navigateReportScreens
)
reportScreenRoute(
onBackPressed = navController::popBackStack
)
}
}

fun NavGraphBuilder.runReportScreenRoute(
onBackPressed: () -> Unit,
onReportSelected: (ClientReportTypeItem) -> Unit
) {
composable(
route = ReportScreens.RunReportScreen.route
) {
RunReportScreen(
onBackPressed = onBackPressed,
onReportClick = onReportSelected
)
}
}

fun NavGraphBuilder.reportDetailsScreenRoute(
onBackPressed: () -> Unit,
runReport: (FullParameterListResponse) -> Unit
) {
composable(
route = ReportScreens.ReportDetailScreen.route,
arguments = listOf(navArgument(Constants.REPORT_TYPE_ITEM, builder = {type = NavType.StringType}))
) {
ReportDetailScreen(
onBackPressed = onBackPressed,
runReport = runReport
)
}
}

fun NavGraphBuilder.reportScreenRoute(
onBackPressed: () -> Unit
) {
composable(
route = ReportScreens.ReportScreen.route,
arguments = listOf(navArgument(Constants.REPORT_PARAMETER_RESPONSE, builder = {type = NavType.StringType}))
) {
ReportScreen(
onBackPressed = onBackPressed
)
}
}

fun NavController.navigateReportDetailsScreen(clientReportTypeItem: ClientReportTypeItem) {
val arg = Gson().toJson(clientReportTypeItem)
navigate(ReportScreens.ReportDetailScreen.argument(arg))
}

fun NavController.navigateReportScreens(fullParameterListResponse: FullParameterListResponse) {
val arg = Gson().toJson(fullParameterListResponse)
navigate(ReportScreens.ReportScreen.argument(arg))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mifos.feature.report.navigation

import com.mifos.core.common.utils.Constants

sealed class ReportScreens(val route: String) {

data object RunReportScreen : ReportScreens(route = "run_report_screen")

data object ReportDetailScreen :
ReportScreens(route = "report_detail_screen/{${Constants.REPORT_TYPE_ITEM}}") {
fun argument(reportTypeItem: String) = "report_detail_screen/${reportTypeItem}"
}

data object ReportScreen :
ReportScreens(route = "report_screen/{${Constants.REPORT_PARAMETER_RESPONSE}}") {
fun argument(reportParameter: String) = "report_screen/${reportParameter}"
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ import kotlinx.coroutines.launch

@Composable
fun ReportScreen(
report: FullParameterListResponse,
onBackPressed: () -> Unit
) {

val viewModel: ReportViewModel = hiltViewModel()
val report = viewModel.report
val state by viewModel.reportUiState.collectAsStateWithLifecycle()
val context = LocalContext.current

Expand Down Expand Up @@ -147,7 +147,7 @@ fun ReportScreen(
)
)
report.data.map { it.row }.forEach {
Text(text = it[index] ?: "-", modifier = Modifier.padding(8.dp))
Text(text = it[index], modifier = Modifier.padding(8.dp))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.mifos.feature.report.report

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.mifos.core.common.utils.Constants
import com.mifos.core.objects.runreports.FullParameterListResponse
import com.mifos.feature.report.R
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -14,7 +17,14 @@ import java.io.FileWriter
import javax.inject.Inject

@HiltViewModel
class ReportViewModel @Inject constructor() : ViewModel() {
class ReportViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle
) : ViewModel() {

private val reportParameterString =
savedStateHandle.getStateFlow(key = Constants.REPORT_PARAMETER_RESPONSE, initialValue = "")
val report: FullParameterListResponse =
Gson().fromJson(reportParameterString.value, FullParameterListResponse::class.java)

private val _reportUiState = MutableStateFlow<ReportUiState>(ReportUiState.Initial)
val reportUiState = _reportUiState.asStateFlow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ import com.mifos.feature.report.R

@Composable
fun ReportDetailScreen(
reportItem: ClientReportTypeItem,
onBackPressed: () -> Unit,
runReport: (FullParameterListResponse) -> Unit
) {

val viewModel: ReportDetailViewModel = hiltViewModel()
val reportItem = viewModel.reportItem
val state by viewModel.reportDetailUiState.collectAsStateWithLifecycle()
val reportParameterList by viewModel.reportParameterList.collectAsStateWithLifecycle()
val reportDetail by viewModel.reportDetail.collectAsStateWithLifecycle()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.mifos.feature.report.report_detail

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.mifos.core.common.utils.Constants
import com.mifos.core.common.utils.Resource
import com.mifos.core.domain.use_cases.GetReportFullParameterListUseCase
import com.mifos.core.domain.use_cases.GetReportParameterDetailsUseCase
Expand All @@ -10,6 +13,7 @@ import com.mifos.core.domain.use_cases.GetRunReportProductUseCase
import com.mifos.core.domain.use_cases.GetRunReportWithQueryUseCase
import com.mifos.core.objects.runreports.DataRow
import com.mifos.core.objects.runreports.FullParameterListResponse
import com.mifos.core.objects.runreports.client.ClientReportTypeItem
import com.mifos.feature.report.R
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
Expand All @@ -24,9 +28,15 @@ class ReportDetailViewModel @Inject constructor(
private val getReportParameterDetailsUseCase: GetReportParameterDetailsUseCase,
private val getRunReportProductUseCase: GetRunReportProductUseCase,
private val getRunReportWithQueryUseCase: GetRunReportWithQueryUseCase,
private val getRunReportOfficesUseCase: GetRunReportOfficesUseCase
private val getRunReportOfficesUseCase: GetRunReportOfficesUseCase,
private val savedStateHandle: SavedStateHandle
) : ViewModel() {

private val reportName =
savedStateHandle.getStateFlow(key = Constants.REPORT_TYPE_ITEM, initialValue = "")
val reportItem: ClientReportTypeItem =
Gson().fromJson(reportName.value, ClientReportTypeItem::class.java)

private val _reportDetailUiState =
MutableStateFlow<ReportDetailUiState>(ReportDetailUiState.Loading)
val reportDetailUiState = _reportDetailUiState.asStateFlow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.mifos.feature.groups.navigation.GROUP_LIST_SCREEN_ROUTE
import com.mifos.feature.individual_collection_sheet.navigation.GENERATE_COLLECTION_SHEET_SCREEN_ROUTE
import com.mifos.feature.individual_collection_sheet.navigation.INDIVIDUAL_COLLECTION_SHEET_SCREEN_ROUTE
import com.mifos.feature.path_tracking.navigation.PATH_TRACKING_SCREEN_ROUTE
import com.mifos.feature.report.navigation.RUN_REPORTS_SCREEN_ROUTE
import com.mifos.feature.search.Navigation.SEARCH_SCREEN_ROUTE
import com.mifos.feature.settings.navigation.SETTINGS_SCREEN_ROUTE

Expand Down Expand Up @@ -72,7 +71,7 @@ sealed class HomeDestinationsScreen(

data object RunReportsScreen : HomeDestinationsScreen(
title = "Run Reports",
route = RUN_REPORTS_SCREEN_ROUTE,
route = "run_report_screen",
icon = Icons.Rounded.Task
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.mifos.feature.groups.navigation.groupListScreen
import com.mifos.feature.individual_collection_sheet.navigation.generateCollectionSheetScreen
import com.mifos.feature.individual_collection_sheet.navigation.individualCollectionSheetScreen
import com.mifos.feature.path_tracking.navigation.pathTrackingScreen
import com.mifos.feature.report.navigation.runReportsScreen
import com.mifos.feature.report.navigation.reportNavGraph
import com.mifos.feature.search.Navigation.SEARCH_SCREEN_ROUTE
import com.mifos.feature.search.Navigation.searchScreen
import com.mifos.feature.settings.navigation.settingsScreen
Expand Down Expand Up @@ -58,6 +58,10 @@ fun Navigation(
addSavingsAccount = { }
)

reportNavGraph(
navController = navController
)

groupListScreen(
paddingValues = padding,
onAddGroupClick = {},
Expand All @@ -84,11 +88,6 @@ fun Navigation(
onBackPressed = { navController.popBackStack() }
)

runReportsScreen(
onBackPressed = { navController.popBackStack() },
onReportClick = { }
)

pathTrackingScreen(
onBackPressed = { navController.popBackStack() }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ReportFragment : MifosBaseFragment() {
report = arg.respose
return ComposeView(requireContext()).apply {
setContent {
ReportScreen(report = report, onBackPressed = {
ReportScreen(onBackPressed = {
findNavController().popBackStack()
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class ReportDetailFragment : MifosBaseFragment() {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
ReportDetailScreen(
reportItem = reportItem,
onBackPressed = {
findNavController().popBackStack()
},
Expand Down

0 comments on commit e1feb06

Please sign in to comment.