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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.example.util
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import android.util.Base64
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.InputStream
|
||||
|
||||
object ImageUtils {
|
||||
|
||||
suspend fun uriToBase64(context: Context, uri: Uri, maxWidth: Int = 480, maxHeight: Int = 480): String? =
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
var inputStream: InputStream? = context.contentResolver.openInputStream(uri) ?: return@withContext null
|
||||
|
||||
// Decode bounds first
|
||||
val options = BitmapFactory.Options().apply {
|
||||
inJustDecodeBounds = true
|
||||
}
|
||||
BitmapFactory.decodeStream(inputStream, null, options)
|
||||
inputStream?.close()
|
||||
|
||||
// Calculate inSampleSize
|
||||
var inSampleSize = 1
|
||||
if (options.outHeight > maxHeight || options.outWidth > maxWidth) {
|
||||
val halfHeight = options.outHeight / 2
|
||||
val halfWidth = options.outWidth / 2
|
||||
while (halfHeight / inSampleSize >= maxHeight && halfWidth / inSampleSize >= maxWidth) {
|
||||
inSampleSize *= 2
|
||||
}
|
||||
}
|
||||
|
||||
// Decode actual bitmap
|
||||
val decodeOptions = BitmapFactory.Options().apply {
|
||||
this.inSampleSize = inSampleSize
|
||||
}
|
||||
inputStream = context.contentResolver.openInputStream(uri)
|
||||
val originalBitmap = BitmapFactory.decodeStream(inputStream, null, decodeOptions)
|
||||
inputStream?.close()
|
||||
|
||||
if (originalBitmap == null) return@withContext null
|
||||
|
||||
// Scale to exact target bounds
|
||||
val scaledBitmap = if (originalBitmap.width > maxWidth || originalBitmap.height > maxHeight) {
|
||||
val ratio = minOf(
|
||||
maxWidth.toFloat() / originalBitmap.width,
|
||||
maxHeight.toFloat() / originalBitmap.height
|
||||
)
|
||||
val targetW = (originalBitmap.width * ratio).toInt().coerceAtLeast(1)
|
||||
val targetH = (originalBitmap.height * ratio).toInt().coerceAtLeast(1)
|
||||
Bitmap.createScaledBitmap(originalBitmap, targetW, targetH, true)
|
||||
} else {
|
||||
originalBitmap
|
||||
}
|
||||
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 75, outputStream)
|
||||
val bytes = outputStream.toByteArray()
|
||||
Base64.encodeToString(bytes, Base64.NO_WRAP)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun base64ToBitmap(base64Str: String): Bitmap? {
|
||||
return try {
|
||||
val bytes = Base64.decode(base64Str, Base64.DEFAULT)
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user