Skip to content

Instantly share code, notes, and snippets.

@Aldikitta
Created June 18, 2023 05:30
Show Gist options
  • Save Aldikitta/5b6d3922b702be34c95298a2cb22654b to your computer and use it in GitHub Desktop.
Save Aldikitta/5b6d3922b702be34c95298a2cb22654b to your computer and use it in GitHub Desktop.
private val filterAgentState = MutableStateFlow(FilterAgentState.default)
@OptIn(ExperimentalCoroutinesApi::class)
val getListAgent: StateFlow<ListAgentVisit> = filterAgentState.flatMapLatest { filterState ->
flow {
val agentList = useCase.getListAgentVisitWithFullResponse(
body = GetAgentVisitRequest(
branchCode = useCase.getAccountInfo().branchCode,
moId = useCase.getAccountInfo().employeeId,
skip = 1,
search = filterState.search,
prospectConsument = filterState.prospectConsument,
agentProfessionCode = filterState.agentProfessionCode,
startDate = filterState.startDate,
endDate = filterState.endDate
)
).cachedIn(viewModelScope)
emit(ListAgentVisit(agentList = agentList))
}
}.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5_000),
ListAgentVisit.default
)
fun updateFilterState(
search: String? = null,
prospectConsument: String? = null,
agentProfessionCode: String? = null,
startDate: String? = null,
endDate: String? = null
) {
val currentFilter = filterAgentState.value
val updatedFilter = currentFilter.copy(
search = search ?: currentFilter.search,
prospectConsument = prospectConsument ?: currentFilter.prospectConsument,
agentProfessionCode = agentProfessionCode ?: currentFilter.agentProfessionCode,
startDate = startDate ?: currentFilter.startDate,
endDate = endDate ?: currentFilter.endDate
)
filterAgentState.value = updatedFilter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment