Compare commits
2 Commits
ANDROID_1.
...
ANDROID_1.
| Author | SHA1 | Date | |
|---|---|---|---|
|
30d2b3938e
|
|||
|
405fb06d5d
|
22
android/README.md
Normal file
22
android/README.md
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# OpenClimb for Android
|
||||||
|
|
||||||
|
This is the native Android app for OpenClimb, built with Kotlin and Jetpack Compose.
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
This is a standard Android Gradle project. The main code lives in `app/src/main/java/com/atridad/openclimb/`.
|
||||||
|
|
||||||
|
- `data/`: Handles all the app's data.
|
||||||
|
- `database/`: Room database setup (DAOs, entities).
|
||||||
|
- `model/`: Core data models (`Problem`, `Gym`, `ClimbSession`).
|
||||||
|
- `repository/`: Manages the data, providing a clean API for the rest of the app.
|
||||||
|
- `sync/`: Handles talking to the sync server.
|
||||||
|
- `ui/`: All the Jetpack Compose UI code.
|
||||||
|
- `screens/`: The main screens of the app.
|
||||||
|
- `components/`: Reusable UI bits used across screens.
|
||||||
|
- `viewmodel/`: `ClimbViewModel` for managing UI state.
|
||||||
|
- `navigation/`: Navigation graph and routes using Jetpack Navigation.
|
||||||
|
- `service/`: Background service for tracking climbing sessions.
|
||||||
|
- `utils/`: Helpers for things like date formatting and image handling.
|
||||||
|
|
||||||
|
The app is built to be offline-first. All data is stored locally on your device and works without an internet connection.
|
||||||
@@ -16,8 +16,8 @@ android {
|
|||||||
applicationId = "com.atridad.openclimb"
|
applicationId = "com.atridad.openclimb"
|
||||||
minSdk = 31
|
minSdk = 31
|
||||||
targetSdk = 36
|
targetSdk = 36
|
||||||
versionCode = 37
|
versionCode = 39
|
||||||
versionName = "1.9.0"
|
versionName = "1.9.2"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
<!-- Permission for sync functionality -->
|
<!-- Permission for sync functionality -->
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
|
||||||
<!-- Health Connect permissions -->
|
<!-- Health Connect permissions -->
|
||||||
<uses-permission android:name="android.permission.health.READ_EXERCISE" />
|
<uses-permission android:name="android.permission.health.READ_EXERCISE" />
|
||||||
|
|||||||
@@ -1,205 +0,0 @@
|
|||||||
package com.atridad.openclimb.data.migration
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.util.Log
|
|
||||||
import com.atridad.openclimb.data.repository.ClimbRepository
|
|
||||||
import com.atridad.openclimb.utils.ImageNamingUtils
|
|
||||||
import com.atridad.openclimb.utils.ImageUtils
|
|
||||||
import kotlinx.coroutines.flow.first
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Service responsible for migrating images to use consistent naming convention across platforms.
|
|
||||||
* This ensures that iOS and Android use the same image filenames for sync compatibility.
|
|
||||||
*/
|
|
||||||
class ImageMigrationService(private val context: Context, private val repository: ClimbRepository) {
|
|
||||||
companion object {
|
|
||||||
private const val TAG = "ImageMigrationService"
|
|
||||||
private const val MIGRATION_PREF_KEY = "image_naming_migration_completed"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs a complete migration of all images in the system to use consistent naming. This
|
|
||||||
* should be called once during app startup after the naming convention is implemented.
|
|
||||||
*/
|
|
||||||
suspend fun performFullMigration(): ImageMigrationResult {
|
|
||||||
Log.i(TAG, "Starting full image naming migration")
|
|
||||||
|
|
||||||
val prefs = context.getSharedPreferences("openclimb_migration", Context.MODE_PRIVATE)
|
|
||||||
if (prefs.getBoolean(MIGRATION_PREF_KEY, false)) {
|
|
||||||
Log.i(TAG, "Image migration already completed, skipping")
|
|
||||||
return ImageMigrationResult.AlreadyCompleted
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
val allProblems = repository.getAllProblems().first()
|
|
||||||
val migrationResults = mutableMapOf<String, String>()
|
|
||||||
var migratedCount = 0
|
|
||||||
var errorCount = 0
|
|
||||||
|
|
||||||
Log.i(TAG, "Found ${allProblems.size} problems to check for image migration")
|
|
||||||
|
|
||||||
for (problem in allProblems) {
|
|
||||||
if (problem.imagePaths.isNotEmpty()) {
|
|
||||||
Log.d(
|
|
||||||
TAG,
|
|
||||||
"Migrating images for problem '${problem.name}': ${problem.imagePaths}"
|
|
||||||
)
|
|
||||||
|
|
||||||
try {
|
|
||||||
val problemMigrations =
|
|
||||||
ImageUtils.migrateImageNaming(
|
|
||||||
context = context,
|
|
||||||
problemId = problem.id,
|
|
||||||
currentImagePaths = problem.imagePaths
|
|
||||||
)
|
|
||||||
|
|
||||||
if (problemMigrations.isNotEmpty()) {
|
|
||||||
migrationResults.putAll(problemMigrations)
|
|
||||||
migratedCount += problemMigrations.size
|
|
||||||
|
|
||||||
// Update image paths
|
|
||||||
val newImagePaths =
|
|
||||||
problem.imagePaths.map { oldPath ->
|
|
||||||
problemMigrations[oldPath] ?: oldPath
|
|
||||||
}
|
|
||||||
|
|
||||||
val updatedProblem = problem.copy(imagePaths = newImagePaths)
|
|
||||||
repository.insertProblem(updatedProblem)
|
|
||||||
|
|
||||||
Log.d(
|
|
||||||
TAG,
|
|
||||||
"Updated problem '${problem.name}' with ${problemMigrations.size} migrated images"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(
|
|
||||||
TAG,
|
|
||||||
"Failed to migrate images for problem '${problem.name}': ${e.message}",
|
|
||||||
e
|
|
||||||
)
|
|
||||||
errorCount++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark migration as completed
|
|
||||||
prefs.edit().putBoolean(MIGRATION_PREF_KEY, true).apply()
|
|
||||||
|
|
||||||
Log.i(
|
|
||||||
TAG,
|
|
||||||
"Image migration completed: $migratedCount images migrated, $errorCount errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
return ImageMigrationResult.Success(
|
|
||||||
totalMigrated = migratedCount,
|
|
||||||
errors = errorCount,
|
|
||||||
migrations = migrationResults
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG, "Image migration failed: ${e.message}", e)
|
|
||||||
return ImageMigrationResult.Failed(e.message ?: "Unknown error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Validates that all images in the system follow the consistent naming convention. */
|
|
||||||
suspend fun validateImageNaming(): ValidationResult {
|
|
||||||
try {
|
|
||||||
val allProblems = repository.getAllProblems().first()
|
|
||||||
val validImages = mutableListOf<String>()
|
|
||||||
val invalidImages = mutableListOf<String>()
|
|
||||||
val missingImages = mutableListOf<String>()
|
|
||||||
|
|
||||||
for (problem in allProblems) {
|
|
||||||
for (imagePath in problem.imagePaths) {
|
|
||||||
val filename = imagePath.substringAfterLast('/')
|
|
||||||
|
|
||||||
// Check if file exists
|
|
||||||
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
|
||||||
if (!imageFile.exists()) {
|
|
||||||
missingImages.add(imagePath)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if filename follows convention
|
|
||||||
if (ImageNamingUtils.isValidImageFilename(filename)) {
|
|
||||||
validImages.add(imagePath)
|
|
||||||
} else {
|
|
||||||
invalidImages.add(imagePath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ValidationResult(
|
|
||||||
totalImages = validImages.size + invalidImages.size + missingImages.size,
|
|
||||||
validImages = validImages,
|
|
||||||
invalidImages = invalidImages,
|
|
||||||
missingImages = missingImages
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG, "Image validation failed: ${e.message}", e)
|
|
||||||
return ValidationResult(
|
|
||||||
totalImages = 0,
|
|
||||||
validImages = emptyList(),
|
|
||||||
invalidImages = emptyList(),
|
|
||||||
missingImages = emptyList()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Migrates images for a specific problem during sync operations. */
|
|
||||||
suspend fun migrateProblemImages(
|
|
||||||
problemId: String,
|
|
||||||
currentImagePaths: List<String>
|
|
||||||
): Map<String, String> {
|
|
||||||
return try {
|
|
||||||
ImageUtils.migrateImageNaming(context, problemId, currentImagePaths)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG, "Failed to migrate images for problem $problemId: ${e.message}", e)
|
|
||||||
emptyMap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cleans up any orphaned image files that don't follow our naming convention and aren't
|
|
||||||
* referenced by any problems.
|
|
||||||
*/
|
|
||||||
suspend fun cleanupOrphanedImages() {
|
|
||||||
try {
|
|
||||||
val allProblems = repository.getAllProblems().first()
|
|
||||||
val referencedPaths = allProblems.flatMap { it.imagePaths }.toSet()
|
|
||||||
|
|
||||||
ImageUtils.cleanupOrphanedImages(context, referencedPaths)
|
|
||||||
|
|
||||||
Log.i(TAG, "Orphaned image cleanup completed")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG, "Failed to cleanup orphaned images: ${e.message}", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Result of an image migration operation */
|
|
||||||
sealed class ImageMigrationResult {
|
|
||||||
object AlreadyCompleted : ImageMigrationResult()
|
|
||||||
|
|
||||||
data class Success(
|
|
||||||
val totalMigrated: Int,
|
|
||||||
val errors: Int,
|
|
||||||
val migrations: Map<String, String>
|
|
||||||
) : ImageMigrationResult()
|
|
||||||
|
|
||||||
data class Failed(val error: String) : ImageMigrationResult()
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Result of image naming validation */
|
|
||||||
data class ValidationResult(
|
|
||||||
val totalImages: Int,
|
|
||||||
val validImages: List<String>,
|
|
||||||
val invalidImages: List<String>,
|
|
||||||
val missingImages: List<String>
|
|
||||||
) {
|
|
||||||
val isAllValid: Boolean
|
|
||||||
get() = invalidImages.isEmpty() && missingImages.isEmpty()
|
|
||||||
|
|
||||||
val validPercentage: Double
|
|
||||||
get() = if (totalImages == 0) 100.0 else (validImages.size.toDouble() / totalImages) * 100
|
|
||||||
}
|
|
||||||
@@ -75,25 +75,4 @@ data class Attempt(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updated(
|
|
||||||
result: AttemptResult? = null,
|
|
||||||
highestHold: String? = null,
|
|
||||||
notes: String? = null,
|
|
||||||
duration: Long? = null,
|
|
||||||
restTime: Long? = null
|
|
||||||
): Attempt {
|
|
||||||
return Attempt(
|
|
||||||
id = this.id,
|
|
||||||
sessionId = this.sessionId,
|
|
||||||
problemId = this.problemId,
|
|
||||||
result = result ?: this.result,
|
|
||||||
highestHold = highestHold ?: this.highestHold,
|
|
||||||
notes = notes ?: this.notes,
|
|
||||||
duration = duration ?: this.duration,
|
|
||||||
restTime = restTime ?: this.restTime,
|
|
||||||
timestamp = this.timestamp,
|
|
||||||
createdAt = this.createdAt,
|
|
||||||
updatedAt = DateFormatUtils.nowISO8601()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -207,7 +207,7 @@ data class DifficultyGrade(val system: DifficultySystem, val grade: String, val
|
|||||||
private fun compareVScaleGrades(grade1: String, grade2: String): Int {
|
private fun compareVScaleGrades(grade1: String, grade2: String): Int {
|
||||||
if (grade1 == "VB" && grade2 != "VB") return -1
|
if (grade1 == "VB" && grade2 != "VB") return -1
|
||||||
if (grade2 == "VB" && grade1 != "VB") return 1
|
if (grade2 == "VB" && grade1 != "VB") return 1
|
||||||
if (grade1 == "VB" && grade2 == "VB") return 0
|
if (grade1 == "VB") return 0
|
||||||
|
|
||||||
val num1 = grade1.removePrefix("V").toIntOrNull() ?: 0
|
val num1 = grade1.removePrefix("V").toIntOrNull() ?: 0
|
||||||
val num2 = grade2.removePrefix("V").toIntOrNull() ?: 0
|
val num2 = grade2.removePrefix("V").toIntOrNull() ?: 0
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ import com.atridad.openclimb.utils.ZipExportImportUtils
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.serialization.decodeFromString
|
|
||||||
import kotlinx.serialization.encodeToString
|
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
class ClimbRepository(database: OpenClimbDatabase, private val context: Context) {
|
class ClimbRepository(database: OpenClimbDatabase, private val context: Context) {
|
||||||
@@ -288,7 +286,7 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
|||||||
try {
|
try {
|
||||||
val deletion = json.decodeFromString<DeletedItem>(value)
|
val deletion = json.decodeFromString<DeletedItem>(value)
|
||||||
deletions.add(deletion)
|
deletions.add(deletion)
|
||||||
} catch (e: Exception) {
|
} catch (_: Exception) {
|
||||||
// Invalid deletion record, ignore
|
// Invalid deletion record, ignore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import android.content.Context
|
|||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.atridad.openclimb.utils.DateFormatUtils
|
import com.atridad.openclimb.utils.DateFormatUtils
|
||||||
|
import androidx.core.content.edit
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages the overall data state timestamp for sync purposes. This tracks when any data in the
|
* Manages the overall data state timestamp for sync purposes. This tracks when any data in the
|
||||||
@@ -35,7 +36,7 @@ class DataStateManager(context: Context) {
|
|||||||
*/
|
*/
|
||||||
fun updateDataState() {
|
fun updateDataState() {
|
||||||
val now = DateFormatUtils.nowISO8601()
|
val now = DateFormatUtils.nowISO8601()
|
||||||
prefs.edit().putString(KEY_LAST_MODIFIED, now).apply()
|
prefs.edit { putString(KEY_LAST_MODIFIED, now) }
|
||||||
Log.d(TAG, "Data state updated to: $now")
|
Log.d(TAG, "Data state updated to: $now")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,21 +49,6 @@ class DataStateManager(context: Context) {
|
|||||||
?: DateFormatUtils.nowISO8601()
|
?: DateFormatUtils.nowISO8601()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the data state timestamp to a specific value. Used when importing data from server to
|
|
||||||
* sync the state.
|
|
||||||
*/
|
|
||||||
fun setLastModified(timestamp: String) {
|
|
||||||
prefs.edit().putString(KEY_LAST_MODIFIED, timestamp).apply()
|
|
||||||
Log.d(TAG, "Data state set to: $timestamp")
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Resets the data state (for testing or complete data wipe). */
|
|
||||||
fun reset() {
|
|
||||||
prefs.edit().clear().apply()
|
|
||||||
Log.d(TAG, "Data state reset")
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Checks if the data state has been initialized. */
|
/** Checks if the data state has been initialized. */
|
||||||
private fun isInitialized(): Boolean {
|
private fun isInitialized(): Boolean {
|
||||||
return prefs.getBoolean(KEY_INITIALIZED, false)
|
return prefs.getBoolean(KEY_INITIALIZED, false)
|
||||||
@@ -70,11 +56,7 @@ class DataStateManager(context: Context) {
|
|||||||
|
|
||||||
/** Marks the data state as initialized. */
|
/** Marks the data state as initialized. */
|
||||||
private fun markAsInitialized() {
|
private fun markAsInitialized() {
|
||||||
prefs.edit().putBoolean(KEY_INITIALIZED, true).apply()
|
prefs.edit { putBoolean(KEY_INITIALIZED, true) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets debug information about the current state. */
|
|
||||||
fun getDebugInfo(): String {
|
|
||||||
return "DataState(lastModified=${getLastModified()}, initialized=${isInitialized()})"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -89,10 +89,6 @@ class SessionTrackingService : Service() {
|
|||||||
return START_REDELIVER_INTENT
|
return START_REDELIVER_INTENT
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
|
||||||
super.onTaskRemoved(rootIntent)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onBind(intent: Intent?): IBinder? = null
|
override fun onBind(intent: Intent?): IBinder? = null
|
||||||
|
|
||||||
private fun startSessionTracking(sessionId: String) {
|
private fun startSessionTracking(sessionId: String) {
|
||||||
@@ -153,7 +149,7 @@ class SessionTrackingService : Service() {
|
|||||||
return try {
|
return try {
|
||||||
val activeNotifications = notificationManager.activeNotifications
|
val activeNotifications = notificationManager.activeNotifications
|
||||||
activeNotifications.any { it.id == NOTIFICATION_ID }
|
activeNotifications.any { it.id == NOTIFICATION_ID }
|
||||||
} catch (e: Exception) {
|
} catch (_: Exception) {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.atridad.openclimb.ui.components
|
|||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.gestures.detectTransformGestures
|
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.foundation.lazy.LazyRow
|
import androidx.compose.foundation.lazy.LazyRow
|
||||||
import androidx.compose.foundation.lazy.itemsIndexed
|
import androidx.compose.foundation.lazy.itemsIndexed
|
||||||
@@ -20,88 +19,60 @@ import androidx.compose.ui.Alignment
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.graphicsLayer
|
|
||||||
import androidx.compose.ui.input.pointer.pointerInput
|
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.window.Dialog
|
import androidx.compose.ui.window.Dialog
|
||||||
import androidx.compose.ui.window.DialogProperties
|
import androidx.compose.ui.window.DialogProperties
|
||||||
import coil.compose.AsyncImage
|
|
||||||
import com.atridad.openclimb.utils.ImageUtils
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@OptIn(ExperimentalFoundationApi::class)
|
@OptIn(ExperimentalFoundationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun FullscreenImageViewer(
|
fun FullscreenImageViewer(imagePaths: List<String>, initialIndex: Int = 0, onDismiss: () -> Unit) {
|
||||||
imagePaths: List<String>,
|
|
||||||
initialIndex: Int = 0,
|
|
||||||
onDismiss: () -> Unit
|
|
||||||
) {
|
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val pagerState = rememberPagerState(
|
val pagerState = rememberPagerState(initialPage = initialIndex, pageCount = { imagePaths.size })
|
||||||
initialPage = initialIndex,
|
|
||||||
pageCount = { imagePaths.size }
|
|
||||||
)
|
|
||||||
val thumbnailListState = rememberLazyListState()
|
val thumbnailListState = rememberLazyListState()
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
|
|
||||||
// Auto-scroll thumbnail list to center current image
|
// Auto-scroll thumbnail list to center current image
|
||||||
LaunchedEffect(pagerState.currentPage) {
|
LaunchedEffect(pagerState.currentPage) {
|
||||||
thumbnailListState.animateScrollToItem(
|
thumbnailListState.animateScrollToItem(index = pagerState.currentPage, scrollOffset = -200)
|
||||||
index = pagerState.currentPage,
|
|
||||||
scrollOffset = -200
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Dialog(
|
Dialog(
|
||||||
onDismissRequest = onDismiss,
|
onDismissRequest = onDismiss,
|
||||||
properties = DialogProperties(
|
properties =
|
||||||
|
DialogProperties(
|
||||||
usePlatformDefaultWidth = false,
|
usePlatformDefaultWidth = false,
|
||||||
decorFitsSystemWindows = false
|
decorFitsSystemWindows = false
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
Box(
|
Box(modifier = Modifier.fillMaxSize().background(Color.Black)) {
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.background(Color.Black)
|
|
||||||
) {
|
|
||||||
// Main image pager
|
// Main image pager
|
||||||
HorizontalPager(
|
HorizontalPager(state = pagerState, modifier = Modifier.fillMaxSize()) { page ->
|
||||||
state = pagerState,
|
OrientationAwareImage(
|
||||||
modifier = Modifier.fillMaxSize()
|
|
||||||
) { page ->
|
|
||||||
ZoomableImage(
|
|
||||||
imagePath = imagePaths[page],
|
imagePath = imagePaths[page],
|
||||||
modifier = Modifier.fillMaxSize()
|
contentDescription = "Full screen image",
|
||||||
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
contentScale = ContentScale.Fit
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close button
|
// Close button
|
||||||
IconButton(
|
IconButton(
|
||||||
onClick = onDismiss,
|
onClick = onDismiss,
|
||||||
modifier = Modifier
|
modifier =
|
||||||
.align(Alignment.TopEnd)
|
Modifier.align(Alignment.TopEnd)
|
||||||
.padding(16.dp)
|
.padding(16.dp)
|
||||||
.background(
|
.background(Color.Black.copy(alpha = 0.5f), CircleShape)
|
||||||
Color.Black.copy(alpha = 0.5f),
|
) { Icon(Icons.Default.Close, contentDescription = "Close", tint = Color.White) }
|
||||||
CircleShape
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
Icons.Default.Close,
|
|
||||||
contentDescription = "Close",
|
|
||||||
tint = Color.White
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Image counter
|
// Image counter
|
||||||
if (imagePaths.size > 1) {
|
if (imagePaths.size > 1) {
|
||||||
Card(
|
Card(
|
||||||
modifier = Modifier
|
modifier = Modifier.align(Alignment.TopCenter).padding(16.dp),
|
||||||
.align(Alignment.TopCenter)
|
colors =
|
||||||
.padding(16.dp),
|
CardDefaults.cardColors(
|
||||||
colors = CardDefaults.cardColors(
|
|
||||||
containerColor = Color.Black.copy(alpha = 0.7f)
|
containerColor = Color.Black.copy(alpha = 0.7f)
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
@@ -116,11 +87,12 @@ fun FullscreenImageViewer(
|
|||||||
// Thumbnail strip (if multiple images)
|
// Thumbnail strip (if multiple images)
|
||||||
if (imagePaths.size > 1) {
|
if (imagePaths.size > 1) {
|
||||||
Card(
|
Card(
|
||||||
modifier = Modifier
|
modifier =
|
||||||
.align(Alignment.BottomCenter)
|
Modifier.align(Alignment.BottomCenter)
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(16.dp),
|
.padding(16.dp),
|
||||||
colors = CardDefaults.cardColors(
|
colors =
|
||||||
|
CardDefaults.cardColors(
|
||||||
containerColor = Color.Black.copy(alpha = 0.7f)
|
containerColor = Color.Black.copy(alpha = 0.7f)
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
@@ -131,14 +103,13 @@ fun FullscreenImageViewer(
|
|||||||
contentPadding = PaddingValues(horizontal = 8.dp)
|
contentPadding = PaddingValues(horizontal = 8.dp)
|
||||||
) {
|
) {
|
||||||
itemsIndexed(imagePaths) { index, imagePath ->
|
itemsIndexed(imagePaths) { index, imagePath ->
|
||||||
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
|
||||||
val isSelected = index == pagerState.currentPage
|
val isSelected = index == pagerState.currentPage
|
||||||
|
|
||||||
AsyncImage(
|
OrientationAwareImage(
|
||||||
model = imageFile,
|
imagePath = imagePath,
|
||||||
contentDescription = "Thumbnail ${index + 1}",
|
contentDescription = "Thumbnail ${index + 1}",
|
||||||
modifier = Modifier
|
modifier =
|
||||||
.size(60.dp)
|
Modifier.size(60.dp)
|
||||||
.clip(RoundedCornerShape(8.dp))
|
.clip(RoundedCornerShape(8.dp))
|
||||||
.clickable {
|
.clickable {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
@@ -148,7 +119,9 @@ fun FullscreenImageViewer(
|
|||||||
.then(
|
.then(
|
||||||
if (isSelected) {
|
if (isSelected) {
|
||||||
Modifier.background(
|
Modifier.background(
|
||||||
Color.White.copy(alpha = 0.3f),
|
Color.White.copy(
|
||||||
|
alpha = 0.3f
|
||||||
|
),
|
||||||
RoundedCornerShape(8.dp)
|
RoundedCornerShape(8.dp)
|
||||||
)
|
)
|
||||||
} else Modifier
|
} else Modifier
|
||||||
@@ -162,48 +135,3 @@ fun FullscreenImageViewer(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
|
||||||
private fun ZoomableImage(
|
|
||||||
imagePath: String,
|
|
||||||
modifier: Modifier = Modifier
|
|
||||||
) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
|
||||||
|
|
||||||
var scale by remember { mutableFloatStateOf(1f) }
|
|
||||||
var offsetX by remember { mutableFloatStateOf(0f) }
|
|
||||||
var offsetY by remember { mutableFloatStateOf(0f) }
|
|
||||||
|
|
||||||
Box(
|
|
||||||
modifier = modifier
|
|
||||||
.pointerInput(Unit) {
|
|
||||||
detectTransformGestures(
|
|
||||||
onGesture = { _, pan, zoom, _ ->
|
|
||||||
scale = (scale * zoom).coerceIn(0.5f, 5f)
|
|
||||||
|
|
||||||
val maxOffsetX = (size.width * (scale - 1)) / 2
|
|
||||||
val maxOffsetY = (size.height * (scale - 1)) / 2
|
|
||||||
|
|
||||||
offsetX = (offsetX + pan.x).coerceIn(-maxOffsetX, maxOffsetX)
|
|
||||||
offsetY = (offsetY + pan.y).coerceIn(-maxOffsetY, maxOffsetY)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
},
|
|
||||||
contentAlignment = Alignment.Center
|
|
||||||
) {
|
|
||||||
AsyncImage(
|
|
||||||
model = imageFile,
|
|
||||||
contentDescription = "Full screen image",
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.graphicsLayer(
|
|
||||||
scaleX = scale,
|
|
||||||
scaleY = scale,
|
|
||||||
translationX = offsetX,
|
|
||||||
translationY = offsetY
|
|
||||||
),
|
|
||||||
contentScale = ContentScale.Fit
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ import androidx.compose.ui.draw.clip
|
|||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import coil.compose.AsyncImage
|
|
||||||
import com.atridad.openclimb.utils.ImageUtils
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ImageDisplay(
|
fun ImageDisplay(
|
||||||
@@ -25,18 +23,13 @@ fun ImageDisplay(
|
|||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
if (imagePaths.isNotEmpty()) {
|
if (imagePaths.isNotEmpty()) {
|
||||||
LazyRow(
|
LazyRow(modifier = modifier, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||||
modifier = modifier,
|
|
||||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
|
||||||
) {
|
|
||||||
itemsIndexed(imagePaths) { index, imagePath ->
|
itemsIndexed(imagePaths) { index, imagePath ->
|
||||||
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
OrientationAwareImage(
|
||||||
|
imagePath = imagePath,
|
||||||
AsyncImage(
|
|
||||||
model = imageFile,
|
|
||||||
contentDescription = "Problem photo",
|
contentDescription = "Problem photo",
|
||||||
modifier = Modifier
|
modifier =
|
||||||
.size(imageSize.dp)
|
Modifier.size(imageSize.dp)
|
||||||
.clip(RoundedCornerShape(8.dp))
|
.clip(RoundedCornerShape(8.dp))
|
||||||
.clickable(enabled = onImageClick != null) {
|
.clickable(enabled = onImageClick != null) {
|
||||||
onImageClick?.invoke(index)
|
onImageClick?.invoke(index)
|
||||||
@@ -65,11 +58,7 @@ fun ImageDisplaySection(
|
|||||||
|
|
||||||
Spacer(modifier = Modifier.height(8.dp))
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
|
|
||||||
ImageDisplay(
|
ImageDisplay(imagePaths = imagePaths, imageSize = 120, onImageClick = onImageClick)
|
||||||
imagePaths = imagePaths,
|
|
||||||
imageSize = 120,
|
|
||||||
onImageClick = onImageClick
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import androidx.compose.ui.platform.LocalContext
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import coil.compose.AsyncImage
|
|
||||||
import com.atridad.openclimb.utils.ImageUtils
|
import com.atridad.openclimb.utils.ImageUtils
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
@@ -259,8 +258,8 @@ private fun ImageItem(imagePath: String, onRemove: () -> Unit, modifier: Modifie
|
|||||||
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
||||||
|
|
||||||
Box(modifier = modifier.size(80.dp)) {
|
Box(modifier = modifier.size(80.dp)) {
|
||||||
AsyncImage(
|
OrientationAwareImage(
|
||||||
model = imageFile,
|
imagePath = imagePath,
|
||||||
contentDescription = "Problem photo",
|
contentDescription = "Problem photo",
|
||||||
modifier = Modifier.fillMaxSize().clip(RoundedCornerShape(8.dp)),
|
modifier = Modifier.fillMaxSize().clip(RoundedCornerShape(8.dp)),
|
||||||
contentScale = ContentScale.Crop
|
contentScale = ContentScale.Crop
|
||||||
|
|||||||
@@ -0,0 +1,149 @@
|
|||||||
|
package com.atridad.openclimb.ui.components
|
||||||
|
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
|
import android.graphics.Matrix
|
||||||
|
import androidx.compose.foundation.Image
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
|
import androidx.compose.runtime.*
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.asImageBitmap
|
||||||
|
import androidx.compose.ui.layout.ContentScale
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.exifinterface.media.ExifInterface
|
||||||
|
import com.atridad.openclimb.utils.ImageUtils
|
||||||
|
import java.io.File
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun OrientationAwareImage(
|
||||||
|
imagePath: String,
|
||||||
|
contentDescription: String? = null,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
contentScale: ContentScale = ContentScale.Fit
|
||||||
|
) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
var imageBitmap by
|
||||||
|
remember(imagePath) { mutableStateOf<androidx.compose.ui.graphics.ImageBitmap?>(null) }
|
||||||
|
var isLoading by remember(imagePath) { mutableStateOf(true) }
|
||||||
|
|
||||||
|
LaunchedEffect(imagePath) {
|
||||||
|
isLoading = true
|
||||||
|
val bitmap =
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
try {
|
||||||
|
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
||||||
|
if (!imageFile.exists()) return@withContext null
|
||||||
|
|
||||||
|
val originalBitmap =
|
||||||
|
BitmapFactory.decodeFile(imageFile.absolutePath)
|
||||||
|
?: return@withContext null
|
||||||
|
val correctedBitmap = correctImageOrientation(imageFile, originalBitmap)
|
||||||
|
correctedBitmap.asImageBitmap()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
imageBitmap = bitmap
|
||||||
|
isLoading = false
|
||||||
|
}
|
||||||
|
|
||||||
|
Box(modifier = modifier) {
|
||||||
|
if (isLoading) {
|
||||||
|
CircularProgressIndicator(modifier = Modifier.fillMaxSize())
|
||||||
|
} else {
|
||||||
|
imageBitmap?.let { bitmap ->
|
||||||
|
Image(
|
||||||
|
bitmap = bitmap,
|
||||||
|
contentDescription = contentDescription,
|
||||||
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
contentScale = contentScale
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun correctImageOrientation(
|
||||||
|
imageFile: File,
|
||||||
|
bitmap: android.graphics.Bitmap
|
||||||
|
): android.graphics.Bitmap {
|
||||||
|
return try {
|
||||||
|
val exif = ExifInterface(imageFile.absolutePath)
|
||||||
|
val orientation =
|
||||||
|
exif.getAttributeInt(
|
||||||
|
ExifInterface.TAG_ORIENTATION,
|
||||||
|
ExifInterface.ORIENTATION_NORMAL
|
||||||
|
)
|
||||||
|
|
||||||
|
val matrix = Matrix()
|
||||||
|
var needsTransform = false
|
||||||
|
|
||||||
|
when (orientation) {
|
||||||
|
ExifInterface.ORIENTATION_ROTATE_90 -> {
|
||||||
|
matrix.postRotate(90f)
|
||||||
|
needsTransform = true
|
||||||
|
}
|
||||||
|
ExifInterface.ORIENTATION_ROTATE_180 -> {
|
||||||
|
matrix.postRotate(180f)
|
||||||
|
needsTransform = true
|
||||||
|
}
|
||||||
|
ExifInterface.ORIENTATION_ROTATE_270 -> {
|
||||||
|
matrix.postRotate(270f)
|
||||||
|
needsTransform = true
|
||||||
|
}
|
||||||
|
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> {
|
||||||
|
matrix.postScale(-1f, 1f)
|
||||||
|
needsTransform = true
|
||||||
|
}
|
||||||
|
ExifInterface.ORIENTATION_FLIP_VERTICAL -> {
|
||||||
|
matrix.postScale(1f, -1f)
|
||||||
|
needsTransform = true
|
||||||
|
}
|
||||||
|
ExifInterface.ORIENTATION_TRANSPOSE -> {
|
||||||
|
matrix.postRotate(90f)
|
||||||
|
matrix.postScale(-1f, 1f)
|
||||||
|
needsTransform = true
|
||||||
|
}
|
||||||
|
ExifInterface.ORIENTATION_TRANSVERSE -> {
|
||||||
|
matrix.postRotate(-90f)
|
||||||
|
matrix.postScale(-1f, 1f)
|
||||||
|
needsTransform = true
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
if (orientation == ExifInterface.ORIENTATION_UNDEFINED || orientation == 0) {
|
||||||
|
if (imageFile.name.startsWith("problem_") &&
|
||||||
|
imageFile.name.contains("_") &&
|
||||||
|
imageFile.name.endsWith(".jpg")
|
||||||
|
) {
|
||||||
|
matrix.postRotate(90f)
|
||||||
|
needsTransform = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!needsTransform) {
|
||||||
|
bitmap
|
||||||
|
} else {
|
||||||
|
val rotatedBitmap =
|
||||||
|
android.graphics.Bitmap.createBitmap(
|
||||||
|
bitmap,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
bitmap.width,
|
||||||
|
bitmap.height,
|
||||||
|
matrix,
|
||||||
|
true
|
||||||
|
)
|
||||||
|
if (rotatedBitmap != bitmap) {
|
||||||
|
bitmap.recycle()
|
||||||
|
}
|
||||||
|
rotatedBitmap
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
bitmap
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,9 @@ import androidx.compose.foundation.layout.*
|
|||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.LazyRow
|
import androidx.compose.foundation.lazy.LazyRow
|
||||||
import androidx.compose.foundation.lazy.items
|
import androidx.compose.foundation.lazy.items
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.CheckCircle
|
||||||
|
import androidx.compose.material.icons.filled.Image
|
||||||
import androidx.compose.material3.*
|
import androidx.compose.material3.*
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
@@ -13,11 +16,11 @@ import androidx.compose.ui.res.painterResource
|
|||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.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.data.model.Attempt
|
||||||
|
import com.atridad.openclimb.data.model.AttemptResult
|
||||||
import com.atridad.openclimb.data.model.ClimbType
|
import com.atridad.openclimb.data.model.ClimbType
|
||||||
import com.atridad.openclimb.data.model.Gym
|
import com.atridad.openclimb.data.model.Gym
|
||||||
import com.atridad.openclimb.data.model.Problem
|
import com.atridad.openclimb.data.model.Problem
|
||||||
import com.atridad.openclimb.ui.components.FullscreenImageViewer
|
|
||||||
import com.atridad.openclimb.ui.components.ImageDisplay
|
|
||||||
import com.atridad.openclimb.ui.components.SyncIndicator
|
import com.atridad.openclimb.ui.components.SyncIndicator
|
||||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||||
|
|
||||||
@@ -26,10 +29,8 @@ import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
|||||||
fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String) -> Unit) {
|
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 attempts by viewModel.attempts.collectAsState()
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
var showImageViewer by remember { mutableStateOf(false) }
|
|
||||||
var selectedImagePaths by remember { mutableStateOf<List<String>>(emptyList()) }
|
|
||||||
var selectedImageIndex by remember { mutableIntStateOf(0) }
|
|
||||||
|
|
||||||
// Filter state
|
// Filter state
|
||||||
var selectedClimbType by remember { mutableStateOf<ClimbType?>(null) }
|
var selectedClimbType by remember { mutableStateOf<ClimbType?>(null) }
|
||||||
@@ -178,12 +179,8 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
|||||||
ProblemCard(
|
ProblemCard(
|
||||||
problem = problem,
|
problem = problem,
|
||||||
gymName = gyms.find { it.id == problem.gymId }?.name ?: "Unknown Gym",
|
gymName = gyms.find { it.id == problem.gymId }?.name ?: "Unknown Gym",
|
||||||
|
attempts = attempts,
|
||||||
onClick = { onNavigateToProblemDetail(problem.id) },
|
onClick = { onNavigateToProblemDetail(problem.id) },
|
||||||
onImageClick = { imagePaths, index ->
|
|
||||||
selectedImagePaths = imagePaths
|
|
||||||
selectedImageIndex = index
|
|
||||||
showImageViewer = true
|
|
||||||
},
|
|
||||||
onToggleActive = {
|
onToggleActive = {
|
||||||
val updatedProblem = problem.copy(isActive = !problem.isActive)
|
val updatedProblem = problem.copy(isActive = !problem.isActive)
|
||||||
viewModel.updateProblem(updatedProblem, context)
|
viewModel.updateProblem(updatedProblem, context)
|
||||||
@@ -194,15 +191,6 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fullscreen Image Viewer
|
|
||||||
if (showImageViewer && selectedImagePaths.isNotEmpty()) {
|
|
||||||
FullscreenImageViewer(
|
|
||||||
imagePaths = selectedImagePaths,
|
|
||||||
initialIndex = selectedImageIndex,
|
|
||||||
onDismiss = { showImageViewer = false }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@@ -210,10 +198,17 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
|||||||
fun ProblemCard(
|
fun ProblemCard(
|
||||||
problem: Problem,
|
problem: Problem,
|
||||||
gymName: String,
|
gymName: String,
|
||||||
|
attempts: List<Attempt>,
|
||||||
onClick: () -> Unit,
|
onClick: () -> Unit,
|
||||||
onImageClick: ((List<String>, Int) -> Unit)? = null,
|
|
||||||
onToggleActive: (() -> Unit)? = null
|
onToggleActive: (() -> Unit)? = null
|
||||||
) {
|
) {
|
||||||
|
val isCompleted =
|
||||||
|
attempts.any { attempt ->
|
||||||
|
attempt.problemId == problem.id &&
|
||||||
|
(attempt.result == AttemptResult.SUCCESS ||
|
||||||
|
attempt.result == AttemptResult.FLASH)
|
||||||
|
}
|
||||||
|
|
||||||
Card(onClick = onClick, modifier = Modifier.fillMaxWidth()) {
|
Card(onClick = onClick, modifier = Modifier.fillMaxWidth()) {
|
||||||
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
|
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
|
||||||
Row(
|
Row(
|
||||||
@@ -242,12 +237,35 @@ fun ProblemCard(
|
|||||||
}
|
}
|
||||||
|
|
||||||
Column(horizontalAlignment = Alignment.End) {
|
Column(horizontalAlignment = Alignment.End) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically
|
||||||
|
) {
|
||||||
|
if (problem.imagePaths.isNotEmpty()) {
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Default.Image,
|
||||||
|
contentDescription = "Has images",
|
||||||
|
modifier = Modifier.size(16.dp),
|
||||||
|
tint = MaterialTheme.colorScheme.primary
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isCompleted) {
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Default.CheckCircle,
|
||||||
|
contentDescription = "Completed",
|
||||||
|
modifier = Modifier.size(16.dp),
|
||||||
|
tint = MaterialTheme.colorScheme.tertiary
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
text = problem.difficulty.grade,
|
text = problem.difficulty.grade,
|
||||||
style = MaterialTheme.typography.titleMedium,
|
style = MaterialTheme.typography.titleMedium,
|
||||||
fontWeight = FontWeight.Bold,
|
fontWeight = FontWeight.Bold,
|
||||||
color = MaterialTheme.colorScheme.primary
|
color = MaterialTheme.colorScheme.primary
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
text = problem.climbType.getDisplayName(),
|
text = problem.climbType.getDisplayName(),
|
||||||
@@ -279,16 +297,6 @@ fun ProblemCard(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display images if any
|
|
||||||
if (problem.imagePaths.isNotEmpty()) {
|
|
||||||
Spacer(modifier = Modifier.height(8.dp))
|
|
||||||
ImageDisplay(
|
|
||||||
imagePaths = problem.imagePaths.take(3), // Show max 3 images in list
|
|
||||||
imageSize = 60,
|
|
||||||
onImageClick = { index -> onImageClick?.invoke(problem.imagePaths, index) }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!problem.isActive) {
|
if (!problem.isActive) {
|
||||||
Spacer(modifier = Modifier.height(8.dp))
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
Text(
|
Text(
|
||||||
|
|||||||
@@ -38,14 +38,15 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
|||||||
val isTesting by syncService.isTesting.collectAsState()
|
val isTesting by syncService.isTesting.collectAsState()
|
||||||
val lastSyncTime by syncService.lastSyncTime.collectAsState()
|
val lastSyncTime by syncService.lastSyncTime.collectAsState()
|
||||||
val syncError by syncService.syncError.collectAsState()
|
val syncError by syncService.syncError.collectAsState()
|
||||||
|
val isAutoSyncEnabled by syncService.isAutoSyncEnabled.collectAsState()
|
||||||
|
|
||||||
// State for dialogs
|
// State for dialogs
|
||||||
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 showDeleteImagesDialog by remember { mutableStateOf(false) }
|
||||||
var isFixingImages by remember { mutableStateOf(false) }
|
|
||||||
var isDeletingImages by remember { mutableStateOf(false) }
|
var isDeletingImages by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
// Sync configuration state
|
// Sync configuration state
|
||||||
@@ -280,8 +281,10 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
|||||||
}
|
}
|
||||||
Spacer(modifier = Modifier.width(16.dp))
|
Spacer(modifier = Modifier.width(16.dp))
|
||||||
Switch(
|
Switch(
|
||||||
checked = syncService.isAutoSyncEnabled,
|
checked = isAutoSyncEnabled,
|
||||||
onCheckedChange = { syncService.isAutoSyncEnabled = it }
|
onCheckedChange = { enabled ->
|
||||||
|
syncService.setAutoSyncEnabled(enabled)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -481,46 +484,6 @@ 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(
|
Card(
|
||||||
shape = RoundedCornerShape(12.dp),
|
shape = RoundedCornerShape(12.dp),
|
||||||
colors =
|
colors =
|
||||||
@@ -1002,35 +965,6 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fix Image Names dialog
|
|
||||||
if (showFixImagesDialog) {
|
|
||||||
AlertDialog(
|
|
||||||
onDismissRequest = { showFixImagesDialog = false },
|
|
||||||
title = { Text("Fix Image Names") },
|
|
||||||
text = {
|
|
||||||
Text(
|
|
||||||
"This will rename all existing image files to use a consistent naming system across devices.\n\nThis improves sync reliability between iOS and Android. Your images will not be lost, only renamed.\n\nThis is safe to run multiple times."
|
|
||||||
)
|
|
||||||
},
|
|
||||||
confirmButton = {
|
|
||||||
TextButton(
|
|
||||||
onClick = {
|
|
||||||
isFixingImages = true
|
|
||||||
showFixImagesDialog = false
|
|
||||||
coroutineScope.launch {
|
|
||||||
viewModel.migrateImageNamesToDeterministic(context)
|
|
||||||
isFixingImages = false
|
|
||||||
viewModel.setMessage("Image names fixed successfully!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
) { Text("Fix Names") }
|
|
||||||
},
|
|
||||||
dismissButton = {
|
|
||||||
TextButton(onClick = { showFixImagesDialog = false }) { Text("Cancel") }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete All Images dialog
|
// Delete All Images dialog
|
||||||
if (showDeleteImagesDialog) {
|
if (showDeleteImagesDialog) {
|
||||||
AlertDialog(
|
AlertDialog(
|
||||||
|
|||||||
@@ -171,64 +171,6 @@ class ClimbViewModel(
|
|||||||
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) {
|
fun deleteAllImages(context: Context) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import android.net.Uri
|
|||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.core.graphics.scale
|
import androidx.core.graphics.scale
|
||||||
|
import androidx.exifinterface.media.ExifInterface
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
@@ -27,7 +28,57 @@ object ImageUtils {
|
|||||||
return imagesDir
|
return imagesDir
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Saves an image from a URI with compression and proper orientation */
|
/** Saves an image from a URI while preserving EXIF orientation data */
|
||||||
|
private fun saveImageWithExif(
|
||||||
|
context: Context,
|
||||||
|
imageUri: Uri,
|
||||||
|
originalBitmap: Bitmap,
|
||||||
|
outputFile: File
|
||||||
|
): Boolean {
|
||||||
|
return try {
|
||||||
|
// Get EXIF data from original image
|
||||||
|
val originalExif =
|
||||||
|
context.contentResolver.openInputStream(imageUri)?.use { input ->
|
||||||
|
ExifInterface(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compress and save the bitmap
|
||||||
|
val compressedBitmap = compressImage(originalBitmap)
|
||||||
|
FileOutputStream(outputFile).use { output ->
|
||||||
|
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy EXIF data to the saved file
|
||||||
|
originalExif?.let { sourceExif ->
|
||||||
|
val destExif = ExifInterface(outputFile.absolutePath)
|
||||||
|
|
||||||
|
// Copy orientation and other important EXIF attributes
|
||||||
|
val orientationValue = sourceExif.getAttribute(ExifInterface.TAG_ORIENTATION)
|
||||||
|
orientationValue?.let { destExif.setAttribute(ExifInterface.TAG_ORIENTATION, it) }
|
||||||
|
|
||||||
|
// Copy other useful EXIF data
|
||||||
|
sourceExif.getAttribute(ExifInterface.TAG_DATETIME)?.let {
|
||||||
|
destExif.setAttribute(ExifInterface.TAG_DATETIME, it)
|
||||||
|
}
|
||||||
|
sourceExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE)?.let {
|
||||||
|
destExif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, it)
|
||||||
|
}
|
||||||
|
sourceExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE)?.let {
|
||||||
|
destExif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, it)
|
||||||
|
}
|
||||||
|
|
||||||
|
destExif.saveAttributes()
|
||||||
|
}
|
||||||
|
|
||||||
|
compressedBitmap.recycle()
|
||||||
|
true
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Saves an image from a URI with compression */
|
||||||
fun saveImageFromUri(
|
fun saveImageFromUri(
|
||||||
context: Context,
|
context: Context,
|
||||||
imageUri: Uri,
|
imageUri: Uri,
|
||||||
@@ -42,10 +93,7 @@ object ImageUtils {
|
|||||||
}
|
}
|
||||||
?: return null
|
?: return null
|
||||||
|
|
||||||
val orientedBitmap = correctImageOrientation(context, imageUri, originalBitmap)
|
// Always require deterministic naming
|
||||||
val compressedBitmap = compressImage(orientedBitmap)
|
|
||||||
|
|
||||||
// Always require deterministic naming - no UUID fallback
|
|
||||||
require(problemId != null && imageIndex != null) {
|
require(problemId != null && imageIndex != null) {
|
||||||
"Problem ID and image index are required for deterministic image naming"
|
"Problem ID and image index are required for deterministic image naming"
|
||||||
}
|
}
|
||||||
@@ -53,15 +101,10 @@ object ImageUtils {
|
|||||||
val filename = ImageNamingUtils.generateImageFilename(problemId, imageIndex)
|
val filename = ImageNamingUtils.generateImageFilename(problemId, imageIndex)
|
||||||
val imageFile = File(getImagesDirectory(context), filename)
|
val imageFile = File(getImagesDirectory(context), filename)
|
||||||
|
|
||||||
FileOutputStream(imageFile).use { output ->
|
val success = saveImageWithExif(context, imageUri, originalBitmap, imageFile)
|
||||||
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
originalBitmap.recycle()
|
originalBitmap.recycle()
|
||||||
if (orientedBitmap != originalBitmap) {
|
|
||||||
orientedBitmap.recycle()
|
if (!success) return null
|
||||||
}
|
|
||||||
compressedBitmap.recycle()
|
|
||||||
|
|
||||||
"$IMAGES_DIR/$filename"
|
"$IMAGES_DIR/$filename"
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -221,23 +264,13 @@ object ImageUtils {
|
|||||||
MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)
|
MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)
|
||||||
?: return null
|
?: return null
|
||||||
|
|
||||||
val orientedBitmap = correctImageOrientation(context, imageUri, originalBitmap)
|
|
||||||
val compressedBitmap = compressImage(orientedBitmap)
|
|
||||||
|
|
||||||
val tempFilename = "temp_${UUID.randomUUID()}.jpg"
|
val tempFilename = "temp_${UUID.randomUUID()}.jpg"
|
||||||
val imageFile = File(getImagesDirectory(context), tempFilename)
|
val imageFile = File(getImagesDirectory(context), tempFilename)
|
||||||
|
|
||||||
FileOutputStream(imageFile).use { output ->
|
val success = saveImageWithExif(context, imageUri, originalBitmap, imageFile)
|
||||||
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
originalBitmap.recycle()
|
originalBitmap.recycle()
|
||||||
if (orientedBitmap != originalBitmap) {
|
|
||||||
orientedBitmap.recycle()
|
if (!success) return null
|
||||||
}
|
|
||||||
if (compressedBitmap != orientedBitmap) {
|
|
||||||
compressedBitmap.recycle()
|
|
||||||
}
|
|
||||||
|
|
||||||
tempFilename
|
tempFilename
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -315,21 +348,40 @@ object ImageUtils {
|
|||||||
filename: String
|
filename: String
|
||||||
): String? {
|
): String? {
|
||||||
return try {
|
return try {
|
||||||
val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) ?: return null
|
|
||||||
|
|
||||||
val compressedBitmap = compressImage(bitmap)
|
|
||||||
|
|
||||||
// Use the provided filename instead of generating a new UUID
|
|
||||||
val imageFile = File(getImagesDirectory(context), filename)
|
val imageFile = File(getImagesDirectory(context), filename)
|
||||||
|
|
||||||
|
// Check if image is too large and needs compression
|
||||||
|
if (imageData.size > 5 * 1024 * 1024) { // 5MB threshold
|
||||||
|
// For large images, decode, compress, and try to preserve EXIF
|
||||||
|
val bitmap =
|
||||||
|
BitmapFactory.decodeByteArray(imageData, 0, imageData.size) ?: return null
|
||||||
|
val compressedBitmap = compressImage(bitmap)
|
||||||
|
|
||||||
// Save compressed image
|
// Save compressed image
|
||||||
FileOutputStream(imageFile).use { output ->
|
FileOutputStream(imageFile).use { output ->
|
||||||
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up bitmaps
|
// Try to preserve EXIF orientation from original data
|
||||||
|
try {
|
||||||
|
val originalExif = ExifInterface(java.io.ByteArrayInputStream(imageData))
|
||||||
|
val destExif = ExifInterface(imageFile.absolutePath)
|
||||||
|
val orientationValue = originalExif.getAttribute(ExifInterface.TAG_ORIENTATION)
|
||||||
|
orientationValue?.let {
|
||||||
|
destExif.setAttribute(ExifInterface.TAG_ORIENTATION, it)
|
||||||
|
}
|
||||||
|
destExif.saveAttributes()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
// If EXIF preservation fails, continue without it
|
||||||
|
Log.w("ImageUtils", "Failed to preserve EXIF data: ${e.message}")
|
||||||
|
}
|
||||||
|
|
||||||
bitmap.recycle()
|
bitmap.recycle()
|
||||||
compressedBitmap.recycle()
|
compressedBitmap.recycle()
|
||||||
|
} else {
|
||||||
|
// For smaller images, save raw data to preserve all EXIF information
|
||||||
|
FileOutputStream(imageFile).use { output -> output.write(imageData) }
|
||||||
|
}
|
||||||
|
|
||||||
// Return relative path
|
// Return relative path
|
||||||
"$IMAGES_DIR/$filename"
|
"$IMAGES_DIR/$filename"
|
||||||
|
|||||||
@@ -465,7 +465,7 @@
|
|||||||
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 = 23;
|
CURRENT_PROJECT_VERSION = 25;
|
||||||
DEVELOPMENT_TEAM = 4BC9Y2LL4B;
|
DEVELOPMENT_TEAM = 4BC9Y2LL4B;
|
||||||
DRIVERKIT_DEPLOYMENT_TARGET = 24.6;
|
DRIVERKIT_DEPLOYMENT_TARGET = 24.6;
|
||||||
ENABLE_PREVIEWS = YES;
|
ENABLE_PREVIEWS = YES;
|
||||||
@@ -513,7 +513,7 @@
|
|||||||
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 = 23;
|
CURRENT_PROJECT_VERSION = 25;
|
||||||
DEVELOPMENT_TEAM = 4BC9Y2LL4B;
|
DEVELOPMENT_TEAM = 4BC9Y2LL4B;
|
||||||
DRIVERKIT_DEPLOYMENT_TARGET = 24.6;
|
DRIVERKIT_DEPLOYMENT_TARGET = 24.6;
|
||||||
ENABLE_PREVIEWS = YES;
|
ENABLE_PREVIEWS = YES;
|
||||||
@@ -602,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 = 23;
|
CURRENT_PROJECT_VERSION = 25;
|
||||||
DEVELOPMENT_TEAM = 4BC9Y2LL4B;
|
DEVELOPMENT_TEAM = 4BC9Y2LL4B;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
INFOPLIST_FILE = SessionStatusLive/Info.plist;
|
INFOPLIST_FILE = SessionStatusLive/Info.plist;
|
||||||
@@ -632,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 = 23;
|
CURRENT_PROJECT_VERSION = 25;
|
||||||
DEVELOPMENT_TEAM = 4BC9Y2LL4B;
|
DEVELOPMENT_TEAM = 4BC9Y2LL4B;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
INFOPLIST_FILE = SessionStatusLive/Info.plist;
|
INFOPLIST_FILE = SessionStatusLive/Info.plist;
|
||||||
|
|||||||
Binary file not shown.
39
ios/OpenClimb/Components/AsyncImageView.swift
Normal file
39
ios/OpenClimb/Components/AsyncImageView.swift
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct AsyncImageView: View {
|
||||||
|
let imagePath: String
|
||||||
|
let targetSize: CGSize
|
||||||
|
|
||||||
|
@State private var image: UIImage?
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
ZStack {
|
||||||
|
Rectangle()
|
||||||
|
.fill(Color(.systemGray6))
|
||||||
|
|
||||||
|
if let image = image {
|
||||||
|
Image(uiImage: image)
|
||||||
|
.resizable()
|
||||||
|
.scaledToFill()
|
||||||
|
.transition(.opacity.animation(.easeInOut(duration: 0.3)))
|
||||||
|
} else {
|
||||||
|
Image(systemName: "photo")
|
||||||
|
.font(.system(size: 24))
|
||||||
|
.foregroundColor(Color(.systemGray3))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.frame(width: targetSize.width, height: targetSize.height)
|
||||||
|
.clipped()
|
||||||
|
.cornerRadius(8)
|
||||||
|
.task(id: imagePath) {
|
||||||
|
if self.image != nil {
|
||||||
|
self.image = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
self.image = await ImageManager.shared.loadThumbnail(
|
||||||
|
fromPath: imagePath,
|
||||||
|
targetSize: targetSize
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ class SyncService: ObservableObject {
|
|||||||
@Published var syncError: String?
|
@Published var syncError: String?
|
||||||
@Published var isConnected = false
|
@Published var isConnected = false
|
||||||
@Published var isTesting = false
|
@Published var isTesting = false
|
||||||
|
@Published var isOfflineMode = false
|
||||||
|
|
||||||
private let userDefaults = UserDefaults.standard
|
private let userDefaults = UserDefaults.standard
|
||||||
private var syncTask: Task<Void, Never>?
|
private var syncTask: Task<Void, Never>?
|
||||||
@@ -19,8 +20,9 @@ class SyncService: ObservableObject {
|
|||||||
static let serverURL = "sync_server_url"
|
static let serverURL = "sync_server_url"
|
||||||
static let authToken = "sync_auth_token"
|
static let authToken = "sync_auth_token"
|
||||||
static let lastSyncTime = "last_sync_time"
|
static let lastSyncTime = "last_sync_time"
|
||||||
static let isConnected = "sync_is_connected"
|
static let isConnected = "is_connected"
|
||||||
static let autoSyncEnabled = "auto_sync_enabled"
|
static let autoSyncEnabled = "auto_sync_enabled"
|
||||||
|
static let offlineMode = "offline_mode"
|
||||||
}
|
}
|
||||||
|
|
||||||
var serverURL: String {
|
var serverURL: String {
|
||||||
@@ -46,12 +48,9 @@ class SyncService: ObservableObject {
|
|||||||
if let lastSync = userDefaults.object(forKey: Keys.lastSyncTime) as? Date {
|
if let lastSync = userDefaults.object(forKey: Keys.lastSyncTime) as? Date {
|
||||||
self.lastSyncTime = lastSync
|
self.lastSyncTime = lastSync
|
||||||
}
|
}
|
||||||
self.isConnected = userDefaults.bool(forKey: Keys.isConnected)
|
isConnected = userDefaults.bool(forKey: Keys.isConnected)
|
||||||
|
isAutoSyncEnabled = userDefaults.object(forKey: Keys.autoSyncEnabled) as? Bool ?? true
|
||||||
// Perform image naming migration on initialization
|
isOfflineMode = userDefaults.bool(forKey: Keys.offlineMode)
|
||||||
Task {
|
|
||||||
await performImageNamingMigration()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadData() async throws -> ClimbDataBackup {
|
func downloadData() async throws -> ClimbDataBackup {
|
||||||
@@ -211,6 +210,11 @@ class SyncService: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func syncWithServer(dataManager: ClimbingDataManager) async throws {
|
func syncWithServer(dataManager: ClimbingDataManager) async throws {
|
||||||
|
if isOfflineMode {
|
||||||
|
print("Sync skipped: Offline mode is enabled.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
guard isConfigured else {
|
guard isConfigured else {
|
||||||
throw SyncError.notConfigured
|
throw SyncError.notConfigured
|
||||||
}
|
}
|
||||||
@@ -1025,105 +1029,7 @@ class SyncService: ObservableObject {
|
|||||||
syncTask?.cancel()
|
syncTask?.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Image Naming Migration
|
// MARK: - Merging
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
private func mergeGyms(local: [Gym], server: [BackupGym], deletedItems: [DeletedItem]) -> [Gym]
|
private func mergeGyms(local: [Gym], server: [BackupGym], deletedItems: [DeletedItem]) -> [Gym]
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
import ImageIO
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import UIKit
|
||||||
|
|
||||||
class ImageManager {
|
class ImageManager {
|
||||||
static let shared = ImageManager()
|
static let shared = ImageManager()
|
||||||
|
|
||||||
|
private let thumbnailCache = NSCache<NSString, UIImage>()
|
||||||
private let fileManager = FileManager.default
|
private let fileManager = FileManager.default
|
||||||
private let appSupportDirectoryName = "OpenClimb"
|
private let appSupportDirectoryName = "OpenClimb"
|
||||||
private let imagesDirectoryName = "Images"
|
private let imagesDirectoryName = "Images"
|
||||||
@@ -478,6 +481,51 @@ class ImageManager {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadThumbnail(fromPath path: String, targetSize: CGSize) async -> UIImage? {
|
||||||
|
let cacheKey = "\(path)-\(targetSize.width)x\(targetSize.height)" as NSString
|
||||||
|
|
||||||
|
if let cachedImage = thumbnailCache.object(forKey: cacheKey) {
|
||||||
|
return cachedImage
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let imageData = loadImageData(fromPath: path) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
let options: [CFString: Any] = [
|
||||||
|
kCGImageSourceCreateThumbnailFromImageIfAbsent: true,
|
||||||
|
kCGImageSourceCreateThumbnailWithTransform: true,
|
||||||
|
kCGImageSourceShouldCacheImmediately: true,
|
||||||
|
kCGImageSourceThumbnailMaxPixelSize: max(targetSize.width, targetSize.height)
|
||||||
|
* UIScreen.main.scale,
|
||||||
|
]
|
||||||
|
|
||||||
|
guard let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil) else {
|
||||||
|
return UIImage(data: imageData)
|
||||||
|
}
|
||||||
|
|
||||||
|
let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [CFString: Any]
|
||||||
|
let orientation = properties?[kCGImagePropertyOrientation] as? UInt32 ?? 1
|
||||||
|
|
||||||
|
if let cgImage = CGImageSourceCreateThumbnailAtIndex(
|
||||||
|
imageSource, 0, options as CFDictionary)
|
||||||
|
{
|
||||||
|
let imageOrientation = UIImage.Orientation(rawValue: Int(orientation - 1)) ?? .up
|
||||||
|
let thumbnail = UIImage(
|
||||||
|
cgImage: cgImage, scale: UIScreen.main.scale, orientation: imageOrientation)
|
||||||
|
|
||||||
|
thumbnailCache.setObject(thumbnail, forKey: cacheKey)
|
||||||
|
return thumbnail
|
||||||
|
} else {
|
||||||
|
if let fallbackImage = UIImage(data: imageData) {
|
||||||
|
thumbnailCache.setObject(fallbackImage, forKey: cacheKey)
|
||||||
|
return fallbackImage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func imageExists(atPath path: String) -> Bool {
|
func imageExists(atPath path: String) -> Bool {
|
||||||
let primaryPath = getFullPath(from: path)
|
let primaryPath = getFullPath(from: path)
|
||||||
let backupPath = backupDirectory.appendingPathComponent(getRelativePath(from: path))
|
let backupPath = backupDirectory.appendingPathComponent(getRelativePath(from: path))
|
||||||
@@ -853,72 +901,4 @@ class ImageManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
147
ios/OpenClimb/Utils/OrientationAwareImage.swift
Normal file
147
ios/OpenClimb/Utils/OrientationAwareImage.swift
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
import SwiftUI
|
||||||
|
import UIKit
|
||||||
|
|
||||||
|
struct OrientationAwareImage: View {
|
||||||
|
let imagePath: String
|
||||||
|
let contentMode: ContentMode
|
||||||
|
|
||||||
|
@State private var uiImage: UIImage?
|
||||||
|
@State private var isLoading = true
|
||||||
|
@State private var hasFailed = false
|
||||||
|
|
||||||
|
init(imagePath: String, contentMode: ContentMode = .fit) {
|
||||||
|
self.imagePath = imagePath
|
||||||
|
self.contentMode = contentMode
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
Group {
|
||||||
|
if let uiImage = uiImage {
|
||||||
|
Image(uiImage: uiImage)
|
||||||
|
.resizable()
|
||||||
|
.aspectRatio(contentMode: contentMode)
|
||||||
|
} else if hasFailed {
|
||||||
|
Image(systemName: "photo")
|
||||||
|
.foregroundColor(.gray)
|
||||||
|
} else {
|
||||||
|
ProgressView()
|
||||||
|
.progressViewStyle(CircularProgressViewStyle())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onAppear {
|
||||||
|
loadImageWithCorrectOrientation()
|
||||||
|
}
|
||||||
|
.onChange(of: imagePath) { _ in
|
||||||
|
loadImageWithCorrectOrientation()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func loadImageWithCorrectOrientation() {
|
||||||
|
Task.detached(priority: .userInitiated) {
|
||||||
|
let correctedImage = await loadAndCorrectImage()
|
||||||
|
await MainActor.run {
|
||||||
|
self.uiImage = correctedImage
|
||||||
|
self.isLoading = false
|
||||||
|
self.hasFailed = correctedImage == nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func loadAndCorrectImage() async -> UIImage? {
|
||||||
|
guard let data = ImageManager.shared.loadImageData(fromPath: imagePath) else { return nil }
|
||||||
|
|
||||||
|
guard let originalImage = UIImage(data: data) else { return nil }
|
||||||
|
|
||||||
|
return correctImageOrientation(originalImage)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Corrects the orientation of a UIImage based on its EXIF data
|
||||||
|
private func correctImageOrientation(_ image: UIImage) -> UIImage {
|
||||||
|
// If the image is already in the correct orientation, return as-is
|
||||||
|
if image.imageOrientation == .up {
|
||||||
|
return image
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the proper transformation matrix
|
||||||
|
var transform = CGAffineTransform.identity
|
||||||
|
|
||||||
|
switch image.imageOrientation {
|
||||||
|
case .down, .downMirrored:
|
||||||
|
transform = transform.translatedBy(x: image.size.width, y: image.size.height)
|
||||||
|
transform = transform.rotated(by: .pi)
|
||||||
|
|
||||||
|
case .left, .leftMirrored:
|
||||||
|
transform = transform.translatedBy(x: image.size.width, y: 0)
|
||||||
|
transform = transform.rotated(by: .pi / 2)
|
||||||
|
|
||||||
|
case .right, .rightMirrored:
|
||||||
|
transform = transform.translatedBy(x: 0, y: image.size.height)
|
||||||
|
transform = transform.rotated(by: -.pi / 2)
|
||||||
|
|
||||||
|
case .up, .upMirrored:
|
||||||
|
break
|
||||||
|
|
||||||
|
@unknown default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
switch image.imageOrientation {
|
||||||
|
case .upMirrored, .downMirrored:
|
||||||
|
transform = transform.translatedBy(x: image.size.width, y: 0)
|
||||||
|
transform = transform.scaledBy(x: -1, y: 1)
|
||||||
|
|
||||||
|
case .leftMirrored, .rightMirrored:
|
||||||
|
transform = transform.translatedBy(x: image.size.height, y: 0)
|
||||||
|
transform = transform.scaledBy(x: -1, y: 1)
|
||||||
|
|
||||||
|
case .up, .down, .left, .right:
|
||||||
|
break
|
||||||
|
|
||||||
|
@unknown default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new image context and apply the transformation
|
||||||
|
guard let cgImage = image.cgImage else { return image }
|
||||||
|
|
||||||
|
let context = CGContext(
|
||||||
|
data: nil,
|
||||||
|
width: Int(image.size.width),
|
||||||
|
height: Int(image.size.height),
|
||||||
|
bitsPerComponent: cgImage.bitsPerComponent,
|
||||||
|
bytesPerRow: 0,
|
||||||
|
space: cgImage.colorSpace ?? CGColorSpaceCreateDeviceRGB(),
|
||||||
|
bitmapInfo: cgImage.bitmapInfo.rawValue
|
||||||
|
)
|
||||||
|
|
||||||
|
guard let ctx = context else { return image }
|
||||||
|
|
||||||
|
ctx.concatenate(transform)
|
||||||
|
|
||||||
|
switch image.imageOrientation {
|
||||||
|
case .left, .leftMirrored, .right, .rightMirrored:
|
||||||
|
ctx.draw(
|
||||||
|
cgImage, in: CGRect(x: 0, y: 0, width: image.size.height, height: image.size.width))
|
||||||
|
default:
|
||||||
|
ctx.draw(
|
||||||
|
cgImage, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let newCGImage = ctx.makeImage() else { return image }
|
||||||
|
return UIImage(cgImage: newCGImage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Convenience Extensions
|
||||||
|
|
||||||
|
extension OrientationAwareImage {
|
||||||
|
/// Creates an orientation-aware image with fill content mode
|
||||||
|
static func fill(imagePath: String) -> OrientationAwareImage {
|
||||||
|
OrientationAwareImage(imagePath: imagePath, contentMode: .fill)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an orientation-aware image with fit content mode
|
||||||
|
static func fit(imagePath: String) -> OrientationAwareImage {
|
||||||
|
OrientationAwareImage(imagePath: imagePath, contentMode: .fit)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1308,131 +1308,19 @@ struct EditAttemptView: View {
|
|||||||
|
|
||||||
struct ProblemSelectionImageView: View {
|
struct ProblemSelectionImageView: View {
|
||||||
let imagePath: String
|
let imagePath: String
|
||||||
@State private var uiImage: UIImage?
|
|
||||||
@State private var isLoading = true
|
|
||||||
@State private var hasFailed = false
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Group {
|
OrientationAwareImage.fill(imagePath: imagePath)
|
||||||
if let uiImage = uiImage {
|
|
||||||
Image(uiImage: uiImage)
|
|
||||||
.resizable()
|
|
||||||
.aspectRatio(contentMode: .fill)
|
|
||||||
.frame(height: 80)
|
.frame(height: 80)
|
||||||
.clipped()
|
.clipped()
|
||||||
.cornerRadius(8)
|
.cornerRadius(8)
|
||||||
} else if hasFailed {
|
|
||||||
RoundedRectangle(cornerRadius: 8)
|
|
||||||
.fill(.gray.opacity(0.2))
|
|
||||||
.frame(height: 80)
|
|
||||||
.overlay {
|
|
||||||
Image(systemName: "photo")
|
|
||||||
.foregroundColor(.gray)
|
|
||||||
.font(.title3)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
RoundedRectangle(cornerRadius: 8)
|
|
||||||
.fill(.gray.opacity(0.3))
|
|
||||||
.frame(height: 80)
|
|
||||||
.overlay {
|
|
||||||
ProgressView()
|
|
||||||
.scaleEffect(0.8)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onAppear {
|
|
||||||
loadImage()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func loadImage() {
|
|
||||||
guard !imagePath.isEmpty else {
|
|
||||||
hasFailed = true
|
|
||||||
isLoading = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Task {
|
|
||||||
let data = await MainActor.run {
|
|
||||||
ImageManager.shared.loadImageData(fromPath: imagePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if let data = data, let image = UIImage(data: data) {
|
|
||||||
await MainActor.run {
|
|
||||||
self.uiImage = image
|
|
||||||
self.isLoading = false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await MainActor.run {
|
|
||||||
self.hasFailed = true
|
|
||||||
self.isLoading = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ProblemSelectionImageFullView: View {
|
struct ProblemSelectionImageFullView: View {
|
||||||
let imagePath: String
|
let imagePath: String
|
||||||
@State private var uiImage: UIImage?
|
|
||||||
@State private var isLoading = true
|
|
||||||
@State private var hasFailed = false
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Group {
|
OrientationAwareImage.fit(imagePath: imagePath)
|
||||||
if let uiImage = uiImage {
|
|
||||||
Image(uiImage: uiImage)
|
|
||||||
.resizable()
|
|
||||||
.aspectRatio(contentMode: .fit)
|
|
||||||
} else if hasFailed {
|
|
||||||
RoundedRectangle(cornerRadius: 12)
|
|
||||||
.fill(.gray.opacity(0.2))
|
|
||||||
.frame(height: 250)
|
|
||||||
.overlay {
|
|
||||||
VStack(spacing: 8) {
|
|
||||||
Image(systemName: "photo")
|
|
||||||
.foregroundColor(.gray)
|
|
||||||
.font(.largeTitle)
|
|
||||||
Text("Image not available")
|
|
||||||
.foregroundColor(.gray)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
RoundedRectangle(cornerRadius: 12)
|
|
||||||
.fill(.gray.opacity(0.3))
|
|
||||||
.frame(height: 250)
|
|
||||||
.overlay {
|
|
||||||
ProgressView()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onAppear {
|
|
||||||
loadImage()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func loadImage() {
|
|
||||||
guard !imagePath.isEmpty else {
|
|
||||||
hasFailed = true
|
|
||||||
isLoading = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
DispatchQueue.global(qos: .userInitiated).async {
|
|
||||||
if let data = ImageManager.shared.loadImageData(fromPath: imagePath),
|
|
||||||
let image = UIImage(data: data)
|
|
||||||
{
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.uiImage = image
|
|
||||||
self.isLoading = false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.hasFailed = true
|
|
||||||
self.isLoading = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -443,132 +443,20 @@ struct ImageViewerView: View {
|
|||||||
|
|
||||||
struct ProblemDetailImageView: View {
|
struct ProblemDetailImageView: View {
|
||||||
let imagePath: String
|
let imagePath: String
|
||||||
@State private var uiImage: UIImage?
|
|
||||||
@State private var isLoading = true
|
|
||||||
@State private var hasFailed = false
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Group {
|
OrientationAwareImage.fill(imagePath: imagePath)
|
||||||
if let uiImage = uiImage {
|
|
||||||
Image(uiImage: uiImage)
|
|
||||||
.resizable()
|
|
||||||
.aspectRatio(contentMode: .fill)
|
|
||||||
.frame(width: 120, height: 120)
|
.frame(width: 120, height: 120)
|
||||||
.clipped()
|
.clipped()
|
||||||
.cornerRadius(12)
|
.cornerRadius(12)
|
||||||
} else if hasFailed {
|
|
||||||
RoundedRectangle(cornerRadius: 12)
|
|
||||||
.fill(.gray.opacity(0.2))
|
|
||||||
.frame(width: 120, height: 120)
|
|
||||||
.overlay {
|
|
||||||
Image(systemName: "photo")
|
|
||||||
.foregroundColor(.gray)
|
|
||||||
.font(.title2)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
RoundedRectangle(cornerRadius: 12)
|
|
||||||
.fill(.gray.opacity(0.3))
|
|
||||||
.frame(width: 120, height: 120)
|
|
||||||
.overlay {
|
|
||||||
ProgressView()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onAppear {
|
|
||||||
loadImage()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func loadImage() {
|
|
||||||
guard !imagePath.isEmpty else {
|
|
||||||
hasFailed = true
|
|
||||||
isLoading = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Task {
|
|
||||||
let data = await MainActor.run {
|
|
||||||
ImageManager.shared.loadImageData(fromPath: imagePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if let data = data, let image = UIImage(data: data) {
|
|
||||||
await MainActor.run {
|
|
||||||
self.uiImage = image
|
|
||||||
self.isLoading = false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await MainActor.run {
|
|
||||||
self.hasFailed = true
|
|
||||||
self.isLoading = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ProblemDetailImageFullView: View {
|
struct ProblemDetailImageFullView: View {
|
||||||
let imagePath: String
|
let imagePath: String
|
||||||
@State private var uiImage: UIImage?
|
|
||||||
@State private var isLoading = true
|
|
||||||
@State private var hasFailed = false
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Group {
|
OrientationAwareImage.fit(imagePath: imagePath)
|
||||||
if let uiImage = uiImage {
|
|
||||||
Image(uiImage: uiImage)
|
|
||||||
.resizable()
|
|
||||||
.aspectRatio(contentMode: .fit)
|
|
||||||
} else if hasFailed {
|
|
||||||
Rectangle()
|
|
||||||
.fill(.gray.opacity(0.2))
|
|
||||||
.frame(height: 250)
|
|
||||||
.overlay {
|
|
||||||
VStack(spacing: 8) {
|
|
||||||
Image(systemName: "photo")
|
|
||||||
.foregroundColor(.gray)
|
|
||||||
.font(.largeTitle)
|
|
||||||
Text("Image not available")
|
|
||||||
.foregroundColor(.gray)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Rectangle()
|
|
||||||
.fill(.gray.opacity(0.3))
|
|
||||||
.frame(height: 250)
|
|
||||||
.overlay {
|
|
||||||
ProgressView()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onAppear {
|
|
||||||
loadImage()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func loadImage() {
|
|
||||||
guard !imagePath.isEmpty else {
|
|
||||||
hasFailed = true
|
|
||||||
isLoading = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Task {
|
|
||||||
let data = await MainActor.run {
|
|
||||||
ImageManager.shared.loadImageData(fromPath: imagePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if let data = data, let image = UIImage(data: data) {
|
|
||||||
await MainActor.run {
|
|
||||||
self.uiImage = image
|
|
||||||
self.isLoading = false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await MainActor.run {
|
|
||||||
self.hasFailed = true
|
|
||||||
self.isLoading = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,26 @@ struct ProblemsView: View {
|
|||||||
@State private var showingSearch = false
|
@State private var showingSearch = false
|
||||||
@FocusState private var isSearchFocused: Bool
|
@FocusState private var isSearchFocused: Bool
|
||||||
|
|
||||||
private var filteredProblems: [Problem] {
|
@State private var cachedFilteredProblems: [Problem] = []
|
||||||
var filtered = dataManager.problems
|
|
||||||
|
private func updateFilteredProblems() {
|
||||||
|
Task(priority: .userInitiated) {
|
||||||
|
let result = await computeFilteredProblems()
|
||||||
|
// Switch back to the main thread to update the UI
|
||||||
|
await MainActor.run {
|
||||||
|
cachedFilteredProblems = result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func computeFilteredProblems() async -> [Problem] {
|
||||||
|
// Capture dependencies for safe background processing
|
||||||
|
let problems = dataManager.problems
|
||||||
|
let searchText = self.searchText
|
||||||
|
let selectedClimbType = self.selectedClimbType
|
||||||
|
let selectedGym = self.selectedGym
|
||||||
|
|
||||||
|
var filtered = problems
|
||||||
|
|
||||||
// Apply search filter
|
// Apply search filter
|
||||||
if !searchText.isEmpty {
|
if !searchText.isEmpty {
|
||||||
@@ -93,19 +111,19 @@ struct ProblemsView: View {
|
|||||||
FilterSection(
|
FilterSection(
|
||||||
selectedClimbType: $selectedClimbType,
|
selectedClimbType: $selectedClimbType,
|
||||||
selectedGym: $selectedGym,
|
selectedGym: $selectedGym,
|
||||||
filteredProblems: filteredProblems
|
filteredProblems: cachedFilteredProblems
|
||||||
)
|
)
|
||||||
.padding()
|
.padding()
|
||||||
.background(.regularMaterial)
|
.background(.regularMaterial)
|
||||||
}
|
}
|
||||||
|
|
||||||
if filteredProblems.isEmpty {
|
if cachedFilteredProblems.isEmpty {
|
||||||
EmptyProblemsView(
|
EmptyProblemsView(
|
||||||
isEmpty: dataManager.problems.isEmpty,
|
isEmpty: dataManager.problems.isEmpty,
|
||||||
isFiltered: !dataManager.problems.isEmpty
|
isFiltered: !dataManager.problems.isEmpty
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
ProblemsList(problems: filteredProblems)
|
ProblemsList(problems: cachedFilteredProblems)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -158,6 +176,21 @@ struct ProblemsView: View {
|
|||||||
AddEditProblemView()
|
AddEditProblemView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onAppear {
|
||||||
|
updateFilteredProblems()
|
||||||
|
}
|
||||||
|
.onChange(of: dataManager.problems) {
|
||||||
|
updateFilteredProblems()
|
||||||
|
}
|
||||||
|
.onChange(of: searchText) {
|
||||||
|
updateFilteredProblems()
|
||||||
|
}
|
||||||
|
.onChange(of: selectedClimbType) {
|
||||||
|
updateFilteredProblems()
|
||||||
|
}
|
||||||
|
.onChange(of: selectedGym) {
|
||||||
|
updateFilteredProblems()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,6 +302,7 @@ struct ProblemsList: View {
|
|||||||
@EnvironmentObject var dataManager: ClimbingDataManager
|
@EnvironmentObject var dataManager: ClimbingDataManager
|
||||||
@State private var problemToDelete: Problem?
|
@State private var problemToDelete: Problem?
|
||||||
@State private var problemToEdit: Problem?
|
@State private var problemToEdit: Problem?
|
||||||
|
@State private var animationKey = 0
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
List(problems, id: \.id) { problem in
|
List(problems, id: \.id) { problem in
|
||||||
@@ -309,8 +343,11 @@ struct ProblemsList: View {
|
|||||||
}
|
}
|
||||||
.animation(
|
.animation(
|
||||||
.spring(response: 0.5, dampingFraction: 0.8, blendDuration: 0.1),
|
.spring(response: 0.5, dampingFraction: 0.8, blendDuration: 0.1),
|
||||||
value: problems.map { "\($0.id):\($0.isActive)" }.joined()
|
value: animationKey
|
||||||
)
|
)
|
||||||
|
.onChange(of: problems) {
|
||||||
|
animationKey += 1
|
||||||
|
}
|
||||||
.listStyle(.plain)
|
.listStyle(.plain)
|
||||||
.scrollContentBackground(.hidden)
|
.scrollContentBackground(.hidden)
|
||||||
.scrollIndicators(.hidden)
|
.scrollIndicators(.hidden)
|
||||||
@@ -344,6 +381,12 @@ struct ProblemRow: View {
|
|||||||
dataManager.gym(withId: problem.gymId)
|
dataManager.gym(withId: problem.gymId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var isCompleted: Bool {
|
||||||
|
dataManager.attempts.contains { attempt in
|
||||||
|
attempt.problemId == problem.id && attempt.result.isSuccessful
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack(alignment: .leading, spacing: 8) {
|
VStack(alignment: .leading, spacing: 8) {
|
||||||
HStack {
|
HStack {
|
||||||
@@ -361,10 +404,24 @@ struct ProblemRow: View {
|
|||||||
Spacer()
|
Spacer()
|
||||||
|
|
||||||
VStack(alignment: .trailing, spacing: 4) {
|
VStack(alignment: .trailing, spacing: 4) {
|
||||||
|
HStack(spacing: 8) {
|
||||||
|
if !problem.imagePaths.isEmpty {
|
||||||
|
Image(systemName: "photo")
|
||||||
|
.font(.system(size: 14, weight: .medium))
|
||||||
|
.foregroundColor(.blue)
|
||||||
|
}
|
||||||
|
|
||||||
|
if isCompleted {
|
||||||
|
Image(systemName: "checkmark.circle.fill")
|
||||||
|
.font(.system(size: 14, weight: .medium))
|
||||||
|
.foregroundColor(.green)
|
||||||
|
}
|
||||||
|
|
||||||
Text(problem.difficulty.grade)
|
Text(problem.difficulty.grade)
|
||||||
.font(.title2)
|
.font(.title2)
|
||||||
.fontWeight(.bold)
|
.fontWeight(.bold)
|
||||||
.foregroundColor(.blue)
|
.foregroundColor(.blue)
|
||||||
|
}
|
||||||
|
|
||||||
Text(problem.climbType.displayName)
|
Text(problem.climbType.displayName)
|
||||||
.font(.caption)
|
.font(.caption)
|
||||||
@@ -396,17 +453,6 @@ struct ProblemRow: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !problem.imagePaths.isEmpty {
|
|
||||||
ScrollView(.horizontal, showsIndicators: false) {
|
|
||||||
LazyHStack(spacing: 8) {
|
|
||||||
ForEach(problem.imagePaths.prefix(3), id: \.self) { imagePath in
|
|
||||||
ProblemImageView(imagePath: imagePath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.padding(.horizontal, 4)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !problem.isActive {
|
if !problem.isActive {
|
||||||
Text("Reset / No Longer Set")
|
Text("Reset / No Longer Set")
|
||||||
.font(.caption)
|
.font(.caption)
|
||||||
@@ -478,86 +524,6 @@ struct EmptyProblemsView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ProblemImageView: View {
|
|
||||||
let imagePath: String
|
|
||||||
@State private var uiImage: UIImage?
|
|
||||||
@State private var isLoading = true
|
|
||||||
@State private var hasFailed = false
|
|
||||||
|
|
||||||
private static let imageCache: NSCache<NSString, UIImage> = {
|
|
||||||
let cache = NSCache<NSString, UIImage>()
|
|
||||||
cache.countLimit = 100
|
|
||||||
cache.totalCostLimit = 50 * 1024 * 1024 // 50MB
|
|
||||||
return cache
|
|
||||||
}()
|
|
||||||
|
|
||||||
var body: some View {
|
|
||||||
Group {
|
|
||||||
if let uiImage = uiImage {
|
|
||||||
Image(uiImage: uiImage)
|
|
||||||
.resizable()
|
|
||||||
.aspectRatio(contentMode: .fill)
|
|
||||||
.frame(width: 60, height: 60)
|
|
||||||
.clipped()
|
|
||||||
.cornerRadius(8)
|
|
||||||
} else if hasFailed {
|
|
||||||
RoundedRectangle(cornerRadius: 8)
|
|
||||||
.fill(.gray.opacity(0.2))
|
|
||||||
.frame(width: 60, height: 60)
|
|
||||||
.overlay {
|
|
||||||
Image(systemName: "photo")
|
|
||||||
.foregroundColor(.gray)
|
|
||||||
.font(.title3)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
RoundedRectangle(cornerRadius: 8)
|
|
||||||
.fill(.gray.opacity(0.3))
|
|
||||||
.frame(width: 60, height: 60)
|
|
||||||
.overlay {
|
|
||||||
ProgressView()
|
|
||||||
.scaleEffect(0.8)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onAppear {
|
|
||||||
loadImage()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func loadImage() {
|
|
||||||
guard !imagePath.isEmpty else {
|
|
||||||
hasFailed = true
|
|
||||||
isLoading = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load image asynchronously
|
|
||||||
Task { @MainActor in
|
|
||||||
let cacheKey = NSString(string: imagePath)
|
|
||||||
|
|
||||||
// Check cache first
|
|
||||||
if let cachedImage = Self.imageCache.object(forKey: cacheKey) {
|
|
||||||
self.uiImage = cachedImage
|
|
||||||
self.isLoading = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load image data
|
|
||||||
if let data = ImageManager.shared.loadImageData(fromPath: imagePath),
|
|
||||||
let image = UIImage(data: data)
|
|
||||||
{
|
|
||||||
// Cache the image
|
|
||||||
Self.imageCache.setObject(image, forKey: cacheKey)
|
|
||||||
self.uiImage = image
|
|
||||||
self.isLoading = false
|
|
||||||
} else {
|
|
||||||
self.hasFailed = true
|
|
||||||
self.isLoading = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
ProblemsView()
|
ProblemsView()
|
||||||
.environmentObject(ClimbingDataManager.preview)
|
.environmentObject(ClimbingDataManager.preview)
|
||||||
|
|||||||
@@ -80,8 +80,7 @@ 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 isDeletingImages = false
|
||||||
@State private var showingDeleteImagesAlert = false
|
@State private var showingDeleteImagesAlert = false
|
||||||
|
|
||||||
@@ -121,27 +120,6 @@ 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
|
// Delete All Images
|
||||||
Button(action: {
|
Button(action: {
|
||||||
showingDeleteImagesAlert = true
|
showingDeleteImagesAlert = true
|
||||||
@@ -186,16 +164,7 @@ 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) {
|
.alert("Delete All Images", isPresented: $showingDeleteImagesAlert) {
|
||||||
Button("Cancel", role: .cancel) {}
|
Button("Cancel", role: .cancel) {}
|
||||||
Button("Delete", role: .destructive) {
|
Button("Delete", role: .destructive) {
|
||||||
@@ -219,17 +188,6 @@ 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() {
|
private func deleteAllImages() {
|
||||||
isDeletingImages = true
|
isDeletingImages = true
|
||||||
Task {
|
Task {
|
||||||
|
|||||||
23
ios/README.md
Normal file
23
ios/README.md
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# OpenClimb for iOS
|
||||||
|
|
||||||
|
The native iOS, watchOS, and widget client for OpenClimb, built with Swift and SwiftUI.
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
This is a standard Xcode project. The main app code is in the `OpenClimb/` directory.
|
||||||
|
|
||||||
|
- `Models/`: Swift `Codable` models (`Problem`, `Gym`, `ClimbSession`) that match the Android app.
|
||||||
|
- `ViewModels/`: App state and logic. `ClimbingDataManager` is the core here, handling data with SwiftData.
|
||||||
|
- `Views/`: All the SwiftUI views.
|
||||||
|
- `AddEdit/`: Views for adding/editing gyms, problems, etc.
|
||||||
|
- `Detail/`: Detail views for items.
|
||||||
|
- `Services/`: Handles HealthKit and sync server communication.
|
||||||
|
- `Utils/`: Helper functions and utilities.
|
||||||
|
|
||||||
|
## Other Targets
|
||||||
|
|
||||||
|
- `OpenClimbWatch/`: The watchOS app for tracking sessions.
|
||||||
|
- `ClimbingActivityWidget/`: A home screen widget.
|
||||||
|
- `SessionStatusLive/`: A Live Activity for the lock screen.
|
||||||
|
|
||||||
|
The app is built to be offline-first. All data is stored locally on your device and works without an internet connection.
|
||||||
37
sync/README.md
Normal file
37
sync/README.md
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
# Sync Server
|
||||||
|
|
||||||
|
A simple Go server for self-hosting your OpenClimb sync data.
|
||||||
|
|
||||||
|
## How It Works
|
||||||
|
|
||||||
|
This server is dead simple. It uses a single `openclimb.json` file for your data and a directory for images. The last client to upload wins, overwriting the old data. Authentication is just a static bearer token.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
1. Create a `.env` file in this directory:
|
||||||
|
```
|
||||||
|
IMAGE=git.atri.dad/atridad/openclimb-sync:latest
|
||||||
|
APP_PORT=8080
|
||||||
|
AUTH_TOKEN=your-super-secret-token
|
||||||
|
DATA_FILE=/data/openclimb.json
|
||||||
|
IMAGES_DIR=/data/images
|
||||||
|
ROOT_DIR=./openclimb-data
|
||||||
|
```
|
||||||
|
Set `AUTH_TOKEN` to a long, random string. `ROOT_DIR` is where the server will store its data on your machine.
|
||||||
|
|
||||||
|
2. Run with Docker:
|
||||||
|
```bash
|
||||||
|
docker-compose up -d
|
||||||
|
```
|
||||||
|
The server will be running on `http://localhost:8080`.
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
The API is minimal, just enough for the app to work. All endpoints require an `Authorization: Bearer <your-auth-token>` header.
|
||||||
|
|
||||||
|
- `GET /sync`: Download `openclimb.json`.
|
||||||
|
- `POST /sync`: Upload `openclimb.json`.
|
||||||
|
- `GET /images/{imageName}`: Download an image.
|
||||||
|
- `POST /images/{imageName}`: Upload an image.
|
||||||
|
|
||||||
|
Check out `main.go` for the full details.
|
||||||
Reference in New Issue
Block a user