Skip to content

Instantly share code, notes, and snippets.

@tfcporciuncula
tfcporciuncula / CompatConnectedConstraint.kt
Created October 22, 2024 16:26
CompatConnectedConstraint.kt
/**
* On API 26+, whenever we pass [NetworkType.CONNECTED] as WorkManager's required network type, the
* constraint will only be met if the network is also validated
* ([NetworkCapabilities.NET_CAPABILITY_VALIDATED]). This takes advantage of the new
* [Constraints.Builder.setRequiredNetworkRequest] API to create a constraint that requires a
* network connection without requiring it to be validated.
*/
fun Constraints.Builder.setCompatConnectedConstraint(): Constraints.Builder {
// setRequiredNetworkRequest doesn't work before API 28, so if we're on API 26+, where we know
// WorkManager will also require the network to be validated, the best we can do is to simply
@tfcporciuncula
tfcporciuncula / DelegationDemo.kt
Last active September 16, 2024 19:07
DelegationDemo
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import de.zalando.lounge.compose.lux.Surface
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
@Composable
fun BottomSheet(
isShown: Boolean,
modifier: Modifier = Modifier,
onDismissRequest: () -> Unit = {},
shape: Shape = BottomSheetDefaults.ExpandedShape,
sheetGesturesEnabled: Boolean = true,
dismissOnBack: Boolean = sheetGesturesEnabled,
windowInsets: WindowInsets = WindowInsets(0), // This is a nice default if the app is edge to edge
content: @Composable ColumnScope.() -> Unit,
@tfcporciuncula
tfcporciuncula / composables.kt
Created April 23, 2024 13:31
Getting arg from ViewModel with Hilt assisted injection
@HiltViewModel(assistedFactory = DetailsViewModel.Factory::class)
class DetailsViewModel @AssistedInject constructor(
@Assisted val arg: String,
) : ViewModel() {
@AssistedFactory interface Factory {
fun create(arg: String): DetailsViewModel
}
private val state = MutableStateFlow(DetailsUiState(arg = arg))
fun state() = state.asStateFlow()
@tfcporciuncula
tfcporciuncula / composables.kt
Last active April 23, 2024 13:29
Getting arg from ViewModel with savedStateHandle
// Both screens look the same, but now we have a ViewModel and some differences on the App composable
@HiltViewModel
class DetailsViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
) : ViewModel() {
private val state = MutableStateFlow(requireNotNull(savedStateHandle.get("arg")))
fun state() = state.asStateFlow()
}
@Composable
@tfcporciuncula
tfcporciuncula / Composables.kt
Last active April 23, 2024 11:51
Getting arg directly from navBackStackEntry
@Composable
fun StartScreen(onNavigateClick: (String) -> Unit) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
var argText by rememberSaveable { mutableStateOf("") }
TextField(
value = argText,
class TokenAuthenticator @Inject constructor(
private val api: Api,
private val tokenStore: TokenStore,
private val logoutTrigger: LogoutTrigger
) : Authenticator {
companion object {
const val AUTH_HEADER_NAME = "Authorization"
const val AUTH_HEADER_VALUE_FORMAT = "Bearer %s"
}
val viewModelStoreOwner = requireNotNull(
LocalViewModelStoreOwner.current as? HasDefaultViewModelProviderFactory
)
val viewModel = viewModel<MyViewModel>(
extras = viewModelStoreOwner
.defaultViewModelCreationExtras
.withCreationCallback<MyViewModel.Factory> { factory ->
factory.create(runtimeArg = "abc")
}
)
val creationCallback: (MyViewModel.Factory) -> MyViewModel = { factory ->
factory.create(runtimeArg = "abc")
}
val viewModel = viewModel<MyViewModel>(
extras = requireNotNull(LocalViewModelStoreOwner.current).run {
if (this is HasDefaultViewModelProviderFactory) {
this.defaultViewModelCreationExtras.withCreationCallback(creationCallback)
} else {
CreationExtras.Empty.withCreationCallback(creationCallback)
}
val viewModel = hiltViewModel<MyViewModel, MyViewModel.Factory>(
creationCallback = { factory -> factory.create(runtimeArg = "abc") }
)