Skip to content

Instantly share code, notes, and snippets.

View tdcolvin's full-sized avatar

Tom Colvin tdcolvin

  • Basingstoke, UK
  • 23:08 (UTC +01:00)
View GitHub Profile
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="external_files" path="." />
</paths>
val tmpFile = File.createTempFile("photo", ".jpg", context.cacheDir).apply {
createNewFile()
deleteOnExit()
}
dependencies {
...
implementation(libs.coil)
}
@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 }
)
viewModelScope.launch {
// Time passing before we start collecting...
delay(4000)
// Start collecting the flow
sharedFlow.collect {
println(it)
}
}
val sharedFlow = MutableSharedFlow<Int>()
viewModelScope.launch {
var count = 0
// Every 500ms, emit a new number in sequence
while (true) {
delay(500)
sharedFlow.emit(count++)
}
@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 {
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()
@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) {
@tdcolvin
tdcolvin / main.kt
Last active January 19, 2024 17:52
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) {