Skip to content

Commit

Permalink
MIFOSAC-257: implemented compose navigation in note module (#2192)
Browse files Browse the repository at this point in the history
navigation

MIFOSAC-257: implemented compose navigation
  • Loading branch information
itsPronay authored Aug 16, 2024
1 parent 634fba5 commit 05ab56e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 32 deletions.
12 changes: 4 additions & 8 deletions feature/note/src/main/java/com/mifos/feature/note/NoteScreen.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mifos.feature.note

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
Expand Down Expand Up @@ -42,25 +43,20 @@ import com.mifos.core.ui.components.MifosEmptyUi

@Composable
fun NoteScreen(
entityId: Int,
entityType: String?,
viewModel: NoteViewModel = hiltViewModel(),
onBackPressed: () -> Unit
) {
val viewModel: NoteViewModel = hiltViewModel()
val uiState by viewModel.noteUiState.collectAsStateWithLifecycle()
val isRefreshing by viewModel.isRefreshing.collectAsStateWithLifecycle()

LaunchedEffect(key1 = Unit) {
viewModel.loadNote(
type = entityType,
id = entityId
)
viewModel.loadNote()
}

NoteScreen(
uiState = uiState,
onBackPressed = onBackPressed,
refresh = { viewModel.refresh(entityType, entityId) },
refresh = { viewModel.refresh() },
isRefreshing = isRefreshing
)
}
Expand Down
28 changes: 15 additions & 13 deletions feature/note/src/main/java/com/mifos/feature/note/NoteViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
package com.mifos.feature.note

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import android.util.Log
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mifos.core.data.repository.NoteRepository
import com.mifos.core.common.utils.Constants
import com.mifos.core.data.repository_imp.NoteRepositoryImp
import com.mifos.core.objects.noncore.Note
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import javax.inject.Inject

@HiltViewModel
class NoteViewModel @Inject constructor(private val repository: NoteRepositoryImp) : ViewModel() {
class NoteViewModel @Inject constructor(
private val repository: NoteRepositoryImp,
savedStateHandle: SavedStateHandle
) : ViewModel() {

val entityId = savedStateHandle.getStateFlow(key = Constants.ENTITY_ID, initialValue = 0)
val entityType : StateFlow<String?> = savedStateHandle.getStateFlow(key = Constants.ENTITY_TYPE, initialValue = null)

private val _noteUiState = MutableStateFlow<NoteUiState>(NoteUiState.ShowProgressbar)
val noteUiState: StateFlow<NoteUiState> get() = _noteUiState

private val _isRefreshing = MutableStateFlow(false)
val isRefreshing: StateFlow<Boolean> get() = _isRefreshing.asStateFlow()

fun refresh(type: String?, id: Int) {
fun refresh() {
viewModelScope.launch {
_isRefreshing.emit(true)
loadNote(type, id)
loadNote()
}
}

/**
* This method load the Notes.
* Response: List<Note>
</Note> */
fun loadNote(type: String?, id: Int) {
fun loadNote() {
Log.d("NoteScreendebug1", "id ${entityId.value} type ${entityType.value}")
viewModelScope.launch {
_noteUiState.value = NoteUiState.ShowProgressbar
try {
val notes = withContext(Dispatchers.IO) {
repository.getNotes(type, id)
repository.getNotes(entityType.value, entityId.value)
}
if (notes.isNotEmpty()) {
_noteUiState.value = NoteUiState.ShowNote(notes)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mifos.feature.note.navigation

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.mifos.core.common.utils.Constants
import com.mifos.feature.note.NoteScreen

/**
* Created by Pronay Sarker on 17/08/2024 (12:05 AM)
*/
fun NavGraphBuilder.noteScreen(
onBackPressed: () -> Unit
) {
composable(
route = NoteScreens.NoteScreen.route,
arguments = listOf(
navArgument(name = Constants.ENTITY_ID, builder = { NavType.IntType }),
navArgument(name = Constants.ENTITY_TYPE, builder = { NavType.StringType })
)
) {
NoteScreen(
onBackPressed = onBackPressed
)
}
}

fun NavController.navigateToNoteScreen(entityId: Int, entityType: String?) {
navigate(NoteScreens.NoteScreen.argument(entityId, entityType))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mifos.feature.note.navigation

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

/**
* Created by Pronay Sarker on 17/08/2024 (12:05 AM)
*/
sealed class NoteScreens(val route: String) {
data object NoteScreen : NoteScreens(route = "note_screen/{${Constants.ENTITY_ID}}/{${Constants.ENTITY_TYPE}}") {
fun argument(entityId: Int, entityType: String?) = "note_screen/$entityId/$entityType"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import com.mifos.core.common.utils.Constants
import com.mifos.feature.about.navigation.aboutScreen
import com.mifos.feature.center.navigation.centerNavGraph
import com.mifos.feature.checker_inbox_task.navigation.checkerInboxTasksScreen
Expand All @@ -17,6 +18,8 @@ import com.mifos.feature.loan.navigation.addLoanAccountScreen
import com.mifos.feature.loan.navigation.loanNavGraph
import com.mifos.feature.loan.navigation.navigateToLoanAccountScreen
import com.mifos.feature.loan.navigation.navigateToLoanAccountSummaryScreen
import com.mifos.feature.note.navigation.navigateToNoteScreen
import com.mifos.feature.note.navigation.noteScreen
import com.mifos.feature.path_tracking.navigation.pathTrackingScreen
import com.mifos.feature.report.navigation.reportNavGraph
import com.mifos.feature.savings.navigation.addSavingsAccountScreen
Expand Down Expand Up @@ -46,10 +49,8 @@ fun Navigation(
addSavingsAccount = { navController.navigateToAddSavingsAccount(it, 0, false) },
documents = {},
moreClientInfo = {},
notes = {},
loanAccountSelected = {
navController.navigateToLoanAccountSummaryScreen(it)
},
notes = { navController.navigateToNoteScreen(it, Constants.ENTITY_TYPE_CLIENTS)},
loanAccountSelected = { navController.navigateToLoanAccountSummaryScreen(it) },
savingsAccountSelected = { id, type ->
navController.navigateToSavingsAccountSummaryScreen(id, type)
},
Expand All @@ -69,6 +70,10 @@ fun Navigation(
onDocumentsClicked = { _, _ -> }
)

noteScreen(
onBackPressed = navController::popBackStack
)

addLoanAccountScreen(
onBackPressed = navController::popBackStack,
dataTable = { _, _ -> }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class NoteFragment : MifosBaseFragment() {

private val arg: NoteFragmentArgs by navArgs()
// private val arg: NoteFragmentArgs by navArgs()

private var entityType: String? = null
private var entityId = 0
// private var entityType: String? = null
// private var entityId = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
entityId = arg.entiyId
entityType = arg.entityType
// entityId = arg.entiyId
// entityType = arg.entityType
}

override fun onCreateView(
Expand All @@ -38,8 +38,8 @@ class NoteFragment : MifosBaseFragment() {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
NoteScreen(
entityType = entityType,
entityId = entityId,
// entityType = entityType,
// entityId = entityId,
onBackPressed = { activity?.supportFragmentManager?.popBackStack() }
)
}
Expand Down

0 comments on commit 05ab56e

Please sign in to comment.