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
/** | |
* 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 |
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
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 |
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
@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, |
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
@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() |
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
// 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 |
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
@Composable | |
fun StartScreen(onNavigateClick: (String) -> Unit) { | |
Column( | |
modifier = Modifier.fillMaxSize(), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally, | |
) { | |
var argText by rememberSaveable { mutableStateOf("") } | |
TextField( | |
value = argText, |
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
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" | |
} |
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
val viewModelStoreOwner = requireNotNull( | |
LocalViewModelStoreOwner.current as? HasDefaultViewModelProviderFactory | |
) | |
val viewModel = viewModel<MyViewModel>( | |
extras = viewModelStoreOwner | |
.defaultViewModelCreationExtras | |
.withCreationCallback<MyViewModel.Factory> { factory -> | |
factory.create(runtimeArg = "abc") | |
} | |
) |
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
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) | |
} |
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
val viewModel = hiltViewModel<MyViewModel, MyViewModel.Factory>( | |
creationCallback = { factory -> factory.create(runtimeArg = "abc") } | |
) |
NewerOlder