[Android] 1.9.2
All checks were successful
OpenClimb Docker Deploy / build-and-push (push) Successful in 2m29s
All checks were successful
OpenClimb Docker Deploy / build-and-push (push) Successful in 2m29s
This commit is contained in:
22
android/README.md
Normal file
22
android/README.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# OpenClimb for Android
|
||||
|
||||
This is the native Android app for OpenClimb, built with Kotlin and Jetpack Compose.
|
||||
|
||||
## Project Structure
|
||||
|
||||
This is a standard Android Gradle project. The main code lives in `app/src/main/java/com/atridad/openclimb/`.
|
||||
|
||||
- `data/`: Handles all the app's data.
|
||||
- `database/`: Room database setup (DAOs, entities).
|
||||
- `model/`: Core data models (`Problem`, `Gym`, `ClimbSession`).
|
||||
- `repository/`: Manages the data, providing a clean API for the rest of the app.
|
||||
- `sync/`: Handles talking to the sync server.
|
||||
- `ui/`: All the Jetpack Compose UI code.
|
||||
- `screens/`: The main screens of the app.
|
||||
- `components/`: Reusable UI bits used across screens.
|
||||
- `viewmodel/`: `ClimbViewModel` for managing UI state.
|
||||
- `navigation/`: Navigation graph and routes using Jetpack Navigation.
|
||||
- `service/`: Background service for tracking climbing sessions.
|
||||
- `utils/`: Helpers for things like date formatting and image handling.
|
||||
|
||||
The app is built to be offline-first. All data is stored locally on your device and works without an internet connection.
|
||||
@@ -16,8 +16,8 @@ android {
|
||||
applicationId = "com.atridad.openclimb"
|
||||
minSdk = 31
|
||||
targetSdk = 36
|
||||
versionCode = 38
|
||||
versionName = "1.9.1"
|
||||
versionCode = 39
|
||||
versionName = "1.9.2"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
<!-- Permission for sync functionality -->
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
<!-- Health Connect permissions -->
|
||||
<uses-permission android:name="android.permission.health.READ_EXERCISE" />
|
||||
|
||||
@@ -1,205 +0,0 @@
|
||||
package com.atridad.openclimb.data.migration
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.atridad.openclimb.data.repository.ClimbRepository
|
||||
import com.atridad.openclimb.utils.ImageNamingUtils
|
||||
import com.atridad.openclimb.utils.ImageUtils
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
/**
|
||||
* Service responsible for migrating images to use consistent naming convention across platforms.
|
||||
* This ensures that iOS and Android use the same image filenames for sync compatibility.
|
||||
*/
|
||||
class ImageMigrationService(private val context: Context, private val repository: ClimbRepository) {
|
||||
companion object {
|
||||
private const val TAG = "ImageMigrationService"
|
||||
private const val MIGRATION_PREF_KEY = "image_naming_migration_completed"
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a complete migration of all images in the system to use consistent naming. This
|
||||
* should be called once during app startup after the naming convention is implemented.
|
||||
*/
|
||||
suspend fun performFullMigration(): ImageMigrationResult {
|
||||
Log.i(TAG, "Starting full image naming migration")
|
||||
|
||||
val prefs = context.getSharedPreferences("openclimb_migration", Context.MODE_PRIVATE)
|
||||
if (prefs.getBoolean(MIGRATION_PREF_KEY, false)) {
|
||||
Log.i(TAG, "Image migration already completed, skipping")
|
||||
return ImageMigrationResult.AlreadyCompleted
|
||||
}
|
||||
|
||||
try {
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
val migrationResults = mutableMapOf<String, String>()
|
||||
var migratedCount = 0
|
||||
var errorCount = 0
|
||||
|
||||
Log.i(TAG, "Found ${allProblems.size} problems to check for image migration")
|
||||
|
||||
for (problem in allProblems) {
|
||||
if (problem.imagePaths.isNotEmpty()) {
|
||||
Log.d(
|
||||
TAG,
|
||||
"Migrating images for problem '${problem.name}': ${problem.imagePaths}"
|
||||
)
|
||||
|
||||
try {
|
||||
val problemMigrations =
|
||||
ImageUtils.migrateImageNaming(
|
||||
context = context,
|
||||
problemId = problem.id,
|
||||
currentImagePaths = problem.imagePaths
|
||||
)
|
||||
|
||||
if (problemMigrations.isNotEmpty()) {
|
||||
migrationResults.putAll(problemMigrations)
|
||||
migratedCount += problemMigrations.size
|
||||
|
||||
// Update image paths
|
||||
val newImagePaths =
|
||||
problem.imagePaths.map { oldPath ->
|
||||
problemMigrations[oldPath] ?: oldPath
|
||||
}
|
||||
|
||||
val updatedProblem = problem.copy(imagePaths = newImagePaths)
|
||||
repository.insertProblem(updatedProblem)
|
||||
|
||||
Log.d(
|
||||
TAG,
|
||||
"Updated problem '${problem.name}' with ${problemMigrations.size} migrated images"
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(
|
||||
TAG,
|
||||
"Failed to migrate images for problem '${problem.name}': ${e.message}",
|
||||
e
|
||||
)
|
||||
errorCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mark migration as completed
|
||||
prefs.edit().putBoolean(MIGRATION_PREF_KEY, true).apply()
|
||||
|
||||
Log.i(
|
||||
TAG,
|
||||
"Image migration completed: $migratedCount images migrated, $errorCount errors"
|
||||
)
|
||||
|
||||
return ImageMigrationResult.Success(
|
||||
totalMigrated = migratedCount,
|
||||
errors = errorCount,
|
||||
migrations = migrationResults
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Image migration failed: ${e.message}", e)
|
||||
return ImageMigrationResult.Failed(e.message ?: "Unknown error")
|
||||
}
|
||||
}
|
||||
|
||||
/** Validates that all images in the system follow the consistent naming convention. */
|
||||
suspend fun validateImageNaming(): ValidationResult {
|
||||
try {
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
val validImages = mutableListOf<String>()
|
||||
val invalidImages = mutableListOf<String>()
|
||||
val missingImages = mutableListOf<String>()
|
||||
|
||||
for (problem in allProblems) {
|
||||
for (imagePath in problem.imagePaths) {
|
||||
val filename = imagePath.substringAfterLast('/')
|
||||
|
||||
// Check if file exists
|
||||
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
||||
if (!imageFile.exists()) {
|
||||
missingImages.add(imagePath)
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if filename follows convention
|
||||
if (ImageNamingUtils.isValidImageFilename(filename)) {
|
||||
validImages.add(imagePath)
|
||||
} else {
|
||||
invalidImages.add(imagePath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ValidationResult(
|
||||
totalImages = validImages.size + invalidImages.size + missingImages.size,
|
||||
validImages = validImages,
|
||||
invalidImages = invalidImages,
|
||||
missingImages = missingImages
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Image validation failed: ${e.message}", e)
|
||||
return ValidationResult(
|
||||
totalImages = 0,
|
||||
validImages = emptyList(),
|
||||
invalidImages = emptyList(),
|
||||
missingImages = emptyList()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Migrates images for a specific problem during sync operations. */
|
||||
suspend fun migrateProblemImages(
|
||||
problemId: String,
|
||||
currentImagePaths: List<String>
|
||||
): Map<String, String> {
|
||||
return try {
|
||||
ImageUtils.migrateImageNaming(context, problemId, currentImagePaths)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to migrate images for problem $problemId: ${e.message}", e)
|
||||
emptyMap()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans up any orphaned image files that don't follow our naming convention and aren't
|
||||
* referenced by any problems.
|
||||
*/
|
||||
suspend fun cleanupOrphanedImages() {
|
||||
try {
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
val referencedPaths = allProblems.flatMap { it.imagePaths }.toSet()
|
||||
|
||||
ImageUtils.cleanupOrphanedImages(context, referencedPaths)
|
||||
|
||||
Log.i(TAG, "Orphaned image cleanup completed")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to cleanup orphaned images: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Result of an image migration operation */
|
||||
sealed class ImageMigrationResult {
|
||||
object AlreadyCompleted : ImageMigrationResult()
|
||||
|
||||
data class Success(
|
||||
val totalMigrated: Int,
|
||||
val errors: Int,
|
||||
val migrations: Map<String, String>
|
||||
) : ImageMigrationResult()
|
||||
|
||||
data class Failed(val error: String) : ImageMigrationResult()
|
||||
}
|
||||
|
||||
/** Result of image naming validation */
|
||||
data class ValidationResult(
|
||||
val totalImages: Int,
|
||||
val validImages: List<String>,
|
||||
val invalidImages: List<String>,
|
||||
val missingImages: List<String>
|
||||
) {
|
||||
val isAllValid: Boolean
|
||||
get() = invalidImages.isEmpty() && missingImages.isEmpty()
|
||||
|
||||
val validPercentage: Double
|
||||
get() = if (totalImages == 0) 100.0 else (validImages.size.toDouble() / totalImages) * 100
|
||||
}
|
||||
@@ -75,25 +75,4 @@ data class Attempt(
|
||||
}
|
||||
}
|
||||
|
||||
fun updated(
|
||||
result: AttemptResult? = null,
|
||||
highestHold: String? = null,
|
||||
notes: String? = null,
|
||||
duration: Long? = null,
|
||||
restTime: Long? = null
|
||||
): Attempt {
|
||||
return Attempt(
|
||||
id = this.id,
|
||||
sessionId = this.sessionId,
|
||||
problemId = this.problemId,
|
||||
result = result ?: this.result,
|
||||
highestHold = highestHold ?: this.highestHold,
|
||||
notes = notes ?: this.notes,
|
||||
duration = duration ?: this.duration,
|
||||
restTime = restTime ?: this.restTime,
|
||||
timestamp = this.timestamp,
|
||||
createdAt = this.createdAt,
|
||||
updatedAt = DateFormatUtils.nowISO8601()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +207,7 @@ data class DifficultyGrade(val system: DifficultySystem, val grade: String, val
|
||||
private fun compareVScaleGrades(grade1: String, grade2: String): Int {
|
||||
if (grade1 == "VB" && grade2 != "VB") return -1
|
||||
if (grade2 == "VB" && grade1 != "VB") return 1
|
||||
if (grade1 == "VB" && grade2 == "VB") return 0
|
||||
if (grade1 == "VB") return 0
|
||||
|
||||
val num1 = grade1.removePrefix("V").toIntOrNull() ?: 0
|
||||
val num2 = grade2.removePrefix("V").toIntOrNull() ?: 0
|
||||
|
||||
@@ -17,8 +17,6 @@ import com.atridad.openclimb.utils.ZipExportImportUtils
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
class ClimbRepository(database: OpenClimbDatabase, private val context: Context) {
|
||||
@@ -288,7 +286,7 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
try {
|
||||
val deletion = json.decodeFromString<DeletedItem>(value)
|
||||
deletions.add(deletion)
|
||||
} catch (e: Exception) {
|
||||
} catch (_: Exception) {
|
||||
// Invalid deletion record, ignore
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.util.Log
|
||||
import com.atridad.openclimb.utils.DateFormatUtils
|
||||
import androidx.core.content.edit
|
||||
|
||||
/**
|
||||
* Manages the overall data state timestamp for sync purposes. This tracks when any data in the
|
||||
@@ -35,7 +36,7 @@ class DataStateManager(context: Context) {
|
||||
*/
|
||||
fun updateDataState() {
|
||||
val now = DateFormatUtils.nowISO8601()
|
||||
prefs.edit().putString(KEY_LAST_MODIFIED, now).apply()
|
||||
prefs.edit { putString(KEY_LAST_MODIFIED, now) }
|
||||
Log.d(TAG, "Data state updated to: $now")
|
||||
}
|
||||
|
||||
@@ -48,21 +49,6 @@ class DataStateManager(context: Context) {
|
||||
?: DateFormatUtils.nowISO8601()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the data state timestamp to a specific value. Used when importing data from server to
|
||||
* sync the state.
|
||||
*/
|
||||
fun setLastModified(timestamp: String) {
|
||||
prefs.edit().putString(KEY_LAST_MODIFIED, timestamp).apply()
|
||||
Log.d(TAG, "Data state set to: $timestamp")
|
||||
}
|
||||
|
||||
/** Resets the data state (for testing or complete data wipe). */
|
||||
fun reset() {
|
||||
prefs.edit().clear().apply()
|
||||
Log.d(TAG, "Data state reset")
|
||||
}
|
||||
|
||||
/** Checks if the data state has been initialized. */
|
||||
private fun isInitialized(): Boolean {
|
||||
return prefs.getBoolean(KEY_INITIALIZED, false)
|
||||
@@ -70,11 +56,7 @@ class DataStateManager(context: Context) {
|
||||
|
||||
/** Marks the data state as initialized. */
|
||||
private fun markAsInitialized() {
|
||||
prefs.edit().putBoolean(KEY_INITIALIZED, true).apply()
|
||||
prefs.edit { putBoolean(KEY_INITIALIZED, true) }
|
||||
}
|
||||
|
||||
/** Gets debug information about the current state. */
|
||||
fun getDebugInfo(): String {
|
||||
return "DataState(lastModified=${getLastModified()}, initialized=${isInitialized()})"
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -88,11 +88,7 @@ class SessionTrackingService : Service() {
|
||||
|
||||
return START_REDELIVER_INTENT
|
||||
}
|
||||
|
||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||
super.onTaskRemoved(rootIntent)
|
||||
}
|
||||
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? = null
|
||||
|
||||
private fun startSessionTracking(sessionId: String) {
|
||||
@@ -153,7 +149,7 @@ class SessionTrackingService : Service() {
|
||||
return try {
|
||||
val activeNotifications = notificationManager.activeNotifications
|
||||
activeNotifications.any { it.id == NOTIFICATION_ID }
|
||||
} catch (e: Exception) {
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.CheckCircle
|
||||
import androidx.compose.material.icons.filled.Image
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
@@ -13,11 +16,11 @@ import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.openclimb.R
|
||||
import com.atridad.openclimb.data.model.Attempt
|
||||
import com.atridad.openclimb.data.model.AttemptResult
|
||||
import com.atridad.openclimb.data.model.ClimbType
|
||||
import com.atridad.openclimb.data.model.Gym
|
||||
import com.atridad.openclimb.data.model.Problem
|
||||
import com.atridad.openclimb.ui.components.FullscreenImageViewer
|
||||
import com.atridad.openclimb.ui.components.ImageDisplay
|
||||
import com.atridad.openclimb.ui.components.SyncIndicator
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
|
||||
@@ -26,10 +29,8 @@ import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String) -> Unit) {
|
||||
val problems by viewModel.problems.collectAsState()
|
||||
val gyms by viewModel.gyms.collectAsState()
|
||||
val attempts by viewModel.attempts.collectAsState()
|
||||
val context = LocalContext.current
|
||||
var showImageViewer by remember { mutableStateOf(false) }
|
||||
var selectedImagePaths by remember { mutableStateOf<List<String>>(emptyList()) }
|
||||
var selectedImageIndex by remember { mutableIntStateOf(0) }
|
||||
|
||||
// Filter state
|
||||
var selectedClimbType by remember { mutableStateOf<ClimbType?>(null) }
|
||||
@@ -178,12 +179,8 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
||||
ProblemCard(
|
||||
problem = problem,
|
||||
gymName = gyms.find { it.id == problem.gymId }?.name ?: "Unknown Gym",
|
||||
attempts = attempts,
|
||||
onClick = { onNavigateToProblemDetail(problem.id) },
|
||||
onImageClick = { imagePaths, index ->
|
||||
selectedImagePaths = imagePaths
|
||||
selectedImageIndex = index
|
||||
showImageViewer = true
|
||||
},
|
||||
onToggleActive = {
|
||||
val updatedProblem = problem.copy(isActive = !problem.isActive)
|
||||
viewModel.updateProblem(updatedProblem, context)
|
||||
@@ -194,15 +191,6 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fullscreen Image Viewer
|
||||
if (showImageViewer && selectedImagePaths.isNotEmpty()) {
|
||||
FullscreenImageViewer(
|
||||
imagePaths = selectedImagePaths,
|
||||
initialIndex = selectedImageIndex,
|
||||
onDismiss = { showImageViewer = false }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@@ -210,10 +198,17 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
||||
fun ProblemCard(
|
||||
problem: Problem,
|
||||
gymName: String,
|
||||
attempts: List<Attempt>,
|
||||
onClick: () -> Unit,
|
||||
onImageClick: ((List<String>, Int) -> Unit)? = null,
|
||||
onToggleActive: (() -> Unit)? = null
|
||||
) {
|
||||
val isCompleted =
|
||||
attempts.any { attempt ->
|
||||
attempt.problemId == problem.id &&
|
||||
(attempt.result == AttemptResult.SUCCESS ||
|
||||
attempt.result == AttemptResult.FLASH)
|
||||
}
|
||||
|
||||
Card(onClick = onClick, modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
|
||||
Row(
|
||||
@@ -242,12 +237,35 @@ fun ProblemCard(
|
||||
}
|
||||
|
||||
Column(horizontalAlignment = Alignment.End) {
|
||||
Text(
|
||||
text = problem.difficulty.grade,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
if (problem.imagePaths.isNotEmpty()) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Image,
|
||||
contentDescription = "Has images",
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
|
||||
if (isCompleted) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CheckCircle,
|
||||
contentDescription = "Completed",
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.tertiary
|
||||
)
|
||||
}
|
||||
|
||||
Text(
|
||||
text = problem.difficulty.grade,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
|
||||
Text(
|
||||
text = problem.climbType.getDisplayName(),
|
||||
@@ -279,16 +297,6 @@ fun ProblemCard(
|
||||
}
|
||||
}
|
||||
|
||||
// Display images if any
|
||||
if (problem.imagePaths.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
ImageDisplay(
|
||||
imagePaths = problem.imagePaths.take(3), // Show max 3 images in list
|
||||
imageSize = 60,
|
||||
onImageClick = { index -> onImageClick?.invoke(problem.imagePaths, index) }
|
||||
)
|
||||
}
|
||||
|
||||
if (!problem.isActive) {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Text(
|
||||
|
||||
@@ -44,9 +44,9 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
var showResetDialog by remember { mutableStateOf(false) }
|
||||
var showSyncConfigDialog by remember { mutableStateOf(false) }
|
||||
var showDisconnectDialog by remember { mutableStateOf(false) }
|
||||
var showFixImagesDialog by remember { mutableStateOf(false) }
|
||||
|
||||
var showDeleteImagesDialog by remember { mutableStateOf(false) }
|
||||
var isFixingImages by remember { mutableStateOf(false) }
|
||||
|
||||
var isDeletingImages by remember { mutableStateOf(false) }
|
||||
|
||||
// Sync configuration state
|
||||
@@ -484,46 +484,6 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Card(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.surfaceVariant.copy(
|
||||
alpha = 0.3f
|
||||
)
|
||||
)
|
||||
) {
|
||||
ListItem(
|
||||
headlineContent = { Text("Fix Image Names") },
|
||||
supportingContent = {
|
||||
Text(
|
||||
"Rename all images to use consistent naming across devices"
|
||||
)
|
||||
},
|
||||
leadingContent = {
|
||||
Icon(Icons.Default.Build, contentDescription = null)
|
||||
},
|
||||
trailingContent = {
|
||||
TextButton(
|
||||
onClick = { showFixImagesDialog = true },
|
||||
enabled = !isFixingImages && !uiState.isLoading
|
||||
) {
|
||||
if (isFixingImages) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(16.dp),
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
} else {
|
||||
Text("Fix Names")
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Card(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors =
|
||||
@@ -1005,35 +965,6 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
)
|
||||
}
|
||||
|
||||
// Fix Image Names dialog
|
||||
if (showFixImagesDialog) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showFixImagesDialog = false },
|
||||
title = { Text("Fix Image Names") },
|
||||
text = {
|
||||
Text(
|
||||
"This will rename all existing image files to use a consistent naming system across devices.\n\nThis improves sync reliability between iOS and Android. Your images will not be lost, only renamed.\n\nThis is safe to run multiple times."
|
||||
)
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
isFixingImages = true
|
||||
showFixImagesDialog = false
|
||||
coroutineScope.launch {
|
||||
viewModel.migrateImageNamesToDeterministic(context)
|
||||
isFixingImages = false
|
||||
viewModel.setMessage("Image names fixed successfully!")
|
||||
}
|
||||
}
|
||||
) { Text("Fix Names") }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showFixImagesDialog = false }) { Text("Cancel") }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Delete All Images dialog
|
||||
if (showDeleteImagesDialog) {
|
||||
AlertDialog(
|
||||
|
||||
@@ -171,64 +171,6 @@ class ClimbViewModel(
|
||||
val referencedImagePaths = allProblems.flatMap { it.imagePaths }.toSet()
|
||||
ImageUtils.cleanupOrphanedImages(context, referencedImagePaths)
|
||||
}
|
||||
fun migrateImageNamesToDeterministic(context: Context) {
|
||||
viewModelScope.launch {
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
var migrationCount = 0
|
||||
val updatedProblems = mutableListOf<Problem>()
|
||||
|
||||
for (problem in allProblems) {
|
||||
if (problem.imagePaths.isEmpty()) continue
|
||||
|
||||
var newImagePaths = mutableListOf<String>()
|
||||
var problemNeedsUpdate = false
|
||||
|
||||
for ((index, imagePath) in problem.imagePaths.withIndex()) {
|
||||
val currentFilename = File(imagePath).name
|
||||
|
||||
if (ImageNamingUtils.isValidImageFilename(currentFilename)) {
|
||||
newImagePaths.add(imagePath)
|
||||
continue
|
||||
}
|
||||
|
||||
val deterministicName =
|
||||
ImageNamingUtils.generateImageFilename(problem.id, index)
|
||||
|
||||
val imagesDir = ImageUtils.getImagesDirectory(context)
|
||||
val oldFile = File(imagesDir, currentFilename)
|
||||
val newFile = File(imagesDir, deterministicName)
|
||||
|
||||
if (oldFile.exists()) {
|
||||
if (oldFile.renameTo(newFile)) {
|
||||
newImagePaths.add(deterministicName)
|
||||
problemNeedsUpdate = true
|
||||
migrationCount++
|
||||
println("Migrated: $currentFilename → $deterministicName")
|
||||
} else {
|
||||
println("Failed to migrate $currentFilename")
|
||||
newImagePaths.add(imagePath)
|
||||
}
|
||||
} else {
|
||||
println("Warning: Image file not found: $currentFilename")
|
||||
newImagePaths.add(imagePath)
|
||||
}
|
||||
}
|
||||
|
||||
if (problemNeedsUpdate) {
|
||||
val updatedProblem = problem.copy(imagePaths = newImagePaths)
|
||||
updatedProblems.add(updatedProblem)
|
||||
}
|
||||
}
|
||||
|
||||
for (updatedProblem in updatedProblems) {
|
||||
repository.insertProblemWithoutSync(updatedProblem)
|
||||
}
|
||||
|
||||
println(
|
||||
"Migration completed: $migrationCount images renamed, ${updatedProblems.size} problems updated"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteAllImages(context: Context) {
|
||||
viewModelScope.launch {
|
||||
|
||||
Reference in New Issue
Block a user