-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
245 additions
and
100 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.capyreader.app.ui | ||
|
||
interface CapySnackbar { | ||
fun show(message: String) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.capyreader.app.ui | ||
|
||
import androidx.compose.runtime.compositionLocalOf | ||
|
||
val LocalSnackbar = compositionLocalOf { SnackbarState {} } | ||
|
||
data class SnackbarState( | ||
val showMessage: (message: String) -> Unit, | ||
) : CapySnackbar { | ||
override fun show(message: String) { | ||
showMessage(message) | ||
} | ||
} |
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
96 changes: 96 additions & 0 deletions
96
app/src/main/java/com/capyreader/app/ui/articles/media/ExternalImages.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.capyreader.app.ui.articles.media | ||
|
||
import android.content.ContentValues | ||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.net.Uri | ||
import android.os.Environment | ||
import android.provider.MediaStore | ||
import androidx.core.graphics.drawable.toBitmap | ||
import androidx.core.net.toUri | ||
import coil.executeBlocking | ||
import coil.imageLoader | ||
import coil.request.ImageRequest | ||
import com.capyreader.app.common.MD5 | ||
import com.capyreader.app.common.externalImageCacheFile | ||
import com.capyreader.app.common.fileURI | ||
import com.jocmp.capy.logging.CapyLog | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import java.io.ByteArrayOutputStream | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
|
||
object ExternalImages { | ||
suspend fun shareImage(imageUrl: String, context: Context): Result<Uri> { | ||
return withContext(Dispatchers.IO) { | ||
return@withContext try { | ||
val bitmap = | ||
createBitmap(imageUrl, context) ?: throw IOException("Failed to generate image") | ||
|
||
val target = context.externalImageCacheFile(fileName(imageUrl)) | ||
|
||
context.contentResolver.openFileDescriptor(target.toUri(), "w")?.use { descriptor -> | ||
FileOutputStream(descriptor.fileDescriptor).use { | ||
it.write(jpegStream(bitmap)) | ||
} | ||
} | ||
|
||
Result.success(context.fileURI(target)) | ||
} catch (e: Exception) { | ||
CapyLog.error("share_img", error = e) | ||
Result.failure(e) | ||
} | ||
} | ||
} | ||
|
||
suspend fun saveImage(imageUrl: String, context: Context): Result<Uri> { | ||
return withContext(Dispatchers.IO) { | ||
return@withContext try { | ||
val bitmap = | ||
createBitmap(imageUrl, context) ?: throw IOException("Failed to generate image") | ||
|
||
val contentValues = ContentValues().apply { | ||
put(MediaStore.MediaColumns.DISPLAY_NAME, fileName(imageUrl)) | ||
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg") | ||
put( | ||
MediaStore.MediaColumns.RELATIVE_PATH, | ||
"${Environment.DIRECTORY_PICTURES}/Capy" | ||
) | ||
} | ||
|
||
val uri = context.contentResolver.insert( | ||
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, | ||
contentValues | ||
) ?: throw IOException("Failed to create MediaStore entry") | ||
|
||
context.contentResolver.openOutputStream(uri)?.use { outputStream -> | ||
outputStream.write(jpegStream(bitmap)) | ||
} ?: throw IOException("Failed to open output stream") | ||
|
||
Result.success(uri) | ||
} catch (e: Exception) { | ||
CapyLog.error("save_img", error = e) | ||
Result.failure(e) | ||
} | ||
} | ||
} | ||
|
||
private fun jpegStream(bitmap: Bitmap): ByteArray { | ||
val byteArrayOutputStream = ByteArrayOutputStream() | ||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream) | ||
return byteArrayOutputStream.toByteArray() | ||
} | ||
|
||
private fun fileName(imageUrl: String): String { | ||
return "${MD5.from(imageUrl)}.jpg" | ||
} | ||
|
||
private fun createBitmap(imageUrl: String, context: Context): Bitmap? { | ||
val imageRequest = ImageRequest.Builder(context) | ||
.data(imageUrl) | ||
.build() | ||
|
||
return context.imageLoader.executeBlocking(imageRequest).drawable?.toBitmap() | ||
} | ||
} |
63 changes: 0 additions & 63 deletions
63
app/src/main/java/com/capyreader/app/ui/articles/media/ImageSaver.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.