Last active
July 24, 2023 11:05
-
-
Save virendersran01/569bb3615bf5be996f53ed3fae1f7626 to your computer and use it in GitHub Desktop.
Code Templates for android studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//initViewsAndListeners | |
private fun initViews() { | |
with(binding) { | |
} | |
} | |
private fun initListeners() { | |
with(binding) { | |
} | |
} | |
//fetchAndObserveData | |
private fun fetch$FUNCNAME$(){ | |
if (requireActivity().isNetworkConnected()){ | |
}else{ | |
requireActivity().toastInternetError() | |
} | |
} | |
private fun observerUiState$FUNCNAME$() { | |
viewLifecycleOwner.lifecycleScope.launch { | |
repeatOnLifecycle(Lifecycle.State.RESUMED) { | |
$VIEWMODEL$.collect { uiState -> | |
when (uiState) { | |
UiState.Init -> {} | |
is UiState.Loading -> handleUiStateLoading(true) | |
is UiState.Error -> handleUiStateError( | |
uiState.exception, | |
uiState.errorMessage | |
) | |
is UiState.Success -> handleUiStateSuccess(uiState.data) | |
} | |
} | |
} | |
} | |
} | |
private fun handleUiStateLoading(isLoading: Boolean) { | |
} | |
private fun handleUiStateError(exception: Exception, errorMessage: String) { | |
handleUiStateLoading(false) | |
if (exception !is CancellationException) { | |
requireActivity().toast(errorMessage) | |
} | |
} | |
private fun handleUiStateSuccess(data: Any?) { | |
handleUiStateLoading(false) | |
data?.let { | |
} ?: run{ | |
} | |
} | |
//stateFlowWithJob | |
private val _uiState$VAR$ = | |
MutableStateFlow<UiState<Any>>(UiState.Init) | |
val uiState$VAR$ = _uiState$VAR$.asStateFlow() | |
private var job$VAR$: Job? = null | |
fun fetch$VAR$( | |
) { | |
job$VAR$?.cancel() | |
job$VAR$ = viewModelScope.launch { | |
try { | |
_uiState$VAR$.update { UiState.Loading } | |
$REPO$.collect { apiResult -> | |
when (apiResult) { | |
is ApiCallResult.Error -> _uiState$VAR$.update { | |
UiState.Error( | |
exception = apiResult.exception, | |
errorMessage = apiResult.errorMessage | |
) | |
} | |
is ApiCallResult.Success -> _uiState$VAR$.update { | |
UiState.Success( | |
data = apiResult.data | |
) | |
} | |
} | |
} | |
} catch (exception: Exception) { | |
_uiState$VAR$.update { | |
UiState.Error( | |
exception = exception, | |
errorMessage = exception.message.toString() | |
) | |
} | |
} | |
} | |
} | |
//sharedFlowWithJob | |
private val _uiState$VAR$ = MutableSharedFlow<UiState<Any>>() | |
val uiState$VAR$ = _uiState$VAR$.asSharedFlow() | |
private var job$VAR$: Job? = null | |
fun fetch$VAR$( | |
) { | |
job$VAR$?.cancel() | |
job$VAR$ = viewModelScope.launch { | |
try { | |
_uiState$VAR$.emit(UiState.Loading) | |
$REPO$.collect { apiResult -> | |
when (apiResult) { | |
is ApiCallResult.Error -> _uiState$VAR$.emit( | |
UiState.Error( | |
exception = apiResult.exception, | |
errorMessage = apiResult.errorMessage | |
) | |
) | |
is ApiCallResult.Success -> _uiState$VAR$.emit( | |
UiState.Success( | |
data = apiResult.data | |
) | |
) | |
} | |
} | |
} catch (exception: Exception) { | |
_uiState$VAR$.emit( | |
UiState.Error( | |
errorMessage = exception.message.toString(), | |
exception = exception | |
) | |
) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment