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
<?xml version="1.0" encoding="utf-8"?> | |
<paths xmlns:android="http://schemas.android.com/apk/res/android"> | |
<cache-path name="external_files" path="." /> | |
</paths> |
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 tmpFile = File.createTempFile("photo", ".jpg", context.cacheDir).apply { | |
createNewFile() | |
deleteOnExit() | |
} |
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
dependencies { | |
... | |
implementation(libs.coil) | |
} |
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 TakePhotoScreen(modifier: Modifier = Modifier) { | |
// The location on disk where the photo was saved to, or null if no photo | |
var imageUri by remember { mutableStateOf<Uri?>(null) } | |
Column(modifier = modifier) { | |
// We'll define this later... | |
TakePhotoButton( | |
onPhotoTaken = { imageUri = it } | |
) |
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
viewModelScope.launch { | |
// Time passing before we start collecting... | |
delay(4000) | |
// Start collecting the flow | |
sharedFlow.collect { | |
println(it) | |
} | |
} |
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 sharedFlow = MutableSharedFlow<Int>() | |
viewModelScope.launch { | |
var count = 0 | |
// Every 500ms, emit a new number in sequence | |
while (true) { | |
delay(500) | |
sharedFlow.emit(count++) | |
} |
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 FlashingCountingButton() { | |
val scope = rememberCoroutineScope() | |
var buttonColor by remember { mutableStateOf(Color.Red) } | |
var count by remember { mutableStateOf(1) } | |
Column { | |
Button(colors = ButtonDefaults.buttonColors(backgroundColor = buttonColor), | |
onClick = { | |
val job = scope.launch { |
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 job = scope.launch { | |
while(true) { | |
delay(500) | |
buttonColor = Color(Random.nextInt(0xFF), Random.nextInt(0xFF), Random.nextInt(0xFF), 0xFF) | |
} | |
} | |
scope.launch { | |
delay(2000) | |
job.cancel() |
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 RandomColourButton() { | |
val scope = rememberCoroutineScope() | |
var buttonColor by remember { mutableStateOf(Color.Red) } | |
Column { | |
Button(colors = ButtonDefaults.buttonColors(backgroundColor = buttonColor), | |
onClick = { | |
scope.launch { | |
while(true) { |
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
suspend fun countToAHundredBillion() { | |
var count = 0L | |
while(count < 100_000_000_000) { | |
count++ | |
// Every 10,000 we yield to the coroutine | |
// dispatcher, allowing this loop to be | |
// cancelled if needed. | |
if (count % 10_000 == 0) { |
NewerOlder