Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ dependencies {
implementation("com.google.zxing:core:3.5.3")

// CDK Kotlin bindings
implementation("org.cashudevkit:cdk-kotlin:0.14.2-rc.3")
implementation("org.cashudevkit:cdk-kotlin:0.14.3-rc.2")

// ML Kit Barcode Scanning
implementation("com.google.mlkit:barcode-scanning:17.2.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,7 @@ object CashuWalletManager : MintManager.MintChangeListener {
}

// Close existing wallet
try {
wallet?.close()
} catch (t: Throwable) {
Log.w(TAG, "Error closing wallet during restore", t)
}

try {
database?.close()
} catch (t: Throwable) {
Log.w(TAG, "Error closing database during restore", t)
}

wallet = null
database = null
closeResources()

// Delete existing database to start fresh
val dbFile = appContext.getDatabasePath(DB_FILE_NAME)
Expand Down Expand Up @@ -351,20 +338,7 @@ object CashuWalletManager : MintManager.MintChangeListener {
private suspend fun rebuildWallet(mints: List<String>) {
try {
// Close any previous instances
try {
wallet?.close()
} catch (t: Throwable) {
Log.w(TAG, "Error closing previous wallet", t)
}

try {
database?.close()
} catch (t: Throwable) {
Log.w(TAG, "Error closing previous DB", t)
}

wallet = null
database = null
closeResources()

if (mints.isEmpty()) {
Log.w(TAG, "No allowed mints configured, skipping wallet init")
Expand Down Expand Up @@ -418,4 +392,22 @@ object CashuWalletManager : MintManager.MintChangeListener {
Log.e(TAG, "Failed to initialize MultiMintWallet", t)
}
}

private fun closeResources() {
try {
wallet?.close()
} catch (t: Throwable) {
Log.w(TAG, "Error closing wallet", t)
} finally {
wallet = null
}

try {
database?.close()
} catch (t: Throwable) {
Log.w(TAG, "Error closing database", t)
} finally {
database = null
}
}
}
46 changes: 24 additions & 22 deletions app/src/main/java/com/electricdreams/numo/core/util/ItemManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -501,33 +501,35 @@ class ItemManager private constructor(context: Context) {
}

// Copy the image from the Uri to the file
val inputStream: InputStream? = context.contentResolver.openInputStream(imageUri)
val inputStream = context.contentResolver.openInputStream(imageUri)
if (inputStream != null) {
var bitmap = BitmapFactory.decodeStream(inputStream)
inputStream.close()

// Scale down if the image is too large
if (bitmap.width > 1024 || bitmap.height > 1024) {
val maxDimension = maxOf(bitmap.width, bitmap.height)
val scale = 1024f / maxDimension
val newWidth = (bitmap.width * scale).toInt()
val newHeight = (bitmap.height * scale).toInt()
bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true)
}
inputStream.use { stream ->
var bitmap = BitmapFactory.decodeStream(stream)

// Scale down if the image is too large
if (bitmap.width > 1024 || bitmap.height > 1024) {
val maxDimension = maxOf(bitmap.width, bitmap.height)
val scale = 1024f / maxDimension
val newWidth = (bitmap.width * scale).toInt()
val newHeight = (bitmap.height * scale).toInt()
bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true)
}

// Save the image as JPEG with 85% quality
FileOutputStream(imageFile).use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outputStream)
outputStream.flush()
}
// Save the image as JPEG with 85% quality
FileOutputStream(imageFile).use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outputStream)
outputStream.flush()
}

// Update the item's image path
item.imagePath = imageFile.absolutePath
updateItem(item)
// Update the item's image path
item.imagePath = imageFile.absolutePath
updateItem(item)

true
return true // Success
}
} else {
false
Log.e(TAG, "Failed to open input stream for image URI")
return false
}
} catch (e: IOException) {
Log.e(TAG, "Error saving item image: ${e.message}", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,43 +74,41 @@ object MintIconCache {
.get()
.build()

val response = client.newCall(request).execute()

if (!response.isSuccessful) {
Log.w(TAG, "Failed to download icon: HTTP ${response.code}")
response.close()
return@withContext null
}

val bytes = response.body?.bytes()
response.close()

if (bytes == null || bytes.isEmpty()) {
Log.w(TAG, "Empty response when downloading icon")
return@withContext null
}

// Decode to bitmap to validate it's a valid image
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
if (bitmap == null) {
Log.w(TAG, "Failed to decode icon as bitmap")
return@withContext null
}

// Save to cache
val fileName = getIconFileName(mintUrl)
val file = File(cacheDir, fileName)

FileOutputStream(file).use { out ->
// Save as PNG for consistent format
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
Log.w(TAG, "Failed to download icon: HTTP ${response.code}")
return@withContext null
}

val bytes = response.body?.bytes()

if (bytes == null || bytes.isEmpty()) {
Log.w(TAG, "Empty response when downloading icon")
return@withContext null
}

// Decode to bitmap to validate it's a valid image
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
if (bitmap == null) {
Log.w(TAG, "Failed to decode icon as bitmap")
return@withContext null
}

// Save to cache
val fileName = getIconFileName(mintUrl)
val file = File(cacheDir, fileName)

FileOutputStream(file).use { out ->
// Save as PNG for consistent format
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
}

bitmap.recycle()

Log.d(TAG, "Successfully cached icon for $mintUrl at ${file.absolutePath}")
return@withContext file
}

bitmap.recycle()

Log.d(TAG, "Successfully cached icon for $mintUrl at ${file.absolutePath}")
return@withContext file

} catch (e: Exception) {
Log.e(TAG, "Error downloading/caching icon for $mintUrl: ${e.message}", e)
return@withContext null
Expand Down