Skip to content

Instantly share code, notes, and snippets.

View Aldikitta's full-sized avatar
🏠
Working from home

Muh. Aldi Andi kitta Aldikitta

🏠
Working from home
View GitHub Profile
@Aldikitta
Aldikitta / PagingCompose.kt
Created August 14, 2023 07:42
PagingCompose
// this is for no filter, just basic paging
val maritalStatusVal: StateFlow<CreateScreenUiState> = flow {
val maritalStatusData = kmbMasterDataUseCase.getMaritalStatus(
body = KmbMasterDataRequestModel.KmbMasterDataRequest()
).cachedIn(viewModelScope)
emit(CreateScreenUiState(maritalStatus = maritalStatusData))
}.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5_000),
CreateScreenUiState.default
@Aldikitta
Aldikitta / BaseResponse.kt
Last active August 7, 2023 09:00
The way using BaseResponse with mapper
// BaseResponseDto
data class KreditPlusBaseResponseDto<DATA>(
@SerializedName("code")
val code: Int? = 0,
@SerializedName("data")
val data: DATA?,
@SerializedName("errors")
val errors: HashMap<String, String>? = hashMapOf(),
@SerializedName("status")
// Dto Class
sealed interface MockDraftDetailResponseDto {
data class MockDraftDetail(
@SerializedName("code")
val code: Int,
@SerializedName("data")
val data: Data,
@SerializedName("errors")
val errors: Errors? = null,
1. collectLatest() dont stop collecting data when onPause() lifecycle, but collect() did stop
//RESOURCE CLASS
package com.kreditplus.sally.core.utils.network
import com.google.gson.Gson
import com.google.gson.JsonParser
import com.google.gson.annotations.SerializedName
import com.kreditplus.sally.core.base.dto.KreditPlusBaseResponseDto
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
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,
val getListAgent: StateFlow<ListAgentVisit> = flow {
val agentList = useCase.getListAgentVisitWithFullResponse(
body = GetAgentVisitRequest(
branchCode = useCase.getAccountInfo().branchCode,
moId = useCase.getAccountInfo().employeeId,
skip = 1,
search = "",
prospectConsument = "",
agentProfessionCode = "",
startDate = "",
override fun onViewCreated() {
observeVisitDetail()
}
private fun observeVisitDetail() {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.agentDetail?.collect { visitingUiState ->
when (visitingUiState) {
is VisitingDetailUiState.Success -> {
val visitId = savedStateHandle.get<Int>("visitId")
val agentDetail: StateFlow<VisitingDetailUiState>? =
visitId?.let {
useCase.getDetailAgentVisitFullResponseFlow(id = it).map { visitDetail ->
Log.d("MYTAG", ": got exec")
when (visitDetail) {
is SallyResponseResource.Loading -> {
VisitingDetailUiState.Loading
}
override fun onViewCreated() {
viewModel.getAgentVisitDetailFullResponseFlow()
observeVisitDetail()
}