diff --git a/app/src/main/java/app/klottr/MainActivity.kt b/app/src/main/java/app/klottr/MainActivity.kt index 4942102..4d17c54 100644 --- a/app/src/main/java/app/klottr/MainActivity.kt +++ b/app/src/main/java/app/klottr/MainActivity.kt @@ -107,7 +107,9 @@ import androidx.work.NetworkType import androidx.work.OneTimeWorkRequestBuilder import androidx.work.WorkManager import app.klottr.create.CreateNoteOutcome +import app.klottr.create.QueueOutboxOutcome import app.klottr.create.createNoteFlow +import app.klottr.create.queueNoteDocOutbox import app.klottr.data.AppDatabase import app.klottr.data.NoteRepository import app.klottr.location.AndroidLocationProvider @@ -1076,57 +1078,144 @@ private fun KlottrApp( showWrite = false } - is CreateNoteOutcome.UploadFailed -> { - val msg = outcome.message ?: "Bilduppladdning misslyckades." - Toast.makeText(context, msg, Toast.LENGTH_LONG).show() - resetSavingCounter++ - // Stanna i dialogen (samma beteende som tidigare) - } + is CreateNoteOutcome.UploadFailed -> { // Fallback till offline-utkorg även när bilduppladdning misslyckas + val titleText = draft.title.trim() + val bodyText = draft.body.trim() - is CreateNoteOutcome.CreateFailed -> { - // Offline: spara till utkorgen (oförändrat beteende) - var imagePath: String? = null - var imageMime: String? = null - if (draft.imageUri != null) { - val mime = context.contentResolver.getType(draft.imageUri!!) ?: "image/jpeg" - val ext = when { - mime.equals("image/jpeg", true) || mime.equals("image/jpg", true) -> ".jpg" - mime.equals("image/png", true) -> ".png" - else -> ".img" - } - val outDir = File(context.filesDir, "outbox").apply { mkdirs() } - val outFile = File(outDir, "img_${created}$ext") - try { - context.contentResolver.openInputStream(draft.imageUri!!)?.use { ins -> - outFile.outputStream().use { outs -> ins.copyTo(outs, 8 * 1024) } - } - imagePath = outFile.absolutePath - imageMime = mime - } catch (_: Exception) { - imagePath = null; imageMime = null - } - } - - OutboxStore.addDoc( - context = context, + val queued = withContext(Dispatchers.IO) { + queueNoteDocOutbox( lat = selLat, lon = selLon, title = titleText, body = bodyText, createdAt = created, radiusM = Config.DEFAULT_KLOTTER_RADIUS_M, - imagePath = imagePath, - imageMime = imageMime + hasImage = (draft.imageUri != null), + persistImageIfAny = { + val uri = draft.imageUri ?: return@queueNoteDocOutbox null + val mime = context.contentResolver.getType(uri) ?: "image/jpeg" + val ext = when { + mime.equals("image/jpeg", true) || mime.equals("image/jpg", true) -> ".jpg" + mime.equals("image/png", true) -> ".png" + else -> ".img" + } + val outDir = File(context.filesDir, "outbox").apply { mkdirs() } + val outFile = File(outDir, "img_${created}$ext") + try { + context.contentResolver.openInputStream(uri)?.use { ins -> + outFile.outputStream().use { outs -> ins.copyTo(outs, 8 * 1024) } + } + outFile.absolutePath to mime + } catch (_: Exception) { + null + } + }, + addOutboxDoc = { la, lo, t, b, ts, rad, imagePath, imageMime -> + OutboxStore.addDoc( + context = context, + lat = la, + lon = lo, + title = t, + body = b, + createdAt = ts, + radiusM = rad, + imagePath = imagePath, + imageMime = imageMime + ) + }, + scheduleUpload = { + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build() + val req = OneTimeWorkRequestBuilder() + .setConstraints(constraints).build() + WorkManager.getInstance(context).enqueue(req) + } ) - Toast.makeText(context, "Offline. Sparas i utkorgen.", Toast.LENGTH_LONG).show() + } - val constraints = Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build() - val req = OneTimeWorkRequestBuilder() - .setConstraints(constraints).build() - WorkManager.getInstance(context).enqueue(req) - showWrite = false + when (queued) { + QueueOutboxOutcome.Queued -> { + Toast.makeText(context, "Offline. Sparas i utkorgen.", Toast.LENGTH_LONG).show() + showWrite = false + } + QueueOutboxOutcome.ImagePersistFailed -> { + val msg = outcome.message ?: "Bilduppladdning misslyckades." + Toast.makeText(context, "$msg\nKunde inte spara bilden lokalt.", Toast.LENGTH_LONG).show() + // Stanna kvar i dialogen + } + } + } + + + is CreateNoteOutcome.CreateFailed -> { + // Lägg i utkorgen via delad use-case (offline) + val titleText = draft.title.trim() + val bodyText = draft.body.trim() + + val queued = withContext(Dispatchers.IO) { + queueNoteDocOutbox( + lat = selLat, + lon = selLon, + title = titleText, + body = bodyText, + createdAt = created, + radiusM = Config.DEFAULT_KLOTTER_RADIUS_M, + hasImage = (draft.imageUri != null), + persistImageIfAny = { + // Spara vald bild till appens outbox-mapp, returnera (path, mime) eller null vid fel + val uri = draft.imageUri ?: return@queueNoteDocOutbox null + val mime = context.contentResolver.getType(uri) ?: "image/jpeg" + val ext = when { + mime.equals("image/jpeg", true) || mime.equals("image/jpg", true) -> ".jpg" + mime.equals("image/png", true) -> ".png" + else -> ".img" + } + val outDir = File(context.filesDir, "outbox").apply { mkdirs() } + val outFile = File(outDir, "img_${created}$ext") + try { + context.contentResolver.openInputStream(uri)?.use { ins -> + outFile.outputStream().use { outs -> ins.copyTo(outs, 8 * 1024) } + } + outFile.absolutePath to mime + } catch (_: Exception) { + null + } + }, + addOutboxDoc = { la, lo, t, b, ts, rad, imagePath, imageMime -> + OutboxStore.addDoc( + context = context, + lat = la, + lon = lo, + title = t, + body = b, + createdAt = ts, + radiusM = rad, + imagePath = imagePath, + imageMime = imageMime + ) + }, + scheduleUpload = { + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build() + val req = OneTimeWorkRequestBuilder() + .setConstraints(constraints).build() + WorkManager.getInstance(context).enqueue(req) + } + ) + } + + when (queued) { + QueueOutboxOutcome.Queued -> { + Toast.makeText(context, "Offline. Sparas i utkorgen.", Toast.LENGTH_LONG).show() + showWrite = false + } + QueueOutboxOutcome.ImagePersistFailed -> { + Toast.makeText(context, "Kunde inte spara bilden lokalt.", Toast.LENGTH_LONG).show() + // Stanna kvar i dialogen, låt användaren försöka igen eller spara utan bild + } + } } } } finally { diff --git a/shared/src/commonMain/kotlin/app/klottr/create/QueueOutBox.kt b/shared/src/commonMain/kotlin/app/klottr/create/QueueOutBox.kt new file mode 100644 index 0000000..e335319 --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/create/QueueOutBox.kt @@ -0,0 +1,33 @@ +package app.klottr.create + +/** Resultat från outbox-köning (offline). */ +sealed class QueueOutboxOutcome { + data object Queued : QueueOutboxOutcome() // Lyckades lägga i utkorgen + data object ImagePersistFailed : QueueOutboxOutcome() // Bilden kunde inte sparas lokalt +} + +/** + * Orkestrerar offline-flödet: ev. spara bild lokalt → lägg DOC i outbox → schemalägg uppladdning. + * Plattformsspecifika detaljer injiceras via lambdas (DI). + */ +suspend fun queueNoteDocOutbox( + lat: Double, + lon: Double, + title: String, + body: String, + createdAt: Long, + radiusM: Int, + hasImage: Boolean, + persistImageIfAny: suspend () -> Pair?, // returnerar (imagePath, imageMime) eller null vid fel + addOutboxDoc: (lat: Double, lon: Double, title: String, body: String, createdAt: Long, radiusM: Int, imagePath: String?, imageMime: String?) -> Unit, + scheduleUpload: () -> Unit +): QueueOutboxOutcome { + val (imagePath, imageMime) = if (hasImage) { + val res = persistImageIfAny() ?: return QueueOutboxOutcome.ImagePersistFailed + res + } else null to null + + addOutboxDoc(lat, lon, title, body, createdAt, radiusM, imagePath, imageMime) + scheduleUpload() + return QueueOutboxOutcome.Queued +}