Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sayanthrock.freeairock.ui

import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
Expand All @@ -21,6 +23,7 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -30,7 +33,12 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.compose.SubcomposeAsyncImage
import coil.imageLoader
import coil.request.ImageRequest
import coil.request.SuccessResult
import com.sayanthrock.freeairock.data.image.BitmapImageState
import com.sayanthrock.freeairock.data.image.BitmapStore
import kotlinx.coroutines.launch

@Composable
fun ImageStudioScreen(
Expand All @@ -39,6 +47,9 @@ fun ImageStudioScreen(
var prompt by remember { mutableStateOf("") }
var imageUrl by remember { mutableStateOf<String?>(null) }
var isGenerating by remember { mutableStateOf(false) }
var isDownloading by remember { mutableStateOf(false) }
val context = LocalContext.current
val scope = rememberCoroutineScope()

Column(
modifier = modifier
Expand Down Expand Up @@ -112,6 +123,38 @@ fun ImageStudioScreen(
modifier = Modifier.fillMaxSize()
)
}

Spacer(modifier = Modifier.height(16.dp))

Button(
onClick = {
if (!isDownloading) {
isDownloading = true
scope.launch {
val request = ImageRequest.Builder(context)
.data(url)
.build()
val result = context.imageLoader.execute(request)
if (result is SuccessResult) {
val bitmap = (result.drawable as BitmapDrawable).bitmap
val saveResult = BitmapStore.writePng(context, bitmap)
Comment thread
SayanthRock marked this conversation as resolved.
Comment thread
SayanthRock marked this conversation as resolved.
saveResult.onSuccess { _ ->
Toast.makeText(context, "Saved image", Toast.LENGTH_SHORT).show()
}.onFailure { error ->
Toast.makeText(context, "Error saving: ${error.message}", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(context, "Error downloading image", Toast.LENGTH_SHORT).show()
}
isDownloading = false
}
Comment thread
SayanthRock marked this conversation as resolved.
}
},
modifier = Modifier.fillMaxWidth(),
enabled = !isDownloading && !isGenerating
) {
Text(if (isDownloading) "Downloading..." else "Download [↓]")
}
}
}
}
Loading