app(write): on UploadFailed, queue to outbox via shared use case; persist image locally and schedule upload worker
This commit is contained in:
@@ -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<OutboxWorker>()
|
||||
.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<OutboxWorker>()
|
||||
.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<OutboxWorker>()
|
||||
.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 {
|
||||
|
||||
Reference in New Issue
Block a user