Skip to content

Commit 442b795

Browse files
committed
chore(contentreader): add deleteFromCache and allow copyToCache with a max size constraint
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent f4361a5 commit 442b795

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ androidx-camerax = "1.6.0"
1818
androidx-credentials = "1.5.0"
1919
androidx-core = "1.18.0"
2020
androidx-datastore = "1.2.1"
21+
androidx-exif = "1.4.2"
2122
androidx-lifecycle = "2.10.0"
2223
androidx-navigation = "2.9.7"
2324
navigation3 = "1.0.1"
@@ -120,6 +121,7 @@ androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-cor
120121
androidx-credentials = { module = "androidx.credentials:credentials", version.ref = "androidx-credentials" }
121122
androidx-credentials-play-auth = { module = "androidx.credentials:credentials-play-services-auth", version.ref = "androidx-credentials" }
122123
androidx-localbroadcastmanager = { module = "androidx.localbroadcastmanager:localbroadcastmanager", version = "1.1.0" }
124+
androidx-exifinterface = { module = "androidx.exifinterface:exifinterface", version.ref = "androidx-exif" }
123125
androidx-lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version.ref = "androidx-lifecycle" }
124126
androidx-lifecycle-runtime = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidx-lifecycle" }
125127
androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "androidx-lifecycle" }

ui/resources/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies {
1010
api(libs.androidx.annotation)
1111
api(libs.androidx.appcompat)
1212
api(libs.androidx.core)
13+
implementation(libs.androidx.exifinterface)
1314
api(libs.kotlin.stdlib)
1415
api(libs.kotlinx.coroutines.core)
1516
api(libs.kotlinx.coroutines.rx3)

ui/resources/src/main/java/com/getcode/util/resources/ContentReader.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ import android.graphics.Bitmap
55
import android.graphics.BitmapFactory
66
import android.graphics.Matrix
77
import android.net.Uri
8-
import android.media.ExifInterface
8+
import androidx.exifinterface.media.ExifInterface
99
import java.io.File
10+
import androidx.core.graphics.scale
1011

1112
interface ContentReader {
1213
fun readBytes(uri: Uri): ByteArray?
13-
fun copyToCache(uri: Uri, fileName: String): Uri?
14+
fun copyToCache(uri: Uri, fileName: String, maxSize: Int = Int.MAX_VALUE): Uri?
15+
fun removeFromCache(uri: Uri)
1416
}
1517

1618
class AndroidContentReader(private val context: Context) : ContentReader {
1719
override fun readBytes(uri: Uri): ByteArray? {
1820
return context.contentResolver.openInputStream(uri)?.use { it.readBytes() }
1921
}
2022

21-
override fun copyToCache(uri: Uri, fileName: String): Uri? {
23+
override fun copyToCache(uri: Uri, fileName: String, maxSize: Int): Uri? {
2224
val bytes = readBytes(uri) ?: return null
2325
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) ?: return null
2426

@@ -39,8 +41,21 @@ class AndroidContentReader(private val context: Context) : ContentReader {
3941
bitmap
4042
}
4143

44+
val scaled = if (oriented.width > maxSize || oriented.height > maxSize) {
45+
val scale = maxSize.toFloat() / maxOf(oriented.width, oriented.height)
46+
val newWidth = (oriented.width * scale).toInt()
47+
val newHeight = (oriented.height * scale).toInt()
48+
oriented.scale(newWidth, newHeight)
49+
} else {
50+
oriented
51+
}
52+
4253
val file = File(context.cacheDir, fileName)
43-
file.outputStream().use { oriented.compress(Bitmap.CompressFormat.PNG, 100, it) }
54+
file.outputStream().use { scaled.compress(Bitmap.CompressFormat.PNG, 100, it) }
4455
return Uri.fromFile(file)
4556
}
57+
58+
override fun removeFromCache(uri: Uri) {
59+
uri.path?.let { File(it).delete() }
60+
}
4661
}

0 commit comments

Comments
 (0)