mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
fix(planning): harden receipt camera capture and image decoding
This commit is contained in:
1 parent
8260dba5a5
commit
e534cfa7b0
5 files changed
+355
-32
No files matched your search
@@ -1,8 +1,10 @@
|
||||
package com.example.ui.planning
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.PickVisualMediaRequest
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
@@ -43,6 +45,7 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -66,6 +69,7 @@ import com.example.data.model.ShoppingListItem
|
||||
import com.example.data.model.StorageLocation
|
||||
import com.example.data.model.WeekPredictionUiState
|
||||
import com.example.util.ImageUtils
|
||||
import kotlinx.coroutines.launch
|
||||
import java.time.LocalDate
|
||||
|
||||
enum class PlanningSubTab(val label: String) {
|
||||
@@ -126,7 +130,8 @@ fun PlanningScreen(
|
||||
var editingInventoryItem by remember { mutableStateOf<InventoryItem?>(null) }
|
||||
|
||||
var showReceiptSourceDialog by remember { mutableStateOf(false) }
|
||||
var pendingCaptureUri by remember { mutableStateOf<Uri?>(null) }
|
||||
var pendingCaptureUriString by rememberSaveable { mutableStateOf<String?>(null) }
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
var shoppingItemForStockPrompt by remember { mutableStateOf<ShoppingListItem?>(null) }
|
||||
var showPredictWeekSheet by rememberSaveable { mutableStateOf(false) }
|
||||
@@ -143,23 +148,38 @@ fun PlanningScreen(
|
||||
}
|
||||
}
|
||||
|
||||
// Camera capture via FileProvider URI
|
||||
// Camera capture via FileProvider URI with explicit read/write grants + OEM fallback handling
|
||||
val takeReceiptPhotoLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.TakePicture()
|
||||
) { success ->
|
||||
val captured = pendingCaptureUri
|
||||
pendingCaptureUri = null
|
||||
if (success && captured != null) {
|
||||
onScanReceiptUri(captured)
|
||||
contract = ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
val expectedUri = pendingCaptureUriString?.let(Uri::parse)
|
||||
pendingCaptureUriString = null
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
val resultData = result.data
|
||||
coroutineScope.launch {
|
||||
val resolvedUri = ImageUtils.resolveCapturedReceiptUri(
|
||||
context = context,
|
||||
expectedUri = expectedUri,
|
||||
resultData = resultData
|
||||
)
|
||||
if (resolvedUri != null) {
|
||||
onScanReceiptUri(resolvedUri)
|
||||
} else {
|
||||
onShowMessage("Impossible de récupérer la photo prise. Réessayez ou choisissez depuis la galerie.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val launchCameraCapture = {
|
||||
try {
|
||||
val uri = ImageUtils.createReceiptCaptureUri(context)
|
||||
pendingCaptureUri = uri
|
||||
takeReceiptPhotoLauncher.launch(uri)
|
||||
} catch (_: Exception) {
|
||||
pendingCaptureUriString = uri.toString()
|
||||
val captureIntent = ImageUtils.buildCameraCaptureIntent(context, uri)
|
||||
takeReceiptPhotoLauncher.launch(captureIntent)
|
||||
} catch (e: Exception) {
|
||||
pendingCaptureUriString = null
|
||||
Log.e("PlanningScreen", "Failed to launch camera capture", e)
|
||||
onShowMessage("Impossible d'ouvrir l'appareil photo")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user