Compare commits

...

6 Commits

Author SHA1 Message Date
77f8110d85 [Android] 1.9.0 2025-10-11 23:23:24 -06:00
53fa74cc83 iOS Build 23 2025-10-11 18:54:24 -06:00
e7c46634da iOS Build 22 2025-10-10 17:09:23 -06:00
40efd6636f Build 21 2025-10-10 16:32:10 -06:00
719181aa16 iOS Build 20 2025-10-10 13:36:07 -06:00
790b7075c5 [iOS] 1.4.0 - Apple Fitness Integration! 2025-10-10 11:44:33 -06:00
50 changed files with 3272 additions and 404 deletions

View File

@@ -12,6 +12,7 @@ For Android do one of the following:
For iOS: For iOS:
Download from the AppStore [here](https://apps.apple.com/ca/app/openclimb/id6752592783)! Download from the AppStore [here](https://apps.apple.com/ca/app/openclimb/id6752592783)!
For development builds, sign up for the TestFlight [here](https://testflight.apple.com/join/88RtxV4J)!
## Self-Hosted Sync Server ## Self-Hosted Sync Server

View File

@@ -16,8 +16,8 @@ android {
applicationId = "com.atridad.openclimb" applicationId = "com.atridad.openclimb"
minSdk = 31 minSdk = 31
targetSdk = 36 targetSdk = 36
versionCode = 35 versionCode = 37
versionName = "1.8.0" versionName = "1.9.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
} }
@@ -60,6 +60,7 @@ dependencies {
// Room Database // Room Database
implementation(libs.androidx.room.runtime) implementation(libs.androidx.room.runtime)
implementation(libs.androidx.room.ktx) implementation(libs.androidx.room.ktx)
implementation(libs.androidx.exifinterface)
ksp(libs.androidx.room.compiler) ksp(libs.androidx.room.compiler)
@@ -78,6 +79,9 @@ dependencies {
// Image Loading // Image Loading
implementation(libs.coil.compose) implementation(libs.coil.compose)
// Health Connect
implementation("androidx.health.connect:connect-client:1.1.0-alpha07")
// Testing // Testing
testImplementation(libs.junit) testImplementation(libs.junit)
testImplementation(libs.mockk) testImplementation(libs.mockk)

View File

@@ -11,6 +11,16 @@
<!-- Permission for sync functionality --> <!-- Permission for sync functionality -->
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<!-- Health Connect permissions -->
<uses-permission android:name="android.permission.health.READ_EXERCISE" />
<uses-permission android:name="android.permission.health.WRITE_EXERCISE" />
<uses-permission android:name="android.permission.health.READ_HEART_RATE" />
<uses-permission android:name="android.permission.health.WRITE_HEART_RATE" />
<uses-permission android:name="android.permission.health.READ_ACTIVE_CALORIES_BURNED" />
<uses-permission android:name="android.permission.health.WRITE_ACTIVE_CALORIES_BURNED" />
<uses-permission android:name="android.permission.health.READ_TOTAL_CALORIES_BURNED" />
<uses-permission android:name="android.permission.health.WRITE_TOTAL_CALORIES_BURNED" />
<!-- Hardware features --> <!-- Hardware features -->
<uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera" android:required="false" />
@@ -19,6 +29,18 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<!-- Health Connect queries -->
<queries>
<package android:name="com.google.android.apps.healthdata" />
<intent>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW_PERMISSION_USAGE"/>
<category android:name="android.intent.category.HEALTH_PERMISSIONS"/>
</intent>
</queries>
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
@@ -40,6 +62,16 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
<!-- Health Connect permission rationale handling -->
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
</intent-filter>
<!-- Permission handling for Android 14 and later -->
<intent-filter>
<action android:name="android.intent.action.VIEW_PERMISSION_USAGE"/>
<category android:name="android.intent.category.HEALTH_PERMISSIONS"/>
</intent-filter>
</activity> </activity>

View File

@@ -0,0 +1,426 @@
package com.atridad.openclimb.data.health
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import androidx.activity.result.contract.ActivityResultContract
import androidx.health.connect.client.HealthConnectClient
import androidx.health.connect.client.PermissionController
import androidx.health.connect.client.permission.HealthPermission
import androidx.health.connect.client.records.ExerciseSessionRecord
import androidx.health.connect.client.records.HeartRateRecord
import androidx.health.connect.client.records.TotalCaloriesBurnedRecord
import androidx.health.connect.client.units.Energy
import com.atridad.openclimb.data.model.ClimbSession
import com.atridad.openclimb.data.model.SessionStatus
import com.atridad.openclimb.utils.DateFormatUtils
import java.time.Duration
import java.time.Instant
import java.time.ZoneOffset
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.flow
/**
* Health Connect manager for OpenClimb that syncs climbing sessions to Samsung Health, Google Fit,
* and other health apps.
*/
@SuppressLint("RestrictedApi")
class HealthConnectManager(private val context: Context) {
private val preferences: SharedPreferences =
context.getSharedPreferences("health_connect_prefs", Context.MODE_PRIVATE)
private val _isEnabled = MutableStateFlow(preferences.getBoolean("enabled", false))
private val _hasPermissions = MutableStateFlow(preferences.getBoolean("permissions", false))
private val _autoSync = MutableStateFlow(preferences.getBoolean("auto_sync", true))
private val _isCompatible = MutableStateFlow(true)
val isEnabled: Flow<Boolean> = _isEnabled.asStateFlow()
val hasPermissions: Flow<Boolean> = _hasPermissions.asStateFlow()
val autoSyncEnabled: Flow<Boolean> = _autoSync.asStateFlow()
val isCompatible: Flow<Boolean> = _isCompatible.asStateFlow()
companion object {
private const val TAG = "HealthConnectManager"
val REQUIRED_PERMISSIONS =
setOf(
HealthPermission.getReadPermission(ExerciseSessionRecord::class),
HealthPermission.getWritePermission(ExerciseSessionRecord::class),
HealthPermission.getReadPermission(HeartRateRecord::class),
HealthPermission.getWritePermission(HeartRateRecord::class),
HealthPermission.getReadPermission(TotalCaloriesBurnedRecord::class),
HealthPermission.getWritePermission(TotalCaloriesBurnedRecord::class)
)
}
private val healthConnectClient by lazy {
try {
HealthConnectClient.getOrCreate(context)
} catch (e: Exception) {
Log.e(TAG, "Failed to create Health Connect client", e)
_isCompatible.value = false
null
}
}
/** Check if Health Connect is available on this device */
fun isHealthConnectAvailable(): Flow<Boolean> = flow {
try {
if (!_isCompatible.value) {
emit(false)
return@flow
}
val status = HealthConnectClient.getSdkStatus(context)
emit(status == HealthConnectClient.SDK_AVAILABLE)
} catch (e: Exception) {
Log.e(TAG, "Error checking Health Connect availability", e)
_isCompatible.value = false
emit(false)
}
}
/** Enable or disable Health Connect integration */
fun setEnabled(enabled: Boolean) {
preferences.edit().putBoolean("enabled", enabled).apply()
_isEnabled.value = enabled
if (!enabled) {
setPermissionsGranted(false)
}
}
/** Update the permissions granted state */
fun setPermissionsGranted(granted: Boolean) {
preferences.edit().putBoolean("permissions", granted).apply()
_hasPermissions.value = granted
}
/** Enable or disable auto-sync */
fun setAutoSyncEnabled(enabled: Boolean) {
preferences.edit().putBoolean("auto_sync", enabled).apply()
_autoSync.value = enabled
}
/** Check if all required permissions are granted */
suspend fun hasAllPermissions(): Boolean {
return try {
if (!_isCompatible.value || healthConnectClient == null) {
return false
}
val grantedPermissions =
healthConnectClient!!.permissionController.getGrantedPermissions()
val hasAll =
REQUIRED_PERMISSIONS.all { permission ->
grantedPermissions.contains(permission)
}
setPermissionsGranted(hasAll)
hasAll
} catch (e: Exception) {
Log.e(TAG, "Error checking permissions", e)
setPermissionsGranted(false)
false
}
}
/** Check if Health Connect is ready for use */
suspend fun isReady(): Boolean {
return try {
if (!_isEnabled.value || !_isCompatible.value || healthConnectClient == null)
return false
val isAvailable =
HealthConnectClient.getSdkStatus(context) == HealthConnectClient.SDK_AVAILABLE
val hasPerms = if (isAvailable) hasAllPermissions() else false
isAvailable && hasPerms
} catch (e: Exception) {
Log.e(TAG, "Error checking Health Connect readiness", e)
false
}
}
/** Get permission request contract */
fun getPermissionRequestContract(): ActivityResultContract<Set<String>, Set<String>> {
return PermissionController.createRequestPermissionResultContract()
}
/** Test Health Connect functionality */
fun testHealthConnectSync(): String {
val results = mutableListOf<String>()
results.add("=== Health Connect Debug Test ===")
try {
// Check availability synchronously
val packageManager = context.packageManager
val healthConnectPackages =
listOf(
"com.google.android.apps.healthdata",
"com.android.health.connect",
"androidx.health.connect"
)
val available =
healthConnectPackages.any { packageName ->
try {
packageManager.getPackageInfo(packageName, 0)
true
} catch (e: Exception) {
false
}
}
results.add("Available: $available")
// Check enabled state
results.add("Enabled in settings: ${_isEnabled.value}")
// Check permissions (simplified)
val hasPerms = _hasPermissions.value
results.add("Has permissions: $hasPerms")
// Check compatibility
results.add("API Compatible: ${_isCompatible.value}")
val ready = _isEnabled.value && _isCompatible.value && available && hasPerms
results.add("Ready to sync: $ready")
if (ready) {
results.add("Health Connect is connected!")
} else {
results.add("❌ Health Connect not ready")
if (!available) results.add("- Health Connect not available on device")
if (!_isEnabled.value) results.add("- Not enabled in OpenClimb settings")
if (!hasPerms) results.add("- Permissions not granted")
if (!_isCompatible.value) results.add("- API compatibility issues")
}
} catch (e: Exception) {
results.add("Test failed with error: ${e.message}")
Log.e(TAG, "Health Connect test failed", e)
}
return results.joinToString("\n")
}
/** Get required permissions as strings */
fun getRequiredPermissions(): Set<String> {
return try {
REQUIRED_PERMISSIONS.map { it.toString() }.toSet()
} catch (e: Exception) {
Log.e(TAG, "Error getting required permissions", e)
emptySet()
}
}
/** Sync a completed climbing session to Health Connect */
@SuppressLint("RestrictedApi")
suspend fun syncClimbingSession(
session: ClimbSession,
gymName: String,
attemptCount: Int = 0
): Result<Unit> {
return try {
if (!isReady()) {
return Result.failure(IllegalStateException("Health Connect not ready"))
}
if (session.status != SessionStatus.COMPLETED) {
return Result.failure(
IllegalArgumentException("Only completed sessions can be synced")
)
}
val startTime = session.startTime?.let { DateFormatUtils.parseISO8601(it) }
val endTime = session.endTime?.let { DateFormatUtils.parseISO8601(it) }
if (startTime == null || endTime == null) {
return Result.failure(
IllegalArgumentException("Session must have valid start and end times")
)
}
Log.d(TAG, "Attempting to sync session '${session.id}' to Health Connect...")
val records = mutableListOf<androidx.health.connect.client.records.Record>()
try {
val exerciseSession =
ExerciseSessionRecord(
startTime = startTime,
startZoneOffset =
ZoneOffset.systemDefault().rules.getOffset(startTime),
endTime = endTime,
endZoneOffset = ZoneOffset.systemDefault().rules.getOffset(endTime),
exerciseType =
ExerciseSessionRecord.EXERCISE_TYPE_STRENGTH_TRAINING,
title = "Rock Climbing at $gymName"
)
records.add(exerciseSession)
} catch (e: Exception) {
Log.w(TAG, "Failed to create exercise session record", e)
}
try {
val durationMinutes = Duration.between(startTime, endTime).toMinutes()
val estimatedCalories = estimateCaloriesForClimbing(durationMinutes, attemptCount)
if (estimatedCalories > 0) {
val caloriesRecord =
TotalCaloriesBurnedRecord(
startTime = startTime,
startZoneOffset =
ZoneOffset.systemDefault().rules.getOffset(startTime),
endTime = endTime,
endZoneOffset =
ZoneOffset.systemDefault().rules.getOffset(endTime),
energy = Energy.calories(estimatedCalories)
)
records.add(caloriesRecord)
}
} catch (e: Exception) {
Log.w(TAG, "Failed to create calories record", e)
}
try {
val heartRateRecord = createHeartRateRecord(startTime, endTime, attemptCount)
heartRateRecord?.let { records.add(it) }
} catch (e: Exception) {
Log.w(TAG, "Failed to create heart rate record", e)
}
if (records.isNotEmpty() && healthConnectClient != null) {
Log.d(TAG, "Writing ${records.size} records to Health Connect...")
healthConnectClient!!.insertRecords(records)
Log.i(
TAG,
"Successfully synced ${records.size} records for session '${session.id}' to Health Connect"
)
preferences
.edit()
.putString("last_sync_success", DateFormatUtils.nowISO8601())
.apply()
} else {
val reason =
when {
records.isEmpty() -> "No records created"
healthConnectClient == null -> "Health Connect client unavailable"
else -> "Unknown reason"
}
Log.w(TAG, "Sync failed for session '${session.id}': $reason")
return Result.failure(Exception("Sync failed: $reason"))
}
Result.success(Unit)
} catch (e: Exception) {
Log.e(TAG, "Error syncing climbing session to Health Connect", e)
Result.failure(e)
}
}
/** Auto-sync a session if enabled */
suspend fun autoSyncSession(
session: ClimbSession,
gymName: String,
attemptCount: Int = 0
): Result<Unit> {
return if (_autoSync.value && isReady()) {
Log.d(TAG, "Auto-syncing session '${session.id}' to Health Connect...")
syncClimbingSession(session, gymName, attemptCount)
} else {
val reason =
when {
!_autoSync.value -> "auto-sync disabled"
!isReady() -> "Health Connect not ready"
else -> "unknown reason"
}
Log.d(TAG, "Auto-sync skipped for session '${session.id}': $reason")
Result.success(Unit)
}
}
/** Estimate calories burned during climbing */
private fun estimateCaloriesForClimbing(durationMinutes: Long, attemptCount: Int): Double {
val baseCaloriesPerMinute = 8.0
val intensityMultiplier =
when {
attemptCount >= 20 -> 1.3
attemptCount >= 10 -> 1.1
else -> 0.9
}
return durationMinutes * baseCaloriesPerMinute * intensityMultiplier
}
/** Create heart rate data */
@SuppressLint("RestrictedApi")
private fun createHeartRateRecord(
startTime: Instant,
endTime: Instant,
attemptCount: Int
): HeartRateRecord? {
return try {
val samples = mutableListOf<HeartRateRecord.Sample>()
val intervalMinutes = 5L
val baseHeartRate =
when {
attemptCount >= 20 -> 155L
attemptCount >= 10 -> 145L
else -> 135L
}
var currentTime = startTime
while (currentTime.isBefore(endTime)) {
val variation = (-15..15).random()
val heartRate = (baseHeartRate + variation).coerceIn(110L, 180L)
samples.add(HeartRateRecord.Sample(time = currentTime, beatsPerMinute = heartRate))
currentTime = currentTime.plusSeconds(intervalMinutes * 60)
}
if (samples.isEmpty()) return null
HeartRateRecord(
startTime = startTime,
startZoneOffset = ZoneOffset.systemDefault().rules.getOffset(startTime),
endTime = endTime,
endZoneOffset = ZoneOffset.systemDefault().rules.getOffset(endTime),
samples = samples
)
} catch (e: Exception) {
Log.e(TAG, "Error creating heart rate record", e)
null
}
}
/** Reset all preferences */
fun reset() {
preferences.edit().clear().apply()
_isEnabled.value = false
_hasPermissions.value = false
_autoSync.value = true
}
/** Check if ready for use */
fun isReadySync(): Boolean {
return _isEnabled.value && _hasPermissions.value
}
/** Get last successful sync timestamp */
fun getLastSyncSuccess(): String? {
return preferences.getString("last_sync_success", null)
}
/** Get detailed status */
fun getDetailedStatus(): Map<String, String> {
return mapOf(
"enabled" to _isEnabled.value.toString(),
"hasPermissions" to _hasPermissions.value.toString(),
"autoSync" to _autoSync.value.toString(),
"compatible" to _isCompatible.value.toString(),
"lastSyncSuccess" to (getLastSyncSuccess() ?: "never")
)
}
}

View File

@@ -25,6 +25,8 @@ import java.io.IOException
import java.time.Instant import java.time.Instant
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
@@ -54,9 +56,9 @@ class SyncService(private val context: Context, private val repository: ClimbRep
private val httpClient = private val httpClient =
OkHttpClient.Builder() OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS) .connectTimeout(45, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS) .readTimeout(90, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS) .writeTimeout(90, TimeUnit.SECONDS)
.build() .build()
private val json = Json { private val json = Json {
@@ -86,6 +88,11 @@ class SyncService(private val context: Context, private val repository: ClimbRep
private val _isTesting = MutableStateFlow(false) private val _isTesting = MutableStateFlow(false)
val isTesting: StateFlow<Boolean> = _isTesting.asStateFlow() val isTesting: StateFlow<Boolean> = _isTesting.asStateFlow()
// Debounced sync properties
private var syncJob: Job? = null
private var pendingChanges = false
private val syncDebounceDelay = 2000L // 2 seconds
// Configuration keys // Configuration keys
private object Keys { private object Keys {
const val SERVER_URL = "sync_server_url" const val SERVER_URL = "sync_server_url"
@@ -137,6 +144,9 @@ class SyncService(private val context: Context, private val repository: ClimbRep
repository.setAutoSyncCallback { repository.setAutoSyncCallback {
kotlinx.coroutines.CoroutineScope(Dispatchers.IO).launch { triggerAutoSync() } kotlinx.coroutines.CoroutineScope(Dispatchers.IO).launch { triggerAutoSync() }
} }
// Perform image naming migration on initialization
kotlinx.coroutines.CoroutineScope(Dispatchers.IO).launch { performImageNamingMigration() }
} }
suspend fun downloadData(): ClimbDataBackup = suspend fun downloadData(): ClimbDataBackup =
@@ -297,6 +307,9 @@ class SyncService(private val context: Context, private val repository: ClimbRep
try { try {
val response = httpClient.newCall(request).execute() val response = httpClient.newCall(request).execute()
Log.d(TAG, "Image download response for $filename: ${response.code}") Log.d(TAG, "Image download response for $filename: ${response.code}")
if (response.code != 200) {
Log.w(TAG, "Failed request URL: ${request.url}")
}
when (response.code) { when (response.code) {
200 -> { 200 -> {
@@ -426,28 +439,23 @@ class SyncService(private val context: Context, private val repository: ClimbRep
totalImages += imageCount totalImages += imageCount
} }
problem.imagePaths?.forEachIndexed { index, imagePath -> problem.imagePaths?.forEach { imagePath ->
try { try {
Log.d(TAG, "Attempting to download image: $imagePath") // Use the server's actual filename, not regenerated
val imageData = downloadImage(imagePath)
val serverFilename = imagePath.substringAfterLast('/') val serverFilename = imagePath.substringAfterLast('/')
val consistentFilename =
if (ImageNamingUtils.isValidImageFilename(serverFilename)) { Log.d(TAG, "Attempting to download image: $serverFilename")
serverFilename val imageData = downloadImage(serverFilename)
} else {
ImageNamingUtils.generateImageFilename(problem.id, index)
}
val localImagePath = val localImagePath =
ImageUtils.saveImageFromBytesWithFilename( ImageUtils.saveImageFromBytesWithFilename(
context, context,
imageData, imageData,
consistentFilename serverFilename
) )
if (localImagePath != null) { if (localImagePath != null) {
imagePathMapping[serverFilename] = localImagePath imagePathMapping[imagePath] = localImagePath
downloadedImages++ downloadedImages++
Log.d( Log.d(
TAG, TAG,
@@ -457,6 +465,12 @@ class SyncService(private val context: Context, private val repository: ClimbRep
Log.w(TAG, "Failed to save downloaded image locally: $imagePath") Log.w(TAG, "Failed to save downloaded image locally: $imagePath")
failedImages++ failedImages++
} }
} catch (e: SyncException.ImageNotFound) {
Log.w(
TAG,
"Image not found on server: $imagePath - might be missing or use different naming"
)
failedImages++
} catch (e: Exception) { } catch (e: Exception) {
Log.w(TAG, "Failed to download image $imagePath: ${e.message}") Log.w(TAG, "Failed to download image $imagePath: ${e.message}")
failedImages++ failedImages++
@@ -495,32 +509,24 @@ class SyncService(private val context: Context, private val repository: ClimbRep
if (imageFile.exists() && imageFile.length() > 0) { if (imageFile.exists() && imageFile.length() > 0) {
val imageData = imageFile.readBytes() val imageData = imageFile.readBytes()
// Always use consistent problem-based naming for uploads
val consistentFilename =
ImageNamingUtils.generateImageFilename(problem.id, index)
val filename = imagePath.substringAfterLast('/') val filename = imagePath.substringAfterLast('/')
val consistentFilename = // Rename local file if needed
if (ImageNamingUtils.isValidImageFilename(filename)) { if (filename != consistentFilename) {
filename val newFile = java.io.File(imageFile.parent, consistentFilename)
} else { if (imageFile.renameTo(newFile)) {
val newFilename = Log.d(
ImageNamingUtils.generateImageFilename( TAG,
problem.id, "Renamed local image file: $filename -> $consistentFilename"
index )
) } else {
val newFile = java.io.File(imageFile.parent, newFilename) Log.w(TAG, "Failed to rename local image file, using original")
if (imageFile.renameTo(newFile)) { }
Log.d( }
TAG,
"Renamed local image file: $filename -> $newFilename"
)
newFilename
} else {
Log.w(
TAG,
"Failed to rename local image file, using original"
)
filename
}
}
Log.d(TAG, "Uploading image: $consistentFilename (${imageData.size} bytes)") Log.d(TAG, "Uploading image: $consistentFilename (${imageData.size} bytes)")
uploadImage(consistentFilename, imageData) uploadImage(consistentFilename, imageData)
@@ -568,7 +574,21 @@ class SyncService(private val context: Context, private val repository: ClimbRep
version = "2.0", version = "2.0",
formatVersion = "2.0", formatVersion = "2.0",
gyms = allGyms.map { BackupGym.fromGym(it) }, gyms = allGyms.map { BackupGym.fromGym(it) },
problems = allProblems.map { BackupProblem.fromProblem(it) }, problems =
allProblems.map { problem ->
// Normalize image paths to consistent naming in backup
val normalizedImagePaths =
problem.imagePaths?.mapIndexed { index, _ ->
ImageNamingUtils.generateImageFilename(problem.id, index)
}
val backupProblem = BackupProblem.fromProblem(problem)
if (!normalizedImagePaths.isNullOrEmpty()) {
backupProblem.copy(imagePaths = normalizedImagePaths)
} else {
backupProblem
}
},
sessions = completedSessions.map { BackupClimbSession.fromClimbSession(it) }, sessions = completedSessions.map { BackupClimbSession.fromClimbSession(it) },
attempts = completedAttempts.map { BackupAttempt.fromAttempt(it) }, attempts = completedAttempts.map { BackupAttempt.fromAttempt(it) },
deletedItems = repository.getDeletedItems() deletedItems = repository.getDeletedItems()
@@ -851,10 +871,8 @@ class SyncService(private val context: Context, private val repository: ClimbRep
val localIds = local.map { it.id }.toSet() val localIds = local.map { it.id }.toSet()
val deletedGymIds = deletedItems.filter { it.type == "gym" }.map { it.id }.toSet() val deletedGymIds = deletedItems.filter { it.type == "gym" }.map { it.id }.toSet()
// Remove items that were deleted on other devices
merged.removeAll { deletedGymIds.contains(it.id) } merged.removeAll { deletedGymIds.contains(it.id) }
// Add new items from server (excluding deleted ones)
server.forEach { serverGym -> server.forEach { serverGym ->
if (!localIds.contains(serverGym.id) && !deletedGymIds.contains(serverGym.id)) { if (!localIds.contains(serverGym.id) && !deletedGymIds.contains(serverGym.id)) {
try { try {
@@ -878,24 +896,26 @@ class SyncService(private val context: Context, private val repository: ClimbRep
val localIds = local.map { it.id }.toSet() val localIds = local.map { it.id }.toSet()
val deletedProblemIds = deletedItems.filter { it.type == "problem" }.map { it.id }.toSet() val deletedProblemIds = deletedItems.filter { it.type == "problem" }.map { it.id }.toSet()
// Remove items that were deleted on other devices
merged.removeAll { deletedProblemIds.contains(it.id) } merged.removeAll { deletedProblemIds.contains(it.id) }
// Add new items from server (excluding deleted ones)
server.forEach { serverProblem -> server.forEach { serverProblem ->
if (!localIds.contains(serverProblem.id) && if (!localIds.contains(serverProblem.id) &&
!deletedProblemIds.contains(serverProblem.id) !deletedProblemIds.contains(serverProblem.id)
) { ) {
try { try {
val problemToAdd = val problemToAdd =
if (imagePathMapping.isNotEmpty()) { if (imagePathMapping.isNotEmpty() &&
val newImagePaths = !serverProblem.imagePaths.isNullOrEmpty()
serverProblem.imagePaths?.map { oldPath -> ) {
val filename = oldPath.substringAfterLast('/') val updatedImagePaths =
imagePathMapping[filename] ?: oldPath serverProblem.imagePaths?.mapNotNull { oldPath ->
imagePathMapping[oldPath] ?: oldPath
} }
?: emptyList() if (updatedImagePaths != serverProblem.imagePaths) {
serverProblem.withUpdatedImagePaths(newImagePaths) serverProblem.copy(imagePaths = updatedImagePaths)
} else {
serverProblem
}
} else { } else {
serverProblem serverProblem
} }
@@ -918,10 +938,8 @@ class SyncService(private val context: Context, private val repository: ClimbRep
val localIds = local.map { it.id }.toSet() val localIds = local.map { it.id }.toSet()
val deletedSessionIds = deletedItems.filter { it.type == "session" }.map { it.id }.toSet() val deletedSessionIds = deletedItems.filter { it.type == "session" }.map { it.id }.toSet()
// Remove items that were deleted on other devices (but never remove active sessions)
merged.removeAll { deletedSessionIds.contains(it.id) && it.status != SessionStatus.ACTIVE } merged.removeAll { deletedSessionIds.contains(it.id) && it.status != SessionStatus.ACTIVE }
// Add new items from server (excluding deleted ones)
server.forEach { serverSession -> server.forEach { serverSession ->
if (!localIds.contains(serverSession.id) && if (!localIds.contains(serverSession.id) &&
!deletedSessionIds.contains(serverSession.id) !deletedSessionIds.contains(serverSession.id)
@@ -946,10 +964,8 @@ class SyncService(private val context: Context, private val repository: ClimbRep
val localIds = local.map { it.id }.toSet() val localIds = local.map { it.id }.toSet()
val deletedAttemptIds = deletedItems.filter { it.type == "attempt" }.map { it.id }.toSet() val deletedAttemptIds = deletedItems.filter { it.type == "attempt" }.map { it.id }.toSet()
// Remove items that were deleted on other devices (but be conservative with attempts)
merged.removeAll { deletedAttemptIds.contains(it.id) } merged.removeAll { deletedAttemptIds.contains(it.id) }
// Add new items from server (excluding deleted ones)
server.forEach { serverAttempt -> server.forEach { serverAttempt ->
if (!localIds.contains(serverAttempt.id) && if (!localIds.contains(serverAttempt.id) &&
!deletedAttemptIds.contains(serverAttempt.id) !deletedAttemptIds.contains(serverAttempt.id)
@@ -1093,19 +1109,54 @@ class SyncService(private val context: Context, private val repository: ClimbRep
} }
if (_isSyncing.value) { if (_isSyncing.value) {
Log.d(TAG, "Sync already in progress, skipping auto-sync") pendingChanges = true
return return
} }
syncJob?.cancel()
syncJob =
kotlinx.coroutines.CoroutineScope(Dispatchers.IO).launch {
delay(syncDebounceDelay)
do {
pendingChanges = false
try {
syncWithServer()
} catch (e: Exception) {
Log.e(TAG, "Auto-sync failed: ${e.message}")
_syncError.value = e.message
return@launch
}
if (pendingChanges) {
delay(syncDebounceDelay)
}
} while (pendingChanges)
}
}
suspend fun forceSyncNow() {
if (!isConfigured || !_isConnected.value) return
syncJob?.cancel()
syncJob = null
pendingChanges = false
try { try {
syncWithServer() syncWithServer()
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Auto-sync failed: ${e.message}") Log.e(TAG, "Force sync failed: ${e.message}")
_syncError.value = e.message _syncError.value = e.message
} }
} }
fun clearConfiguration() { fun clearConfiguration() {
syncJob?.cancel()
syncJob = null
pendingChanges = false
serverURL = "" serverURL = ""
authToken = "" authToken = ""
isAutoSyncEnabled = true isAutoSyncEnabled = true
@@ -1116,6 +1167,113 @@ class SyncService(private val context: Context, private val repository: ClimbRep
sharedPreferences.edit().clear().apply() sharedPreferences.edit().clear().apply()
updateConfiguredState() updateConfiguredState()
} }
// MARK: - Image Naming Migration
private suspend fun performImageNamingMigration() =
withContext(Dispatchers.IO) {
val migrationKey = "image_naming_migration_completed"
if (sharedPreferences.getBoolean(migrationKey, false)) {
Log.d(TAG, "Image naming migration already completed")
return@withContext
}
Log.d(TAG, "Starting image naming migration...")
var updateCount = 0
try {
// Get all problems with images
val problems = repository.getAllProblems().first()
val updatedProblems = mutableListOf<Problem>()
for (problem in problems) {
if (problem.imagePaths.isNullOrEmpty()) {
continue
}
val updatedImagePaths = mutableListOf<String>()
var hasChanges = false
problem.imagePaths.forEachIndexed { index, imagePath ->
val currentFilename = imagePath.substringAfterLast('/')
val consistentFilename =
ImageNamingUtils.generateImageFilename(problem.id, index)
if (currentFilename != consistentFilename) {
// Get the image file
val oldFile = ImageUtils.getImageFile(context, imagePath)
if (oldFile.exists()) {
val newPath = "problem_images/$consistentFilename"
val newFile = ImageUtils.getImageFile(context, newPath)
try {
// Create parent directory if needed
newFile.parentFile?.mkdirs()
if (oldFile.renameTo(newFile)) {
updatedImagePaths.add(newPath)
hasChanges = true
updateCount++
Log.d(
TAG,
"Migrated image: $currentFilename -> $consistentFilename"
)
} else {
Log.w(TAG, "Failed to migrate image $currentFilename")
updatedImagePaths.add(
imagePath
) // Keep original on failure
}
} catch (e: Exception) {
Log.w(
TAG,
"Failed to migrate image $currentFilename: ${e.message}"
)
updatedImagePaths.add(imagePath) // Keep original on failure
}
} else {
updatedImagePaths.add(
imagePath
) // Keep original if file doesn't exist
}
} else {
updatedImagePaths.add(imagePath) // Already consistent
}
}
if (hasChanges) {
val updatedProblem =
problem.copy(
imagePaths = updatedImagePaths,
updatedAt = DateFormatUtils.formatISO8601(Instant.now())
)
updatedProblems.add(updatedProblem)
}
}
// Update problems in database
if (updatedProblems.isNotEmpty()) {
updatedProblems.forEach { problem -> repository.updateProblem(problem) }
Log.d(
TAG,
"Updated ${updatedProblems.size} problems with migrated image paths"
)
}
// Mark migration as completed
sharedPreferences.edit().putBoolean(migrationKey, true).apply()
Log.d(TAG, "Image naming migration completed, updated $updateCount images")
// Trigger sync after migration if images were updated
if (updateCount > 0) {
Log.d(TAG, "Triggering sync after image migration")
triggerAutoSync()
}
} catch (e: Exception) {
Log.e(TAG, "Image naming migration failed: ${e.message}", e)
}
}
} }
sealed class SyncException(message: String) : Exception(message) { sealed class SyncException(message: String) : Exception(message) {

View File

@@ -45,7 +45,7 @@ fun OpenClimbApp(
val repository = remember { ClimbRepository(database, context) } val repository = remember { ClimbRepository(database, context) }
val syncService = remember { SyncService(context, repository) } val syncService = remember { SyncService(context, repository) }
val viewModel: ClimbViewModel = val viewModel: ClimbViewModel =
viewModel(factory = ClimbViewModelFactory(repository, syncService)) viewModel(factory = ClimbViewModelFactory(repository, syncService, context))
var showNotificationPermissionDialog by remember { mutableStateOf(false) } var showNotificationPermissionDialog by remember { mutableStateOf(false) }
var hasCheckedNotificationPermission by remember { mutableStateOf(false) } var hasCheckedNotificationPermission by remember { mutableStateOf(false) }

View File

@@ -56,7 +56,7 @@ fun ImagePicker(
// Process images // Process images
val newImagePaths = mutableListOf<String>() val newImagePaths = mutableListOf<String>()
urisToProcess.forEach { uri -> urisToProcess.forEach { uri ->
val imagePath = ImageUtils.saveImageFromUri(context, uri) val imagePath = ImageUtils.saveTemporaryImageFromUri(context, uri)
if (imagePath != null) { if (imagePath != null) {
newImagePaths.add(imagePath) newImagePaths.add(imagePath)
} }
@@ -76,7 +76,7 @@ fun ImagePicker(
success -> success ->
if (success) { if (success) {
cameraImageUri?.let { uri -> cameraImageUri?.let { uri ->
val imagePath = ImageUtils.saveImageFromUri(context, uri) val imagePath = ImageUtils.saveTemporaryImageFromUri(context, uri)
if (imagePath != null) { if (imagePath != null) {
val updatedUris = tempImageUris + imagePath val updatedUris = tempImageUris + imagePath
tempImageUris = updatedUris tempImageUris = updatedUris

View File

@@ -0,0 +1,440 @@
package com.atridad.openclimb.ui.health
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.atridad.openclimb.data.health.HealthConnectManager
import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HealthConnectCard(modifier: Modifier = Modifier) {
val context = LocalContext.current
val healthConnectManager = remember { HealthConnectManager(context) }
val coroutineScope = rememberCoroutineScope()
// State tracking
var isHealthConnectAvailable by remember { mutableStateOf(false) }
var isLoading by remember { mutableStateOf(true) }
var errorMessage by remember { mutableStateOf<String?>(null) }
// Collect flows
val isEnabled by healthConnectManager.isEnabled.collectAsState(initial = false)
val hasPermissions by healthConnectManager.hasPermissions.collectAsState(initial = false)
val autoSyncEnabled by healthConnectManager.autoSyncEnabled.collectAsState(initial = true)
val isCompatible by healthConnectManager.isCompatible.collectAsState(initial = true)
// Permission launcher
val permissionLauncher =
rememberLauncherForActivityResult(
contract = healthConnectManager.getPermissionRequestContract()
) { grantedPermissions ->
coroutineScope.launch {
val allGranted = healthConnectManager.hasAllPermissions()
if (!allGranted) {
errorMessage =
"Some Health Connect permissions were not granted. Please grant all permissions to enable syncing."
} else {
errorMessage = null
}
}
}
// Check Health Connect availability on first load
LaunchedEffect(Unit) {
coroutineScope.launch {
try {
healthConnectManager.isHealthConnectAvailable().collect { available ->
isHealthConnectAvailable = available
isLoading = false
if (!available && isCompatible) {
errorMessage = "Health Connect is not available on this device"
} else if (!isCompatible) {
errorMessage =
"Health Connect API compatibility issue. Please update your device or the app."
}
}
} catch (e: Exception) {
isLoading = false
errorMessage = "Error checking Health Connect availability: ${e.message}"
}
}
}
Card(
modifier = modifier.fillMaxWidth(),
shape = RoundedCornerShape(16.dp),
colors =
CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant
)
) {
Column(
modifier = Modifier.fillMaxWidth().padding(20.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
// Header with icon and title
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
imageVector = Icons.Default.HealthAndSafety,
contentDescription = null,
modifier = Modifier.size(32.dp),
tint =
if (isHealthConnectAvailable && isEnabled && hasPermissions) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.onSurfaceVariant
}
)
Spacer(modifier = Modifier.width(12.dp))
Column(modifier = Modifier.weight(1f)) {
Text(
text = "Health Connect",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Text(
text =
when {
isLoading -> "Checking availability..."
!isCompatible -> "API Issue"
!isHealthConnectAvailable -> "Not available"
isEnabled && hasPermissions -> "Connected"
isEnabled && !hasPermissions -> "Needs permissions"
else -> "Disabled"
},
style = MaterialTheme.typography.bodySmall,
color =
when {
isLoading ->
MaterialTheme.colorScheme.onSurfaceVariant.copy(
alpha = 0.7f
)
!isCompatible -> MaterialTheme.colorScheme.error
!isHealthConnectAvailable -> MaterialTheme.colorScheme.error
isEnabled && hasPermissions ->
MaterialTheme.colorScheme.primary
isEnabled && !hasPermissions ->
MaterialTheme.colorScheme.tertiary
else ->
MaterialTheme.colorScheme.onSurfaceVariant.copy(
alpha = 0.7f
)
}
)
}
// Main toggle switch
Switch(
checked = isEnabled,
onCheckedChange = { enabled ->
if (enabled && isHealthConnectAvailable) {
healthConnectManager.setEnabled(true)
coroutineScope.launch {
try {
val permissionSet =
healthConnectManager.getRequiredPermissions()
if (permissionSet.isNotEmpty()) {
permissionLauncher.launch(permissionSet)
}
} catch (e: Exception) {
errorMessage = "Error requesting permissions: ${e.message}"
}
}
} else {
healthConnectManager.setEnabled(false)
errorMessage = null
}
},
enabled = isHealthConnectAvailable && !isLoading && isCompatible
)
}
if (isEnabled) {
Spacer(modifier = Modifier.height(16.dp))
Card(
shape = RoundedCornerShape(12.dp),
colors =
CardDefaults.cardColors(
containerColor =
if (hasPermissions) {
MaterialTheme.colorScheme.primaryContainer.copy(
alpha = 0.3f
)
} else {
MaterialTheme.colorScheme.errorContainer.copy(
alpha = 0.3f
)
}
)
) {
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
imageVector =
if (hasPermissions) Icons.Default.CheckCircle
else Icons.Default.Warning,
contentDescription = null,
modifier = Modifier.size(20.dp),
tint =
if (hasPermissions) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.error
}
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text =
if (hasPermissions) "Ready to sync"
else "Permissions needed",
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
if (!hasPermissions) {
Spacer(modifier = Modifier.height(8.dp))
Text(
text =
"Grant Health Connect permissions to sync your climbing sessions",
style = MaterialTheme.typography.bodySmall,
color =
MaterialTheme.colorScheme.onSurfaceVariant.copy(
alpha = 0.8f
)
)
Spacer(modifier = Modifier.height(8.dp))
OutlinedButton(
onClick = {
coroutineScope.launch {
try {
val permissionSet =
healthConnectManager
.getRequiredPermissions()
if (permissionSet.isNotEmpty()) {
permissionLauncher.launch(permissionSet)
}
} catch (e: Exception) {
errorMessage =
"Error requesting permissions: ${e.message}"
}
}
},
modifier = Modifier.fillMaxWidth()
) { Text("Grant Permissions") }
}
}
}
if (hasPermissions) {
Spacer(modifier = Modifier.height(12.dp))
Card(
shape = RoundedCornerShape(12.dp),
colors =
CardDefaults.cardColors(
containerColor =
MaterialTheme.colorScheme.surfaceVariant.copy(
alpha = 0.3f
)
)
) {
Row(
modifier = Modifier.fillMaxWidth().padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Column(modifier = Modifier.weight(1f)) {
Text(
text = "Auto-sync sessions",
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Text(
text = "Automatically sync completed climbing sessions",
style = MaterialTheme.typography.bodySmall,
color =
MaterialTheme.colorScheme.onSurfaceVariant.copy(
alpha = 0.7f
)
)
}
Switch(
checked = autoSyncEnabled,
onCheckedChange = { enabled ->
healthConnectManager.setAutoSyncEnabled(enabled)
}
)
}
}
}
} else {
Spacer(modifier = Modifier.height(16.dp))
Text(
text =
"Sync your climbing sessions to Samsung Health, Google Fit, and other fitness apps through Health Connect.",
style = MaterialTheme.typography.bodyMedium,
textAlign = TextAlign.Center,
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.8f)
)
}
errorMessage?.let { error ->
Spacer(modifier = Modifier.height(12.dp))
Card(
shape = RoundedCornerShape(8.dp),
colors =
CardDefaults.cardColors(
containerColor =
MaterialTheme.colorScheme.errorContainer.copy(
alpha = 0.5f
)
)
) {
Row(
modifier = Modifier.fillMaxWidth().padding(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
imageVector = Icons.Default.Warning,
contentDescription = null,
modifier = Modifier.size(16.dp),
tint = MaterialTheme.colorScheme.error
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = error,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onErrorContainer
)
}
}
}
if (isEnabled) {
Spacer(modifier = Modifier.height(12.dp))
var testResult by remember { mutableStateOf<String?>(null) }
var isTestRunning by remember { mutableStateOf(false) }
OutlinedButton(
onClick = {
isTestRunning = true
coroutineScope.launch {
try {
testResult = healthConnectManager.testHealthConnectSync()
} catch (e: Exception) {
testResult = "Test failed: ${e.message}"
} finally {
isTestRunning = false
}
}
},
enabled = !isTestRunning,
modifier = Modifier.fillMaxWidth()
) {
if (isTestRunning) {
CircularProgressIndicator(
modifier = Modifier.size(16.dp),
strokeWidth = 2.dp
)
Spacer(modifier = Modifier.width(8.dp))
}
Text(if (isTestRunning) "Testing..." else "Test Connection")
}
testResult?.let { result ->
Spacer(modifier = Modifier.height(8.dp))
Card(
shape = RoundedCornerShape(8.dp),
colors =
CardDefaults.cardColors(
containerColor =
MaterialTheme.colorScheme.surfaceVariant.copy(
alpha = 0.5f
)
)
) {
Column(modifier = Modifier.fillMaxWidth().padding(12.dp)) {
Text(
text = "Debug Results:",
style = MaterialTheme.typography.labelSmall,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Spacer(modifier = Modifier.height(4.dp))
Text(
text = result,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
fontFamily = androidx.compose.ui.text.font.FontFamily.Monospace
)
}
}
}
}
}
}
}
@Composable
fun HealthConnectStatusBanner(isConnected: Boolean, modifier: Modifier = Modifier) {
if (isConnected) {
Card(
modifier = modifier.fillMaxWidth(),
shape = RoundedCornerShape(8.dp),
colors =
CardDefaults.cardColors(
containerColor =
MaterialTheme.colorScheme.primaryContainer.copy(
alpha = 0.5f
)
)
) {
Row(
modifier = Modifier.fillMaxWidth().padding(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
imageVector = Icons.Default.CloudDone,
contentDescription = null,
modifier = Modifier.size(16.dp),
tint = MaterialTheme.colorScheme.primary
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = "Health Connect active - sessions will sync automatically",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onPrimaryContainer
)
}
}
}
}

View File

@@ -248,6 +248,7 @@ fun AddEditProblemScreen(
) { ) {
val isEditing = problemId != null val isEditing = problemId != null
val gyms by viewModel.gyms.collectAsState() val gyms by viewModel.gyms.collectAsState()
val context = LocalContext.current
// Problem form state // Problem form state
var selectedGym by remember { var selectedGym by remember {
@@ -387,10 +388,11 @@ fun AddEditProblemScreen(
if (isEditing) { if (isEditing) {
viewModel.updateProblem( viewModel.updateProblem(
problem.copy(id = problemId!!) problem.copy(id = problemId),
context
) )
} else { } else {
viewModel.addProblem(problem) viewModel.addProblem(problem, context)
} }
onNavigateBack() onNavigateBack()
} }

View File

@@ -16,6 +16,7 @@ import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Check import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Delete import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.HealthAndSafety
import androidx.compose.material.icons.filled.KeyboardArrowDown import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material.icons.filled.Share import androidx.compose.material.icons.filled.Share
@@ -260,6 +261,32 @@ fun SessionDetailScreen(
} }
}, },
actions = { actions = {
if (session?.duration != null) {
val healthConnectManager = viewModel.getHealthConnectManager()
val isHealthConnectEnabled by
healthConnectManager.isEnabled.collectAsState(
initial = false
)
val hasPermissions by
healthConnectManager.hasPermissions.collectAsState(
initial = false
)
if (isHealthConnectEnabled && hasPermissions) {
IconButton(
onClick = {
viewModel.manualSyncToHealthConnect(sessionId)
}
) {
Icon(
imageVector = Icons.Default.HealthAndSafety,
contentDescription = "Sync to Health Connect",
tint = MaterialTheme.colorScheme.primary
)
}
}
}
// Share button // Share button
if (session?.duration != null) { // Only show for completed sessions if (session?.duration != null) { // Only show for completed sessions
IconButton( IconButton(
@@ -537,7 +564,7 @@ fun SessionDetailScreen(
viewModel.addAttempt(attempt) viewModel.addAttempt(attempt)
showAddAttemptDialog = false showAddAttemptDialog = false
}, },
onProblemCreated = { problem -> viewModel.addProblem(problem) } onProblemCreated = { problem -> viewModel.addProblem(problem, context) }
) )
} }

View File

@@ -8,6 +8,7 @@ import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@@ -25,6 +26,7 @@ import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String) -> Unit) { fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String) -> Unit) {
val problems by viewModel.problems.collectAsState() val problems by viewModel.problems.collectAsState()
val gyms by viewModel.gyms.collectAsState() val gyms by viewModel.gyms.collectAsState()
val context = LocalContext.current
var showImageViewer by remember { mutableStateOf(false) } var showImageViewer by remember { mutableStateOf(false) }
var selectedImagePaths by remember { mutableStateOf<List<String>>(emptyList()) } var selectedImagePaths by remember { mutableStateOf<List<String>>(emptyList()) }
var selectedImageIndex by remember { mutableIntStateOf(0) } var selectedImageIndex by remember { mutableIntStateOf(0) }
@@ -184,7 +186,7 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
}, },
onToggleActive = { onToggleActive = {
val updatedProblem = problem.copy(isActive = !problem.isActive) val updatedProblem = problem.copy(isActive = !problem.isActive)
viewModel.updateProblem(updatedProblem) viewModel.updateProblem(updatedProblem, context)
} }
) )
Spacer(modifier = Modifier.height(8.dp)) Spacer(modifier = Modifier.height(8.dp))

View File

@@ -17,6 +17,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.atridad.openclimb.R import com.atridad.openclimb.R
import com.atridad.openclimb.ui.components.SyncIndicator import com.atridad.openclimb.ui.components.SyncIndicator
import com.atridad.openclimb.ui.health.HealthConnectCard
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
import java.io.File import java.io.File
import java.time.Instant import java.time.Instant
@@ -42,6 +43,10 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
var showResetDialog by remember { mutableStateOf(false) } var showResetDialog by remember { mutableStateOf(false) }
var showSyncConfigDialog by remember { mutableStateOf(false) } var showSyncConfigDialog by remember { mutableStateOf(false) }
var showDisconnectDialog 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 // Sync configuration state
var serverUrl by remember { mutableStateOf(syncService.serverURL) } var serverUrl by remember { mutableStateOf(syncService.serverURL) }
@@ -375,7 +380,8 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
} }
} }
// Data Management Section item { HealthConnectCard() }
item { item {
Card(modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(16.dp)) { Card(modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(16.dp)) {
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) { Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
@@ -475,6 +481,88 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
Spacer(modifier = Modifier.height(8.dp)) 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 =
CardDefaults.cardColors(
containerColor =
MaterialTheme.colorScheme.errorContainer.copy(
alpha = 0.3f
)
)
) {
ListItem(
headlineContent = { Text("Delete All Images") },
supportingContent = {
Text("Permanently delete all image files from device")
},
leadingContent = {
Icon(
Icons.Default.Delete,
contentDescription = null,
tint = MaterialTheme.colorScheme.error
)
},
trailingContent = {
TextButton(
onClick = { showDeleteImagesDialog = true },
enabled = !isDeletingImages && !uiState.isLoading
) {
if (isDeletingImages) {
CircularProgressIndicator(
modifier = Modifier.size(16.dp),
strokeWidth = 2.dp
)
} else {
Text("Delete", color = MaterialTheme.colorScheme.error)
}
}
}
)
}
Spacer(modifier = Modifier.height(8.dp))
Card( Card(
shape = RoundedCornerShape(12.dp), shape = RoundedCornerShape(12.dp),
colors = colors =
@@ -903,16 +991,72 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
confirmButton = { confirmButton = {
TextButton( TextButton(
onClick = { onClick = {
syncService.clearConfiguration() viewModel.syncService.clearConfiguration()
serverUrl = ""
authToken = ""
showDisconnectDialog = false showDisconnectDialog = false
} }
) { Text("Disconnect", color = MaterialTheme.colorScheme.error) } ) { Text("Disconnect") }
}, },
dismissButton = { dismissButton = {
TextButton(onClick = { showDisconnectDialog = false }) { Text("Cancel") } TextButton(onClick = { showDisconnectDialog = false }) { Text("Cancel") }
} }
) )
} }
// 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(
onDismissRequest = { showDeleteImagesDialog = false },
title = { Text("Delete All Images") },
text = {
Text(
"This will permanently delete ALL image files from your device.\n\nProblems will keep their references but the actual image files will be removed. This cannot be undone.\n\nConsider exporting your data first if you want to keep your images."
)
},
confirmButton = {
TextButton(
onClick = {
isDeletingImages = true
showDeleteImagesDialog = false
coroutineScope.launch {
viewModel.deleteAllImages(context)
isDeletingImages = false
viewModel.setMessage("All images deleted successfully!")
}
}
) { Text("Delete", color = MaterialTheme.colorScheme.error) }
},
dismissButton = {
TextButton(onClick = { showDeleteImagesDialog = false }) { Text("Cancel") }
}
)
}
} }

View File

@@ -3,10 +3,12 @@ package com.atridad.openclimb.ui.viewmodel
import android.content.Context import android.content.Context
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.atridad.openclimb.data.health.HealthConnectManager
import com.atridad.openclimb.data.model.* import com.atridad.openclimb.data.model.*
import com.atridad.openclimb.data.repository.ClimbRepository import com.atridad.openclimb.data.repository.ClimbRepository
import com.atridad.openclimb.data.sync.SyncService import com.atridad.openclimb.data.sync.SyncService
import com.atridad.openclimb.service.SessionTrackingService import com.atridad.openclimb.service.SessionTrackingService
import com.atridad.openclimb.utils.ImageNamingUtils
import com.atridad.openclimb.utils.ImageUtils import com.atridad.openclimb.utils.ImageUtils
import com.atridad.openclimb.utils.SessionShareUtils import com.atridad.openclimb.utils.SessionShareUtils
import com.atridad.openclimb.widget.ClimbStatsWidgetProvider import com.atridad.openclimb.widget.ClimbStatsWidgetProvider
@@ -16,8 +18,14 @@ import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class ClimbViewModel(private val repository: ClimbRepository, val syncService: SyncService) : class ClimbViewModel(
ViewModel() { private val repository: ClimbRepository,
val syncService: SyncService,
private val context: Context
) : ViewModel() {
// Health Connect manager
private val healthConnectManager = HealthConnectManager(context)
// UI State flows // UI State flows
private val _uiState = MutableStateFlow(ClimbUiState()) private val _uiState = MutableStateFlow(ClimbUiState())
@@ -106,25 +114,41 @@ class ClimbViewModel(private val repository: ClimbRepository, val syncService: S
fun getGymById(id: String): Flow<Gym?> = flow { emit(repository.getGymById(id)) } fun getGymById(id: String): Flow<Gym?> = flow { emit(repository.getGymById(id)) }
// Problem operations // Problem operations
fun addProblem(problem: Problem) {
viewModelScope.launch { repository.insertProblem(problem) }
}
fun addProblem(problem: Problem, context: Context) { fun addProblem(problem: Problem, context: Context) {
viewModelScope.launch { viewModelScope.launch {
repository.insertProblem(problem) val finalProblem = renameTemporaryImages(problem, context)
repository.insertProblem(finalProblem)
ClimbStatsWidgetProvider.updateAllWidgets(context) ClimbStatsWidgetProvider.updateAllWidgets(context)
// Auto-sync now happens automatically via repository callback // Auto-sync now happens automatically via repository callback
} }
} }
fun updateProblem(problem: Problem) { private suspend fun renameTemporaryImages(problem: Problem, context: Context? = null): Problem {
viewModelScope.launch { repository.updateProblem(problem) } if (problem.imagePaths.isEmpty()) {
return problem
}
val appContext = context ?: return problem
val finalImagePaths = mutableListOf<String>()
problem.imagePaths.forEachIndexed { index, tempPath ->
if (tempPath.startsWith("temp_")) {
val deterministicName = ImageNamingUtils.generateImageFilename(problem.id, index)
val finalPath =
ImageUtils.renameTemporaryImage(appContext, tempPath, problem.id, index)
finalImagePaths.add(finalPath ?: tempPath)
} else {
finalImagePaths.add(tempPath)
}
}
return problem.copy(imagePaths = finalImagePaths)
} }
fun updateProblem(problem: Problem, context: Context) { fun updateProblem(problem: Problem, context: Context) {
viewModelScope.launch { viewModelScope.launch {
repository.updateProblem(problem) val finalProblem = renameTemporaryImages(problem, context)
repository.updateProblem(finalProblem)
ClimbStatsWidgetProvider.updateAllWidgets(context) ClimbStatsWidgetProvider.updateAllWidgets(context)
} }
} }
@@ -147,6 +171,99 @@ class ClimbViewModel(private val repository: ClimbRepository, val syncService: S
val referencedImagePaths = allProblems.flatMap { it.imagePaths }.toSet() val referencedImagePaths = allProblems.flatMap { it.imagePaths }.toSet()
ImageUtils.cleanupOrphanedImages(context, referencedImagePaths) 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 {
val imagesDir = ImageUtils.getImagesDirectory(context)
var deletedCount = 0
imagesDir.listFiles()?.forEach { file ->
if (file.isFile && file.extension.lowercase() == "jpg") {
if (file.delete()) {
deletedCount++
}
}
}
val allProblems = repository.getAllProblems().first()
val updatedProblems =
allProblems.map { problem ->
if (problem.imagePaths.isNotEmpty()) {
problem.copy(imagePaths = emptyList())
} else {
problem
}
}
for (updatedProblem in updatedProblems) {
if (updatedProblem.imagePaths !=
allProblems.find { it.id == updatedProblem.id }?.imagePaths
) {
repository.insertProblemWithoutSync(updatedProblem)
}
}
println("Deleted $deletedCount image files and cleared image references")
}
}
fun getProblemById(id: String): Flow<Problem?> = flow { emit(repository.getProblemById(id)) } fun getProblemById(id: String): Flow<Problem?> = flow { emit(repository.getProblemById(id)) }
@@ -240,7 +357,6 @@ class ClimbViewModel(private val repository: ClimbRepository, val syncService: S
ClimbStatsWidgetProvider.updateAllWidgets(context) ClimbStatsWidgetProvider.updateAllWidgets(context)
android.util.Log.d("ClimbViewModel", "Session started successfully")
_uiState.value = _uiState.value.copy(message = "Session started successfully!") _uiState.value = _uiState.value.copy(message = "Session started successfully!")
} }
} }
@@ -268,7 +384,7 @@ class ClimbViewModel(private val repository: ClimbRepository, val syncService: S
ClimbStatsWidgetProvider.updateAllWidgets(context) ClimbStatsWidgetProvider.updateAllWidgets(context)
// Auto-sync now happens automatically via repository callback syncToHealthConnect(completedSession)
_uiState.value = _uiState.value.copy(message = "Session completed!") _uiState.value = _uiState.value.copy(message = "Session completed!")
} }
@@ -295,7 +411,6 @@ class ClimbViewModel(private val repository: ClimbRepository, val syncService: S
viewModelScope.launch { viewModelScope.launch {
repository.insertAttempt(attempt) repository.insertAttempt(attempt)
ClimbStatsWidgetProvider.updateAllWidgets(context) ClimbStatsWidgetProvider.updateAllWidgets(context)
// Auto-sync now happens automatically via repository callback
} }
} }
@@ -410,6 +525,10 @@ class ClimbViewModel(private val repository: ClimbRepository, val syncService: S
_uiState.value = _uiState.value.copy(error = message) _uiState.value = _uiState.value.copy(error = message)
} }
fun setMessage(message: String) {
_uiState.value = _uiState.value.copy(message = message)
}
fun resetAllData() { fun resetAllData() {
viewModelScope.launch { viewModelScope.launch {
try { try {
@@ -429,6 +548,90 @@ class ClimbViewModel(private val repository: ClimbRepository, val syncService: S
} }
} }
private fun syncToHealthConnect(session: ClimbSession) {
viewModelScope.launch {
try {
val gym = repository.getGymById(session.gymId)
val gymName = gym?.name ?: "Unknown Gym"
val attempts = repository.getAttemptsBySession(session.id).first()
val attemptCount = attempts.size
val result = healthConnectManager.autoSyncSession(session, gymName, attemptCount)
result
.onSuccess {
_uiState.value =
_uiState.value.copy(
message =
"Session synced to Health Connect successfully!"
)
}
.onFailure { error ->
if (healthConnectManager.isReadySync()) {
_uiState.value =
_uiState.value.copy(
error =
"Failed to sync to Health Connect: ${error.message}"
)
}
}
} catch (e: Exception) {
if (healthConnectManager.isReadySync()) {
_uiState.value =
_uiState.value.copy(error = "Health Connect sync error: ${e.message}")
}
}
}
}
fun manualSyncToHealthConnect(sessionId: String) {
viewModelScope.launch {
try {
val session = repository.getSessionById(sessionId)
if (session == null) {
_uiState.value = _uiState.value.copy(error = "Session not found")
return@launch
}
if (session.status != SessionStatus.COMPLETED) {
_uiState.value =
_uiState.value.copy(error = "Only completed sessions can be synced")
return@launch
}
val gym = repository.getGymById(session.gymId)
val gymName = gym?.name ?: "Unknown Gym"
val attempts = repository.getAttemptsBySession(session.id).first()
val attemptCount = attempts.size
val result =
healthConnectManager.syncClimbingSession(session, gymName, attemptCount)
result
.onSuccess {
_uiState.value =
_uiState.value.copy(
message =
"Session synced to Health Connect successfully!"
)
}
.onFailure { error ->
_uiState.value =
_uiState.value.copy(
error =
"Failed to sync to Health Connect: ${error.message}"
)
}
} catch (e: Exception) {
_uiState.value =
_uiState.value.copy(error = "Health Connect sync error: ${e.message}")
}
}
}
fun getHealthConnectManager(): HealthConnectManager = healthConnectManager
// Share operations // Share operations
suspend fun generateSessionShareCard(context: Context, sessionId: String): File? = suspend fun generateSessionShareCard(context: Context, sessionId: String): File? =
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {

View File

@@ -1,5 +1,6 @@
package com.atridad.openclimb.ui.viewmodel package com.atridad.openclimb.ui.viewmodel
import android.content.Context
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import com.atridad.openclimb.data.repository.ClimbRepository import com.atridad.openclimb.data.repository.ClimbRepository
@@ -7,13 +8,14 @@ import com.atridad.openclimb.data.sync.SyncService
class ClimbViewModelFactory( class ClimbViewModelFactory(
private val repository: ClimbRepository, private val repository: ClimbRepository,
private val syncService: SyncService private val syncService: SyncService,
private val context: Context
) : ViewModelProvider.Factory { ) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T { override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(ClimbViewModel::class.java)) { if (modelClass.isAssignableFrom(ClimbViewModel::class.java)) {
return ClimbViewModel(repository, syncService) as T return ClimbViewModel(repository, syncService, context) as T
} }
throw IllegalArgumentException("Unknown ViewModel class") throw IllegalArgumentException("Unknown ViewModel class")
} }

View File

@@ -13,18 +13,16 @@ object ImageNamingUtils {
private const val HASH_LENGTH = 12 // First 12 chars of SHA-256 private const val HASH_LENGTH = 12 // First 12 chars of SHA-256
/** Generates a deterministic filename for a problem image */ /** Generates a deterministic filename for a problem image */
fun generateImageFilename(problemId: String, timestamp: String, imageIndex: Int): String { fun generateImageFilename(problemId: String, imageIndex: Int): String {
// Create a deterministic hash from problemId + timestamp + index val input = "${problemId}_${imageIndex}"
val input = "${problemId}_${timestamp}_${imageIndex}"
val hash = createHash(input) val hash = createHash(input)
return "problem_${hash}_${imageIndex}${IMAGE_EXTENSION}" return "problem_${hash}_${imageIndex}${IMAGE_EXTENSION}"
} }
/** Generates a deterministic filename using current timestamp */ /** Legacy method for backward compatibility */
fun generateImageFilename(problemId: String, imageIndex: Int): String { fun generateImageFilename(problemId: String, timestamp: String, imageIndex: Int): String {
val timestamp = DateFormatUtils.nowISO8601() return generateImageFilename(problemId, imageIndex)
return generateImageFilename(problemId, timestamp, imageIndex)
} }
/** Extracts problem ID from an image filename */ /** Extracts problem ID from an image filename */
@@ -41,9 +39,7 @@ object ImageNamingUtils {
return null return null
} }
// We can't extract the original problem ID from the hash, return parts[1]
// but we can validate the format
return parts[1] // Return the hash as identifier
} }
/** Validates if a filename follows our naming convention */ /** Validates if a filename follows our naming convention */
@@ -63,15 +59,11 @@ object ImageNamingUtils {
/** Migrates an existing filename to our naming convention */ /** Migrates an existing filename to our naming convention */
fun migrateFilename(oldFilename: String, problemId: String, imageIndex: Int): String { fun migrateFilename(oldFilename: String, problemId: String, imageIndex: Int): String {
// If it's already using our convention, keep it
if (isValidImageFilename(oldFilename)) { if (isValidImageFilename(oldFilename)) {
return oldFilename return oldFilename
} }
// Generate new deterministic name return generateImageFilename(problemId, imageIndex)
// Use a timestamp based on the old filename to maintain some consistency
val timestamp = DateFormatUtils.nowISO8601()
return generateImageFilename(problemId, timestamp, imageIndex)
} }
/** Creates a deterministic hash from input string */ /** Creates a deterministic hash from input string */
@@ -90,7 +82,7 @@ object ImageNamingUtils {
val renameMap = mutableMapOf<String, String>() val renameMap = mutableMapOf<String, String>()
existingFilenames.forEachIndexed { index, oldFilename -> existingFilenames.forEachIndexed { index, oldFilename ->
val newFilename = migrateFilename(oldFilename, problemId, index) val newFilename = generateImageFilename(problemId, index)
if (newFilename != oldFilename) { if (newFilename != oldFilename) {
renameMap[oldFilename] = newFilename renameMap[oldFilename] = newFilename
} }
@@ -98,4 +90,37 @@ object ImageNamingUtils {
return renameMap return renameMap
} }
/** Generates the canonical filename for a problem image */
fun getCanonicalImageFilename(problemId: String, imageIndex: Int): String {
return generateImageFilename(problemId, imageIndex)
}
/** Creates a mapping of existing server filenames to canonical filenames */
fun createServerMigrationMap(
problemId: String,
serverImageFilenames: List<String>,
localImageCount: Int
): Map<String, String> {
val migrationMap = mutableMapOf<String, String>()
for (imageIndex in 0 until localImageCount) {
val canonicalName = getCanonicalImageFilename(problemId, imageIndex)
if (serverImageFilenames.contains(canonicalName)) {
continue
}
for (serverFilename in serverImageFilenames) {
if (isValidImageFilename(serverFilename) &&
!migrationMap.values.contains(serverFilename)
) {
migrationMap[serverFilename] = canonicalName
break
}
}
}
return migrationMap
}
} }

View File

@@ -5,6 +5,8 @@ import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.net.Uri import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import androidx.core.graphics.scale import androidx.core.graphics.scale
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@@ -17,7 +19,7 @@ object ImageUtils {
private const val IMAGE_QUALITY = 85 private const val IMAGE_QUALITY = 85
// Creates the images directory if it doesn't exist // Creates the images directory if it doesn't exist
private fun getImagesDirectory(context: Context): File { fun getImagesDirectory(context: Context): File {
val imagesDir = File(context.filesDir, IMAGES_DIR) val imagesDir = File(context.filesDir, IMAGES_DIR)
if (!imagesDir.exists()) { if (!imagesDir.exists()) {
imagesDir.mkdirs() imagesDir.mkdirs()
@@ -43,12 +45,12 @@ object ImageUtils {
val orientedBitmap = correctImageOrientation(context, imageUri, originalBitmap) val orientedBitmap = correctImageOrientation(context, imageUri, originalBitmap)
val compressedBitmap = compressImage(orientedBitmap) val compressedBitmap = compressImage(orientedBitmap)
val filename = // Always require deterministic naming - no UUID fallback
if (problemId != null && imageIndex != null) { require(problemId != null && imageIndex != null) {
ImageNamingUtils.generateImageFilename(problemId, imageIndex) "Problem ID and image index are required for deterministic image naming"
} else { }
"${UUID.randomUUID()}.jpg"
} val filename = ImageNamingUtils.generateImageFilename(problemId, imageIndex)
val imageFile = File(getImagesDirectory(context), filename) val imageFile = File(getImagesDirectory(context), filename)
FileOutputStream(imageFile).use { output -> FileOutputStream(imageFile).use { output ->
@@ -73,35 +75,35 @@ object ImageUtils {
return try { return try {
val inputStream = context.contentResolver.openInputStream(imageUri) val inputStream = context.contentResolver.openInputStream(imageUri)
inputStream?.use { input -> inputStream?.use { input ->
val exif = android.media.ExifInterface(input) val exif = androidx.exifinterface.media.ExifInterface(input)
val orientation = val orientation =
exif.getAttributeInt( exif.getAttributeInt(
android.media.ExifInterface.TAG_ORIENTATION, androidx.exifinterface.media.ExifInterface.TAG_ORIENTATION,
android.media.ExifInterface.ORIENTATION_NORMAL androidx.exifinterface.media.ExifInterface.ORIENTATION_NORMAL
) )
val matrix = android.graphics.Matrix() val matrix = android.graphics.Matrix()
when (orientation) { when (orientation) {
android.media.ExifInterface.ORIENTATION_ROTATE_90 -> { androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_90 -> {
matrix.postRotate(90f) matrix.postRotate(90f)
} }
android.media.ExifInterface.ORIENTATION_ROTATE_180 -> { androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_180 -> {
matrix.postRotate(180f) matrix.postRotate(180f)
} }
android.media.ExifInterface.ORIENTATION_ROTATE_270 -> { androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_270 -> {
matrix.postRotate(270f) matrix.postRotate(270f)
} }
android.media.ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> { androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> {
matrix.postScale(-1f, 1f) matrix.postScale(-1f, 1f)
} }
android.media.ExifInterface.ORIENTATION_FLIP_VERTICAL -> { androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_VERTICAL -> {
matrix.postScale(1f, -1f) matrix.postScale(1f, -1f)
} }
android.media.ExifInterface.ORIENTATION_TRANSPOSE -> { androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSPOSE -> {
matrix.postRotate(90f) matrix.postRotate(90f)
matrix.postScale(-1f, 1f) matrix.postScale(-1f, 1f)
} }
android.media.ExifInterface.ORIENTATION_TRANSVERSE -> { androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSVERSE -> {
matrix.postRotate(-90f) matrix.postRotate(-90f)
matrix.postScale(-1f, 1f) matrix.postScale(-1f, 1f)
} }
@@ -212,6 +214,72 @@ object ImageUtils {
} }
} }
/** Temporarily saves an image during selection process */
fun saveTemporaryImageFromUri(context: Context, imageUri: Uri): String? {
return try {
val originalBitmap =
MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)
?: return null
val orientedBitmap = correctImageOrientation(context, imageUri, originalBitmap)
val compressedBitmap = compressImage(orientedBitmap)
val tempFilename = "temp_${UUID.randomUUID()}.jpg"
val imageFile = File(getImagesDirectory(context), tempFilename)
FileOutputStream(imageFile).use { output ->
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
}
originalBitmap.recycle()
if (orientedBitmap != originalBitmap) {
orientedBitmap.recycle()
}
if (compressedBitmap != orientedBitmap) {
compressedBitmap.recycle()
}
tempFilename
} catch (e: Exception) {
Log.e("ImageUtils", "Error saving temporary image from URI", e)
null
}
}
/** Renames a temporary image */
fun renameTemporaryImage(
context: Context,
tempFilename: String,
problemId: String,
imageIndex: Int
): String? {
return try {
val tempFile = File(getImagesDirectory(context), tempFilename)
if (!tempFile.exists()) {
Log.e("ImageUtils", "Temporary file does not exist: $tempFilename")
return null
}
val deterministicFilename =
ImageNamingUtils.generateImageFilename(problemId, imageIndex)
val finalFile = File(getImagesDirectory(context), deterministicFilename)
if (tempFile.renameTo(finalFile)) {
Log.d(
"ImageUtils",
"Renamed temporary image: $tempFilename -> $deterministicFilename"
)
deterministicFilename
} else {
Log.e("ImageUtils", "Failed to rename temporary image: $tempFilename")
null
}
} catch (e: Exception) {
Log.e("ImageUtils", "Error renaming temporary image", e)
null
}
}
/** Saves an image from byte array to app's private storage */ /** Saves an image from byte array to app's private storage */
fun saveImageFromBytes(context: Context, imageData: ByteArray): String? { fun saveImageFromBytes(context: Context, imageData: ByteArray): String? {
return try { return try {

View File

@@ -19,6 +19,7 @@ kotlinxSerialization = "1.9.0"
kotlinxCoroutines = "1.10.2" kotlinxCoroutines = "1.10.2"
coil = "2.7.0" coil = "2.7.0"
ksp = "2.2.20-2.0.3" ksp = "2.2.20-2.0.3"
exifinterface = "1.3.6"
[libraries] [libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -66,6 +67,7 @@ mockk = { group = "io.mockk", name = "mockk", version = "1.14.6" }
# Image Loading # Image Loading
coil-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" } coil-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" }
androidx-exifinterface = { group = "androidx.exifinterface", name = "exifinterface", version.ref = "exifinterface" }
[plugins] [plugins]
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }

View File

@@ -465,8 +465,9 @@
CODE_SIGN_ENTITLEMENTS = OpenClimb/OpenClimb.entitlements; CODE_SIGN_ENTITLEMENTS = OpenClimb/OpenClimb.entitlements;
CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 18; CURRENT_PROJECT_VERSION = 23;
DEVELOPMENT_TEAM = 4BC9Y2LL4B; DEVELOPMENT_TEAM = 4BC9Y2LL4B;
DRIVERKIT_DEPLOYMENT_TARGET = 24.6;
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = OpenClimb/Info.plist; INFOPLIST_FILE = OpenClimb/Info.plist;
@@ -485,7 +486,8 @@
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
); );
MARKETING_VERSION = 1.3.0; MACOSX_DEPLOYMENT_TARGET = 15.6;
MARKETING_VERSION = 1.4.0;
PRODUCT_BUNDLE_IDENTIFIER = com.atridad.OpenClimb; PRODUCT_BUNDLE_IDENTIFIER = com.atridad.OpenClimb;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = ""; PROVISIONING_PROFILE_SPECIFIER = "";
@@ -495,8 +497,11 @@
SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor;
SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES;
SWIFT_VERSION = 5.0; SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = 1; TARGETED_DEVICE_FAMILY = 1;
TVOS_DEPLOYMENT_TARGET = 18.6;
WATCHOS_DEPLOYMENT_TARGET = 11.6;
XROS_DEPLOYMENT_TARGET = 2.6;
}; };
name = Debug; name = Debug;
}; };
@@ -508,8 +513,9 @@
CODE_SIGN_ENTITLEMENTS = OpenClimb/OpenClimb.entitlements; CODE_SIGN_ENTITLEMENTS = OpenClimb/OpenClimb.entitlements;
CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 18; CURRENT_PROJECT_VERSION = 23;
DEVELOPMENT_TEAM = 4BC9Y2LL4B; DEVELOPMENT_TEAM = 4BC9Y2LL4B;
DRIVERKIT_DEPLOYMENT_TARGET = 24.6;
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = OpenClimb/Info.plist; INFOPLIST_FILE = OpenClimb/Info.plist;
@@ -528,7 +534,8 @@
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
); );
MARKETING_VERSION = 1.3.0; MACOSX_DEPLOYMENT_TARGET = 15.6;
MARKETING_VERSION = 1.4.0;
PRODUCT_BUNDLE_IDENTIFIER = com.atridad.OpenClimb; PRODUCT_BUNDLE_IDENTIFIER = com.atridad.OpenClimb;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = ""; PROVISIONING_PROFILE_SPECIFIER = "";
@@ -538,8 +545,11 @@
SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor;
SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES;
SWIFT_VERSION = 5.0; SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = 1; TARGETED_DEVICE_FAMILY = 1;
TVOS_DEPLOYMENT_TARGET = 18.6;
WATCHOS_DEPLOYMENT_TARGET = 11.6;
XROS_DEPLOYMENT_TARGET = 2.6;
}; };
name = Release; name = Release;
}; };
@@ -592,7 +602,7 @@
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground; ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = SessionStatusLiveExtension.entitlements; CODE_SIGN_ENTITLEMENTS = SessionStatusLiveExtension.entitlements;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 18; CURRENT_PROJECT_VERSION = 23;
DEVELOPMENT_TEAM = 4BC9Y2LL4B; DEVELOPMENT_TEAM = 4BC9Y2LL4B;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = SessionStatusLive/Info.plist; INFOPLIST_FILE = SessionStatusLive/Info.plist;
@@ -603,7 +613,7 @@
"@executable_path/Frameworks", "@executable_path/Frameworks",
"@executable_path/../../Frameworks", "@executable_path/../../Frameworks",
); );
MARKETING_VERSION = 1.3.0; MARKETING_VERSION = 1.4.0;
PRODUCT_BUNDLE_IDENTIFIER = com.atridad.OpenClimb.SessionStatusLive; PRODUCT_BUNDLE_IDENTIFIER = com.atridad.OpenClimb.SessionStatusLive;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES; SKIP_INSTALL = YES;
@@ -622,7 +632,7 @@
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground; ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = SessionStatusLiveExtension.entitlements; CODE_SIGN_ENTITLEMENTS = SessionStatusLiveExtension.entitlements;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 18; CURRENT_PROJECT_VERSION = 23;
DEVELOPMENT_TEAM = 4BC9Y2LL4B; DEVELOPMENT_TEAM = 4BC9Y2LL4B;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = SessionStatusLive/Info.plist; INFOPLIST_FILE = SessionStatusLive/Info.plist;
@@ -633,7 +643,7 @@
"@executable_path/Frameworks", "@executable_path/Frameworks",
"@executable_path/../../Frameworks", "@executable_path/../../Frameworks",
); );
MARKETING_VERSION = 1.3.0; MARKETING_VERSION = 1.4.0;
PRODUCT_BUNDLE_IDENTIFIER = com.atridad.OpenClimb.SessionStatusLive; PRODUCT_BUNDLE_IDENTIFIER = com.atridad.OpenClimb.SessionStatusLive;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES; SKIP_INSTALL = YES;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

View File

@@ -1,18 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<svg width="1024" height="1024" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"> <svg width="1024" height="1024" viewBox="0 0 1024 1024" xmlns="http://schemas.android.com/2000/svg">
<!-- Dark background with rounded corners for iOS -->
<rect width="1024" height="1024" fill="#1A1A1A" rx="180" ry="180"/> <rect width="1024" height="1024" fill="#1A1A1A" rx="180" ry="180"/>
<g transform="translate(512, 512) scale(2.5)"> <!-- Transform to match Android layout exactly -->
<polygon points="-70,80 -20,-60 30,80" <g transform="translate(512, 512) scale(4.75) translate(-54, -42.5)">
<!-- Left mountain (yellow/amber) - matches Android coordinates with white border -->
<polygon points="15,70 35,25 55,70"
fill="#FFC107" fill="#FFC107"
stroke="#1C1C1C" stroke="#FFFFFF"
stroke-width="4" stroke-width="3"
stroke-linejoin="round"/> stroke-linejoin="round"/>
<polygon points="0,80 50,-80 100,80" <!-- Right mountain (red) - matches Android coordinates with white border -->
<polygon points="40,70 65,15 90,70"
fill="#F44336" fill="#F44336"
stroke="#1C1C1C" stroke="#FFFFFF"
stroke-width="4" stroke-width="3"
stroke-linejoin="round"/> stroke-linejoin="round"/>
</g> </g>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 604 B

After

Width:  |  Height:  |  Size: 913 B

View File

@@ -1,18 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<svg width="1024" height="1024" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"> <svg width="1024" height="1024" viewBox="0 0 1024 1024" xmlns="http://schemas.android.com/2000/svg">
<!-- White background with rounded corners for iOS -->
<rect width="1024" height="1024" fill="#FFFFFF" rx="180" ry="180"/> <rect width="1024" height="1024" fill="#FFFFFF" rx="180" ry="180"/>
<g transform="translate(512, 512) scale(2.5)"> <!-- Transform to match Android layout exactly -->
<polygon points="-70,80 -20,-60 30,80" <g transform="translate(512, 512) scale(4.75) translate(-54, -42.5)">
<!-- Left mountain (yellow/amber) - matches Android coordinates -->
<polygon points="15,70 35,25 55,70"
fill="#FFC107" fill="#FFC107"
stroke="#1C1C1C" stroke="#1C1C1C"
stroke-width="4" stroke-width="3"
stroke-linejoin="round"/> stroke-linejoin="round"/>
<polygon points="0,80 50,-80 100,80" <!-- Right mountain (red) - matches Android coordinates -->
<polygon points="40,70 65,15 90,70"
fill="#F44336" fill="#F44336"
stroke="#1C1C1C" stroke="#1C1C1C"
stroke-width="4" stroke-width="3"
stroke-linejoin="round"/> stroke-linejoin="round"/>
</g> </g>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 604 B

After

Width:  |  Height:  |  Size: 878 B

View File

@@ -1,19 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<svg width="1024" height="1024" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"> <svg width="1024" height="1024" viewBox="0 0 1024 1024" xmlns="http://schemas.android.com/2000/svg">
<!-- Transparent background with rounded corners for iOS tinted mode -->
<rect width="1024" height="1024" fill="transparent" rx="180" ry="180"/> <rect width="1024" height="1024" fill="transparent" rx="180" ry="180"/>
<g transform="translate(512, 512) scale(2.5)"> <!-- Transform to match Android layout exactly -->
<polygon points="-70,80 -20,-60 30,80" <g transform="translate(512, 512) scale(4.75) translate(-54, -42.5)">
<!-- Left mountain - matches Android coordinates, black fill for tinting -->
<polygon points="15,70 35,25 55,70"
fill="#000000" fill="#000000"
stroke="#000000" stroke="#000000"
stroke-width="4" stroke-width="3"
stroke-linejoin="round" stroke-linejoin="round"
opacity="0.8"/> opacity="0.8"/>
<polygon points="0,80 50,-80 100,80" <!-- Right mountain - matches Android coordinates, black fill for tinting -->
<polygon points="40,70 65,15 90,70"
fill="#000000" fill="#000000"
stroke="#000000" stroke="#000000"
stroke-width="4" stroke-width="3"
stroke-linejoin="round" stroke-linejoin="round"
opacity="0.9"/> opacity="0.9"/>
</g> </g>

Before

Width:  |  Height:  |  Size: 662 B

After

Width:  |  Height:  |  Size: 981 B

View File

@@ -0,0 +1,56 @@
{
"images": [
{
"filename": "app_logo_256.png",
"idiom": "universal",
"scale": "1x"
},
{
"appearances": [
{
"appearance": "luminosity",
"value": "dark"
}
],
"filename": "app_logo_256_dark.png",
"idiom": "universal",
"scale": "1x"
},
{
"filename": "app_logo_256.png",
"idiom": "universal",
"scale": "2x"
},
{
"appearances": [
{
"appearance": "luminosity",
"value": "dark"
}
],
"filename": "app_logo_256_dark.png",
"idiom": "universal",
"scale": "2x"
},
{
"filename": "app_logo_256.png",
"idiom": "universal",
"scale": "3x"
},
{
"appearances": [
{
"appearance": "luminosity",
"value": "dark"
}
],
"filename": "app_logo_256_dark.png",
"idiom": "universal",
"scale": "3x"
}
],
"info": {
"author": "xcode",
"version": 1
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@@ -1,56 +0,0 @@
{
"images" : [
{
"filename" : "mountains_icon_256.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"filename" : "mountains_icon_256_dark.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "mountains_icon_256.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"filename" : "mountains_icon_256_dark.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "mountains_icon_256.png",
"idiom" : "universal",
"scale" : "3x"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"filename" : "mountains_icon_256_dark.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

View File

@@ -44,8 +44,10 @@ struct PhotoOptionSheet: View {
.buttonStyle(PlainButtonStyle()) .buttonStyle(PlainButtonStyle())
Button(action: { Button(action: {
onCameraSelected()
onDismiss() onDismiss()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
onCameraSelected()
}
}) { }) {
HStack { HStack {
Image(systemName: "camera.fill") Image(systemName: "camera.fill")

View File

@@ -50,6 +50,8 @@ struct ContentView: View {
Task { Task {
try? await Task.sleep(nanoseconds: 200_000_000) // 0.2 seconds try? await Task.sleep(nanoseconds: 200_000_000) // 0.2 seconds
dataManager.onAppBecomeActive() dataManager.onAppBecomeActive()
// Re-verify health integration when app becomes active
await dataManager.healthKitService.verifyAndRestoreIntegration()
} }
} else if newPhase == .background { } else if newPhase == .background {
dataManager.onAppEnterBackground() dataManager.onAppEnterBackground()
@@ -59,6 +61,10 @@ struct ContentView: View {
setupNotificationObservers() setupNotificationObservers()
// Trigger auto-sync on app start only // Trigger auto-sync on app start only
dataManager.syncService.triggerAutoSync(dataManager: dataManager) dataManager.syncService.triggerAutoSync(dataManager: dataManager)
// Verify and restore health integration if it was previously enabled
Task {
await dataManager.healthKitService.verifyAndRestoreIntegration()
}
} }
.onDisappear { .onDisappear {
removeNotificationObservers() removeNotificationObservers()
@@ -90,6 +96,8 @@ struct ContentView: View {
// Small delay to ensure app is fully active // Small delay to ensure app is fully active
try? await Task.sleep(nanoseconds: 800_000_000) // 0.8 seconds try? await Task.sleep(nanoseconds: 800_000_000) // 0.8 seconds
await dataManager.onAppBecomeActive() await dataManager.onAppBecomeActive()
// Re-verify health integration when returning from background
await dataManager.healthKitService.verifyAndRestoreIntegration()
} }
} }
@@ -103,6 +111,8 @@ struct ContentView: View {
Task { Task {
try? await Task.sleep(nanoseconds: 300_000_000) // 0.3 seconds try? await Task.sleep(nanoseconds: 300_000_000) // 0.3 seconds
await dataManager.onAppBecomeActive() await dataManager.onAppBecomeActive()
// Ensure health integration is verified
await dataManager.healthKitService.verifyAndRestoreIntegration()
} }
} }

View File

@@ -10,5 +10,9 @@
<string>This app needs access to your photo library to add photos to climbing problems.</string> <string>This app needs access to your photo library to add photos to climbing problems.</string>
<key>NSCameraUsageDescription</key> <key>NSCameraUsageDescription</key>
<string>This app needs access to your camera to take photos of climbing problems.</string> <string>This app needs access to your camera to take photos of climbing problems.</string>
<key>NSHealthShareUsageDescription</key>
<string>This app needs access to save your climbing workouts to Apple Health.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>This app needs access to save your climbing workouts to Apple Health.</string>
</dict> </dict>
</plist> </plist>

View File

@@ -1,8 +1,8 @@
import ActivityKit import ActivityKit
import Foundation import Foundation
struct SessionActivityAttributes: ActivityAttributes { struct SessionActivityAttributes: ActivityAttributes, Sendable {
public struct ContentState: Codable, Hashable { public struct ContentState: Codable, Hashable, Sendable {
var elapsed: TimeInterval var elapsed: TimeInterval
var totalAttempts: Int var totalAttempts: Int
var completedProblems: Int var completedProblems: Int
@@ -17,4 +17,3 @@ extension SessionActivityAttributes {
SessionActivityAttributes(gymName: "Summit Climbing", startTime: Date()) SessionActivityAttributes(gymName: "Summit Climbing", startTime: Date())
} }
} }

View File

@@ -6,5 +6,9 @@
<array> <array>
<string>group.com.atridad.OpenClimb</string> <string>group.com.atridad.OpenClimb</string>
</array> </array>
<key>com.apple.developer.healthkit</key>
<true/>
<key>com.apple.developer.healthkit.access</key>
<array/>
</dict> </dict>
</plist> </plist>

View File

@@ -0,0 +1,236 @@
import Combine
import Foundation
import HealthKit
@MainActor
class HealthKitService: ObservableObject {
static let shared = HealthKitService()
private let healthStore = HKHealthStore()
private var currentWorkoutStartDate: Date?
private var currentWorkoutSessionId: UUID?
@Published var isAuthorized = false
@Published var isEnabled = false
private let userDefaults = UserDefaults.standard
private let isEnabledKey = "healthKitEnabled"
private let workoutStartDateKey = "healthKitWorkoutStartDate"
private let workoutSessionIdKey = "healthKitWorkoutSessionId"
private init() {
loadSettings()
restoreActiveWorkout()
}
func loadSettings() {
isEnabled = userDefaults.bool(forKey: isEnabledKey)
if HKHealthStore.isHealthDataAvailable() {
checkAuthorization()
}
}
/// Restore active workout state
private func restoreActiveWorkout() {
if let startDate = userDefaults.object(forKey: workoutStartDateKey) as? Date,
let sessionIdString = userDefaults.string(forKey: workoutSessionIdKey),
let sessionId = UUID(uuidString: sessionIdString)
{
currentWorkoutStartDate = startDate
currentWorkoutSessionId = sessionId
print("HealthKit: Restored active workout from \(startDate)")
}
}
/// Persist active workout state
private func persistActiveWorkout() {
if let startDate = currentWorkoutStartDate, let sessionId = currentWorkoutSessionId {
userDefaults.set(startDate, forKey: workoutStartDateKey)
userDefaults.set(sessionId.uuidString, forKey: workoutSessionIdKey)
} else {
userDefaults.removeObject(forKey: workoutStartDateKey)
userDefaults.removeObject(forKey: workoutSessionIdKey)
}
}
/// Verify and restore health integration
func verifyAndRestoreIntegration() async {
guard isEnabled else { return }
guard HKHealthStore.isHealthDataAvailable() else {
print("HealthKit: Device does not support HealthKit")
return
}
checkAuthorization()
if !isAuthorized {
print(
"HealthKit: Integration was enabled but authorization lost, attempting to restore..."
)
do {
try await requestAuthorization()
print("HealthKit: Authorization restored successfully")
} catch {
print("HealthKit: Failed to restore authorization: \(error.localizedDescription)")
}
} else {
print("HealthKit: Integration verified - authorization is valid")
}
if hasActiveWorkout() {
print(
"HealthKit: Active workout restored - started at \(currentWorkoutStartDate!)"
)
}
}
func setEnabled(_ enabled: Bool) {
isEnabled = enabled
userDefaults.set(enabled, forKey: isEnabledKey)
}
func requestAuthorization() async throws {
guard HKHealthStore.isHealthDataAvailable() else {
throw HealthKitError.notAvailable
}
let workoutType = HKObjectType.workoutType()
let energyBurnedType = HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!
let typesToShare: Set<HKSampleType> = [
workoutType,
energyBurnedType,
]
let typesToRead: Set<HKObjectType> = [
workoutType
]
try await healthStore.requestAuthorization(toShare: typesToShare, read: typesToRead)
self.isAuthorized = true
}
private func checkAuthorization() {
let workoutType = HKObjectType.workoutType()
let status = healthStore.authorizationStatus(for: workoutType)
isAuthorized = (status == .sharingAuthorized)
}
func startWorkout(startDate: Date, sessionId: UUID) async throws {
guard isEnabled && isAuthorized else {
return
}
guard HKHealthStore.isHealthDataAvailable() else {
throw HealthKitError.notAvailable
}
currentWorkoutStartDate = startDate
currentWorkoutSessionId = sessionId
persistActiveWorkout()
print("HealthKit: Started workout for session \(sessionId)")
}
func endWorkout(endDate: Date) async throws {
guard isEnabled && isAuthorized else {
return
}
guard let startDate = currentWorkoutStartDate else {
return
}
guard HKHealthStore.isHealthDataAvailable() else {
throw HealthKitError.notAvailable
}
let duration = endDate.timeIntervalSince(startDate)
let calories = estimateCalories(durationInMinutes: duration / 60.0)
let energyBurned = HKQuantity(unit: .kilocalorie(), doubleValue: calories)
let workoutConfiguration = HKWorkoutConfiguration()
workoutConfiguration.activityType = .climbing
workoutConfiguration.locationType = .indoor
let builder = HKWorkoutBuilder(
healthStore: healthStore,
configuration: workoutConfiguration,
device: .local()
)
do {
try await builder.beginCollection(at: startDate)
let energyBurnedType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!
let energySample = HKQuantitySample(
type: energyBurnedType,
quantity: energyBurned,
start: startDate,
end: endDate
)
try await builder.addSamples([energySample])
try await builder.addMetadata([HKMetadataKeyIndoorWorkout: true])
try await builder.endCollection(at: endDate)
let workout = try await builder.finishWorkout()
print(
"HealthKit: Workout saved successfully with id: \(workout?.uuid.uuidString ?? "unknown")"
)
currentWorkoutStartDate = nil
currentWorkoutSessionId = nil
persistActiveWorkout()
} catch {
print("HealthKit: Failed to save workout: \(error.localizedDescription)")
currentWorkoutStartDate = nil
currentWorkoutSessionId = nil
persistActiveWorkout()
throw HealthKitError.workoutSaveFailed
}
}
func cancelWorkout() {
currentWorkoutStartDate = nil
currentWorkoutSessionId = nil
persistActiveWorkout()
print("HealthKit: Workout cancelled")
}
func hasActiveWorkout() -> Bool {
return currentWorkoutStartDate != nil
}
private func estimateCalories(durationInMinutes: Double) -> Double {
let caloriesPerMinute = 8.0
return durationInMinutes * caloriesPerMinute
}
}
enum HealthKitError: LocalizedError {
case notAvailable
case notAuthorized
case workoutStartFailed
case workoutSaveFailed
var errorDescription: String? {
switch self {
case .notAvailable:
return "HealthKit is not available on this device"
case .notAuthorized:
return "HealthKit authorization not granted"
case .workoutStartFailed:
return "Failed to start HealthKit workout"
case .workoutSaveFailed:
return "Failed to save workout to HealthKit"
}
}
}

View File

@@ -11,6 +11,9 @@ class SyncService: ObservableObject {
@Published var isTesting = false @Published var isTesting = false
private let userDefaults = UserDefaults.standard private let userDefaults = UserDefaults.standard
private var syncTask: Task<Void, Never>?
private var pendingChanges = false
private let syncDebounceDelay: TimeInterval = 2.0
private enum Keys { private enum Keys {
static let serverURL = "sync_server_url" static let serverURL = "sync_server_url"
@@ -44,6 +47,11 @@ class SyncService: ObservableObject {
self.lastSyncTime = lastSync self.lastSyncTime = lastSync
} }
self.isConnected = userDefaults.bool(forKey: Keys.isConnected) self.isConnected = userDefaults.bool(forKey: Keys.isConnected)
// Perform image naming migration on initialization
Task {
await performImageNamingMigration()
}
} }
func downloadData() async throws -> ClimbDataBackup { func downloadData() async throws -> ClimbDataBackup {
@@ -144,6 +152,9 @@ class SyncService: ObservableObject {
request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type") request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
request.httpBody = imageData request.httpBody = imageData
request.timeoutInterval = 60.0
request.cachePolicy = .reloadIgnoringLocalCacheData
let (_, response) = try await URLSession.shared.data(for: request) let (_, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else { guard let httpResponse = response as? HTTPURLResponse else {
@@ -173,6 +184,9 @@ class SyncService: ObservableObject {
request.httpMethod = "GET" request.httpMethod = "GET"
request.setValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization") request.setValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization")
request.timeoutInterval = 45.0
request.cachePolicy = .returnCacheDataElseLoad
let (data, response) = try await URLSession.shared.data(for: request) let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else { guard let httpResponse = response as? HTTPURLResponse else {
@@ -283,7 +297,6 @@ class SyncService: ObservableObject {
{ {
var imagePathMapping: [String: String] = [:] var imagePathMapping: [String: String] = [:]
// Process images by problem to maintain consistent naming
for problem in backup.problems { for problem in backup.problems {
guard let imagePaths = problem.imagePaths, !imagePaths.isEmpty else { continue } guard let imagePaths = problem.imagePaths, !imagePaths.isEmpty else { continue }
@@ -293,19 +306,13 @@ class SyncService: ObservableObject {
do { do {
let imageData = try await downloadImage(filename: serverFilename) let imageData = try await downloadImage(filename: serverFilename)
// Generate consistent filename if needed let consistentFilename = ImageNamingUtils.generateImageFilename(
let consistentFilename = problemId: problem.id, imageIndex: index)
ImageNamingUtils.isValidImageFilename(serverFilename)
? serverFilename
: ImageNamingUtils.generateImageFilename(
problemId: problem.id, imageIndex: index)
// Save image with consistent filename
let imageManager = ImageManager.shared let imageManager = ImageManager.shared
_ = try imageManager.saveImportedImage( _ = try imageManager.saveImportedImage(
imageData, filename: consistentFilename) imageData, filename: consistentFilename)
// Map server filename to consistent local filename
imagePathMapping[serverFilename] = consistentFilename imagePathMapping[serverFilename] = consistentFilename
print("Downloaded and mapped image: \(serverFilename) -> \(consistentFilename)") print("Downloaded and mapped image: \(serverFilename) -> \(consistentFilename)")
} catch SyncError.imageNotFound { } catch SyncError.imageNotFound {
@@ -329,12 +336,8 @@ class SyncService: ObservableObject {
for (index, imagePath) in problem.imagePaths.enumerated() { for (index, imagePath) in problem.imagePaths.enumerated() {
let filename = URL(fileURLWithPath: imagePath).lastPathComponent let filename = URL(fileURLWithPath: imagePath).lastPathComponent
// Ensure filename follows consistent naming convention let consistentFilename = ImageNamingUtils.generateImageFilename(
let consistentFilename = problemId: problem.id.uuidString, imageIndex: index)
ImageNamingUtils.isValidImageFilename(filename)
? filename
: ImageNamingUtils.generateImageFilename(
problemId: problem.id.uuidString, imageIndex: index)
// Load image data // Load image data
let imageManager = ImageManager.shared let imageManager = ImageManager.shared
@@ -392,6 +395,53 @@ class SyncService: ObservableObject {
) )
} }
func createBackupForExport(_ dataManager: ClimbingDataManager) -> ClimbDataBackup {
// Filter out active sessions and their attempts from sync
let completedSessions = dataManager.sessions.filter { $0.status != .active }
let activeSessionIds = Set(
dataManager.sessions.filter { $0.status == .active }.map { $0.id })
let completedAttempts = dataManager.attempts.filter {
!activeSessionIds.contains($0.sessionId)
}
// Create backup with normalized image paths for export
return ClimbDataBackup(
exportedAt: DataStateManager.shared.getLastModified(),
gyms: dataManager.gyms.map { BackupGym(from: $0) },
problems: dataManager.problems.map { problem in
var backupProblem = BackupProblem(from: problem)
if !problem.imagePaths.isEmpty {
let normalizedPaths = problem.imagePaths.enumerated().map { index, _ in
ImageNamingUtils.generateImageFilename(
problemId: problem.id.uuidString, imageIndex: index)
}
backupProblem = BackupProblem(
id: backupProblem.id,
gymId: backupProblem.gymId,
name: backupProblem.name,
description: backupProblem.description,
climbType: backupProblem.climbType,
difficulty: backupProblem.difficulty,
tags: backupProblem.tags,
location: backupProblem.location,
imagePaths: normalizedPaths,
isActive: backupProblem.isActive,
dateSet: backupProblem.dateSet,
notes: backupProblem.notes,
createdAt: backupProblem.createdAt,
updatedAt: backupProblem.updatedAt
)
}
return backupProblem
},
sessions: completedSessions.map { BackupClimbSession(from: $0) },
attempts: completedAttempts.map { BackupAttempt(from: $0) },
deletedItems: dataManager.getDeletedItems()
)
}
private func mergeDataSafely( private func mergeDataSafely(
localBackup: ClimbDataBackup, localBackup: ClimbDataBackup,
serverBackup: ClimbDataBackup, serverBackup: ClimbDataBackup,
@@ -620,17 +670,31 @@ class SyncService: ObservableObject {
} }
let jsonData = try encoder.encode(backup) let jsonData = try encoder.encode(backup)
// Collect all downloaded images from ImageManager // Collect all images from ImageManager
let imageManager = ImageManager.shared let imageManager = ImageManager.shared
var imageFiles: [(filename: String, data: Data)] = [] var imageFiles: [(filename: String, data: Data)] = []
let imagePaths = Set(backup.problems.flatMap { $0.imagePaths ?? [] })
for imagePath in imagePaths { // Get original problems to access actual image paths on disk
let filename = URL(fileURLWithPath: imagePath).lastPathComponent if let problemsData = userDefaults.data(forKey: "problems"),
let fullPath = imageManager.imagesDirectory.appendingPathComponent(filename).path let problems = try? JSONDecoder().decode([Problem].self, from: problemsData)
if let imageData = imageManager.loadImageData(fromPath: fullPath) { {
imageFiles.append((filename: filename, data: imageData)) // Create a mapping from normalized paths to actual paths
for problem in problems {
for (index, imagePath) in problem.imagePaths.enumerated() {
// Get the actual filename on disk
let actualFilename = URL(fileURLWithPath: imagePath).lastPathComponent
let fullPath = imageManager.imagesDirectory.appendingPathComponent(
actualFilename
).path
// Generate the normalized filename for the ZIP
let normalizedFilename = ImageNamingUtils.generateImageFilename(
problemId: problem.id.uuidString, imageIndex: index)
if let imageData = imageManager.loadImageData(fromPath: fullPath) {
imageFiles.append((filename: normalizedFilename, data: imageData))
}
}
} }
} }
@@ -875,20 +939,51 @@ class SyncService: ObservableObject {
} }
func triggerAutoSync(dataManager: ClimbingDataManager) { func triggerAutoSync(dataManager: ClimbingDataManager) {
// Early exit if sync cannot proceed - don't set isSyncing
guard isConnected && isConfigured && isAutoSyncEnabled else { guard isConnected && isConfigured && isAutoSyncEnabled else {
// Ensure isSyncing is false when sync is not possible
if isSyncing { if isSyncing {
isSyncing = false isSyncing = false
} }
return return
} }
// Prevent multiple simultaneous syncs if isSyncing {
guard !isSyncing else { pendingChanges = true
return return
} }
syncTask?.cancel()
syncTask = Task {
try? await Task.sleep(nanoseconds: UInt64(syncDebounceDelay * 1_000_000_000))
guard !Task.isCancelled else { return }
repeat {
pendingChanges = false
do {
try await syncWithServer(dataManager: dataManager)
} catch {
await MainActor.run {
self.isSyncing = false
}
return
}
if pendingChanges {
try? await Task.sleep(nanoseconds: UInt64(syncDebounceDelay * 1_000_000_000))
}
} while pendingChanges && !Task.isCancelled
}
}
func forceSyncNow(dataManager: ClimbingDataManager) {
guard isConnected && isConfigured else { return }
syncTask?.cancel()
syncTask = nil
pendingChanges = false
Task { Task {
do { do {
try await syncWithServer(dataManager: dataManager) try await syncWithServer(dataManager: dataManager)
@@ -901,6 +996,10 @@ class SyncService: ObservableObject {
} }
func disconnect() { func disconnect() {
syncTask?.cancel()
syncTask = nil
pendingChanges = false
isSyncing = false
isConnected = false isConnected = false
lastSyncTime = nil lastSyncTime = nil
syncError = nil syncError = nil
@@ -917,6 +1016,112 @@ class SyncService: ObservableObject {
userDefaults.removeObject(forKey: Keys.lastSyncTime) userDefaults.removeObject(forKey: Keys.lastSyncTime)
userDefaults.removeObject(forKey: Keys.isConnected) userDefaults.removeObject(forKey: Keys.isConnected)
userDefaults.removeObject(forKey: Keys.autoSyncEnabled) userDefaults.removeObject(forKey: Keys.autoSyncEnabled)
syncTask?.cancel()
syncTask = nil
pendingChanges = false
}
deinit {
syncTask?.cancel()
}
// MARK: - Image Naming Migration
private func performImageNamingMigration() async {
let migrationKey = "image_naming_migration_completed_v2"
guard !userDefaults.bool(forKey: migrationKey) else {
print("Image naming migration already completed")
return
}
print("Starting image naming migration...")
var updateCount = 0
let imageManager = ImageManager.shared
// Get all problems from UserDefaults
if let problemsData = userDefaults.data(forKey: "problems"),
var problems = try? JSONDecoder().decode([Problem].self, from: problemsData)
{
for problemIndex in 0..<problems.count {
let problem = problems[problemIndex]
guard !problem.imagePaths.isEmpty else { continue }
var updatedImagePaths: [String] = []
var hasChanges = false
for (imageIndex, imagePath) in problem.imagePaths.enumerated() {
let currentFilename = URL(fileURLWithPath: imagePath).lastPathComponent
let consistentFilename = ImageNamingUtils.generateImageFilename(
problemId: problem.id.uuidString, imageIndex: imageIndex)
if currentFilename != consistentFilename {
let oldPath = imageManager.imagesDirectory.appendingPathComponent(
currentFilename
).path
let newPath = imageManager.imagesDirectory.appendingPathComponent(
consistentFilename
).path
if FileManager.default.fileExists(atPath: oldPath) {
do {
try FileManager.default.moveItem(atPath: oldPath, toPath: newPath)
updatedImagePaths.append(consistentFilename)
hasChanges = true
updateCount += 1
print("Migrated image: \(currentFilename) -> \(consistentFilename)")
} catch {
print("Failed to migrate image \(currentFilename): \(error)")
updatedImagePaths.append(imagePath)
}
} else {
updatedImagePaths.append(imagePath)
}
} else {
updatedImagePaths.append(imagePath)
}
}
if hasChanges {
// Decode problem to dictionary, update imagePaths, re-encode
if let problemData = try? JSONEncoder().encode(problem),
var problemDict = try? JSONSerialization.jsonObject(with: problemData)
as? [String: Any]
{
problemDict["imagePaths"] = updatedImagePaths
problemDict["updatedAt"] = ISO8601DateFormatter().string(from: Date())
if let updatedData = try? JSONSerialization.data(
withJSONObject: problemDict),
let updatedProblem = try? JSONDecoder().decode(
Problem.self, from: updatedData)
{
problems[problemIndex] = updatedProblem
}
}
}
}
if updateCount > 0 {
if let updatedData = try? JSONEncoder().encode(problems) {
userDefaults.set(updatedData, forKey: "problems")
print("Updated \(updateCount) image paths in UserDefaults")
}
}
}
userDefaults.set(true, forKey: migrationKey)
print("Image naming migration completed, updated \(updateCount) images")
// Notify ClimbingDataManager to reload data if images were updated
if updateCount > 0 {
DispatchQueue.main.async {
NotificationCenter.default.post(
name: NSNotification.Name("ImageMigrationCompleted"),
object: nil,
userInfo: ["updateCount": updateCount]
)
}
}
} }
// MARK: - Safe Merge Functions // MARK: - Safe Merge Functions
@@ -926,13 +1131,14 @@ class SyncService: ObservableObject {
var merged = local var merged = local
let deletedGymIds = Set(deletedItems.filter { $0.type == "gym" }.map { $0.id }) let deletedGymIds = Set(deletedItems.filter { $0.type == "gym" }.map { $0.id })
// Remove items that were deleted on other devices let localGymIds = Set(local.map { $0.id.uuidString })
merged.removeAll { deletedGymIds.contains($0.id.uuidString) } merged.removeAll { deletedGymIds.contains($0.id.uuidString) }
// Add new items from server (excluding deleted ones) // Add new items from server (excluding deleted ones)
for serverGym in server { for serverGym in server {
if let serverGymConverted = try? serverGym.toGym() { if let serverGymConverted = try? serverGym.toGym() {
let localHasGym = local.contains(where: { $0.id.uuidString == serverGym.id }) let localHasGym = localGymIds.contains(serverGym.id)
let isDeleted = deletedGymIds.contains(serverGym.id) let isDeleted = deletedGymIds.contains(serverGym.id)
if !localHasGym && !isDeleted { if !localHasGym && !isDeleted {
@@ -953,41 +1159,44 @@ class SyncService: ObservableObject {
var merged = local var merged = local
let deletedProblemIds = Set(deletedItems.filter { $0.type == "problem" }.map { $0.id }) let deletedProblemIds = Set(deletedItems.filter { $0.type == "problem" }.map { $0.id })
// Remove items that were deleted on other devices let localProblemIds = Set(local.map { $0.id.uuidString })
merged.removeAll { deletedProblemIds.contains($0.id.uuidString) } merged.removeAll { deletedProblemIds.contains($0.id.uuidString) }
// Add new items from server (excluding deleted ones)
for serverProblem in server { for serverProblem in server {
var problemToAdd = serverProblem let localHasProblem = localProblemIds.contains(serverProblem.id)
let isDeleted = deletedProblemIds.contains(serverProblem.id)
// Update image paths if needed if !localHasProblem && !isDeleted {
if !imagePathMapping.isEmpty { var problemToAdd = serverProblem
let updatedImagePaths = serverProblem.imagePaths?.compactMap { oldPath in
imagePathMapping[oldPath] ?? oldPath if !imagePathMapping.isEmpty, let imagePaths = serverProblem.imagePaths,
!imagePaths.isEmpty
{
let updatedImagePaths = imagePaths.compactMap { oldPath in
imagePathMapping[oldPath] ?? oldPath
}
if updatedImagePaths != imagePaths {
problemToAdd = BackupProblem(
id: serverProblem.id,
gymId: serverProblem.gymId,
name: serverProblem.name,
description: serverProblem.description,
climbType: serverProblem.climbType,
difficulty: serverProblem.difficulty,
tags: serverProblem.tags,
location: serverProblem.location,
imagePaths: updatedImagePaths,
isActive: serverProblem.isActive,
dateSet: serverProblem.dateSet,
notes: serverProblem.notes,
createdAt: serverProblem.createdAt,
updatedAt: serverProblem.updatedAt
)
}
} }
problemToAdd = BackupProblem(
id: serverProblem.id,
gymId: serverProblem.gymId,
name: serverProblem.name,
description: serverProblem.description,
climbType: serverProblem.climbType,
difficulty: serverProblem.difficulty,
tags: serverProblem.tags,
location: serverProblem.location,
imagePaths: updatedImagePaths,
isActive: serverProblem.isActive,
dateSet: serverProblem.dateSet,
notes: serverProblem.notes,
createdAt: serverProblem.createdAt,
updatedAt: serverProblem.updatedAt
)
}
if let serverProblemConverted = try? problemToAdd.toProblem() { if let serverProblemConverted = try? problemToAdd.toProblem() {
let localHasProblem = local.contains(where: { $0.id.uuidString == problemToAdd.id })
let isDeleted = deletedProblemIds.contains(problemToAdd.id)
if !localHasProblem && !isDeleted {
merged.append(serverProblemConverted) merged.append(serverProblemConverted)
} }
} }
@@ -1004,19 +1213,18 @@ class SyncService: ObservableObject {
var merged = local var merged = local
let deletedSessionIds = Set(deletedItems.filter { $0.type == "session" }.map { $0.id }) let deletedSessionIds = Set(deletedItems.filter { $0.type == "session" }.map { $0.id })
// Remove items that were deleted on other devices (but never remove active sessions) let localSessionIds = Set(local.map { $0.id.uuidString })
merged.removeAll { session in merged.removeAll { session in
deletedSessionIds.contains(session.id.uuidString) && session.status != .active deletedSessionIds.contains(session.id.uuidString) && session.status != .active
} }
// Add new items from server (excluding deleted ones)
for serverSession in server { for serverSession in server {
if let serverSessionConverted = try? serverSession.toClimbSession() { let localHasSession = localSessionIds.contains(serverSession.id)
let localHasSession = local.contains(where: { $0.id.uuidString == serverSession.id } let isDeleted = deletedSessionIds.contains(serverSession.id)
)
let isDeleted = deletedSessionIds.contains(serverSession.id)
if !localHasSession && !isDeleted { if !localHasSession && !isDeleted {
if let serverSessionConverted = try? serverSession.toClimbSession() {
merged.append(serverSessionConverted) merged.append(serverSessionConverted)
} }
} }
@@ -1031,6 +1239,8 @@ class SyncService: ObservableObject {
var merged = local var merged = local
let deletedAttemptIds = Set(deletedItems.filter { $0.type == "attempt" }.map { $0.id }) let deletedAttemptIds = Set(deletedItems.filter { $0.type == "attempt" }.map { $0.id })
let localAttemptIds = Set(local.map { $0.id.uuidString })
// Get active session IDs to protect their attempts // Get active session IDs to protect their attempts
let activeSessionIds = Set( let activeSessionIds = Set(
local.compactMap { attempt in local.compactMap { attempt in
@@ -1048,14 +1258,12 @@ class SyncService: ObservableObject {
&& !activeSessionIds.contains(attempt.sessionId) && !activeSessionIds.contains(attempt.sessionId)
} }
// Add new items from server (excluding deleted ones)
for serverAttempt in server { for serverAttempt in server {
if let serverAttemptConverted = try? serverAttempt.toAttempt() { let localHasAttempt = localAttemptIds.contains(serverAttempt.id)
let localHasAttempt = local.contains(where: { $0.id.uuidString == serverAttempt.id } let isDeleted = deletedAttemptIds.contains(serverAttempt.id)
)
let isDeleted = deletedAttemptIds.contains(serverAttempt.id)
if !localHasAttempt && !isDeleted { if !localHasAttempt && !isDeleted {
if let serverAttemptConverted = try? serverAttempt.toAttempt() {
merged.append(serverAttemptConverted) merged.append(serverAttemptConverted)
} }
} }

View File

@@ -106,7 +106,7 @@ import SwiftUI
.font(.subheadline) .font(.subheadline)
.fontWeight(.medium) .fontWeight(.medium)
Image("MountainsIcon") Image("AppLogo")
.resizable() .resizable()
.frame(width: 24, height: 24) .frame(width: 24, height: 24)
.background(Circle().fill(.quaternary)) .background(Circle().fill(.quaternary))
@@ -115,7 +115,7 @@ import SwiftUI
.font(.caption) .font(.caption)
.foregroundColor(.secondary) .foregroundColor(.secondary)
Image("MountainsIcon") Image("AppLogo")
.resizable() .resizable()
.frame(width: 32, height: 32) .frame(width: 32, height: 32)
.background(Circle().fill(.quaternary)) .background(Circle().fill(.quaternary))
@@ -322,7 +322,7 @@ import SwiftUI
// Check if main bundle contains the expected icon assets // Check if main bundle contains the expected icon assets
let expectedAssets = [ let expectedAssets = [
"AppIcon", "AppIcon",
"MountainsIcon", "AppLogo",
] ]
for asset in expectedAssets { for asset in expectedAssets {
@@ -376,7 +376,7 @@ import SwiftUI
.font(.headline) .font(.headline)
HStack(spacing: 20) { HStack(spacing: 20) {
Image("MountainsIcon") Image("AppLogo")
.resizable() .resizable()
.frame(width: 64, height: 64) .frame(width: 64, height: 64)
.background( .background(
@@ -385,7 +385,7 @@ import SwiftUI
) )
VStack(alignment: .leading) { VStack(alignment: .leading) {
Text("MountainsIcon") Text("AppLogo")
.font(.subheadline) .font(.subheadline)
.fontWeight(.medium) .fontWeight(.medium)
Text("In-app icon display") Text("In-app icon display")

View File

@@ -852,4 +852,73 @@ class ImageManager {
print("ERROR: Failed to migrate from previous Application Support: \(error)") print("ERROR: Failed to migrate from previous Application Support: \(error)")
} }
} }
func migrateImageNamesToDeterministic(dataManager: ClimbingDataManager) {
print("Starting migration of image names to deterministic format...")
var migrationCount = 0
var updatedProblems: [Problem] = []
for problem in dataManager.problems {
guard !problem.imagePaths.isEmpty else { continue }
var newImagePaths: [String] = []
var problemNeedsUpdate = false
for (index, imagePath) in problem.imagePaths.enumerated() {
let currentFilename = URL(fileURLWithPath: imagePath).lastPathComponent
if ImageNamingUtils.isValidImageFilename(currentFilename) {
newImagePaths.append(imagePath)
continue
}
let deterministicName = ImageNamingUtils.generateImageFilename(
problemId: problem.id.uuidString, imageIndex: index)
let oldPath = imagesDirectory.appendingPathComponent(currentFilename)
let newPath = imagesDirectory.appendingPathComponent(deterministicName)
if fileManager.fileExists(atPath: oldPath.path) {
do {
try fileManager.moveItem(at: oldPath, to: newPath)
let oldBackupPath = backupDirectory.appendingPathComponent(currentFilename)
let newBackupPath = backupDirectory.appendingPathComponent(
deterministicName)
if fileManager.fileExists(atPath: oldBackupPath.path) {
try? fileManager.moveItem(at: oldBackupPath, to: newBackupPath)
}
newImagePaths.append(deterministicName)
problemNeedsUpdate = true
migrationCount += 1
print("Migrated: \(currentFilename)\(deterministicName)")
} catch {
print("Failed to migrate \(currentFilename): \(error)")
newImagePaths.append(imagePath)
}
} else {
print("Warning: Image file not found: \(currentFilename)")
newImagePaths.append(imagePath)
}
}
if problemNeedsUpdate {
let updatedProblem = problem.updated(imagePaths: newImagePaths)
updatedProblems.append(updatedProblem)
}
}
for updatedProblem in updatedProblems {
dataManager.updateProblem(updatedProblem)
}
print(
"Migration completed: \(migrationCount) images renamed, \(updatedProblems.count) problems updated"
)
}
} }

View File

@@ -11,21 +11,18 @@ class ImageNamingUtils {
private static let hashLength = 12 private static let hashLength = 12
/// Generates a deterministic filename for a problem image /// Generates a deterministic filename for a problem image
static func generateImageFilename(problemId: String, timestamp: String, imageIndex: Int) static func generateImageFilename(problemId: String, imageIndex: Int) -> String {
-> String let input = "\(problemId)_\(imageIndex)"
{
let input = "\(problemId)_\(timestamp)_\(imageIndex)"
let hash = createHash(from: input) let hash = createHash(from: input)
return "problem_\(hash)_\(imageIndex)\(imageExtension)" return "problem_\(hash)_\(imageIndex)\(imageExtension)"
} }
/// Generates a deterministic filename using current timestamp /// Legacy method for backward compatibility
static func generateImageFilename(problemId: String, imageIndex: Int) -> String { static func generateImageFilename(problemId: String, timestamp: String, imageIndex: Int)
let timestamp = ISO8601DateFormatter().string(from: Date()) -> String
return generateImageFilename( {
problemId: problemId, timestamp: timestamp, imageIndex: imageIndex) return generateImageFilename(problemId: problemId, imageIndex: imageIndex)
} }
/// Extracts problem ID from an image filename /// Extracts problem ID from an image filename
@@ -64,9 +61,7 @@ class ImageNamingUtils {
return oldFilename return oldFilename
} }
let timestamp = ISO8601DateFormatter().string(from: Date()) return generateImageFilename(problemId: problemId, imageIndex: imageIndex)
return generateImageFilename(
problemId: problemId, timestamp: timestamp, imageIndex: imageIndex)
} }
/// Creates a deterministic hash from input string /// Creates a deterministic hash from input string
@@ -84,8 +79,7 @@ class ImageNamingUtils {
var renameMap: [String: String] = [:] var renameMap: [String: String] = [:]
for (index, oldFilename) in existingFilenames.enumerated() { for (index, oldFilename) in existingFilenames.enumerated() {
let newFilename = migrateFilename( let newFilename = generateImageFilename(problemId: problemId, imageIndex: index)
oldFilename: oldFilename, problemId: problemId, imageIndex: index)
if newFilename != oldFilename { if newFilename != oldFilename {
renameMap[oldFilename] = newFilename renameMap[oldFilename] = newFilename
} }
@@ -113,6 +107,40 @@ class ImageNamingUtils {
invalidImages: invalidImages invalidImages: invalidImages
) )
} }
/// Generates the canonical filename that should be used for a problem image
static func getCanonicalImageFilename(problemId: String, imageIndex: Int) -> String {
return generateImageFilename(problemId: problemId, imageIndex: imageIndex)
}
/// Creates a mapping of existing server filenames to canonical filenames
static func createServerMigrationMap(
problemId: String,
serverImageFilenames: [String],
localImageCount: Int
) -> [String: String] {
var migrationMap: [String: String] = [:]
for imageIndex in 0..<localImageCount {
let canonicalName = getCanonicalImageFilename(
problemId: problemId, imageIndex: imageIndex)
if serverImageFilenames.contains(canonicalName) {
continue
}
for serverFilename in serverImageFilenames {
if isValidImageFilename(serverFilename)
&& !migrationMap.values.contains(serverFilename)
{
migrationMap[serverFilename] = canonicalName
break
}
}
}
return migrationMap
}
} }
// Result of image filename validation // Result of image filename validation

View File

@@ -1,5 +1,6 @@
import Combine import Combine
import Foundation import Foundation
import HealthKit
import SwiftUI import SwiftUI
import UniformTypeIdentifiers import UniformTypeIdentifiers
@@ -27,12 +28,12 @@ class ClimbingDataManager: ObservableObject {
private let sharedUserDefaults = UserDefaults(suiteName: "group.com.atridad.OpenClimb") private let sharedUserDefaults = UserDefaults(suiteName: "group.com.atridad.OpenClimb")
private let encoder = JSONEncoder() private let encoder = JSONEncoder()
private let decoder = JSONDecoder() private let decoder = JSONDecoder()
private var liveActivityObserver: NSObjectProtocol? nonisolated(unsafe) private var liveActivityObserver: NSObjectProtocol?
nonisolated(unsafe) private var migrationObserver: NSObjectProtocol?
// Sync service for automatic syncing
let syncService = SyncService() let syncService = SyncService()
let healthKitService = HealthKitService.shared
// Published property to propagate sync state changes
@Published var isSyncing = false @Published var isSyncing = false
private enum Keys { private enum Keys {
@@ -68,8 +69,8 @@ class ClimbingDataManager: ObservableObject {
init() { init() {
_ = ImageManager.shared _ = ImageManager.shared
loadAllData() loadAllData()
migrateImagePaths()
setupLiveActivityNotifications() setupLiveActivityNotifications()
setupMigrationNotifications()
// Keep our published isSyncing in sync with syncService.isSyncing // Keep our published isSyncing in sync with syncService.isSyncing
syncService.$isSyncing syncService.$isSyncing
@@ -88,6 +89,9 @@ class ClimbingDataManager: ObservableObject {
if let observer = liveActivityObserver { if let observer = liveActivityObserver {
NotificationCenter.default.removeObserver(observer) NotificationCenter.default.removeObserver(observer)
} }
if let observer = migrationObserver {
NotificationCenter.default.removeObserver(observer)
}
} }
private func loadAllData() { private func loadAllData() {
@@ -96,6 +100,9 @@ class ClimbingDataManager: ObservableObject {
loadSessions() loadSessions()
loadAttempts() loadAttempts()
loadActiveSession() loadActiveSession()
// Clean up orphaned data after loading
cleanupOrphanedData()
} }
private func loadGyms() { private func loadGyms() {
@@ -286,7 +293,16 @@ class ClimbingDataManager: ObservableObject {
} }
func deleteProblem(_ problem: Problem) { func deleteProblem(_ problem: Problem) {
// Delete associated attempts first // Track deletion of the problem
trackDeletion(itemId: problem.id.uuidString, itemType: "problem")
// Find and track all attempts for this problem as deleted
let problemAttempts = attempts.filter { $0.problemId == problem.id }
for attempt in problemAttempts {
trackDeletion(itemId: attempt.id.uuidString, itemType: "attempt")
}
// Delete associated attempts
attempts.removeAll { $0.problemId == problem.id } attempts.removeAll { $0.problemId == problem.id }
saveAttempts() saveAttempts()
@@ -295,7 +311,6 @@ class ClimbingDataManager: ObservableObject {
// Delete the problem // Delete the problem
problems.removeAll { $0.id == problem.id } problems.removeAll { $0.id == problem.id }
trackDeletion(itemId: problem.id.uuidString, itemType: "problem")
saveProblems() saveProblems()
DataStateManager.shared.updateDataState() DataStateManager.shared.updateDataState()
@@ -336,6 +351,18 @@ class ClimbingDataManager: ObservableObject {
for: newSession, gymName: gym.name) for: newSession, gymName: gym.name)
} }
} }
if healthKitService.isEnabled {
Task {
do {
try await healthKitService.startWorkout(
startDate: newSession.startTime ?? Date(),
sessionId: newSession.id)
} catch {
print("Failed to start HealthKit workout: \(error.localizedDescription)")
}
}
}
} }
func endSession(_ sessionId: UUID) { func endSession(_ sessionId: UUID) {
@@ -361,6 +388,17 @@ class ClimbingDataManager: ObservableObject {
Task { Task {
await LiveActivityManager.shared.endLiveActivity() await LiveActivityManager.shared.endLiveActivity()
} }
if healthKitService.isEnabled {
Task {
do {
try await healthKitService.endWorkout(
endDate: completedSession.endTime ?? Date())
} catch {
print("Failed to end HealthKit workout: \(error.localizedDescription)")
}
}
}
} }
} }
@@ -387,7 +425,16 @@ class ClimbingDataManager: ObservableObject {
} }
func deleteSession(_ session: ClimbSession) { func deleteSession(_ session: ClimbSession) {
// Delete associated attempts first // Track deletion of the session
trackDeletion(itemId: session.id.uuidString, itemType: "session")
// Find and track all attempts for this session as deleted
let sessionAttempts = attempts.filter { $0.sessionId == session.id }
for attempt in sessionAttempts {
trackDeletion(itemId: attempt.id.uuidString, itemType: "attempt")
}
// Delete associated attempts
attempts.removeAll { $0.sessionId == session.id } attempts.removeAll { $0.sessionId == session.id }
saveAttempts() saveAttempts()
@@ -399,7 +446,6 @@ class ClimbingDataManager: ObservableObject {
// Delete the session // Delete the session
sessions.removeAll { $0.id == session.id } sessions.removeAll { $0.id == session.id }
trackDeletion(itemId: session.id.uuidString, itemType: "session")
saveSessions() saveSessions()
DataStateManager.shared.updateDataState() DataStateManager.shared.updateDataState()
@@ -525,6 +571,162 @@ class ClimbingDataManager: ObservableObject {
return gym(withId: mostUsedGymId) return gym(withId: mostUsedGymId)
} }
/// Clean up orphaned data - removes attempts that reference non-existent sessions
/// and removes duplicate attempts. This ensures data integrity and prevents
/// orphaned attempts from appearing in widgets
private func cleanupOrphanedData() {
let validSessionIds = Set(sessions.map { $0.id })
let validProblemIds = Set(problems.map { $0.id })
let validGymIds = Set(gyms.map { $0.id })
let initialAttemptCount = attempts.count
// Remove attempts that reference deleted sessions or problems
let orphanedAttempts = attempts.filter { attempt in
!validSessionIds.contains(attempt.sessionId)
|| !validProblemIds.contains(attempt.problemId)
}
if !orphanedAttempts.isEmpty {
print("🧹 Cleaning up \(orphanedAttempts.count) orphaned attempts")
// Track these as deleted to prevent sync from re-introducing them
for attempt in orphanedAttempts {
trackDeletion(itemId: attempt.id.uuidString, itemType: "attempt")
}
// Remove orphaned attempts
attempts.removeAll { attempt in
!validSessionIds.contains(attempt.sessionId)
|| !validProblemIds.contains(attempt.problemId)
}
}
// Remove duplicate attempts (same session, problem, and timestamp within 1 second)
var seenAttempts: Set<String> = []
var duplicateIds: [UUID] = []
for attempt in attempts.sorted(by: { $0.timestamp < $1.timestamp }) {
// Create a unique key based on session, problem, and rounded timestamp
let timestampKey = Int(attempt.timestamp.timeIntervalSince1970)
let key =
"\(attempt.sessionId.uuidString)_\(attempt.problemId.uuidString)_\(timestampKey)"
if seenAttempts.contains(key) {
duplicateIds.append(attempt.id)
print("🧹 Found duplicate attempt: \(attempt.id)")
} else {
seenAttempts.insert(key)
}
}
if !duplicateIds.isEmpty {
print("🧹 Removing \(duplicateIds.count) duplicate attempts")
// Track duplicates as deleted
for attemptId in duplicateIds {
trackDeletion(itemId: attemptId.uuidString, itemType: "attempt")
}
// Remove duplicates
attempts.removeAll { duplicateIds.contains($0.id) }
}
if initialAttemptCount != attempts.count {
saveAttempts()
let removedCount = initialAttemptCount - attempts.count
print(
"Cleanup complete. Removed \(removedCount) attempts. Remaining: \(attempts.count)"
)
}
// Remove problems that reference deleted gyms
let orphanedProblems = problems.filter { problem in
!validGymIds.contains(problem.gymId)
}
if !orphanedProblems.isEmpty {
print("🧹 Cleaning up \(orphanedProblems.count) orphaned problems")
for problem in orphanedProblems {
trackDeletion(itemId: problem.id.uuidString, itemType: "problem")
}
problems.removeAll { problem in
!validGymIds.contains(problem.gymId)
}
saveProblems()
}
// Remove sessions that reference deleted gyms
let orphanedSessions = sessions.filter { session in
!validGymIds.contains(session.gymId)
}
if !orphanedSessions.isEmpty {
print("🧹 Cleaning up \(orphanedSessions.count) orphaned sessions")
for session in orphanedSessions {
trackDeletion(itemId: session.id.uuidString, itemType: "session")
}
sessions.removeAll { session in
!validGymIds.contains(session.gymId)
}
saveSessions()
}
}
/// Validate data integrity and return a report
/// This can be called manually to check for issues
func validateDataIntegrity() -> String {
let validSessionIds = Set(sessions.map { $0.id })
let validProblemIds = Set(problems.map { $0.id })
let validGymIds = Set(gyms.map { $0.id })
let orphanedAttempts = attempts.filter { attempt in
!validSessionIds.contains(attempt.sessionId)
|| !validProblemIds.contains(attempt.problemId)
}
let orphanedProblems = problems.filter { problem in
!validGymIds.contains(problem.gymId)
}
let orphanedSessions = sessions.filter { session in
!validGymIds.contains(session.gymId)
}
var report = "Data Integrity Report:\n"
report += "---------------------\n"
report += "Gyms: \(gyms.count)\n"
report += "Problems: \(problems.count)\n"
report += "Sessions: \(sessions.count)\n"
report += "Attempts: \(attempts.count)\n"
report += "\nOrphaned Data:\n"
report += "Orphaned Attempts: \(orphanedAttempts.count)\n"
report += "Orphaned Problems: \(orphanedProblems.count)\n"
report += "Orphaned Sessions: \(orphanedSessions.count)\n"
if orphanedAttempts.isEmpty && orphanedProblems.isEmpty && orphanedSessions.isEmpty {
report += "\nNo integrity issues found"
} else {
report += "\nIssues found - run cleanup to fix"
}
return report
}
/// Manually trigger cleanup of orphaned data
/// This can be called from settings or debug menu
func manualDataCleanup() {
cleanupOrphanedData()
successMessage = "Data cleanup completed"
clearMessageAfterDelay()
}
func resetAllData(showSuccessMessage: Bool = true) { func resetAllData(showSuccessMessage: Bool = true) {
gyms.removeAll() gyms.removeAll()
problems.removeAll() problems.removeAll()
@@ -551,6 +753,7 @@ class ClimbingDataManager: ObservableObject {
let dateFormatter = DateFormatter() let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS" dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
// Create export data with normalized image paths
let exportData = ClimbDataBackup( let exportData = ClimbDataBackup(
exportedAt: dateFormatter.string(from: Date()), exportedAt: dateFormatter.string(from: Date()),
version: "2.0", version: "2.0",
@@ -561,7 +764,7 @@ class ClimbingDataManager: ObservableObject {
attempts: attempts.map { BackupAttempt(from: $0) } attempts: attempts.map { BackupAttempt(from: $0) }
) )
// Collect referenced image paths // Collect actual image paths from disk for the ZIP
let referencedImagePaths = collectReferencedImagePaths() let referencedImagePaths = collectReferencedImagePaths()
print("Starting export with \(referencedImagePaths.count) images") print("Starting export with \(referencedImagePaths.count) images")
@@ -680,17 +883,19 @@ extension ClimbingDataManager {
"Problem '\(problem.name ?? "Unnamed")' has \(problem.imagePaths.count) images" "Problem '\(problem.name ?? "Unnamed")' has \(problem.imagePaths.count) images"
) )
for imagePath in problem.imagePaths { for imagePath in problem.imagePaths {
print(" - Relative path: \(imagePath)") print(" - Stored path: \(imagePath)")
let fullPath = ImageManager.shared.getFullPath(from: imagePath)
print(" - Full path: \(fullPath)") // Extract just the filename (migration should have normalized these)
let filename = URL(fileURLWithPath: imagePath).lastPathComponent
let fullPath = ImageManager.shared.getFullPath(from: filename)
print(" - Full disk path: \(fullPath)")
// Check if file exists
if FileManager.default.fileExists(atPath: fullPath) { if FileManager.default.fileExists(atPath: fullPath) {
print(" File exists") print(" File exists")
imagePaths.insert(fullPath) imagePaths.insert(fullPath)
} else { } else {
print(" File does NOT exist") print(" ✗ WARNING: File not found at \(fullPath)")
// Still add it to let ZipUtils handle the error logging // Still add it to let ZipUtils handle the logging
imagePaths.insert(fullPath) imagePaths.insert(fullPath)
} }
} }
@@ -706,11 +911,53 @@ extension ClimbingDataManager {
imagePathMapping: [String: String] imagePathMapping: [String: String]
) -> [BackupProblem] { ) -> [BackupProblem] {
return problems.map { problem in return problems.map { problem in
let updatedImagePaths = (problem.imagePaths ?? []).compactMap { oldPath in guard let originalImagePaths = problem.imagePaths, !originalImagePaths.isEmpty else {
let fileName = URL(fileURLWithPath: oldPath).lastPathComponent return problem
return imagePathMapping[fileName]
} }
return problem.withUpdatedImagePaths(updatedImagePaths)
var deterministicImagePaths: [String] = []
for (index, oldPath) in originalImagePaths.enumerated() {
let originalFileName = URL(fileURLWithPath: oldPath).lastPathComponent
let deterministicName = ImageNamingUtils.generateImageFilename(
problemId: problem.id, imageIndex: index)
if let tempFileName = imagePathMapping[originalFileName] {
let imageManager = ImageManager.shared
let tempPath = imageManager.imagesDirectory.appendingPathComponent(tempFileName)
let deterministicPath = imageManager.imagesDirectory.appendingPathComponent(
deterministicName)
do {
if FileManager.default.fileExists(atPath: tempPath.path) {
try FileManager.default.moveItem(at: tempPath, to: deterministicPath)
let tempBackupPath = imageManager.backupDirectory
.appendingPathComponent(tempFileName)
let deterministicBackupPath = imageManager.backupDirectory
.appendingPathComponent(deterministicName)
if FileManager.default.fileExists(atPath: tempBackupPath.path) {
try? FileManager.default.moveItem(
at: tempBackupPath, to: deterministicBackupPath)
}
deterministicImagePaths.append(deterministicName)
print("Renamed imported image: \(tempFileName)\(deterministicName)")
}
} catch {
print(
"Failed to rename imported image \(tempFileName) to \(deterministicName): \(error)"
)
deterministicImagePaths.append(tempFileName)
}
} else {
deterministicImagePaths.append(deterministicName)
}
}
return problem.withUpdatedImagePaths(deterministicImagePaths)
} }
} }
@@ -936,6 +1183,19 @@ extension ClimbingDataManager {
} }
} }
private func setupMigrationNotifications() {
migrationObserver = NotificationCenter.default.addObserver(
forName: NSNotification.Name("ImageMigrationCompleted"),
object: nil,
queue: .main
) { [weak self] notification in
if let updateCount = notification.userInfo?["updateCount"] as? Int {
print("🔔 Image migration completed with \(updateCount) updates - reloading data")
self?.loadProblems()
}
}
}
/// Handle Live Activity being dismissed by user /// Handle Live Activity being dismissed by user
private func handleLiveActivityDismissed() async { private func handleLiveActivityDismissed() async {
guard let activeSession = activeSession, guard let activeSession = activeSession,

View File

@@ -10,14 +10,10 @@ final class LiveActivityManager {
static let shared = LiveActivityManager() static let shared = LiveActivityManager()
private init() {} private init() {}
private var currentActivity: Activity<SessionActivityAttributes>? nonisolated(unsafe) private var currentActivity: Activity<SessionActivityAttributes>?
private var healthCheckTimer: Timer? private var healthCheckTimer: Timer?
private var lastHealthCheck: Date = Date() private var lastHealthCheck: Date = Date()
deinit {
healthCheckTimer?.invalidate()
}
/// Check if there's an active session and restart Live Activity if needed /// Check if there's an active session and restart Live Activity if needed
func restartLiveActivityIfNeeded(activeSession: ClimbSession?, gymName: String?) async { func restartLiveActivityIfNeeded(activeSession: ClimbSession?, gymName: String?) async {
// If we have an active session but no Live Activity, restart it // If we have an active session but no Live Activity, restart it
@@ -130,7 +126,8 @@ final class LiveActivityManager {
completedProblems: completedProblems completedProblems: completedProblems
) )
await currentActivity.update(.init(state: updatedContentState, staleDate: nil)) nonisolated(unsafe) let activity = currentActivity
await activity.update(.init(state: updatedContentState, staleDate: nil))
} }
/// Call this when a ClimbSession ends to end the Live Activity /// Call this when a ClimbSession ends to end the Live Activity
@@ -141,7 +138,8 @@ final class LiveActivityManager {
// First end the tracked activity if it exists // First end the tracked activity if it exists
if let currentActivity { if let currentActivity {
print("Ending tracked Live Activity: \(currentActivity.id)") print("Ending tracked Live Activity: \(currentActivity.id)")
await currentActivity.end(nil, dismissalPolicy: .immediate) nonisolated(unsafe) let activity = currentActivity
await activity.end(nil, dismissalPolicy: .immediate)
self.currentActivity = nil self.currentActivity = nil
print("Tracked Live Activity ended successfully") print("Tracked Live Activity ended successfully")
} }
@@ -200,7 +198,7 @@ final class LiveActivityManager {
print("🩺 Starting Live Activity health checks") print("🩺 Starting Live Activity health checks")
healthCheckTimer = Timer.scheduledTimer(withTimeInterval: 30.0, repeats: true) { healthCheckTimer = Timer.scheduledTimer(withTimeInterval: 30.0, repeats: true) {
[weak self] _ in [weak self] _ in
Task { @MainActor in Task { @MainActor [weak self] in
await self?.performHealthCheck() await self?.performHealthCheck()
} }
} }
@@ -257,7 +255,7 @@ final class LiveActivityManager {
{ {
guard currentActivity != nil else { return } guard currentActivity != nil else { return }
Task { Task { @MainActor in
while currentActivity != nil { while currentActivity != nil {
let elapsed = Date().timeIntervalSince(session.startTime ?? session.date) let elapsed = Date().timeIntervalSince(session.startTime ?? session.date)
await updateLiveActivity( await updateLiveActivity(

View File

@@ -458,24 +458,36 @@ struct AddAttemptView: View {
let difficulty = DifficultyGrade( let difficulty = DifficultyGrade(
system: selectedDifficultySystem, grade: newProblemGrade) system: selectedDifficultySystem, grade: newProblemGrade)
// Save images and get paths
var imagePaths: [String] = []
for data in imageData {
if let relativePath = ImageManager.shared.saveImageData(data) {
imagePaths.append(relativePath)
}
}
let newProblem = Problem( let newProblem = Problem(
gymId: gym.id, gymId: gym.id,
name: newProblemName.isEmpty ? nil : newProblemName, name: newProblemName.isEmpty ? nil : newProblemName,
climbType: selectedClimbType, climbType: selectedClimbType,
difficulty: difficulty, difficulty: difficulty,
imagePaths: imagePaths imagePaths: []
) )
dataManager.addProblem(newProblem) dataManager.addProblem(newProblem)
if !imageData.isEmpty {
var imagePaths: [String] = []
for (index, data) in imageData.enumerated() {
let deterministicName = ImageNamingUtils.generateImageFilename(
problemId: newProblem.id.uuidString, imageIndex: index)
if let relativePath = ImageManager.shared.saveImageData(
data, withName: deterministicName)
{
imagePaths.append(relativePath)
}
}
if !imagePaths.isEmpty {
let updatedProblem = newProblem.updated(imagePaths: imagePaths)
dataManager.updateProblem(updatedProblem)
}
}
let attempt = Attempt( let attempt = Attempt(
sessionId: session.id, sessionId: session.id,
problemId: newProblem.id, problemId: newProblem.id,
@@ -1218,24 +1230,36 @@ struct EditAttemptView: View {
let difficulty = DifficultyGrade( let difficulty = DifficultyGrade(
system: selectedDifficultySystem, grade: newProblemGrade) system: selectedDifficultySystem, grade: newProblemGrade)
// Save images and get paths
var imagePaths: [String] = []
for data in imageData {
if let relativePath = ImageManager.shared.saveImageData(data) {
imagePaths.append(relativePath)
}
}
let newProblem = Problem( let newProblem = Problem(
gymId: gym.id, gymId: gym.id,
name: newProblemName.isEmpty ? nil : newProblemName, name: newProblemName.isEmpty ? nil : newProblemName,
climbType: selectedClimbType, climbType: selectedClimbType,
difficulty: difficulty, difficulty: difficulty,
imagePaths: imagePaths imagePaths: []
) )
dataManager.addProblem(newProblem) dataManager.addProblem(newProblem)
if !imageData.isEmpty {
var imagePaths: [String] = []
for (index, data) in imageData.enumerated() {
let deterministicName = ImageNamingUtils.generateImageFilename(
problemId: newProblem.id.uuidString, imageIndex: index)
if let relativePath = ImageManager.shared.saveImageData(
data, withName: deterministicName)
{
imagePaths.append(relativePath)
}
}
if !imagePaths.isEmpty {
let updatedProblem = newProblem.updated(imagePaths: imagePaths)
dataManager.updateProblem(updatedProblem)
}
}
let updatedAttempt = attempt.updated( let updatedAttempt = attempt.updated(
problemId: newProblem.id, problemId: newProblem.id,
result: selectedResult, result: selectedResult,
@@ -1329,16 +1353,18 @@ struct ProblemSelectionImageView: View {
return return
} }
DispatchQueue.global(qos: .userInitiated).async { Task {
if let data = ImageManager.shared.loadImageData(fromPath: imagePath), let data = await MainActor.run {
let image = UIImage(data: data) ImageManager.shared.loadImageData(fromPath: imagePath)
{ }
DispatchQueue.main.async {
if let data = data, let image = UIImage(data: data) {
await MainActor.run {
self.uiImage = image self.uiImage = image
self.isLoading = false self.isLoading = false
} }
} else { } else {
DispatchQueue.main.async { await MainActor.run {
self.hasFailed = true self.hasFailed = true
self.isLoading = false self.isLoading = false
} }

View File

@@ -556,21 +556,25 @@ struct AddEditProblemView: View {
let difficulty = DifficultyGrade(system: selectedDifficultySystem, grade: difficultyGrade) let difficulty = DifficultyGrade(system: selectedDifficultySystem, grade: difficultyGrade)
// Save new image data and combine with existing paths if isEditing, let problem = existingProblem {
var allImagePaths = imagePaths var allImagePaths = imagePaths
// Only save NEW images (those beyond the existing imagePaths count) let newImagesStartIndex = imagePaths.count
let newImagesStartIndex = imagePaths.count if imageData.count > newImagesStartIndex {
if imageData.count > newImagesStartIndex { for i in newImagesStartIndex..<imageData.count {
for i in newImagesStartIndex..<imageData.count { let data = imageData[i]
let data = imageData[i] let imageIndex = allImagePaths.count
if let relativePath = ImageManager.shared.saveImageData(data) { let deterministicName = ImageNamingUtils.generateImageFilename(
allImagePaths.append(relativePath) problemId: problem.id.uuidString, imageIndex: imageIndex)
if let relativePath = ImageManager.shared.saveImageData(
data, withName: deterministicName)
{
allImagePaths.append(relativePath)
}
} }
} }
}
if isEditing, let problem = existingProblem {
let updatedProblem = problem.updated( let updatedProblem = problem.updated(
name: trimmedName.isEmpty ? nil : trimmedName, name: trimmedName.isEmpty ? nil : trimmedName,
description: trimmedDescription.isEmpty ? nil : trimmedDescription, description: trimmedDescription.isEmpty ? nil : trimmedDescription,
@@ -595,11 +599,32 @@ struct AddEditProblemView: View {
tags: trimmedTags, tags: trimmedTags,
location: trimmedLocation.isEmpty ? nil : trimmedLocation, location: trimmedLocation.isEmpty ? nil : trimmedLocation,
imagePaths: allImagePaths, imagePaths: [],
dateSet: dateSet, dateSet: dateSet,
notes: trimmedNotes.isEmpty ? nil : trimmedNotes notes: trimmedNotes.isEmpty ? nil : trimmedNotes
) )
dataManager.addProblem(newProblem) dataManager.addProblem(newProblem)
if !imageData.isEmpty {
var imagePaths: [String] = []
for (index, data) in imageData.enumerated() {
let deterministicName = ImageNamingUtils.generateImageFilename(
problemId: newProblem.id.uuidString, imageIndex: index)
if let relativePath = ImageManager.shared.saveImageData(
data, withName: deterministicName)
{
imagePaths.append(relativePath)
}
}
if !imagePaths.isEmpty {
let updatedProblem = newProblem.updated(imagePaths: imagePaths)
dataManager.updateProblem(updatedProblem)
}
}
} }
dismiss() dismiss()

View File

@@ -486,16 +486,18 @@ struct ProblemDetailImageView: View {
return return
} }
DispatchQueue.global(qos: .userInitiated).async { Task {
if let data = ImageManager.shared.loadImageData(fromPath: imagePath), let data = await MainActor.run {
let image = UIImage(data: data) ImageManager.shared.loadImageData(fromPath: imagePath)
{ }
DispatchQueue.main.async {
if let data = data, let image = UIImage(data: data) {
await MainActor.run {
self.uiImage = image self.uiImage = image
self.isLoading = false self.isLoading = false
} }
} else { } else {
DispatchQueue.main.async { await MainActor.run {
self.hasFailed = true self.hasFailed = true
self.isLoading = false self.isLoading = false
} }
@@ -550,16 +552,18 @@ struct ProblemDetailImageFullView: View {
return return
} }
DispatchQueue.global(qos: .userInitiated).async { Task {
if let data = ImageManager.shared.loadImageData(fromPath: imagePath), let data = await MainActor.run {
let image = UIImage(data: data) ImageManager.shared.loadImageData(fromPath: imagePath)
{ }
DispatchQueue.main.async {
if let data = data, let image = UIImage(data: data) {
await MainActor.run {
self.uiImage = image self.uiImage = image
self.isLoading = false self.isLoading = false
} }
} else { } else {
DispatchQueue.main.async { await MainActor.run {
self.hasFailed = true self.hasFailed = true
self.isLoading = false self.isLoading = false
} }

View File

@@ -32,9 +32,19 @@ struct ProblemsView: View {
filtered = filtered.filter { $0.gymId == gym.id } filtered = filtered.filter { $0.gymId == gym.id }
} }
// Separate active and inactive problems // Separate active and inactive problems with stable sorting
let active = filtered.filter { $0.isActive }.sorted { $0.updatedAt > $1.updatedAt } let active = filtered.filter { $0.isActive }.sorted {
let inactive = filtered.filter { !$0.isActive }.sorted { $0.updatedAt > $1.updatedAt } if $0.updatedAt == $1.updatedAt {
return $0.id.uuidString < $1.id.uuidString // Stable fallback
}
return $0.updatedAt > $1.updatedAt
}
let inactive = filtered.filter { !$0.isActive }.sorted {
if $0.updatedAt == $1.updatedAt {
return $0.id.uuidString < $1.id.uuidString // Stable fallback
}
return $0.updatedAt > $1.updatedAt
}
return active + inactive return active + inactive
} }
@@ -261,7 +271,7 @@ struct ProblemsList: View {
@State private var problemToEdit: Problem? @State private var problemToEdit: Problem?
var body: some View { var body: some View {
List(problems) { problem in List(problems, id: \.id) { problem in
NavigationLink(destination: ProblemDetailView(problemId: problem.id)) { NavigationLink(destination: ProblemDetailView(problemId: problem.id)) {
ProblemRow(problem: problem) ProblemRow(problem: problem)
} }
@@ -273,8 +283,12 @@ struct ProblemsList: View {
} }
Button { Button {
let updatedProblem = problem.updated(isActive: !problem.isActive) // Use a spring animation for more natural movement
dataManager.updateProblem(updatedProblem) withAnimation(.spring(response: 0.5, dampingFraction: 0.8, blendDuration: 0.1))
{
let updatedProblem = problem.updated(isActive: !problem.isActive)
dataManager.updateProblem(updatedProblem)
}
} label: { } label: {
Label( Label(
problem.isActive ? "Mark as Reset" : "Mark as Active", problem.isActive ? "Mark as Reset" : "Mark as Active",
@@ -293,6 +307,14 @@ struct ProblemsList: View {
.tint(.blue) .tint(.blue)
} }
} }
.animation(
.spring(response: 0.5, dampingFraction: 0.8, blendDuration: 0.1),
value: problems.map { "\($0.id):\($0.isActive)" }.joined()
)
.listStyle(.plain)
.scrollContentBackground(.hidden)
.scrollIndicators(.hidden)
.clipped()
.alert("Delete Problem", isPresented: .constant(problemToDelete != nil)) { .alert("Delete Problem", isPresented: .constant(problemToDelete != nil)) {
Button("Cancel", role: .cancel) { Button("Cancel", role: .cancel) {
problemToDelete = nil problemToDelete = nil
@@ -462,7 +484,7 @@ struct ProblemImageView: View {
@State private var isLoading = true @State private var isLoading = true
@State private var hasFailed = false @State private var hasFailed = false
private static var imageCache: NSCache<NSString, UIImage> = { private static let imageCache: NSCache<NSString, UIImage> = {
let cache = NSCache<NSString, UIImage>() let cache = NSCache<NSString, UIImage>()
cache.countLimit = 100 cache.countLimit = 100
cache.totalCostLimit = 50 * 1024 * 1024 // 50MB cache.totalCostLimit = 50 * 1024 * 1024 // 50MB
@@ -509,31 +531,28 @@ struct ProblemImageView: View {
return return
} }
let cacheKey = NSString(string: imagePath) // Load image asynchronously
Task { @MainActor in
let cacheKey = NSString(string: imagePath)
// Check cache first // Check cache first
if let cachedImage = Self.imageCache.object(forKey: cacheKey) { if let cachedImage = Self.imageCache.object(forKey: cacheKey) {
self.uiImage = cachedImage self.uiImage = cachedImage
self.isLoading = false self.isLoading = false
return return
} }
DispatchQueue.global(qos: .userInitiated).async { // Load image data
if let data = ImageManager.shared.loadImageData(fromPath: imagePath), if let data = ImageManager.shared.loadImageData(fromPath: imagePath),
let image = UIImage(data: data) let image = UIImage(data: data)
{ {
// Cache the image // Cache the image
Self.imageCache.setObject(image, forKey: cacheKey) Self.imageCache.setObject(image, forKey: cacheKey)
self.uiImage = image
DispatchQueue.main.async { self.isLoading = false
self.uiImage = image
self.isLoading = false
}
} else { } else {
DispatchQueue.main.async { self.hasFailed = true
self.hasFailed = true self.isLoading = false
self.isLoading = false
}
} }
} }
} }

View File

@@ -1,3 +1,4 @@
import HealthKit
import SwiftUI import SwiftUI
import UniformTypeIdentifiers import UniformTypeIdentifiers
@@ -16,6 +17,9 @@ struct SettingsView: View {
SyncSection() SyncSection()
.environmentObject(dataManager.syncService) .environmentObject(dataManager.syncService)
HealthKitSection()
.environmentObject(dataManager.healthKitService)
DataManagementSection( DataManagementSection(
activeSheet: $activeSheet activeSheet: $activeSheet
) )
@@ -76,6 +80,10 @@ struct DataManagementSection: View {
@Binding var activeSheet: SheetType? @Binding var activeSheet: SheetType?
@State private var showingResetAlert = false @State private var showingResetAlert = false
@State private var isExporting = false @State private var isExporting = false
@State private var isMigrating = false
@State private var showingMigrationAlert = false
@State private var isDeletingImages = false
@State private var showingDeleteImagesAlert = false
var body: some View { var body: some View {
Section("Data Management") { Section("Data Management") {
@@ -113,6 +121,48 @@ struct DataManagementSection: View {
} }
.foregroundColor(.primary) .foregroundColor(.primary)
// Migrate Image Names
Button(action: {
showingMigrationAlert = true
}) {
HStack {
if isMigrating {
ProgressView()
.scaleEffect(0.8)
Text("Migrating Images...")
.foregroundColor(.secondary)
} else {
Image(systemName: "photo.badge.arrow.down")
.foregroundColor(.orange)
Text("Fix Image Names")
}
Spacer()
}
}
.disabled(isMigrating)
.foregroundColor(.primary)
// Delete All Images
Button(action: {
showingDeleteImagesAlert = true
}) {
HStack {
if isDeletingImages {
ProgressView()
.scaleEffect(0.8)
Text("Deleting Images...")
.foregroundColor(.secondary)
} else {
Image(systemName: "trash")
.foregroundColor(.red)
Text("Delete All Images")
}
Spacer()
}
}
.disabled(isDeletingImages)
.foregroundColor(.red)
// Reset All Data // Reset All Data
Button(action: { Button(action: {
showingResetAlert = true showingResetAlert = true
@@ -136,6 +186,26 @@ struct DataManagementSection: View {
"Are you sure you want to reset all data? This will permanently delete:\n\n• All gyms and their information\n• All problems and their images\n• All climbing sessions\n• All attempts and progress data\n\nThis action cannot be undone. Consider exporting your data first." "Are you sure you want to reset all data? This will permanently delete:\n\n• All gyms and their information\n• All problems and their images\n• All climbing sessions\n• All attempts and progress data\n\nThis action cannot be undone. Consider exporting your data first."
) )
} }
.alert("Fix Image Names", isPresented: $showingMigrationAlert) {
Button("Cancel", role: .cancel) {}
Button("Fix Names") {
migrateImageNames()
}
} message: {
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."
)
}
.alert("Delete All Images", isPresented: $showingDeleteImagesAlert) {
Button("Cancel", role: .cancel) {}
Button("Delete", role: .destructive) {
deleteAllImages()
}
} message: {
Text(
"This will permanently delete ALL image files from your device.\n\nProblems will keep their references but the actual image files will be removed. This cannot be undone.\n\nConsider exporting your data first if you want to keep your images."
)
}
} }
private func exportDataAsync() { private func exportDataAsync() {
@@ -148,6 +218,75 @@ struct DataManagementSection: View {
} }
} }
} }
private func migrateImageNames() {
isMigrating = true
Task {
await MainActor.run {
ImageManager.shared.migrateImageNamesToDeterministic(dataManager: dataManager)
isMigrating = false
dataManager.successMessage = "Image names fixed successfully!"
}
}
}
private func deleteAllImages() {
isDeletingImages = true
Task {
await MainActor.run {
deleteAllImageFiles()
isDeletingImages = false
dataManager.successMessage = "All images deleted successfully!"
}
}
}
private func deleteAllImageFiles() {
let imageManager = ImageManager.shared
let fileManager = FileManager.default
// Delete all images from the images directory
let imagesDir = imageManager.imagesDirectory
do {
let imageFiles = try fileManager.contentsOfDirectory(
at: imagesDir, includingPropertiesForKeys: nil)
var deletedCount = 0
for imageFile in imageFiles {
do {
try fileManager.removeItem(at: imageFile)
deletedCount += 1
} catch {
print("Failed to delete image: \(imageFile.lastPathComponent)")
}
}
print("Deleted \(deletedCount) image files")
} catch {
print("Failed to access images directory: \(error)")
}
// Delete all images from backup directory
let backupDir = imageManager.backupDirectory
do {
let backupFiles = try fileManager.contentsOfDirectory(
at: backupDir, includingPropertiesForKeys: nil)
for backupFile in backupFiles {
try? fileManager.removeItem(at: backupFile)
}
} catch {
print("Failed to access backup directory: \(error)")
}
// Clear image paths from all problems
let updatedProblems = dataManager.problems.map { problem in
problem.updated(imagePaths: [])
}
for problem in updatedProblems {
dataManager.updateProblem(problem)
}
}
} }
struct AppInfoSection: View { struct AppInfoSection: View {
@@ -162,7 +301,7 @@ struct AppInfoSection: View {
var body: some View { var body: some View {
Section("App Information") { Section("App Information") {
HStack { HStack {
Image("MountainsIcon") Image("AppLogo")
.resizable() .resizable()
.frame(width: 24, height: 24) .frame(width: 24, height: 24)
VStack(alignment: .leading) { VStack(alignment: .leading) {
@@ -236,7 +375,7 @@ struct ExportDataView: View {
item: fileURL, item: fileURL,
preview: SharePreview( preview: SharePreview(
"OpenClimb Data Export", "OpenClimb Data Export",
image: Image("MountainsIcon")) image: Image("AppLogo"))
) { ) {
Label("Share Data", systemImage: "square.and.arrow.up") Label("Share Data", systemImage: "square.and.arrow.up")
.font(.headline) .font(.headline)
@@ -815,6 +954,86 @@ struct ImportDataView: View {
} }
} }
struct HealthKitSection: View {
@EnvironmentObject var healthKitService: HealthKitService
@State private var showingAuthorizationError = false
@State private var isRequestingAuthorization = false
var body: some View {
Section {
if !HKHealthStore.isHealthDataAvailable() {
HStack {
Image(systemName: "heart.slash")
.foregroundColor(.secondary)
Text("Apple Health not available")
.foregroundColor(.secondary)
}
} else {
Toggle(
isOn: Binding(
get: { healthKitService.isEnabled },
set: { newValue in
if newValue && !healthKitService.isAuthorized {
isRequestingAuthorization = true
Task {
do {
try await healthKitService.requestAuthorization()
await MainActor.run {
healthKitService.setEnabled(true)
isRequestingAuthorization = false
}
} catch {
await MainActor.run {
showingAuthorizationError = true
isRequestingAuthorization = false
}
}
}
} else if newValue {
healthKitService.setEnabled(true)
} else {
healthKitService.setEnabled(false)
}
}
)
) {
HStack {
Image(systemName: "heart.fill")
.foregroundColor(.red)
Text("Apple Health Integration")
}
}
.disabled(isRequestingAuthorization)
if healthKitService.isEnabled {
VStack(alignment: .leading, spacing: 4) {
Text(
"Climbing sessions will be recorded as workouts in Apple Health"
)
.font(.caption)
.foregroundColor(.secondary)
}
}
}
} header: {
Text("Health")
} footer: {
if healthKitService.isEnabled {
Text(
"Each climbing session will automatically be saved to Apple Health as a \"Climbing\" workout with the session duration."
)
}
}
.alert("Authorization Required", isPresented: $showingAuthorizationError) {
Button("OK", role: .cancel) {}
} message: {
Text(
"Please grant access to Apple Health in Settings to enable this feature."
)
}
}
}
#Preview { #Preview {
SettingsView() SettingsView()
.environmentObject(ClimbingDataManager.preview) .environmentObject(ClimbingDataManager.preview)

View File

@@ -326,4 +326,203 @@ final class OpenClimbTests: XCTestCase {
XCTAssertTrue(hasActiveSession, "Combined sessions should contain active session") XCTAssertTrue(hasActiveSession, "Combined sessions should contain active session")
XCTAssertTrue(hasCompletedSession, "Combined sessions should contain completed session") XCTAssertTrue(hasCompletedSession, "Combined sessions should contain completed session")
} }
// MARK: - Orphaned Data Cleanup Tests
func testOrphanedAttemptDetection() throws {
// Test that we can detect orphaned attempts (attempts referencing non-existent sessions)
let validSessionId = UUID()
let deletedSessionId = UUID()
let validProblemId = UUID()
// Simulate a list of valid sessions
let validSessions = [validSessionId]
// Simulate attempts - one valid, one orphaned
let validAttempt: [String: Any] = [
"id": UUID().uuidString,
"sessionId": validSessionId.uuidString,
"problemId": validProblemId.uuidString,
"result": "completed",
]
let orphanedAttempt: [String: Any] = [
"id": UUID().uuidString,
"sessionId": deletedSessionId.uuidString,
"problemId": validProblemId.uuidString,
"result": "completed",
]
let allAttempts = [validAttempt, orphanedAttempt]
// Filter to find orphaned attempts
let orphaned = allAttempts.filter { attempt in
guard let sessionIdString = attempt["sessionId"] as? String,
let sessionId = UUID(uuidString: sessionIdString)
else {
return false
}
return !validSessions.contains(sessionId)
}
XCTAssertEqual(orphaned.count, 1, "Should detect exactly one orphaned attempt")
XCTAssertEqual(orphaned[0]["sessionId"] as? String, deletedSessionId.uuidString)
}
func testOrphanedAttemptRemoval() throws {
// Test that orphaned attempts can be properly removed from a list
let validSessionId = UUID()
let deletedSessionId = UUID()
let problemId = UUID()
let validSessions = Set([validSessionId])
// Create test attempts
var attempts: [[String: Any]] = [
[
"id": UUID().uuidString,
"sessionId": validSessionId.uuidString,
"problemId": problemId.uuidString,
"result": "completed",
],
[
"id": UUID().uuidString,
"sessionId": deletedSessionId.uuidString,
"problemId": problemId.uuidString,
"result": "failed",
],
[
"id": UUID().uuidString,
"sessionId": validSessionId.uuidString,
"problemId": problemId.uuidString,
"result": "flash",
],
]
XCTAssertEqual(attempts.count, 3, "Should start with 3 attempts")
// Remove orphaned attempts
attempts.removeAll { attempt in
guard let sessionIdString = attempt["sessionId"] as? String,
let sessionId = UUID(uuidString: sessionIdString)
else {
return true
}
return !validSessions.contains(sessionId)
}
XCTAssertEqual(attempts.count, 2, "Should have 2 attempts after cleanup")
// Verify remaining attempts are all valid
for attempt in attempts {
if let sessionIdString = attempt["sessionId"] as? String,
let sessionId = UUID(uuidString: sessionIdString)
{
XCTAssertTrue(
validSessions.contains(sessionId),
"All remaining attempts should reference valid sessions")
}
}
}
func testCascadeDeleteSessionWithAttempts() throws {
// Test that deleting a session properly tracks all its attempts as deleted
let sessionId = UUID()
let problemId = UUID()
// Create attempts for this session
let sessionAttempts: [[String: Any]] = [
[
"id": UUID().uuidString, "sessionId": sessionId.uuidString,
"problemId": problemId.uuidString,
],
[
"id": UUID().uuidString, "sessionId": sessionId.uuidString,
"problemId": problemId.uuidString,
],
[
"id": UUID().uuidString, "sessionId": sessionId.uuidString,
"problemId": problemId.uuidString,
],
]
XCTAssertEqual(sessionAttempts.count, 3, "Session should have 3 attempts")
// Simulate tracking deletions
var deletedItems: [String] = []
// Add session to deleted items
deletedItems.append(sessionId.uuidString)
// Add all attempts to deleted items
for attempt in sessionAttempts {
if let attemptId = attempt["id"] as? String {
deletedItems.append(attemptId)
}
}
XCTAssertEqual(deletedItems.count, 4, "Should track 1 session + 3 attempts as deleted")
XCTAssertTrue(deletedItems.contains(sessionId.uuidString), "Should track session deletion")
// Verify all attempt IDs are tracked
let attemptIds = sessionAttempts.compactMap { $0["id"] as? String }
for attemptId in attemptIds {
XCTAssertTrue(
deletedItems.contains(attemptId), "Should track attempt \(attemptId) deletion")
}
}
func testDataIntegrityValidation() throws {
// Test data integrity validation logic
let gymId = UUID()
let sessionId = UUID()
let problemId = UUID()
// Valid data setup
let gyms = [gymId]
let sessions = [(id: sessionId, gymId: gymId)]
let problems = [(id: problemId, gymId: gymId)]
let attempts = [
(id: UUID(), sessionId: sessionId, problemId: problemId),
(id: UUID(), sessionId: sessionId, problemId: problemId),
]
// Validate that all relationships are correct
let validGyms = Set(gyms)
let validSessions = Set(sessions.map { $0.id })
let validProblems = Set(problems.map { $0.id })
// Check sessions reference valid gyms
for session in sessions {
XCTAssertTrue(validGyms.contains(session.gymId), "Session should reference valid gym")
}
// Check problems reference valid gyms
for problem in problems {
XCTAssertTrue(validGyms.contains(problem.gymId), "Problem should reference valid gym")
}
// Check attempts reference valid sessions and problems
for attempt in attempts {
XCTAssertTrue(
validSessions.contains(attempt.sessionId), "Attempt should reference valid session")
XCTAssertTrue(
validProblems.contains(attempt.problemId), "Attempt should reference valid problem")
}
// Test integrity check passes
let hasOrphanedSessions = sessions.contains { !validGyms.contains($0.gymId) }
let hasOrphanedProblems = problems.contains { !validGyms.contains($0.gymId) }
let hasOrphanedAttempts = attempts.contains {
!validSessions.contains($0.sessionId) || !validProblems.contains($0.problemId)
}
XCTAssertFalse(hasOrphanedSessions, "Should not have orphaned sessions")
XCTAssertFalse(hasOrphanedProblems, "Should not have orphaned problems")
XCTAssertFalse(hasOrphanedAttempts, "Should not have orphaned attempts")
}
} }

View File

@@ -5,8 +5,8 @@ import ActivityKit
import SwiftUI import SwiftUI
import WidgetKit import WidgetKit
struct SessionActivityAttributes: ActivityAttributes { struct SessionActivityAttributes: ActivityAttributes, Sendable {
public struct ContentState: Codable, Hashable { public struct ContentState: Codable, Hashable, Sendable {
var elapsed: TimeInterval var elapsed: TimeInterval
var totalAttempts: Int var totalAttempts: Int
var completedProblems: Int var completedProblems: Int