स्निपेट·मुफ़्त
Kotlin और Android स्निपेट
Android ऐप्स के लिए 20 Kotlin स्निपेट: sealed UI स्टेट, StateFlow वाला Hilt ViewModel, कोरूटीन, Room एंटिटी और DAO, और Compose स्टेट व लिस्ट। किसी .kt फ़ाइल में uistate या hiltvm जैसा प्रीफ़िक्स लिखें।
मुफ़्त में इंस्टॉल करें।
अगर आपके Android डिवाइस पर PocketCode इंस्टॉल है, तो यह पेज ऐप में खुलेगा।
प्रीव्यू
20 में से 5
Sealed UI stateuistate
Loading, success and error states for a screen
sealed interface ${1:Ui}State {
data object Loading : ${1:Ui}State
data class Success(val ${2:data}: ${3:String}) : ${1:Ui}State
data class Error(val message: String) : ${1:Ui}State
}Hilt ViewModel with StateFlowhiltvm
ViewModel with an injected repository and a read-only state
@HiltViewModel
class ${1:Feature}ViewModel @Inject constructor(
private val ${2:repository}: ${3:Repository},
) : ViewModel() {
private val _state = MutableStateFlow(${1:Feature}UiState())
val state: StateFlow<${1:Feature}UiState> = _state.asStateFlow()
fun ${4:load}() {
viewModelScope.launch {
$0
}
}
}Update StateFlow statestupd
Change a field of the state with update and copy
_state.update { it.copy(${1:isLoading = true}) }Collect state in Composecollectst
Read a StateFlow in a composable, aware of the lifecycle
val ${1:state} by ${2:viewModel}.${3:state}.collectAsStateWithLifecycle()Switch to the IO dispatcherwithio
Suspend function that runs blocking work off the main thread
suspend fun ${1:loadData}(): ${2:String} = withContext(Dispatchers.IO) {
${3:TODO()}
}1.0.0 में बदलाव
पहला संस्करण।