shared(create): add createNoteFlow use case (upload→create orchestration) with CreateNoteOutcome
app(main): use createNoteFlow in WriteDocDialog onSave; keep offline outbox path unchanged
This commit is contained in:
@@ -106,6 +106,8 @@ import androidx.work.Constraints
|
||||
import androidx.work.NetworkType
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import app.klottr.create.CreateNoteOutcome
|
||||
import app.klottr.create.createNoteFlow
|
||||
import app.klottr.data.AppDatabase
|
||||
import app.klottr.data.NoteRepository
|
||||
import app.klottr.location.AndroidLocationProvider
|
||||
@@ -1010,47 +1012,46 @@ private fun KlottrApp(
|
||||
isSavingDoc = true
|
||||
scope.launch {
|
||||
try {
|
||||
// 1) Ladda ev. upp bilden först (passera alltid *vald* position)
|
||||
val imgId = withContext(Dispatchers.IO) {
|
||||
draft.imageUri?.let { uri ->
|
||||
uploadImageFromUri(context, uri, selLat, selLon)
|
||||
}
|
||||
}
|
||||
if (draft.imageUri != null && imgId == null) {
|
||||
withContext(Dispatchers.Main) {
|
||||
val msg = Api.lastUploadError
|
||||
?: "Bilduppladdning misslyckades."
|
||||
Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
|
||||
resetSavingCounter++
|
||||
}
|
||||
return@launch
|
||||
}
|
||||
val titleText = draft.title.trim()
|
||||
val bodyText = draft.body.trim()
|
||||
|
||||
// 2) Skapa klotter på *vald* position
|
||||
val ok = withContext(Dispatchers.IO) {
|
||||
// Kör delat online-flöde i IO-tråd
|
||||
val outcome = withContext(Dispatchers.IO) {
|
||||
createNoteFlow(
|
||||
hasImage = draft.imageUri != null,
|
||||
uploadImage = {
|
||||
// Plattformsspecifik bilduppladdning (oförändrad funktion i :app)
|
||||
uploadImageFromUri(context, draft.imageUri!!, selLat, selLon)
|
||||
},
|
||||
createNote = { imageId ->
|
||||
createNoteDoc(
|
||||
lat = selLat,
|
||||
lon = selLon,
|
||||
title = draft.title.trim(),
|
||||
body = draft.body.trim(),
|
||||
imageId = imgId,
|
||||
title = titleText,
|
||||
body = bodyText,
|
||||
imageId = imageId,
|
||||
radiusM = Config.DEFAULT_KLOTTER_RADIUS_M,
|
||||
createdAt = created,
|
||||
clientRequestId = requestId
|
||||
)
|
||||
},
|
||||
getUploadError = { Api.lastUploadError }
|
||||
)
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
when (outcome) {
|
||||
is CreateNoteOutcome.Success -> {
|
||||
Toast.makeText(context, "Sparat.", Toast.LENGTH_SHORT).show()
|
||||
// (valfritt) hitta posten nära *vald* position och markera som funnen
|
||||
|
||||
// Förladda närliggande post och ev. bild (oförändrat beteende)
|
||||
scope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val nearby = Geocache.getNearby(
|
||||
lat = selLat, lon = selLon, radiusMeters = 100, ttlMs = 0L
|
||||
)
|
||||
val combined = buildString {
|
||||
if (draft.title.isNotBlank()) append(draft.title).append("\n\n")
|
||||
append(draft.body.trim())
|
||||
if (titleText.isNotBlank()) append(titleText).append("\n\n")
|
||||
append(bodyText)
|
||||
}.trim()
|
||||
val match = nearby.firstOrNull {
|
||||
it.t == combined && abs(it.c - created) <= 2_000
|
||||
@@ -1073,11 +1074,19 @@ private fun KlottrApp(
|
||||
}
|
||||
} catch (_: Throwable) { }
|
||||
}
|
||||
|
||||
showWrite = false
|
||||
} else {
|
||||
// OFFLINE: lägg i utkorg som DOC (spara *vald* position + lokal bild)
|
||||
val titleText = draft.title.trim()
|
||||
val bodyText = draft.body.trim()
|
||||
}
|
||||
|
||||
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.CreateFailed -> {
|
||||
// Offline: spara till utkorgen (oförändrat beteende)
|
||||
var imagePath: String? = null
|
||||
var imageMime: String? = null
|
||||
if (draft.imageUri != null) {
|
||||
@@ -1121,6 +1130,7 @@ private fun KlottrApp(
|
||||
WorkManager.getInstance(context).enqueue(req)
|
||||
showWrite = false
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
isSavingDoc = false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package app.klottr.create
|
||||
|
||||
/** Resultat från create-flödet. */
|
||||
sealed class CreateNoteOutcome {
|
||||
data object Success : CreateNoteOutcome()
|
||||
data class UploadFailed(val message: String?) : CreateNoteOutcome()
|
||||
data object CreateFailed : CreateNoteOutcome()
|
||||
}
|
||||
|
||||
/**
|
||||
* Orkestrerar online-flödet: ev. bilduppladdning → skapa notis.
|
||||
* Plattformsspecifika detaljer injiceras via lambdas.
|
||||
*/
|
||||
suspend fun createNoteFlow(
|
||||
hasImage: Boolean,
|
||||
uploadImage: suspend () -> Long?, // Körs bara om hasImage = true
|
||||
createNote: suspend (imageId: Long?) -> Boolean, // Skapa klotter med ev. imageId
|
||||
getUploadError: () -> String? = { null } // För t.ex. Api.lastUploadError (valfritt)
|
||||
): CreateNoteOutcome {
|
||||
val imageId: Long? = if (hasImage) {
|
||||
val id = uploadImage()
|
||||
if (id == null) return CreateNoteOutcome.UploadFailed(getUploadError())
|
||||
id
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
val ok = createNote(imageId)
|
||||
return if (ok) CreateNoteOutcome.Success else CreateNoteOutcome.CreateFailed
|
||||
}
|
||||
Reference in New Issue
Block a user