mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
build: initialize project structure
Set up Android project configuration, Gradle build scripts, typography, and default resource assets.
This commit is contained in:
1 parent
0a595f619a
commit
f13d46f778
65 files changed
+6056
-11
No files matched your search
@@ -0,0 +1,105 @@
|
||||
package com.example.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.core.content.FileProvider
|
||||
import com.example.data.model.HungerSatietyScale
|
||||
import com.example.data.model.MealEntry
|
||||
import com.example.data.model.MealType
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
object CsvExporter {
|
||||
|
||||
fun buildDefaultFileName(): String {
|
||||
val dateStr = SimpleDateFormat("yyyy-MM-dd", Locale.FRENCH).format(Date())
|
||||
return "satiea_repas_$dateStr.csv"
|
||||
}
|
||||
|
||||
fun generateCsvContent(meals: List<MealEntry>): String {
|
||||
val dateFormat = SimpleDateFormat("dd/MM/yyyy", Locale.FRENCH)
|
||||
val timeFormat = SimpleDateFormat("HH:mm", Locale.FRENCH)
|
||||
|
||||
val sb = StringBuilder()
|
||||
// UTF-8 BOM for proper French accent display in Excel / LibreOffice / Google Sheets
|
||||
sb.append('\uFEFF')
|
||||
sb.appendLine("Date,Heure,Type de repas,Faim avant (/10),Ressenti faim,Satiété après (/10),Ressenti satiété,Aliments & Poids,Note globale,Photos jointes")
|
||||
|
||||
val sortedMeals = meals.sortedByDescending { it.mealTimestamp }
|
||||
for (meal in sortedMeals) {
|
||||
val dateObj = Date(meal.mealTimestamp)
|
||||
val dateStr = dateFormat.format(dateObj)
|
||||
val timeStr = timeFormat.format(dateObj)
|
||||
val mealTypeLabel = MealType.fromString(meal.mealType).displayName
|
||||
val hungerLabel = HungerSatietyScale.getHungerLabel(meal.hungerBefore)
|
||||
val satietyLabel = HungerSatietyScale.getSatietyLabel(meal.satietyAfter)
|
||||
val foodsFormatted = meal.parsedFoodItems().joinToString(" ; ") { it.displayCsv() }
|
||||
val cleanNote = escapeCsvField(meal.note)
|
||||
val photoCount = meal.photosBase64.size
|
||||
|
||||
sb.append(escapeCsvField(dateStr)).append(',')
|
||||
sb.append(escapeCsvField(timeStr)).append(',')
|
||||
sb.append(escapeCsvField(mealTypeLabel)).append(',')
|
||||
sb.append(meal.hungerBefore).append(',')
|
||||
sb.append(escapeCsvField(hungerLabel)).append(',')
|
||||
sb.append(meal.satietyAfter).append(',')
|
||||
sb.append(escapeCsvField(satietyLabel)).append(',')
|
||||
sb.append(escapeCsvField(foodsFormatted)).append(',')
|
||||
sb.append(cleanNote).append(',')
|
||||
sb.append(photoCount)
|
||||
sb.appendLine()
|
||||
}
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
private fun escapeCsvField(value: String): String {
|
||||
val sanitized = value.replace("\r\n", " ").replace("\n", " ").trim()
|
||||
return if (sanitized.contains(",") || sanitized.contains("\"") || sanitized.contains(";")) {
|
||||
"\"${sanitized.replace("\"", "\"\"")}\""
|
||||
} else {
|
||||
sanitized
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun writeCsvToUri(context: Context, targetUri: Uri, meals: List<MealEntry>): Result<Int> =
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val csvContent = generateCsvContent(meals)
|
||||
context.contentResolver.openOutputStream(targetUri)?.use { outputStream ->
|
||||
outputStream.write(csvContent.toByteArray(Charsets.UTF_8))
|
||||
outputStream.flush()
|
||||
} ?: return@withContext Result.failure(IllegalStateException("Impossible d'ouvrir le fichier de destination"))
|
||||
Result.success(meals.size)
|
||||
} catch (e: Exception) {
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun shareCsvFile(context: Context, meals: List<MealEntry>): Result<Intent> =
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val csvContent = generateCsvContent(meals)
|
||||
val exportDir = File(context.cacheDir, "exports").apply { mkdirs() }
|
||||
val csvFile = File(exportDir, buildDefaultFileName())
|
||||
csvFile.writeText(csvContent, Charsets.UTF_8)
|
||||
|
||||
val authority = "${context.packageName}.fileprovider"
|
||||
val contentUri = FileProvider.getUriForFile(context, authority, csvFile)
|
||||
|
||||
val shareIntent = Intent(Intent.ACTION_SEND).apply {
|
||||
type = "text/csv"
|
||||
putExtra(Intent.EXTRA_SUBJECT, "Export Satiéa — Journal des repas")
|
||||
putExtra(Intent.EXTRA_STREAM, contentUri)
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
}
|
||||
Result.success(Intent.createChooser(shareIntent, "Partager le fichier CSV"))
|
||||
} catch (e: Exception) {
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user