Compare commits
37 Commits
ANDROID_1.
...
IOS
| Author | SHA1 | Date | |
|---|---|---|---|
| 869ca0fc0d | |||
| 33562e9d16 | |||
|
a212f3f3b5
|
|||
|
a99196b9ca
|
|||
|
93fb7a41fb
|
|||
|
6d67ae6d81
|
|||
| 071e47f95e | |||
| c6c3e6084b | |||
|
c2f95f2793
|
|||
|
b7a3c98b2c
|
|||
|
fed9bab2ea
|
|||
|
862622b07b
|
|||
|
eba503eb5e
|
|||
|
8c4a78ad50
|
|||
|
3b16475dc6
|
|||
|
105d39689d
|
|||
|
d4023133b7
|
|||
|
602b5f8938
|
|||
| 8529f76c22 | |||
|
879aae0721
|
|||
|
6fc86558b2
|
|||
|
23de8a6fc6
|
|||
|
01d85a4add
|
|||
|
ef1cf3583a
|
|||
|
3c7290f7e7
|
|||
|
5a49b9f0b2
|
|||
| 7601e7bb03 | |||
| 082cb79630 | |||
| 79b87a088d | |||
| 1a8f41ecde | |||
|
175dad8342
|
|||
|
4559d5e2f2
|
|||
|
d57149d29c
|
|||
|
d5cf14d466
|
|||
|
09b4055985
|
|||
|
30d2b3938e
|
|||
|
405fb06d5d
|
38
.github/workflows/deploy_docs.yml
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
name: Ascently - Docs Deploy
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
paths: ["docs/**"]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
paths: ["docs/**"]
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Login to Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ secrets.REPO_HOST }}
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.DEPLOY_TOKEN }}
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: ./docs
|
||||
platforms: linux/amd64
|
||||
push: true
|
||||
tags: |
|
||||
${{ secrets.REPO_HOST }}/${{ github.repository_owner }}/ascently-docs:${{ github.sha }}
|
||||
${{ secrets.REPO_HOST }}/${{ github.repository_owner }}/ascently-docs:latest
|
||||
@@ -1,4 +1,4 @@
|
||||
name: OpenClimb Docker Deploy
|
||||
name: Ascently - Sync Deploy
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
@@ -14,7 +14,7 @@ jobs:
|
||||
packages: write
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v2
|
||||
@@ -27,12 +27,12 @@ jobs:
|
||||
password: ${{ secrets.DEPLOY_TOKEN }}
|
||||
|
||||
- name: Build and push sync-server
|
||||
uses: docker/build-push-action@v4
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: ./sync
|
||||
file: ./sync/Dockerfile
|
||||
platforms: linux/amd64
|
||||
push: true
|
||||
tags: |
|
||||
${{ secrets.REPO_HOST }}/${{ github.repository_owner }}/openclimb-sync:${{ github.sha }}
|
||||
${{ secrets.REPO_HOST }}/${{ github.repository_owner }}/openclimb-sync:latest
|
||||
${{ secrets.REPO_HOST }}/${{ github.repository_owner }}/ascently-sync:${{ github.sha }}
|
||||
${{ secrets.REPO_HOST }}/${{ github.repository_owner }}/ascently-sync:latest
|
||||
21
PRIVACY.md
@@ -1,21 +0,0 @@
|
||||
# Privacy Policy
|
||||
|
||||
**Last updated: September 29, 2025**
|
||||
|
||||
This Privacy Policy describes our policies and procedures regarding the collection, use, and disclosure of your information when you use my software.
|
||||
|
||||
## No Data Collection
|
||||
|
||||
I do not collect any personal information, analytics, or data of any kind. This software is designed to be self-hosted or run entirely offline.
|
||||
|
||||
All data generated by or used with this software remains on your local machine or self-hosted environment under your control. I have no access to it.
|
||||
|
||||
## No Tracking or Analytics
|
||||
|
||||
This software does not use cookies, tracking pixels, or any other analytics or tracking mechanisms. Your usage of the software is completely private.
|
||||
|
||||
## Contact Us
|
||||
|
||||
If you have any questions about this Privacy Policy, you can contact me:
|
||||
|
||||
* **By email:** me@atri.dad
|
||||
40
README.md
@@ -1,42 +1,12 @@
|
||||
# OpenClimb
|
||||
# Ascently
|
||||
|
||||
This is a FOSS app meant to help climbers track their sessions, routes/problems, and overall progress. This app is offline-only and requires no special permissions to run. Its built using Jetpack Compose with Material You support on Android and SwiftUI on iOS.
|
||||
_Formerly OpenClimb_
|
||||
|
||||
## Download
|
||||
Ascently is an **offline-first FOSS** app designed to help climbers track their sessions, routes/problems, and overall progress. There is an optional self-hosted sync server and integrations with Apple Health and Health Connect. There are no analytics or tracking baked into any part of this project. I am committed to maintaining a transparent and open-source solution for climbers, ensuring that you have full control over your data and privacy.
|
||||
|
||||
For Android do one of the following:
|
||||
## Documentation
|
||||
|
||||
1. Download the latest APK from the Releases page
|
||||
2. [<img src="https://github.com/ImranR98/Obtainium/blob/main/assets/graphics/badge_obtainium.png?raw=true" alt="Obtainium" height="41">](https://apps.obtainium.imranr.dev/redirect?r=obtainium://app/%7B%22id%22%3A%22com.atridad.openclimb%22%2C%22url%22%3A%22https%3A%2F%2Fgit.atri.dad%2Fatridad%2FOpenClimb%2Freleases%22%2C%22author%22%3A%22git.atri.dad%22%2C%22name%22%3A%22OpenClimb%22%2C%22preferredApkIndex%22%3A0%2C%22additionalSettings%22%3A%22%7B%5C%22intermediateLink%5C%22%3A%5B%5D%2C%5C%22customLinkFilterRegex%5C%22%3A%5C%22%5C%22%2C%5C%22filterByLinkText%5C%22%3Afalse%2C%5C%22skipSort%5C%22%3Afalse%2C%5C%22reverseSort%5C%22%3Afalse%2C%5C%22sortByLastLinkSegment%5C%22%3Afalse%2C%5C%22versionExtractWholePage%5C%22%3Afalse%2C%5C%22requestHeader%5C%22%3A%5B%7B%5C%22requestHeader%5C%22%3A%5C%22User-Agent%3A%20Mozilla%2F5.0%20(Linux%3B%20Android%2010%3B%20K)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F114.0.0.0%20Mobile%20Safari%2F537.36%5C%22%7D%5D%2C%5C%22defaultPseudoVersioningMethod%5C%22%3A%5C%22partialAPKHash%5C%22%2C%5C%22trackOnly%5C%22%3Afalse%2C%5C%22versionExtractionRegEx%5C%22%3A%5C%22%5C%22%2C%5C%22matchGroupToUse%5C%22%3A%5C%22%5C%22%2C%5C%22versionDetection%5C%22%3Afalse%2C%5C%22useVersionCodeAsOSVersion%5C%22%3Afalse%2C%5C%22apkFilterRegEx%5C%22%3A%5C%22%5C%22%2C%5C%22invertAPKFilter%5C%22%3Afalse%2C%5C%22autoApkFilterByArch%5C%22%3Atrue%2C%5C%22appName%5C%22%3A%5C%22OpenClimb%5C%22%2C%5C%22appAuthor%5C%22%3A%5C%22%5C%22%2C%5C%22shizukuPretendToBeGooglePlay%5C%22%3Afalse%2C%5C%22allowInsecure%5C%22%3Afalse%2C%5C%22exemptFromBackgroundUpdates%5C%22%3Afalse%2C%5C%22skipUpdateNotifications%5C%22%3Afalse%2C%5C%22about%5C%22%3A%5C%22%5C%22%2C%5C%22refreshBeforeDownload%5C%22%3Afalse%7D%22%2C%22overrideSource%22%3Anull%7D)
|
||||
|
||||
For iOS:
|
||||
|
||||
Download from the AppStore [here](https://apps.apple.com/ca/app/openclimb/id6752592783)!
|
||||
For development builds, sign up for the TestFlight [here](https://testflight.apple.com/join/88RtxV4J)!
|
||||
|
||||
## Self-Hosted Sync Server
|
||||
|
||||
You can run your own sync server to keep your data in sync across devices. The server is lightweight and easy to set up using Docker.
|
||||
|
||||
### Quick Start with Docker Compose
|
||||
|
||||
1. Create a `.env` file with your configuration:
|
||||
```
|
||||
IMAGE=git.atri.dad/atridad/openclimb-sync:latest
|
||||
APP_PORT=8080
|
||||
AUTH_TOKEN=your-secure-auth-token-here
|
||||
DATA_FILE=/data/openclimb.json
|
||||
IMAGES_DIR=/data/images
|
||||
ROOT_DIR=./openclimb-data
|
||||
```
|
||||
|
||||
2. Use the provided `docker-compose.yml` in the `sync/` directory:
|
||||
```bash
|
||||
cd sync/
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
The server will be available at `http://localhost:8080`. Configure your clients with your server URL and auth token to start syncing.
|
||||
Documentation can be found at [https://ascently.atri.dad](https://ascently.atri.dad)!
|
||||
|
||||
## Requirements
|
||||
|
||||
|
||||
20
android/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Ascently for Android
|
||||
|
||||
This is the native Android app for Ascently, 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/ascently/`.
|
||||
|
||||
- `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.
|
||||
@@ -9,15 +9,15 @@ plugins {
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "com.atridad.openclimb"
|
||||
namespace = "com.atridad.ascently"
|
||||
compileSdk = 36
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "com.atridad.openclimb"
|
||||
applicationId = "com.atridad.ascently"
|
||||
minSdk = 31
|
||||
targetSdk = 36
|
||||
versionCode = 37
|
||||
versionName = "1.9.0"
|
||||
versionCode = 4
|
||||
versionName = "2.3.0"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
@@ -38,7 +38,10 @@ android {
|
||||
|
||||
java { toolchain { languageVersion.set(JavaLanguageVersion.of(17)) } }
|
||||
|
||||
buildFeatures { compose = true }
|
||||
buildFeatures {
|
||||
compose = true
|
||||
buildConfig = true
|
||||
}
|
||||
}
|
||||
|
||||
kotlin { compilerOptions { jvmTarget.set(JvmTarget.JVM_17) } }
|
||||
|
||||
@@ -9,11 +9,11 @@ plugins {
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "com.atridad.openclimb"
|
||||
namespace = "com.atridad.ascently"
|
||||
compileSdk = 36
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "com.atridad.openclimb"
|
||||
applicationId = "com.atridad.ascently"
|
||||
minSdk = 31
|
||||
targetSdk = 36
|
||||
versionCode = 27
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
<!-- Permission for sync functionality -->
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
<!-- Health Connect permissions -->
|
||||
<uses-permission android:name="android.permission.health.READ_EXERCISE" />
|
||||
@@ -49,13 +50,13 @@
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.OpenClimb"
|
||||
android:theme="@style/Theme.Ascently"
|
||||
tools:targetApi="31">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/Theme.OpenClimb.Splash">
|
||||
android:theme="@style/Theme.Ascently.Splash">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb
|
||||
package com.atridad.ascently
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
@@ -9,8 +9,9 @@ import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import com.atridad.openclimb.ui.OpenClimbApp
|
||||
import com.atridad.openclimb.ui.theme.OpenClimbTheme
|
||||
import com.atridad.ascently.ui.AscentlyApp
|
||||
import com.atridad.ascently.ui.theme.AscentlyTheme
|
||||
import com.atridad.ascently.utils.MigrationManager
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private var shortcutAction by mutableStateOf<String?>(null)
|
||||
@@ -23,16 +24,19 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setTheme(R.style.Theme_OpenClimb)
|
||||
setTheme(R.style.Theme_Ascently)
|
||||
enableEdgeToEdge()
|
||||
|
||||
// Perform migration from OpenClimb to Ascently if needed
|
||||
MigrationManager(this).migrateIfNeeded()
|
||||
|
||||
shortcutAction = intent?.action
|
||||
lastUsedGymId = intent?.getStringExtra("LAST_USED_GYM_ID")
|
||||
|
||||
setContent {
|
||||
OpenClimbTheme {
|
||||
AscentlyTheme {
|
||||
Surface(modifier = Modifier.fillMaxSize()) {
|
||||
OpenClimbApp(
|
||||
AscentlyApp(
|
||||
shortcutAction = shortcutAction,
|
||||
lastUsedGymId = lastUsedGymId,
|
||||
onShortcutActionProcessed = { clearShortcutAction() }
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.atridad.openclimb.data.database
|
||||
package com.atridad.ascently.data.database
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import com.atridad.openclimb.data.model.*
|
||||
import com.atridad.ascently.data.model.*
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.data.database
|
||||
package com.atridad.ascently.data.database
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
@@ -7,8 +7,8 @@ import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.atridad.openclimb.data.database.dao.*
|
||||
import com.atridad.openclimb.data.model.*
|
||||
import com.atridad.ascently.data.database.dao.*
|
||||
import com.atridad.ascently.data.model.*
|
||||
|
||||
@Database(
|
||||
entities = [Gym::class, Problem::class, ClimbSession::class, Attempt::class],
|
||||
@@ -16,7 +16,7 @@ import com.atridad.openclimb.data.model.*
|
||||
exportSchema = false
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
abstract class OpenClimbDatabase : RoomDatabase() {
|
||||
abstract class AscentlyDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun gymDao(): GymDao
|
||||
abstract fun problemDao(): ProblemDao
|
||||
@@ -24,12 +24,12 @@ abstract class OpenClimbDatabase : RoomDatabase() {
|
||||
abstract fun attemptDao(): AttemptDao
|
||||
|
||||
companion object {
|
||||
@Volatile private var INSTANCE: OpenClimbDatabase? = null
|
||||
@Volatile private var INSTANCE: AscentlyDatabase? = null
|
||||
|
||||
val MIGRATION_4_5 =
|
||||
object : Migration(4, 5) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
val cursor = database.query("PRAGMA table_info(climb_sessions)")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
val cursor = db.query("PRAGMA table_info(climb_sessions)")
|
||||
val existingColumns = mutableSetOf<String>()
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
@@ -39,21 +39,21 @@ abstract class OpenClimbDatabase : RoomDatabase() {
|
||||
cursor.close()
|
||||
|
||||
if (!existingColumns.contains("startTime")) {
|
||||
database.execSQL("ALTER TABLE climb_sessions ADD COLUMN startTime TEXT")
|
||||
db.execSQL("ALTER TABLE climb_sessions ADD COLUMN startTime TEXT")
|
||||
}
|
||||
if (!existingColumns.contains("endTime")) {
|
||||
database.execSQL("ALTER TABLE climb_sessions ADD COLUMN endTime TEXT")
|
||||
db.execSQL("ALTER TABLE climb_sessions ADD COLUMN endTime TEXT")
|
||||
}
|
||||
if (!existingColumns.contains("status")) {
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"ALTER TABLE climb_sessions ADD COLUMN status TEXT NOT NULL DEFAULT 'COMPLETED'"
|
||||
)
|
||||
}
|
||||
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"UPDATE climb_sessions SET startTime = createdAt WHERE startTime IS NULL"
|
||||
)
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"UPDATE climb_sessions SET status = 'COMPLETED' WHERE status IS NULL OR status = ''"
|
||||
)
|
||||
}
|
||||
@@ -61,9 +61,9 @@ abstract class OpenClimbDatabase : RoomDatabase() {
|
||||
|
||||
val MIGRATION_5_6 =
|
||||
object : Migration(5, 6) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
// Add updatedAt column to attempts table
|
||||
val cursor = database.query("PRAGMA table_info(attempts)")
|
||||
val cursor = db.query("PRAGMA table_info(attempts)")
|
||||
val existingColumns = mutableSetOf<String>()
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
@@ -73,29 +73,29 @@ abstract class OpenClimbDatabase : RoomDatabase() {
|
||||
cursor.close()
|
||||
|
||||
if (!existingColumns.contains("updatedAt")) {
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"ALTER TABLE attempts ADD COLUMN updatedAt TEXT NOT NULL DEFAULT ''"
|
||||
)
|
||||
// Set updatedAt to createdAt for existing records
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"UPDATE attempts SET updatedAt = createdAt WHERE updatedAt = ''"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getDatabase(context: Context): OpenClimbDatabase {
|
||||
fun getDatabase(context: Context): AscentlyDatabase {
|
||||
return INSTANCE
|
||||
?: synchronized(this) {
|
||||
val instance =
|
||||
Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
OpenClimbDatabase::class.java,
|
||||
"openclimb_database"
|
||||
)
|
||||
.addMigrations(MIGRATION_4_5, MIGRATION_5_6)
|
||||
.enableMultiInstanceInvalidation()
|
||||
.fallbackToDestructiveMigration()
|
||||
Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
AscentlyDatabase::class.java,
|
||||
"ascently_database"
|
||||
)
|
||||
.addMigrations(MIGRATION_4_5, MIGRATION_5_6)
|
||||
.enableMultiInstanceInvalidation()
|
||||
.fallbackToDestructiveMigration(false)
|
||||
.build()
|
||||
INSTANCE = instance
|
||||
instance
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.atridad.openclimb.data.database.dao
|
||||
package com.atridad.ascently.data.database.dao
|
||||
|
||||
import androidx.room.*
|
||||
import com.atridad.openclimb.data.model.Attempt
|
||||
import com.atridad.openclimb.data.model.AttemptResult
|
||||
import com.atridad.ascently.data.model.Attempt
|
||||
import com.atridad.ascently.data.model.AttemptResult
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.atridad.openclimb.data.database.dao
|
||||
package com.atridad.ascently.data.database.dao
|
||||
|
||||
import androidx.room.*
|
||||
import com.atridad.openclimb.data.model.ClimbSession
|
||||
import com.atridad.openclimb.data.model.SessionStatus
|
||||
import com.atridad.ascently.data.model.ClimbSession
|
||||
import com.atridad.ascently.data.model.SessionStatus
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.atridad.openclimb.data.database.dao
|
||||
package com.atridad.ascently.data.database.dao
|
||||
|
||||
import androidx.room.*
|
||||
import com.atridad.openclimb.data.model.ClimbType
|
||||
import com.atridad.openclimb.data.model.Gym
|
||||
import com.atridad.ascently.data.model.ClimbType
|
||||
import com.atridad.ascently.data.model.Gym
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.atridad.openclimb.data.database.dao
|
||||
package com.atridad.ascently.data.database.dao
|
||||
|
||||
import androidx.room.*
|
||||
import com.atridad.openclimb.data.model.ClimbType
|
||||
import com.atridad.openclimb.data.model.Problem
|
||||
import com.atridad.ascently.data.model.ClimbType
|
||||
import com.atridad.ascently.data.model.Problem
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.atridad.openclimb.data.format
|
||||
package com.atridad.ascently.data.format
|
||||
|
||||
import com.atridad.openclimb.data.model.*
|
||||
import com.atridad.ascently.data.model.*
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
// Root structure for OpenClimb backup data
|
||||
// Root structure for Ascently backup data
|
||||
@Serializable
|
||||
data class ClimbDataBackup(
|
||||
val exportedAt: String,
|
||||
@@ -32,13 +32,12 @@ data class BackupGym(
|
||||
val supportedClimbTypes: List<ClimbType>,
|
||||
val difficultySystems: List<DifficultySystem>,
|
||||
@kotlinx.serialization.SerialName("customDifficultyGrades")
|
||||
val customDifficultyGrades: List<String> = emptyList(),
|
||||
val customDifficultyGrades: List<String>? = null,
|
||||
val notes: String? = null,
|
||||
val createdAt: String,
|
||||
val updatedAt: String
|
||||
) {
|
||||
companion object {
|
||||
/** Create BackupGym from native Android Gym model */
|
||||
fun fromGym(gym: Gym): BackupGym {
|
||||
return BackupGym(
|
||||
id = gym.id,
|
||||
@@ -46,7 +45,7 @@ data class BackupGym(
|
||||
location = gym.location,
|
||||
supportedClimbTypes = gym.supportedClimbTypes,
|
||||
difficultySystems = gym.difficultySystems,
|
||||
customDifficultyGrades = gym.customDifficultyGrades,
|
||||
customDifficultyGrades = gym.customDifficultyGrades.ifEmpty { null },
|
||||
notes = gym.notes,
|
||||
createdAt = gym.createdAt,
|
||||
updatedAt = gym.updatedAt
|
||||
@@ -54,7 +53,6 @@ data class BackupGym(
|
||||
}
|
||||
}
|
||||
|
||||
/** Convert to native Android Gym model */
|
||||
fun toGym(): Gym {
|
||||
return Gym(
|
||||
id = id,
|
||||
@@ -62,7 +60,7 @@ data class BackupGym(
|
||||
location = location,
|
||||
supportedClimbTypes = supportedClimbTypes,
|
||||
difficultySystems = difficultySystems,
|
||||
customDifficultyGrades = customDifficultyGrades,
|
||||
customDifficultyGrades = customDifficultyGrades ?: emptyList(),
|
||||
notes = notes,
|
||||
createdAt = createdAt,
|
||||
updatedAt = updatedAt
|
||||
@@ -79,7 +77,7 @@ data class BackupProblem(
|
||||
val description: String? = null,
|
||||
val climbType: ClimbType,
|
||||
val difficulty: DifficultyGrade,
|
||||
val tags: List<String> = emptyList(),
|
||||
val tags: List<String>? = null,
|
||||
val location: String? = null,
|
||||
val imagePaths: List<String>? = null,
|
||||
val isActive: Boolean = true,
|
||||
@@ -89,7 +87,6 @@ data class BackupProblem(
|
||||
val updatedAt: String
|
||||
) {
|
||||
companion object {
|
||||
/** Create BackupProblem from native Android Problem model */
|
||||
fun fromProblem(problem: Problem): BackupProblem {
|
||||
return BackupProblem(
|
||||
id = problem.id,
|
||||
@@ -112,7 +109,6 @@ data class BackupProblem(
|
||||
}
|
||||
}
|
||||
|
||||
/** Convert to native Android Problem model */
|
||||
fun toProblem(): Problem {
|
||||
return Problem(
|
||||
id = id,
|
||||
@@ -121,7 +117,7 @@ data class BackupProblem(
|
||||
description = description,
|
||||
climbType = climbType,
|
||||
difficulty = difficulty,
|
||||
tags = tags,
|
||||
tags = tags ?: emptyList(),
|
||||
location = location,
|
||||
imagePaths = imagePaths ?: emptyList(),
|
||||
isActive = isActive,
|
||||
@@ -132,7 +128,6 @@ data class BackupProblem(
|
||||
)
|
||||
}
|
||||
|
||||
/** Create a copy with updated image paths for import processing */
|
||||
fun withUpdatedImagePaths(newImagePaths: List<String>): BackupProblem {
|
||||
return copy(imagePaths = newImagePaths.ifEmpty { null })
|
||||
}
|
||||
@@ -153,7 +148,6 @@ data class BackupClimbSession(
|
||||
val updatedAt: String
|
||||
) {
|
||||
companion object {
|
||||
/** Create BackupClimbSession from native Android ClimbSession model */
|
||||
fun fromClimbSession(session: ClimbSession): BackupClimbSession {
|
||||
return BackupClimbSession(
|
||||
id = session.id,
|
||||
@@ -170,7 +164,6 @@ data class BackupClimbSession(
|
||||
}
|
||||
}
|
||||
|
||||
/** Convert to native Android ClimbSession model */
|
||||
fun toClimbSession(): ClimbSession {
|
||||
return ClimbSession(
|
||||
id = id,
|
||||
@@ -203,7 +196,6 @@ data class BackupAttempt(
|
||||
val updatedAt: String? = null
|
||||
) {
|
||||
companion object {
|
||||
/** Create BackupAttempt from native Android Attempt model */
|
||||
fun fromAttempt(attempt: Attempt): BackupAttempt {
|
||||
return BackupAttempt(
|
||||
id = attempt.id,
|
||||
@@ -221,7 +213,6 @@ data class BackupAttempt(
|
||||
}
|
||||
}
|
||||
|
||||
/** Convert to native Android Attempt model */
|
||||
fun toAttempt(): Attempt {
|
||||
return Attempt(
|
||||
id = id,
|
||||
@@ -0,0 +1,338 @@
|
||||
package com.atridad.ascently.data.health
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import com.atridad.ascently.utils.AppLogger
|
||||
import androidx.activity.result.contract.ActivityResultContract
|
||||
import androidx.health.connect.client.HealthConnectClient
|
||||
import androidx.health.connect.client.PermissionController
|
||||
import androidx.health.connect.client.permission.HealthPermission
|
||||
import androidx.health.connect.client.records.ExerciseSessionRecord
|
||||
import androidx.health.connect.client.records.HeartRateRecord
|
||||
import androidx.health.connect.client.records.TotalCaloriesBurnedRecord
|
||||
import androidx.health.connect.client.units.Energy
|
||||
import com.atridad.ascently.data.model.ClimbSession
|
||||
import com.atridad.ascently.data.model.SessionStatus
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import java.time.Duration
|
||||
import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
|
||||
/**
|
||||
* Health Connect manager for Ascently that syncs climbing sessions to Samsung Health, Google Fit,
|
||||
* and other health apps.
|
||||
*/
|
||||
@SuppressLint("RestrictedApi")
|
||||
class HealthConnectManager(private val context: Context) {
|
||||
|
||||
private val preferences: SharedPreferences =
|
||||
context.getSharedPreferences("health_connect_prefs", Context.MODE_PRIVATE)
|
||||
|
||||
private val _isEnabled = MutableStateFlow(preferences.getBoolean("enabled", false))
|
||||
private val _hasPermissions = MutableStateFlow(preferences.getBoolean("permissions", false))
|
||||
private val _autoSync = MutableStateFlow(preferences.getBoolean("auto_sync", true))
|
||||
private val _isCompatible = MutableStateFlow(true)
|
||||
|
||||
val isEnabled: Flow<Boolean> = _isEnabled.asStateFlow()
|
||||
val hasPermissions: Flow<Boolean> = _hasPermissions.asStateFlow()
|
||||
val isCompatible: Flow<Boolean> = _isCompatible.asStateFlow()
|
||||
|
||||
companion object {
|
||||
private const val TAG = "HealthConnectManager"
|
||||
|
||||
val REQUIRED_PERMISSIONS =
|
||||
setOf(
|
||||
HealthPermission.getReadPermission(ExerciseSessionRecord::class),
|
||||
HealthPermission.getWritePermission(ExerciseSessionRecord::class),
|
||||
HealthPermission.getReadPermission(HeartRateRecord::class),
|
||||
HealthPermission.getWritePermission(HeartRateRecord::class),
|
||||
HealthPermission.getReadPermission(TotalCaloriesBurnedRecord::class),
|
||||
HealthPermission.getWritePermission(TotalCaloriesBurnedRecord::class)
|
||||
)
|
||||
}
|
||||
|
||||
private val healthConnectClient by lazy {
|
||||
try {
|
||||
HealthConnectClient.getOrCreate(context)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(TAG, e) { "Failed to create Health Connect client" }
|
||||
_isCompatible.value = false
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun isHealthConnectAvailable(): Flow<Boolean> = flow {
|
||||
try {
|
||||
if (!_isCompatible.value) {
|
||||
emit(false)
|
||||
return@flow
|
||||
}
|
||||
val status = HealthConnectClient.getSdkStatus(context)
|
||||
emit(status == HealthConnectClient.SDK_AVAILABLE)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(TAG, e) { "Error checking Health Connect availability" }
|
||||
_isCompatible.value = false
|
||||
emit(false)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setEnabled(enabled: Boolean) {
|
||||
preferences.edit().putBoolean("enabled", enabled).apply()
|
||||
_isEnabled.value = enabled
|
||||
|
||||
if (enabled && _isCompatible.value) {
|
||||
// Automatically request permissions when enabling
|
||||
try {
|
||||
val alreadyHasPermissions = hasAllPermissions()
|
||||
if (!alreadyHasPermissions) {
|
||||
AppLogger.d(TAG) { "Health Connect enabled - permissions will be requested by UI" }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLogger.w(TAG, e) { "Error checking permissions when enabling Health Connect" }
|
||||
}
|
||||
} else if (!enabled) {
|
||||
setPermissionsGranted(false)
|
||||
}
|
||||
}
|
||||
|
||||
fun setPermissionsGranted(granted: Boolean) {
|
||||
preferences.edit().putBoolean("permissions", granted).apply()
|
||||
_hasPermissions.value = granted
|
||||
}
|
||||
|
||||
suspend fun hasAllPermissions(): Boolean {
|
||||
return try {
|
||||
if (!_isCompatible.value || healthConnectClient == null) {
|
||||
return false
|
||||
}
|
||||
val grantedPermissions =
|
||||
healthConnectClient!!.permissionController.getGrantedPermissions()
|
||||
val hasAll =
|
||||
REQUIRED_PERMISSIONS.all { permission ->
|
||||
grantedPermissions.contains(permission)
|
||||
}
|
||||
setPermissionsGranted(hasAll)
|
||||
hasAll
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(TAG, e) { "Error checking permissions" }
|
||||
setPermissionsGranted(false)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun isReady(): Boolean {
|
||||
return try {
|
||||
if (!_isEnabled.value || !_isCompatible.value || healthConnectClient == null)
|
||||
return false
|
||||
|
||||
val isAvailable =
|
||||
HealthConnectClient.getSdkStatus(context) == HealthConnectClient.SDK_AVAILABLE
|
||||
val hasPerms = if (isAvailable) hasAllPermissions() else false
|
||||
isAvailable && hasPerms
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(TAG, e) { "Error checking Health Connect readiness" }
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun getPermissionRequestContract(): ActivityResultContract<Set<String>, Set<String>> {
|
||||
return PermissionController.createRequestPermissionResultContract()
|
||||
}
|
||||
|
||||
fun getRequiredPermissions(): Set<String> {
|
||||
return try {
|
||||
REQUIRED_PERMISSIONS.map { it }.toSet()
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(TAG, e) { "Error getting required permissions" }
|
||||
emptySet()
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
suspend fun syncCompletedSession(
|
||||
session: ClimbSession,
|
||||
gymName: String,
|
||||
attemptCount: Int = 0
|
||||
): Result<Unit> {
|
||||
return try {
|
||||
if (!isReady() || !_autoSync.value) {
|
||||
return Result.failure(
|
||||
IllegalStateException("Health Connect not ready or auto-sync disabled")
|
||||
)
|
||||
}
|
||||
|
||||
if (session.status != SessionStatus.COMPLETED) {
|
||||
return Result.failure(
|
||||
IllegalArgumentException("Only completed sessions can be synced")
|
||||
)
|
||||
}
|
||||
|
||||
val startTime = session.startTime?.let { DateFormatUtils.parseISO8601(it) }
|
||||
val endTime = session.endTime?.let { DateFormatUtils.parseISO8601(it) }
|
||||
|
||||
if (startTime == null || endTime == null) {
|
||||
return Result.failure(
|
||||
IllegalArgumentException("Session must have valid start and end times")
|
||||
)
|
||||
}
|
||||
|
||||
AppLogger.d(TAG) { "Attempting to sync session '${session.id}' to Health Connect..." }
|
||||
|
||||
val records = mutableListOf<androidx.health.connect.client.records.Record>()
|
||||
|
||||
try {
|
||||
val exerciseSession =
|
||||
ExerciseSessionRecord(
|
||||
startTime = startTime,
|
||||
startZoneOffset =
|
||||
ZoneOffset.systemDefault().rules.getOffset(startTime),
|
||||
endTime = endTime,
|
||||
endZoneOffset = ZoneOffset.systemDefault().rules.getOffset(endTime),
|
||||
exerciseType =
|
||||
ExerciseSessionRecord.EXERCISE_TYPE_STRENGTH_TRAINING,
|
||||
title = "Rock Climbing at $gymName"
|
||||
)
|
||||
records.add(exerciseSession)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.w(TAG, e) { "Failed to create exercise session record" }
|
||||
}
|
||||
|
||||
try {
|
||||
val durationMinutes = Duration.between(startTime, endTime).toMinutes()
|
||||
val estimatedCalories = estimateCaloriesForClimbing(durationMinutes, attemptCount)
|
||||
|
||||
if (estimatedCalories > 0) {
|
||||
val caloriesRecord =
|
||||
TotalCaloriesBurnedRecord(
|
||||
startTime = startTime,
|
||||
startZoneOffset =
|
||||
ZoneOffset.systemDefault().rules.getOffset(startTime),
|
||||
endTime = endTime,
|
||||
endZoneOffset =
|
||||
ZoneOffset.systemDefault().rules.getOffset(endTime),
|
||||
energy = Energy.calories(estimatedCalories)
|
||||
)
|
||||
records.add(caloriesRecord)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLogger.w(TAG, e) { "Failed to create calories record" }
|
||||
}
|
||||
|
||||
try {
|
||||
val heartRateRecord = createHeartRateRecord(startTime, endTime, attemptCount)
|
||||
heartRateRecord?.let { records.add(it) }
|
||||
} catch (e: Exception) {
|
||||
AppLogger.w(TAG, e) { "Failed to create heart rate record" }
|
||||
}
|
||||
|
||||
if (records.isNotEmpty() && healthConnectClient != null) {
|
||||
AppLogger.d(TAG) { "Writing ${records.size} records to Health Connect..." }
|
||||
healthConnectClient!!.insertRecords(records)
|
||||
AppLogger.i(TAG) {
|
||||
"Successfully synced ${records.size} records for session '${session.id}' to Health Connect"
|
||||
}
|
||||
|
||||
preferences
|
||||
.edit()
|
||||
.putString("last_sync_success", DateFormatUtils.nowISO8601())
|
||||
.apply()
|
||||
} else {
|
||||
val reason =
|
||||
when {
|
||||
records.isEmpty() -> "No records created"
|
||||
healthConnectClient == null -> "Health Connect client unavailable"
|
||||
else -> "Unknown reason"
|
||||
}
|
||||
AppLogger.w(TAG) { "Sync failed for session '${session.id}': $reason" }
|
||||
return Result.failure(Exception("Sync failed: $reason"))
|
||||
}
|
||||
|
||||
Result.success(Unit)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(TAG, e) { "Error syncing climbing session to Health Connect" }
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun autoSyncCompletedSession(
|
||||
session: ClimbSession,
|
||||
gymName: String,
|
||||
attemptCount: Int = 0
|
||||
): Result<Unit> {
|
||||
return if (_autoSync.value && isReady() && session.status == SessionStatus.COMPLETED) {
|
||||
AppLogger.d(TAG) { "Auto-syncing completed session '${session.id}' to Health Connect..." }
|
||||
syncCompletedSession(session, gymName, attemptCount)
|
||||
} else {
|
||||
val reason =
|
||||
when {
|
||||
session.status != SessionStatus.COMPLETED -> "session not completed"
|
||||
!_autoSync.value -> "auto-sync disabled"
|
||||
!isReady() -> "Health Connect not ready"
|
||||
else -> "unknown reason"
|
||||
}
|
||||
AppLogger.d(TAG) { "Auto-sync skipped for session '${session.id}': $reason" }
|
||||
Result.success(Unit)
|
||||
}
|
||||
}
|
||||
|
||||
private fun estimateCaloriesForClimbing(durationMinutes: Long, attemptCount: Int): Double {
|
||||
val baseCaloriesPerMinute = 8.0
|
||||
val intensityMultiplier =
|
||||
when {
|
||||
attemptCount >= 20 -> 1.3
|
||||
attemptCount >= 10 -> 1.1
|
||||
else -> 0.9
|
||||
}
|
||||
return durationMinutes * baseCaloriesPerMinute * intensityMultiplier
|
||||
}
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
private fun createHeartRateRecord(
|
||||
startTime: Instant,
|
||||
endTime: Instant,
|
||||
attemptCount: Int
|
||||
): HeartRateRecord? {
|
||||
return try {
|
||||
val samples = mutableListOf<HeartRateRecord.Sample>()
|
||||
val intervalMinutes = 5L
|
||||
|
||||
val baseHeartRate =
|
||||
when {
|
||||
attemptCount >= 20 -> 155L
|
||||
attemptCount >= 10 -> 145L
|
||||
else -> 135L
|
||||
}
|
||||
|
||||
var currentTime = startTime
|
||||
while (currentTime.isBefore(endTime)) {
|
||||
val variation = (-15..15).random()
|
||||
val heartRate = (baseHeartRate + variation).coerceIn(110L, 180L)
|
||||
|
||||
samples.add(HeartRateRecord.Sample(time = currentTime, beatsPerMinute = heartRate))
|
||||
currentTime = currentTime.plusSeconds(intervalMinutes * 60)
|
||||
}
|
||||
|
||||
if (samples.isEmpty()) return null
|
||||
|
||||
HeartRateRecord(
|
||||
startTime = startTime,
|
||||
startZoneOffset = ZoneOffset.systemDefault().rules.getOffset(startTime),
|
||||
endTime = endTime,
|
||||
endZoneOffset = ZoneOffset.systemDefault().rules.getOffset(endTime),
|
||||
samples = samples
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(TAG, e) { "Error creating heart rate record" }
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun isReadySync(): Boolean {
|
||||
return _isEnabled.value && _hasPermissions.value
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.atridad.openclimb.data.model
|
||||
package com.atridad.ascently.data.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import com.atridad.openclimb.utils.DateFormatUtils
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
@@ -12,7 +12,7 @@ enum class AttemptResult {
|
||||
SUCCESS,
|
||||
FALL,
|
||||
NO_PROGRESS,
|
||||
FLASH,
|
||||
FLASH
|
||||
}
|
||||
|
||||
@Entity(
|
||||
@@ -74,26 +74,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()
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.atridad.openclimb.data.model
|
||||
package com.atridad.ascently.data.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import com.atridad.openclimb.utils.DateFormatUtils
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.atridad.ascently.data.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
enum class ClimbType {
|
||||
ROPE,
|
||||
BOULDER;
|
||||
|
||||
val displayName: String
|
||||
get() =
|
||||
when (this) {
|
||||
ROPE -> "Rope"
|
||||
BOULDER -> "Bouldering"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
package com.atridad.ascently.data.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
enum class DifficultySystem {
|
||||
// Bouldering
|
||||
V_SCALE,
|
||||
FONT,
|
||||
|
||||
// Rope
|
||||
YDS,
|
||||
CUSTOM;
|
||||
|
||||
val displayName: String
|
||||
get() =
|
||||
when (this) {
|
||||
V_SCALE -> "V Scale"
|
||||
FONT -> "Font Scale"
|
||||
YDS -> "YDS (Yosemite)"
|
||||
CUSTOM -> "Custom"
|
||||
}
|
||||
|
||||
val isBoulderingSystem: Boolean
|
||||
get() =
|
||||
when (this) {
|
||||
V_SCALE, FONT -> true
|
||||
YDS -> false
|
||||
CUSTOM -> true
|
||||
}
|
||||
|
||||
val isRopeSystem: Boolean
|
||||
get() =
|
||||
when (this) {
|
||||
YDS -> true
|
||||
V_SCALE, FONT -> false
|
||||
CUSTOM -> true
|
||||
}
|
||||
|
||||
val availableGrades: List<String>
|
||||
get() =
|
||||
when (this) {
|
||||
V_SCALE ->
|
||||
listOf(
|
||||
"VB",
|
||||
"V0",
|
||||
"V1",
|
||||
"V2",
|
||||
"V3",
|
||||
"V4",
|
||||
"V5",
|
||||
"V6",
|
||||
"V7",
|
||||
"V8",
|
||||
"V9",
|
||||
"V10",
|
||||
"V11",
|
||||
"V12",
|
||||
"V13",
|
||||
"V14",
|
||||
"V15",
|
||||
"V16",
|
||||
"V17"
|
||||
)
|
||||
FONT ->
|
||||
listOf(
|
||||
"3",
|
||||
"4A",
|
||||
"4B",
|
||||
"4C",
|
||||
"5A",
|
||||
"5B",
|
||||
"5C",
|
||||
"6A",
|
||||
"6A+",
|
||||
"6B",
|
||||
"6B+",
|
||||
"6C",
|
||||
"6C+",
|
||||
"7A",
|
||||
"7A+",
|
||||
"7B",
|
||||
"7B+",
|
||||
"7C",
|
||||
"7C+",
|
||||
"8A",
|
||||
"8A+",
|
||||
"8B",
|
||||
"8B+",
|
||||
"8C",
|
||||
"8C+"
|
||||
)
|
||||
YDS ->
|
||||
listOf(
|
||||
"5.0",
|
||||
"5.1",
|
||||
"5.2",
|
||||
"5.3",
|
||||
"5.4",
|
||||
"5.5",
|
||||
"5.6",
|
||||
"5.7",
|
||||
"5.8",
|
||||
"5.9",
|
||||
"5.10a",
|
||||
"5.10b",
|
||||
"5.10c",
|
||||
"5.10d",
|
||||
"5.11a",
|
||||
"5.11b",
|
||||
"5.11c",
|
||||
"5.11d",
|
||||
"5.12a",
|
||||
"5.12b",
|
||||
"5.12c",
|
||||
"5.12d",
|
||||
"5.13a",
|
||||
"5.13b",
|
||||
"5.13c",
|
||||
"5.13d",
|
||||
"5.14a",
|
||||
"5.14b",
|
||||
"5.14c",
|
||||
"5.14d",
|
||||
"5.15a",
|
||||
"5.15b",
|
||||
"5.15c",
|
||||
"5.15d"
|
||||
)
|
||||
CUSTOM -> emptyList()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun systemsForClimbType(climbType: ClimbType): List<DifficultySystem> =
|
||||
when (climbType) {
|
||||
ClimbType.BOULDER -> entries.filter { it.isBoulderingSystem }
|
||||
ClimbType.ROPE -> entries.filter { it.isRopeSystem }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class DifficultyGrade(val system: DifficultySystem, val grade: String, val numericValue: Int) {
|
||||
|
||||
constructor(
|
||||
system: DifficultySystem,
|
||||
grade: String
|
||||
) : this(system = system, grade = grade, numericValue = calculateNumericValue(system, grade))
|
||||
|
||||
companion object {
|
||||
private fun calculateNumericValue(system: DifficultySystem, grade: String): Int {
|
||||
return when (system) {
|
||||
DifficultySystem.V_SCALE -> {
|
||||
if (grade == "VB") 0 else grade.removePrefix("V").toIntOrNull() ?: 0
|
||||
}
|
||||
DifficultySystem.FONT -> {
|
||||
val fontMapping: Map<String, Int> =
|
||||
mapOf(
|
||||
"3" to 3,
|
||||
"4A" to 4,
|
||||
"4B" to 5,
|
||||
"4C" to 6,
|
||||
"5A" to 7,
|
||||
"5B" to 8,
|
||||
"5C" to 9,
|
||||
"6A" to 10,
|
||||
"6A+" to 11,
|
||||
"6B" to 12,
|
||||
"6B+" to 13,
|
||||
"6C" to 14,
|
||||
"6C+" to 15,
|
||||
"7A" to 16,
|
||||
"7A+" to 17,
|
||||
"7B" to 18,
|
||||
"7B+" to 19,
|
||||
"7C" to 20,
|
||||
"7C+" to 21,
|
||||
"8A" to 22,
|
||||
"8A+" to 23,
|
||||
"8B" to 24,
|
||||
"8B+" to 25,
|
||||
"8C" to 26,
|
||||
"8C+" to 27
|
||||
)
|
||||
fontMapping[grade] ?: 0
|
||||
}
|
||||
DifficultySystem.YDS -> {
|
||||
val ydsMapping: Map<String, Int> =
|
||||
mapOf(
|
||||
"5.0" to 50,
|
||||
"5.1" to 51,
|
||||
"5.2" to 52,
|
||||
"5.3" to 53,
|
||||
"5.4" to 54,
|
||||
"5.5" to 55,
|
||||
"5.6" to 56,
|
||||
"5.7" to 57,
|
||||
"5.8" to 58,
|
||||
"5.9" to 59,
|
||||
"5.10a" to 60,
|
||||
"5.10b" to 61,
|
||||
"5.10c" to 62,
|
||||
"5.10d" to 63,
|
||||
"5.11a" to 64,
|
||||
"5.11b" to 65,
|
||||
"5.11c" to 66,
|
||||
"5.11d" to 67,
|
||||
"5.12a" to 68,
|
||||
"5.12b" to 69,
|
||||
"5.12c" to 70,
|
||||
"5.12d" to 71,
|
||||
"5.13a" to 72,
|
||||
"5.13b" to 73,
|
||||
"5.13c" to 74,
|
||||
"5.13d" to 75,
|
||||
"5.14a" to 76,
|
||||
"5.14b" to 77,
|
||||
"5.14c" to 78,
|
||||
"5.14d" to 79,
|
||||
"5.15a" to 80,
|
||||
"5.15b" to 81,
|
||||
"5.15c" to 82,
|
||||
"5.15d" to 83
|
||||
)
|
||||
ydsMapping[grade] ?: 0
|
||||
}
|
||||
DifficultySystem.CUSTOM -> grade.toIntOrNull() ?: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Compare this grade with another grade of the same system Returns negative if this grade is
|
||||
* easier, positive if harder, 0 if equal
|
||||
*/
|
||||
fun compareTo(other: DifficultyGrade): Int {
|
||||
if (system != other.system) return 0
|
||||
|
||||
return when (system) {
|
||||
DifficultySystem.V_SCALE -> compareVScaleGrades(grade, other.grade)
|
||||
DifficultySystem.FONT -> compareFontGrades(grade, other.grade)
|
||||
DifficultySystem.YDS -> compareYDSGrades(grade, other.grade)
|
||||
DifficultySystem.CUSTOM -> grade.compareTo(other.grade)
|
||||
}
|
||||
}
|
||||
|
||||
private fun compareVScaleGrades(grade1: String, grade2: String): Int {
|
||||
if (grade1 == "VB" && grade2 != "VB") return -1
|
||||
if (grade2 == "VB" && grade1 != "VB") return 1
|
||||
if (grade1 == "VB") return 0
|
||||
|
||||
val num1 = grade1.removePrefix("V").toIntOrNull() ?: 0
|
||||
val num2 = grade2.removePrefix("V").toIntOrNull() ?: 0
|
||||
return num1.compareTo(num2)
|
||||
}
|
||||
|
||||
private fun compareFontGrades(grade1: String, grade2: String): Int {
|
||||
return grade1.compareTo(grade2)
|
||||
}
|
||||
|
||||
private fun compareYDSGrades(grade1: String, grade2: String): Int {
|
||||
return grade1.compareTo(grade2)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.atridad.openclimb.data.model
|
||||
package com.atridad.ascently.data.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import com.atridad.openclimb.utils.DateFormatUtils
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Entity(tableName = "gyms")
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.atridad.openclimb.data.model
|
||||
package com.atridad.ascently.data.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import com.atridad.openclimb.utils.DateFormatUtils
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Entity(
|
||||
@@ -1,34 +1,33 @@
|
||||
package com.atridad.openclimb.data.repository
|
||||
package com.atridad.ascently.data.repository
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import androidx.core.content.edit
|
||||
import com.atridad.openclimb.data.database.OpenClimbDatabase
|
||||
import com.atridad.openclimb.data.format.BackupAttempt
|
||||
import com.atridad.openclimb.data.format.BackupClimbSession
|
||||
import com.atridad.openclimb.data.format.BackupGym
|
||||
import com.atridad.openclimb.data.format.BackupProblem
|
||||
import com.atridad.openclimb.data.format.ClimbDataBackup
|
||||
import com.atridad.openclimb.data.format.DeletedItem
|
||||
import com.atridad.openclimb.data.model.*
|
||||
import com.atridad.openclimb.data.state.DataStateManager
|
||||
import com.atridad.openclimb.utils.DateFormatUtils
|
||||
import com.atridad.openclimb.utils.ZipExportImportUtils
|
||||
import com.atridad.ascently.data.database.AscentlyDatabase
|
||||
import com.atridad.ascently.data.format.BackupAttempt
|
||||
import com.atridad.ascently.data.format.BackupClimbSession
|
||||
import com.atridad.ascently.data.format.BackupGym
|
||||
import com.atridad.ascently.data.format.BackupProblem
|
||||
import com.atridad.ascently.data.format.ClimbDataBackup
|
||||
import com.atridad.ascently.data.format.DeletedItem
|
||||
import com.atridad.ascently.data.model.*
|
||||
import com.atridad.ascently.data.state.DataStateManager
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import com.atridad.ascently.utils.AppLogger
|
||||
import com.atridad.ascently.utils.ZipExportImportUtils
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
class ClimbRepository(database: OpenClimbDatabase, private val context: Context) {
|
||||
class ClimbRepository(database: AscentlyDatabase, private val context: Context) {
|
||||
private val gymDao = database.gymDao()
|
||||
private val problemDao = database.problemDao()
|
||||
private val sessionDao = database.climbSessionDao()
|
||||
private val attemptDao = database.attemptDao()
|
||||
private val dataStateManager = DataStateManager(context)
|
||||
private val deletionPreferences: SharedPreferences =
|
||||
context.getSharedPreferences("deleted_items", Context.MODE_PRIVATE)
|
||||
context.getSharedPreferences("deleted_items", Context.MODE_PRIVATE)
|
||||
|
||||
private var autoSyncCallback: (() -> Unit)? = null
|
||||
|
||||
@@ -45,11 +44,13 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
dataStateManager.updateDataState()
|
||||
triggerAutoSync()
|
||||
}
|
||||
|
||||
suspend fun updateGym(gym: Gym) {
|
||||
gymDao.updateGym(gym)
|
||||
dataStateManager.updateDataState()
|
||||
triggerAutoSync()
|
||||
}
|
||||
|
||||
suspend fun deleteGym(gym: Gym) {
|
||||
gymDao.deleteGym(gym)
|
||||
trackDeletion(gym.id, "gym")
|
||||
@@ -65,10 +66,12 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
problemDao.insertProblem(problem)
|
||||
dataStateManager.updateDataState()
|
||||
}
|
||||
|
||||
suspend fun updateProblem(problem: Problem) {
|
||||
problemDao.updateProblem(problem)
|
||||
dataStateManager.updateDataState()
|
||||
}
|
||||
|
||||
suspend fun deleteProblem(problem: Problem) {
|
||||
problemDao.deleteProblem(problem)
|
||||
trackDeletion(problem.id, "problem")
|
||||
@@ -79,7 +82,8 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
fun getAllSessions(): Flow<List<ClimbSession>> = sessionDao.getAllSessions()
|
||||
suspend fun getSessionById(id: String): ClimbSession? = sessionDao.getSessionById(id)
|
||||
fun getSessionsByGym(gymId: String): Flow<List<ClimbSession>> =
|
||||
sessionDao.getSessionsByGym(gymId)
|
||||
sessionDao.getSessionsByGym(gymId)
|
||||
|
||||
suspend fun getActiveSession(): ClimbSession? = sessionDao.getActiveSession()
|
||||
fun getActiveSessionFlow(): Flow<ClimbSession?> = sessionDao.getActiveSessionFlow()
|
||||
suspend fun insertSession(session: ClimbSession) {
|
||||
@@ -90,6 +94,7 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
triggerAutoSync()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateSession(session: ClimbSession) {
|
||||
sessionDao.updateSession(session)
|
||||
dataStateManager.updateDataState()
|
||||
@@ -98,12 +103,14 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
triggerAutoSync()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun deleteSession(session: ClimbSession) {
|
||||
sessionDao.deleteSession(session)
|
||||
trackDeletion(session.id, "session")
|
||||
dataStateManager.updateDataState()
|
||||
triggerAutoSync()
|
||||
}
|
||||
|
||||
suspend fun getLastUsedGym(): Gym? {
|
||||
val recentSessions = sessionDao.getRecentSessions(1).first()
|
||||
return if (recentSessions.isNotEmpty()) {
|
||||
@@ -116,17 +123,21 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
// Attempt operations
|
||||
fun getAllAttempts(): Flow<List<Attempt>> = attemptDao.getAllAttempts()
|
||||
fun getAttemptsBySession(sessionId: String): Flow<List<Attempt>> =
|
||||
attemptDao.getAttemptsBySession(sessionId)
|
||||
attemptDao.getAttemptsBySession(sessionId)
|
||||
|
||||
fun getAttemptsByProblem(problemId: String): Flow<List<Attempt>> =
|
||||
attemptDao.getAttemptsByProblem(problemId)
|
||||
attemptDao.getAttemptsByProblem(problemId)
|
||||
|
||||
suspend fun insertAttempt(attempt: Attempt) {
|
||||
attemptDao.insertAttempt(attempt)
|
||||
dataStateManager.updateDataState()
|
||||
}
|
||||
|
||||
suspend fun updateAttempt(attempt: Attempt) {
|
||||
attemptDao.updateAttempt(attempt)
|
||||
dataStateManager.updateDataState()
|
||||
}
|
||||
|
||||
suspend fun deleteAttempt(attempt: Attempt) {
|
||||
attemptDao.deleteAttempt(attempt)
|
||||
trackDeletion(attempt.id, "attempt")
|
||||
@@ -143,38 +154,38 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
validateDataIntegrity(allGyms, allProblems, allSessions, allAttempts)
|
||||
|
||||
val backupData =
|
||||
ClimbDataBackup(
|
||||
exportedAt = DateFormatUtils.nowISO8601(),
|
||||
version = "2.0",
|
||||
formatVersion = "2.0",
|
||||
gyms = allGyms.map { BackupGym.fromGym(it) },
|
||||
problems = allProblems.map { BackupProblem.fromProblem(it) },
|
||||
sessions = allSessions.map { BackupClimbSession.fromClimbSession(it) },
|
||||
attempts = allAttempts.map { BackupAttempt.fromAttempt(it) }
|
||||
)
|
||||
ClimbDataBackup(
|
||||
exportedAt = DateFormatUtils.nowISO8601(),
|
||||
version = "2.0",
|
||||
formatVersion = "2.0",
|
||||
gyms = allGyms.map { BackupGym.fromGym(it) },
|
||||
problems = allProblems.map { BackupProblem.fromProblem(it) },
|
||||
sessions = allSessions.map { BackupClimbSession.fromClimbSession(it) },
|
||||
attempts = allAttempts.map { BackupAttempt.fromAttempt(it) }
|
||||
)
|
||||
|
||||
val referencedImagePaths = allProblems.flatMap { it.imagePaths }.toSet()
|
||||
val validImagePaths =
|
||||
referencedImagePaths
|
||||
.filter { imagePath ->
|
||||
try {
|
||||
val imageFile =
|
||||
com.atridad.openclimb.utils.ImageUtils.getImageFile(
|
||||
context,
|
||||
imagePath
|
||||
)
|
||||
imageFile.exists() && imageFile.length() > 0
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
.toSet()
|
||||
referencedImagePaths
|
||||
.filter { imagePath ->
|
||||
try {
|
||||
val imageFile =
|
||||
com.atridad.ascently.utils.ImageUtils.getImageFile(
|
||||
context,
|
||||
imagePath
|
||||
)
|
||||
imageFile.exists() && imageFile.length() > 0
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
.toSet()
|
||||
|
||||
ZipExportImportUtils.createExportZipToUri(
|
||||
context = context,
|
||||
uri = uri,
|
||||
exportData = backupData,
|
||||
referencedImagePaths = validImagePaths
|
||||
context = context,
|
||||
uri = uri,
|
||||
exportData = backupData,
|
||||
referencedImagePaths = validImagePaths
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
throw Exception("Export failed: ${e.message}")
|
||||
@@ -194,11 +205,11 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
}
|
||||
|
||||
val importData =
|
||||
try {
|
||||
json.decodeFromString<ClimbDataBackup>(importResult.jsonContent)
|
||||
} catch (e: Exception) {
|
||||
throw Exception("Invalid data format: ${e.message}")
|
||||
}
|
||||
try {
|
||||
json.decodeFromString<ClimbDataBackup>(importResult.jsonContent)
|
||||
} catch (e: Exception) {
|
||||
throw Exception("Invalid data format: ${e.message}")
|
||||
}
|
||||
|
||||
validateImportData(importData)
|
||||
|
||||
@@ -216,17 +227,17 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
}
|
||||
|
||||
val updatedBackupProblems =
|
||||
ZipExportImportUtils.updateProblemImagePaths(
|
||||
importData.problems,
|
||||
importResult.importedImagePaths
|
||||
)
|
||||
ZipExportImportUtils.updateProblemImagePaths(
|
||||
importData.problems,
|
||||
importResult.importedImagePaths
|
||||
)
|
||||
|
||||
updatedBackupProblems.forEach { backupProblem ->
|
||||
try {
|
||||
problemDao.insertProblem(backupProblem.toProblem())
|
||||
} catch (e: Exception) {
|
||||
throw Exception(
|
||||
"Failed to import problem '${backupProblem.name}': ${e.message}"
|
||||
"Failed to import problem '${backupProblem.name}': ${e.message}"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -253,26 +264,18 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the callback for auto-sync functionality. This should be called by the SyncService to
|
||||
* register itself for auto-sync triggers.
|
||||
*/
|
||||
fun setAutoSyncCallback(callback: (() -> Unit)?) {
|
||||
autoSyncCallback = callback
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers auto-sync if enabled. This is called after any data modification to keep data
|
||||
* synchronized across devices automatically.
|
||||
*/
|
||||
private fun triggerAutoSync() {
|
||||
autoSyncCallback?.invoke()
|
||||
}
|
||||
|
||||
private fun trackDeletion(itemId: String, itemType: String) {
|
||||
fun trackDeletion(itemId: String, itemType: String) {
|
||||
val currentDeletions = getDeletedItems().toMutableList()
|
||||
val newDeletion =
|
||||
DeletedItem(id = itemId, type = itemType, deletedAt = DateFormatUtils.nowISO8601())
|
||||
DeletedItem(id = itemId, type = itemType, deletedAt = DateFormatUtils.nowISO8601())
|
||||
currentDeletions.add(newDeletion)
|
||||
|
||||
val json = json.encodeToString(newDeletion)
|
||||
@@ -288,7 +291,7 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
try {
|
||||
val deletion = json.decodeFromString<DeletedItem>(value)
|
||||
deletions.add(deletion)
|
||||
} catch (e: Exception) {
|
||||
} catch (_: Exception) {
|
||||
// Invalid deletion record, ignore
|
||||
}
|
||||
}
|
||||
@@ -302,23 +305,23 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
}
|
||||
|
||||
private fun validateDataIntegrity(
|
||||
gyms: List<Gym>,
|
||||
problems: List<Problem>,
|
||||
sessions: List<ClimbSession>,
|
||||
attempts: List<Attempt>
|
||||
gyms: List<Gym>,
|
||||
problems: List<Problem>,
|
||||
sessions: List<ClimbSession>,
|
||||
attempts: List<Attempt>
|
||||
) {
|
||||
val gymIds = gyms.map { it.id }.toSet()
|
||||
val invalidProblems = problems.filter { it.gymId !in gymIds }
|
||||
if (invalidProblems.isNotEmpty()) {
|
||||
throw Exception(
|
||||
"Data integrity error: ${invalidProblems.size} problems reference non-existent gyms"
|
||||
"Data integrity error: ${invalidProblems.size} problems reference non-existent gyms"
|
||||
)
|
||||
}
|
||||
|
||||
val invalidSessions = sessions.filter { it.gymId !in gymIds }
|
||||
if (invalidSessions.isNotEmpty()) {
|
||||
throw Exception(
|
||||
"Data integrity error: ${invalidSessions.size} sessions reference non-existent gyms"
|
||||
"Data integrity error: ${invalidSessions.size} sessions reference non-existent gyms"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -326,10 +329,10 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
val sessionIds = sessions.map { it.id }.toSet()
|
||||
|
||||
val invalidAttempts =
|
||||
attempts.filter { it.problemId !in problemIds || it.sessionId !in sessionIds }
|
||||
attempts.filter { it.problemId !in problemIds || it.sessionId !in sessionIds }
|
||||
if (invalidAttempts.isNotEmpty()) {
|
||||
throw Exception(
|
||||
"Data integrity error: ${invalidAttempts.size} attempts reference non-existent problems or sessions"
|
||||
"Data integrity error: ${invalidAttempts.size} attempts reference non-existent problems or sessions"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -344,9 +347,9 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
}
|
||||
|
||||
if (importData.gyms.size > 1000 ||
|
||||
importData.problems.size > 10000 ||
|
||||
importData.sessions.size > 10000 ||
|
||||
importData.attempts.size > 100000
|
||||
importData.problems.size > 10000 ||
|
||||
importData.sessions.size > 10000 ||
|
||||
importData.attempts.size > 100000
|
||||
) {
|
||||
throw Exception("Import data is too large: possible corruption or malicious file")
|
||||
}
|
||||
@@ -396,10 +399,10 @@ class ClimbRepository(database: OpenClimbDatabase, private val context: Context)
|
||||
if (imagesDir.exists() && imagesDir.isDirectory) {
|
||||
val deletedCount = imagesDir.listFiles()?.size ?: 0
|
||||
imagesDir.deleteRecursively()
|
||||
android.util.Log.i("ClimbRepository", "Cleared $deletedCount image files")
|
||||
AppLogger.i("ClimbRepository") { "Cleared $deletedCount image files" }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.w("ClimbRepository", "Failed to clear some images: ${e.message}")
|
||||
AppLogger.w("ClimbRepository", e) { "Failed to clear some images: ${e.message}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.atridad.openclimb.data.state
|
||||
package com.atridad.ascently.data.state
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.util.Log
|
||||
import com.atridad.openclimb.utils.DateFormatUtils
|
||||
import androidx.core.content.edit
|
||||
import com.atridad.ascently.utils.AppLogger
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
|
||||
/**
|
||||
* Manages the overall data state timestamp for sync purposes. This tracks when any data in the
|
||||
@@ -13,19 +14,19 @@ class DataStateManager(context: Context) {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "DataStateManager"
|
||||
private const val PREFS_NAME = "openclimb_data_state"
|
||||
private const val PREFS_NAME = "ascently_data_state"
|
||||
private const val KEY_LAST_MODIFIED = "last_modified_timestamp"
|
||||
private const val KEY_INITIALIZED = "state_initialized"
|
||||
}
|
||||
|
||||
private val prefs: SharedPreferences =
|
||||
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
|
||||
init {
|
||||
if (!isInitialized()) {
|
||||
updateDataState()
|
||||
markAsInitialized()
|
||||
Log.d(TAG, "DataStateManager initialized with timestamp: ${getLastModified()}")
|
||||
AppLogger.d(TAG) { "DataStateManager initialized with timestamp: ${getLastModified()}" }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +36,8 @@ class DataStateManager(context: Context) {
|
||||
*/
|
||||
fun updateDataState() {
|
||||
val now = DateFormatUtils.nowISO8601()
|
||||
prefs.edit().putString(KEY_LAST_MODIFIED, now).apply()
|
||||
Log.d(TAG, "Data state updated to: $now")
|
||||
prefs.edit { putString(KEY_LAST_MODIFIED, now) }
|
||||
AppLogger.d(TAG) { "Data state updated to: $now" }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,22 +46,7 @@ class DataStateManager(context: Context) {
|
||||
*/
|
||||
fun getLastModified(): String {
|
||||
return prefs.getString(KEY_LAST_MODIFIED, 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")
|
||||
?: DateFormatUtils.nowISO8601()
|
||||
}
|
||||
|
||||
/** Checks if the data state has been initialized. */
|
||||
@@ -70,11 +56,6 @@ class DataStateManager(context: Context) {
|
||||
|
||||
/** Marks the data state as initialized. */
|
||||
private fun markAsInitialized() {
|
||||
prefs.edit().putBoolean(KEY_INITIALIZED, true).apply()
|
||||
}
|
||||
|
||||
/** Gets debug information about the current state. */
|
||||
fun getDebugInfo(): String {
|
||||
return "DataState(lastModified=${getLastModified()}, initialized=${isInitialized()})"
|
||||
prefs.edit { putBoolean(KEY_INITIALIZED, true) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.atridad.ascently.data.sync
|
||||
|
||||
import com.atridad.ascently.data.format.BackupAttempt
|
||||
import com.atridad.ascently.data.format.BackupClimbSession
|
||||
import com.atridad.ascently.data.format.BackupGym
|
||||
import com.atridad.ascently.data.format.BackupProblem
|
||||
import com.atridad.ascently.data.format.DeletedItem
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/** Request structure for delta sync - sends only changes since last sync */
|
||||
@Serializable
|
||||
data class DeltaSyncRequest(
|
||||
val lastSyncTime: String,
|
||||
val gyms: List<BackupGym>,
|
||||
val problems: List<BackupProblem>,
|
||||
val sessions: List<BackupClimbSession>,
|
||||
val attempts: List<BackupAttempt>,
|
||||
val deletedItems: List<DeletedItem>
|
||||
)
|
||||
|
||||
/** Response structure for delta sync - receives only changes from server */
|
||||
@Serializable
|
||||
data class DeltaSyncResponse(
|
||||
val serverTime: String,
|
||||
val gyms: List<BackupGym>,
|
||||
val problems: List<BackupProblem>,
|
||||
val sessions: List<BackupClimbSession>,
|
||||
val attempts: List<BackupAttempt>,
|
||||
val deletedItems: List<DeletedItem>
|
||||
)
|
||||
@@ -0,0 +1,841 @@
|
||||
package com.atridad.ascently.data.sync
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities
|
||||
import androidx.annotation.RequiresPermission
|
||||
import androidx.core.content.edit
|
||||
import com.atridad.ascently.data.format.BackupAttempt
|
||||
import com.atridad.ascently.utils.AppLogger
|
||||
import com.atridad.ascently.data.format.BackupClimbSession
|
||||
import com.atridad.ascently.data.format.BackupGym
|
||||
import com.atridad.ascently.data.format.BackupProblem
|
||||
import com.atridad.ascently.data.format.ClimbDataBackup
|
||||
import com.atridad.ascently.data.repository.ClimbRepository
|
||||
import com.atridad.ascently.data.state.DataStateManager
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import com.atridad.ascently.utils.ImageNamingUtils
|
||||
import com.atridad.ascently.utils.ImageUtils
|
||||
import java.io.IOException
|
||||
import java.io.Serializable
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
|
||||
class SyncService(private val context: Context, private val repository: ClimbRepository) {
|
||||
|
||||
private val dataStateManager = DataStateManager(context)
|
||||
private val syncMutex = Mutex()
|
||||
private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
companion object {
|
||||
private const val TAG = "SyncService"
|
||||
}
|
||||
|
||||
private val sharedPreferences: SharedPreferences =
|
||||
context.getSharedPreferences("sync_preferences", Context.MODE_PRIVATE)
|
||||
|
||||
private val httpClient =
|
||||
OkHttpClient.Builder()
|
||||
.connectTimeout(45, TimeUnit.SECONDS)
|
||||
.readTimeout(90, TimeUnit.SECONDS)
|
||||
.writeTimeout(90, TimeUnit.SECONDS)
|
||||
.build()
|
||||
|
||||
private val json = Json {
|
||||
prettyPrint = true
|
||||
ignoreUnknownKeys = true
|
||||
explicitNulls = false
|
||||
coerceInputValues = true
|
||||
}
|
||||
|
||||
// State
|
||||
private val _isSyncing = MutableStateFlow(false)
|
||||
val isSyncing: StateFlow<Boolean> = _isSyncing.asStateFlow()
|
||||
|
||||
private val _lastSyncTime = MutableStateFlow<String?>(null)
|
||||
val lastSyncTime: StateFlow<String?> = _lastSyncTime.asStateFlow()
|
||||
|
||||
private val _syncError = MutableStateFlow<String?>(null)
|
||||
val syncError: StateFlow<String?> = _syncError.asStateFlow()
|
||||
|
||||
private val _isConnected = MutableStateFlow(false)
|
||||
val isConnected: StateFlow<Boolean> = _isConnected.asStateFlow()
|
||||
|
||||
private val _isConfigured = MutableStateFlow(false)
|
||||
val isConfiguredFlow: StateFlow<Boolean> = _isConfigured.asStateFlow()
|
||||
|
||||
private val _isTesting = MutableStateFlow(false)
|
||||
val isTesting: StateFlow<Boolean> = _isTesting.asStateFlow()
|
||||
|
||||
private val _isAutoSyncEnabled = MutableStateFlow(true)
|
||||
val isAutoSyncEnabled: StateFlow<Boolean> = _isAutoSyncEnabled.asStateFlow()
|
||||
|
||||
private var isOfflineMode = false
|
||||
|
||||
// Debounced sync properties
|
||||
private var syncJob: Job? = null
|
||||
private var pendingChanges = false
|
||||
private val syncDebounceDelay = 2000L // 2 seconds
|
||||
|
||||
// Configuration keys
|
||||
private object Keys {
|
||||
const val SERVER_URL = "server_url"
|
||||
const val AUTH_TOKEN = "auth_token"
|
||||
const val IS_CONNECTED = "is_connected"
|
||||
const val LAST_SYNC_TIME = "last_sync_time"
|
||||
const val AUTO_SYNC_ENABLED = "auto_sync_enabled"
|
||||
const val OFFLINE_MODE = "offline_mode"
|
||||
}
|
||||
|
||||
init {
|
||||
loadInitialState()
|
||||
updateConfiguredState()
|
||||
repository.setAutoSyncCallback { serviceScope.launch { triggerAutoSync() } }
|
||||
}
|
||||
|
||||
private fun loadInitialState() {
|
||||
_lastSyncTime.value = sharedPreferences.getString(Keys.LAST_SYNC_TIME, null)
|
||||
_isConnected.value = sharedPreferences.getBoolean(Keys.IS_CONNECTED, false)
|
||||
_isAutoSyncEnabled.value = sharedPreferences.getBoolean(Keys.AUTO_SYNC_ENABLED, true)
|
||||
isOfflineMode = sharedPreferences.getBoolean(Keys.OFFLINE_MODE, false)
|
||||
}
|
||||
|
||||
private fun updateConfiguredState() {
|
||||
_isConfigured.value = serverUrl.isNotBlank() && authToken.isNotBlank()
|
||||
}
|
||||
|
||||
var serverUrl: String
|
||||
get() = sharedPreferences.getString(Keys.SERVER_URL, "") ?: ""
|
||||
set(value) {
|
||||
sharedPreferences.edit { putString(Keys.SERVER_URL, value) }
|
||||
updateConfiguredState()
|
||||
_isConnected.value = false
|
||||
sharedPreferences.edit { putBoolean(Keys.IS_CONNECTED, false) }
|
||||
}
|
||||
|
||||
var authToken: String
|
||||
get() = sharedPreferences.getString(Keys.AUTH_TOKEN, "") ?: ""
|
||||
set(value) {
|
||||
sharedPreferences.edit { putString(Keys.AUTH_TOKEN, value) }
|
||||
updateConfiguredState()
|
||||
_isConnected.value = false
|
||||
sharedPreferences.edit { putBoolean(Keys.IS_CONNECTED, false) }
|
||||
}
|
||||
|
||||
fun setAutoSyncEnabled(enabled: Boolean) {
|
||||
_isAutoSyncEnabled.value = enabled
|
||||
sharedPreferences.edit { putBoolean(Keys.AUTO_SYNC_ENABLED, enabled) }
|
||||
}
|
||||
|
||||
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
|
||||
private fun isNetworkAvailable(): Boolean {
|
||||
val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val network = connectivityManager.activeNetwork ?: return false
|
||||
val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false
|
||||
return when {
|
||||
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
|
||||
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
|
||||
suspend fun syncWithServer() {
|
||||
if (isOfflineMode) {
|
||||
AppLogger.d(TAG) { "Sync skipped: Offline mode is enabled." }
|
||||
return
|
||||
}
|
||||
if (!isNetworkAvailable()) {
|
||||
_syncError.value = "No internet connection."
|
||||
AppLogger.d(TAG) { "Sync skipped: No internet connection." }
|
||||
return
|
||||
}
|
||||
if (!_isConfigured.value) {
|
||||
throw SyncException.NotConfigured
|
||||
}
|
||||
if (!_isConnected.value) {
|
||||
throw SyncException.NotConnected
|
||||
}
|
||||
|
||||
syncMutex.withLock {
|
||||
_isSyncing.value = true
|
||||
_syncError.value = null
|
||||
|
||||
try {
|
||||
val localBackup = createBackupFromRepository()
|
||||
val serverBackup = downloadData()
|
||||
|
||||
val hasLocalData =
|
||||
localBackup.gyms.isNotEmpty() ||
|
||||
localBackup.problems.isNotEmpty() ||
|
||||
localBackup.sessions.isNotEmpty() ||
|
||||
localBackup.attempts.isNotEmpty()
|
||||
|
||||
val hasServerData =
|
||||
serverBackup.gyms.isNotEmpty() ||
|
||||
serverBackup.problems.isNotEmpty() ||
|
||||
serverBackup.sessions.isNotEmpty() ||
|
||||
serverBackup.attempts.isNotEmpty()
|
||||
|
||||
// If both client and server have been synced before, use delta sync
|
||||
val lastSyncTimeStr = sharedPreferences.getString(Keys.LAST_SYNC_TIME, null)
|
||||
if (hasLocalData && hasServerData && lastSyncTimeStr != null) {
|
||||
AppLogger.d(TAG) { "Using delta sync for incremental updates" }
|
||||
performDeltaSync(lastSyncTimeStr)
|
||||
} else {
|
||||
when {
|
||||
!hasLocalData && hasServerData -> {
|
||||
AppLogger.d(TAG) { "No local data found, performing full restore from server" }
|
||||
val imagePathMapping = syncImagesFromServer(serverBackup)
|
||||
importBackupToRepository(serverBackup, imagePathMapping)
|
||||
AppLogger.d(TAG) { "Full restore completed" }
|
||||
}
|
||||
|
||||
hasLocalData && !hasServerData -> {
|
||||
AppLogger.d(TAG) { "No server data found, uploading local data to server" }
|
||||
uploadData(localBackup)
|
||||
syncImagesForBackup(localBackup)
|
||||
AppLogger.d(TAG) { "Initial upload completed" }
|
||||
}
|
||||
|
||||
hasLocalData && hasServerData -> {
|
||||
AppLogger.d(TAG) { "Both local and server data exist, merging (server wins)" }
|
||||
mergeDataSafely(serverBackup)
|
||||
AppLogger.d(TAG) { "Merge completed" }
|
||||
}
|
||||
|
||||
else -> {
|
||||
AppLogger.d(TAG) { "No data to sync" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val now = DateFormatUtils.nowISO8601()
|
||||
_lastSyncTime.value = now
|
||||
sharedPreferences.edit { putString(Keys.LAST_SYNC_TIME, now) }
|
||||
} catch (e: Exception) {
|
||||
_syncError.value = e.message
|
||||
throw e
|
||||
} finally {
|
||||
_isSyncing.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun performDeltaSync(lastSyncTimeStr: String) {
|
||||
AppLogger.d(TAG) { "Starting delta sync with lastSyncTime=$lastSyncTimeStr" }
|
||||
|
||||
// Parse last sync time to filter modified items
|
||||
val lastSyncDate = parseISO8601(lastSyncTimeStr) ?: Date(0)
|
||||
|
||||
// Collect items modified since last sync
|
||||
val allGyms = repository.getAllGyms().first()
|
||||
val modifiedGyms =
|
||||
allGyms
|
||||
.filter { gym -> parseISO8601(gym.updatedAt)?.after(lastSyncDate) == true }
|
||||
.map { BackupGym.fromGym(it) }
|
||||
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
val modifiedProblems =
|
||||
allProblems
|
||||
.filter { problem ->
|
||||
parseISO8601(problem.updatedAt)?.after(lastSyncDate) == true
|
||||
}
|
||||
.map { problem ->
|
||||
val backupProblem = BackupProblem.fromProblem(problem)
|
||||
val normalizedImagePaths =
|
||||
problem.imagePaths.mapIndexed { index, _ ->
|
||||
ImageNamingUtils.generateImageFilename(problem.id, index)
|
||||
}
|
||||
if (normalizedImagePaths.isNotEmpty()) {
|
||||
backupProblem.copy(imagePaths = normalizedImagePaths)
|
||||
} else {
|
||||
backupProblem
|
||||
}
|
||||
}
|
||||
|
||||
val allSessions = repository.getAllSessions().first()
|
||||
val modifiedSessions =
|
||||
allSessions
|
||||
.filter { session ->
|
||||
parseISO8601(session.updatedAt)?.after(lastSyncDate) == true
|
||||
}
|
||||
.map { BackupClimbSession.fromClimbSession(it) }
|
||||
|
||||
val allAttempts = repository.getAllAttempts().first()
|
||||
val modifiedAttempts =
|
||||
allAttempts
|
||||
.filter { attempt ->
|
||||
parseISO8601(attempt.createdAt)?.after(lastSyncDate) == true
|
||||
}
|
||||
.map { BackupAttempt.fromAttempt(it) }
|
||||
|
||||
val allDeletions = repository.getDeletedItems()
|
||||
val modifiedDeletions =
|
||||
allDeletions.filter { item ->
|
||||
parseISO8601(item.deletedAt)?.after(lastSyncDate) == true
|
||||
}
|
||||
|
||||
AppLogger.d(TAG) {
|
||||
"Delta sync sending: gyms=${modifiedGyms.size}, problems=${modifiedProblems.size}, sessions=${modifiedSessions.size}, attempts=${modifiedAttempts.size}, deletions=${modifiedDeletions.size}"
|
||||
}
|
||||
|
||||
// Create delta request
|
||||
val deltaRequest =
|
||||
DeltaSyncRequest(
|
||||
lastSyncTime = lastSyncTimeStr,
|
||||
gyms = modifiedGyms,
|
||||
problems = modifiedProblems,
|
||||
sessions = modifiedSessions,
|
||||
attempts = modifiedAttempts,
|
||||
deletedItems = modifiedDeletions
|
||||
)
|
||||
|
||||
val requestBody =
|
||||
json.encodeToString(DeltaSyncRequest.serializer(), deltaRequest)
|
||||
.toRequestBody("application/json".toMediaType())
|
||||
|
||||
val request =
|
||||
Request.Builder()
|
||||
.url("$serverUrl/sync/delta")
|
||||
.header("Authorization", "Bearer $authToken")
|
||||
.post(requestBody)
|
||||
.build()
|
||||
|
||||
val deltaResponse =
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
httpClient.newCall(request).execute().use { response ->
|
||||
if (response.isSuccessful) {
|
||||
val body = response.body?.string()
|
||||
if (!body.isNullOrEmpty()) {
|
||||
json.decodeFromString(DeltaSyncResponse.serializer(), body)
|
||||
} else {
|
||||
throw SyncException.InvalidResponse("Empty response body")
|
||||
}
|
||||
} else {
|
||||
handleHttpError(response.code)
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
throw SyncException.NetworkError(e.message ?: "Network error")
|
||||
}
|
||||
}
|
||||
|
||||
AppLogger.d(TAG) {
|
||||
"Delta sync received: gyms=${deltaResponse.gyms.size}, problems=${deltaResponse.problems.size}, sessions=${deltaResponse.sessions.size}, attempts=${deltaResponse.attempts.size}, deletions=${deltaResponse.deletedItems.size}"
|
||||
}
|
||||
|
||||
// Apply server changes to local data
|
||||
applyDeltaResponse(deltaResponse)
|
||||
|
||||
// Sync only modified problem images
|
||||
syncModifiedImages(modifiedProblems)
|
||||
}
|
||||
|
||||
private fun parseISO8601(dateString: String): Date? {
|
||||
return try {
|
||||
val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US)
|
||||
format.parse(dateString)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun applyDeltaResponse(response: DeltaSyncResponse) {
|
||||
// Temporarily disable auto-sync to prevent recursive sync triggers
|
||||
repository.setAutoSyncCallback(null)
|
||||
|
||||
try {
|
||||
// Merge and apply deletions first to prevent resurrection
|
||||
val allDeletions = repository.getDeletedItems() + response.deletedItems
|
||||
val uniqueDeletions = allDeletions.distinctBy { "${it.type}:${it.id}" }
|
||||
|
||||
AppLogger.d(TAG) { "Applying ${uniqueDeletions.size} deletion records before merging data" }
|
||||
applyDeletions(uniqueDeletions)
|
||||
|
||||
// Build deleted item lookup set
|
||||
val deletedItemSet = uniqueDeletions.map { "${it.type}:${it.id}" }.toSet()
|
||||
|
||||
// Download images for new/modified problems from server
|
||||
val imagePathMapping = mutableMapOf<String, String>()
|
||||
for (problem in response.problems) {
|
||||
if (deletedItemSet.contains("problem:${problem.id}")) {
|
||||
continue
|
||||
}
|
||||
problem.imagePaths?.forEach { imagePath ->
|
||||
val serverFilename = imagePath.substringAfterLast('/')
|
||||
try {
|
||||
val localImagePath = downloadImage(serverFilename)
|
||||
if (localImagePath != null) {
|
||||
imagePathMapping[imagePath] = localImagePath
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLogger.w(TAG) { "Failed to download image $imagePath: ${e.message}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merge gyms
|
||||
val existingGyms = repository.getAllGyms().first()
|
||||
for (backupGym in response.gyms) {
|
||||
if (deletedItemSet.contains("gym:${backupGym.id}")) {
|
||||
continue
|
||||
}
|
||||
val existing = existingGyms.find { it.id == backupGym.id }
|
||||
if (existing == null || backupGym.updatedAt >= existing.updatedAt) {
|
||||
val gym = backupGym.toGym()
|
||||
if (existing != null) {
|
||||
repository.updateGym(gym)
|
||||
} else {
|
||||
repository.insertGym(gym)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merge problems
|
||||
val existingProblems = repository.getAllProblems().first()
|
||||
for (backupProblem in response.problems) {
|
||||
if (deletedItemSet.contains("problem:${backupProblem.id}")) {
|
||||
continue
|
||||
}
|
||||
val updatedImagePaths =
|
||||
backupProblem.imagePaths?.map { oldPath ->
|
||||
imagePathMapping[oldPath] ?: oldPath
|
||||
}
|
||||
val problemToMerge = backupProblem.copy(imagePaths = updatedImagePaths)
|
||||
val problem = problemToMerge.toProblem()
|
||||
|
||||
val existing = existingProblems.find { it.id == backupProblem.id }
|
||||
if (existing == null || backupProblem.updatedAt >= existing.updatedAt) {
|
||||
if (existing != null) {
|
||||
repository.updateProblem(problem)
|
||||
} else {
|
||||
repository.insertProblem(problem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merge sessions
|
||||
val existingSessions = repository.getAllSessions().first()
|
||||
for (backupSession in response.sessions) {
|
||||
if (deletedItemSet.contains("session:${backupSession.id}")) {
|
||||
continue
|
||||
}
|
||||
val session = backupSession.toClimbSession()
|
||||
val existing = existingSessions.find { it.id == backupSession.id }
|
||||
if (existing == null || backupSession.updatedAt >= existing.updatedAt) {
|
||||
if (existing != null) {
|
||||
repository.updateSession(session)
|
||||
} else {
|
||||
repository.insertSession(session)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merge attempts
|
||||
val existingAttempts = repository.getAllAttempts().first()
|
||||
for (backupAttempt in response.attempts) {
|
||||
if (deletedItemSet.contains("attempt:${backupAttempt.id}")) {
|
||||
continue
|
||||
}
|
||||
val attempt = backupAttempt.toAttempt()
|
||||
val existing = existingAttempts.find { it.id == backupAttempt.id }
|
||||
if (existing == null || backupAttempt.createdAt >= existing.createdAt) {
|
||||
if (existing != null) {
|
||||
repository.updateAttempt(attempt)
|
||||
} else {
|
||||
repository.insertAttempt(attempt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply deletions again for safety
|
||||
applyDeletions(uniqueDeletions)
|
||||
|
||||
// Update deletion records
|
||||
repository.clearDeletedItems()
|
||||
uniqueDeletions.forEach { repository.trackDeletion(it.id, it.type) }
|
||||
} finally {
|
||||
// Re-enable auto-sync
|
||||
repository.setAutoSyncCallback { serviceScope.launch { triggerAutoSync() } }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun applyDeletions(
|
||||
deletions: List<com.atridad.ascently.data.format.DeletedItem>
|
||||
) {
|
||||
val existingGyms = repository.getAllGyms().first()
|
||||
val existingProblems = repository.getAllProblems().first()
|
||||
val existingSessions = repository.getAllSessions().first()
|
||||
val existingAttempts = repository.getAllAttempts().first()
|
||||
|
||||
for (item in deletions) {
|
||||
when (item.type) {
|
||||
"gym" -> {
|
||||
existingGyms.find { it.id == item.id }?.let { repository.deleteGym(it) }
|
||||
}
|
||||
|
||||
"problem" -> {
|
||||
existingProblems.find { it.id == item.id }?.let { repository.deleteProblem(it) }
|
||||
}
|
||||
|
||||
"session" -> {
|
||||
existingSessions.find { it.id == item.id }?.let { repository.deleteSession(it) }
|
||||
}
|
||||
|
||||
"attempt" -> {
|
||||
existingAttempts.find { it.id == item.id }?.let { repository.deleteAttempt(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun syncModifiedImages(modifiedProblems: List<BackupProblem>) {
|
||||
if (modifiedProblems.isEmpty()) return
|
||||
|
||||
AppLogger.d(TAG) { "Syncing images for ${modifiedProblems.size} modified problems" }
|
||||
|
||||
for (backupProblem in modifiedProblems) {
|
||||
backupProblem.imagePaths?.forEach { imagePath ->
|
||||
val filename = imagePath.substringAfterLast('/')
|
||||
uploadImage(imagePath, filename)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun downloadData(): ClimbDataBackup {
|
||||
val request =
|
||||
Request.Builder()
|
||||
.url("$serverUrl/sync")
|
||||
.header("Authorization", "Bearer $authToken")
|
||||
.get()
|
||||
.build()
|
||||
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
httpClient.newCall(request).execute().use { response ->
|
||||
if (response.isSuccessful) {
|
||||
val body = response.body?.string()
|
||||
if (!body.isNullOrEmpty()) {
|
||||
json.decodeFromString(body)
|
||||
} else {
|
||||
ClimbDataBackup(
|
||||
exportedAt = DateFormatUtils.nowISO8601(),
|
||||
gyms = emptyList(),
|
||||
problems = emptyList(),
|
||||
sessions = emptyList(),
|
||||
attempts = emptyList()
|
||||
)
|
||||
}
|
||||
} else {
|
||||
handleHttpError(response.code)
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
throw SyncException.NetworkError(e.message ?: "Network error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun uploadData(backup: ClimbDataBackup) {
|
||||
val requestBody =
|
||||
json.encodeToString(backup).toRequestBody("application/json".toMediaType())
|
||||
|
||||
val request =
|
||||
Request.Builder()
|
||||
.url("$serverUrl/sync")
|
||||
.header("Authorization", "Bearer $authToken")
|
||||
.put(requestBody)
|
||||
.build()
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
httpClient.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) {
|
||||
handleHttpError(response.code)
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
throw SyncException.NetworkError(e.message ?: "Network error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun syncImagesFromServer(backup: ClimbDataBackup): Map<String, String> {
|
||||
val imagePathMapping = mutableMapOf<String, String>()
|
||||
val totalImages = backup.problems.sumOf { it.imagePaths?.size ?: 0 }
|
||||
AppLogger.d(TAG) { "Starting image download from server for $totalImages images" }
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
backup.problems.forEach { problem ->
|
||||
problem.imagePaths?.forEach { imagePath ->
|
||||
val serverFilename = imagePath.substringAfterLast('/')
|
||||
try {
|
||||
val localImagePath = downloadImage(serverFilename)
|
||||
if (localImagePath != null) {
|
||||
imagePathMapping[imagePath] = localImagePath
|
||||
}
|
||||
} catch (_: SyncException.ImageNotFound) {
|
||||
AppLogger.w(TAG) { "Image not found on server: $imagePath" }
|
||||
} catch (e: Exception) {
|
||||
AppLogger.w(TAG) { "Failed to download image $imagePath: ${e.message}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return imagePathMapping
|
||||
}
|
||||
|
||||
private suspend fun downloadImage(serverFilename: String): String? {
|
||||
val request =
|
||||
Request.Builder()
|
||||
.url("$serverUrl/images/download?filename=$serverFilename")
|
||||
.header("Authorization", "Bearer $authToken")
|
||||
.build()
|
||||
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
httpClient.newCall(request).execute().use { response ->
|
||||
if (response.isSuccessful) {
|
||||
response.body?.bytes()?.let {
|
||||
ImageUtils.saveImageFromBytesWithFilename(context, it, serverFilename)
|
||||
}
|
||||
} else {
|
||||
if (response.code == 404) throw SyncException.ImageNotFound
|
||||
null
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
AppLogger.e(TAG, e) { "Network error downloading image $serverFilename" }
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun syncImagesForBackup(backup: ClimbDataBackup) {
|
||||
AppLogger.d(TAG) { "Starting image sync for backup with ${backup.problems.size} problems" }
|
||||
withContext(Dispatchers.IO) {
|
||||
backup.problems.forEach { problem ->
|
||||
problem.imagePaths?.forEach { localPath ->
|
||||
val filename = localPath.substringAfterLast('/')
|
||||
uploadImage(localPath, filename)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun uploadImage(localPath: String, filename: String) {
|
||||
val file = ImageUtils.getImageFile(context, localPath)
|
||||
if (!file.exists()) {
|
||||
AppLogger.w(TAG) { "Local image file not found, cannot upload: $localPath" }
|
||||
return
|
||||
}
|
||||
|
||||
val requestBody = file.readBytes().toRequestBody("application/octet-stream".toMediaType())
|
||||
|
||||
val request =
|
||||
Request.Builder()
|
||||
.url("$serverUrl/images/upload?filename=$filename")
|
||||
.header("Authorization", "Bearer $authToken")
|
||||
.post(requestBody)
|
||||
.build()
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
httpClient.newCall(request).execute().use { response ->
|
||||
if (response.isSuccessful) {
|
||||
AppLogger.d(TAG) { "Successfully uploaded image: $filename" }
|
||||
} else {
|
||||
AppLogger.w(TAG) {
|
||||
"Failed to upload image $filename. Server responded with ${response.code}"
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
AppLogger.e(TAG, e) { "Network error uploading image $filename" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun createBackupFromRepository(): ClimbDataBackup {
|
||||
return withContext(Dispatchers.Default) {
|
||||
ClimbDataBackup(
|
||||
exportedAt = dataStateManager.getLastModified(),
|
||||
gyms = repository.getAllGyms().first().map { BackupGym.fromGym(it) },
|
||||
problems =
|
||||
repository.getAllProblems().first().map { problem ->
|
||||
val backupProblem = BackupProblem.fromProblem(problem)
|
||||
val normalizedImagePaths =
|
||||
problem.imagePaths.mapIndexed { index, _ ->
|
||||
ImageNamingUtils.generateImageFilename(
|
||||
problem.id,
|
||||
index
|
||||
)
|
||||
}
|
||||
if (normalizedImagePaths.isNotEmpty()) {
|
||||
backupProblem.copy(imagePaths = normalizedImagePaths)
|
||||
} else {
|
||||
backupProblem
|
||||
}
|
||||
},
|
||||
sessions =
|
||||
repository.getAllSessions().first().map {
|
||||
BackupClimbSession.fromClimbSession(it)
|
||||
},
|
||||
attempts =
|
||||
repository.getAllAttempts().first().map {
|
||||
BackupAttempt.fromAttempt(it)
|
||||
},
|
||||
deletedItems = repository.getDeletedItems()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun importBackupToRepository(
|
||||
backup: ClimbDataBackup,
|
||||
imagePathMapping: Map<String, String>
|
||||
) {
|
||||
val gyms = backup.gyms.map { it.toGym() }
|
||||
val problems =
|
||||
backup.problems.map { backupProblem ->
|
||||
val imagePaths = backupProblem.imagePaths
|
||||
val updatedImagePaths =
|
||||
imagePaths?.map { oldPath -> imagePathMapping[oldPath] ?: oldPath }
|
||||
backupProblem.copy(imagePaths = updatedImagePaths).toProblem()
|
||||
}
|
||||
val sessions = backup.sessions.map { it.toClimbSession() }
|
||||
val attempts = backup.attempts.map { it.toAttempt() }
|
||||
|
||||
repository.resetAllData()
|
||||
|
||||
gyms.forEach { repository.insertGymWithoutSync(it) }
|
||||
problems.forEach { repository.insertProblemWithoutSync(it) }
|
||||
sessions.forEach { repository.insertSessionWithoutSync(it) }
|
||||
attempts.forEach { repository.insertAttemptWithoutSync(it) }
|
||||
|
||||
repository.clearDeletedItems()
|
||||
}
|
||||
|
||||
private suspend fun mergeDataSafely(serverBackup: ClimbDataBackup) {
|
||||
AppLogger.d(TAG) { "Server data will overwrite local data. Performing full restore." }
|
||||
val imagePathMapping = syncImagesFromServer(serverBackup)
|
||||
importBackupToRepository(serverBackup, imagePathMapping)
|
||||
}
|
||||
|
||||
private fun handleHttpError(code: Int): Nothing {
|
||||
when (code) {
|
||||
401 -> throw SyncException.Unauthorized
|
||||
in 500..599 -> throw SyncException.ServerError(code)
|
||||
else -> throw SyncException.InvalidResponse("HTTP error code: $code")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun testConnection() {
|
||||
if (!_isConfigured.value) {
|
||||
_isConnected.value = false
|
||||
_syncError.value = "Server URL or Auth Token is not set."
|
||||
return
|
||||
}
|
||||
_isTesting.value = true
|
||||
_syncError.value = null
|
||||
|
||||
val request =
|
||||
Request.Builder()
|
||||
.url("$serverUrl/sync")
|
||||
.header("Authorization", "Bearer $authToken")
|
||||
.head()
|
||||
.build()
|
||||
try {
|
||||
withContext(Dispatchers.IO) {
|
||||
httpClient.newCall(request).execute().use { response ->
|
||||
_isConnected.value = response.isSuccessful || response.code == 405
|
||||
}
|
||||
}
|
||||
if (!_isConnected.value) {
|
||||
_syncError.value = "Connection failed. Check URL and token."
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_isConnected.value = false
|
||||
_syncError.value = "Connection error: ${e.message}"
|
||||
} finally {
|
||||
sharedPreferences.edit { putBoolean(Keys.IS_CONNECTED, _isConnected.value) }
|
||||
_isTesting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
fun triggerAutoSync() {
|
||||
if (!_isConfigured.value || !_isConnected.value || !_isAutoSyncEnabled.value) {
|
||||
return
|
||||
}
|
||||
if (_isSyncing.value) {
|
||||
pendingChanges = true
|
||||
return
|
||||
}
|
||||
syncJob?.cancel()
|
||||
syncJob =
|
||||
serviceScope.launch {
|
||||
delay(syncDebounceDelay)
|
||||
try {
|
||||
syncWithServer()
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(TAG, e) { "Auto-sync failed" }
|
||||
}
|
||||
if (pendingChanges) {
|
||||
pendingChanges = false
|
||||
triggerAutoSync()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun clearConfiguration() {
|
||||
syncJob?.cancel()
|
||||
serverUrl = ""
|
||||
authToken = ""
|
||||
setAutoSyncEnabled(true)
|
||||
_lastSyncTime.value = null
|
||||
_isConnected.value = false
|
||||
_syncError.value = null
|
||||
sharedPreferences.edit { clear() }
|
||||
updateConfiguredState()
|
||||
}
|
||||
}
|
||||
|
||||
sealed class SyncException(message: String) : IOException(message), Serializable {
|
||||
object NotConfigured :
|
||||
SyncException("Sync is not configured. Please set server URL and auth token.")
|
||||
|
||||
object NotConnected : SyncException("Not connected to server. Please test connection first.")
|
||||
|
||||
object Unauthorized : SyncException("Unauthorized. Please check your auth token.")
|
||||
|
||||
object ImageNotFound : SyncException("Image not found on server")
|
||||
|
||||
data class ServerError(val code: Int) : SyncException("Server error: HTTP $code")
|
||||
data class InvalidResponse(val details: String) :
|
||||
SyncException("Invalid server response: $details")
|
||||
|
||||
data class NetworkError(val details: String) : SyncException("Network error: $details")
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.navigation
|
||||
package com.atridad.ascently.navigation
|
||||
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.*
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.navigation
|
||||
package com.atridad.ascently.navigation
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@@ -0,0 +1,316 @@
|
||||
package com.atridad.ascently.service
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.IBinder
|
||||
import androidx.core.app.NotificationCompat
|
||||
import com.atridad.ascently.MainActivity
|
||||
import com.atridad.ascently.R
|
||||
import com.atridad.ascently.data.database.AscentlyDatabase
|
||||
import com.atridad.ascently.data.repository.ClimbRepository
|
||||
import com.atridad.ascently.utils.AppLogger
|
||||
import com.atridad.ascently.widget.ClimbStatsWidgetProvider
|
||||
import java.time.LocalDateTime
|
||||
import java.time.temporal.ChronoUnit
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
class SessionTrackingService : Service() {
|
||||
|
||||
private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
private var notificationJob: Job? = null
|
||||
private var monitoringJob: Job? = null
|
||||
|
||||
private lateinit var repository: ClimbRepository
|
||||
private lateinit var notificationManager: NotificationManager
|
||||
|
||||
companion object {
|
||||
private const val LOG_TAG = "SessionTrackingService"
|
||||
const val NOTIFICATION_ID = 1001
|
||||
const val CHANNEL_ID = "session_tracking_channel"
|
||||
const val ACTION_START_SESSION = "start_session"
|
||||
const val ACTION_STOP_SESSION = "stop_session"
|
||||
const val EXTRA_SESSION_ID = "session_id"
|
||||
|
||||
fun createStartIntent(context: Context, sessionId: String): Intent {
|
||||
return Intent(context, SessionTrackingService::class.java).apply {
|
||||
action = ACTION_START_SESSION
|
||||
putExtra(EXTRA_SESSION_ID, sessionId)
|
||||
}
|
||||
}
|
||||
|
||||
fun createStopIntent(context: Context, sessionId: String): Intent {
|
||||
return Intent(context, SessionTrackingService::class.java).apply {
|
||||
action = ACTION_STOP_SESSION
|
||||
putExtra(EXTRA_SESSION_ID, sessionId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
val database = AscentlyDatabase.getDatabase(this)
|
||||
repository = ClimbRepository(database, this)
|
||||
notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
createNotificationChannel()
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
when (intent?.action) {
|
||||
ACTION_START_SESSION -> {
|
||||
val sessionId = intent.getStringExtra(EXTRA_SESSION_ID)
|
||||
if (sessionId != null) {
|
||||
startSessionTracking(sessionId)
|
||||
}
|
||||
}
|
||||
|
||||
ACTION_STOP_SESSION -> {
|
||||
val sessionId = intent.getStringExtra(EXTRA_SESSION_ID)
|
||||
serviceScope.launch {
|
||||
try {
|
||||
val targetSession =
|
||||
when {
|
||||
sessionId != null -> repository.getSessionById(sessionId)
|
||||
else -> repository.getActiveSession()
|
||||
}
|
||||
if (targetSession != null &&
|
||||
targetSession.status ==
|
||||
com.atridad.ascently.data.model.SessionStatus.ACTIVE
|
||||
) {
|
||||
val completed =
|
||||
with(com.atridad.ascently.data.model.ClimbSession) {
|
||||
targetSession.complete()
|
||||
}
|
||||
repository.updateSession(completed)
|
||||
}
|
||||
} finally {
|
||||
stopSessionTracking()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return START_REDELIVER_INTENT
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? = null
|
||||
|
||||
private fun startSessionTracking(sessionId: String) {
|
||||
notificationJob?.cancel()
|
||||
monitoringJob?.cancel()
|
||||
|
||||
try {
|
||||
createAndShowNotification(sessionId)
|
||||
// Update widget when session tracking starts
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(this)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(LOG_TAG, e) { "Failed to initialize session tracking notification" }
|
||||
}
|
||||
|
||||
notificationJob =
|
||||
serviceScope.launch {
|
||||
try {
|
||||
if (!isNotificationActive()) {
|
||||
delay(1000L)
|
||||
createAndShowNotification(sessionId)
|
||||
}
|
||||
|
||||
while (isActive) {
|
||||
delay(5000L)
|
||||
updateNotification(sessionId)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(LOG_TAG, e) { "Notification updater loop crashed" }
|
||||
}
|
||||
}
|
||||
|
||||
monitoringJob =
|
||||
serviceScope.launch {
|
||||
try {
|
||||
while (isActive) {
|
||||
delay(10000L)
|
||||
|
||||
if (!isNotificationActive()) {
|
||||
updateNotification(sessionId)
|
||||
}
|
||||
|
||||
val session = repository.getSessionById(sessionId)
|
||||
if (session == null ||
|
||||
session.status !=
|
||||
com.atridad.ascently.data.model.SessionStatus
|
||||
.ACTIVE
|
||||
) {
|
||||
stopSessionTracking()
|
||||
break
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(LOG_TAG, e) { "Session monitoring loop crashed" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopSessionTracking() {
|
||||
notificationJob?.cancel()
|
||||
monitoringJob?.cancel()
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
// Update widget when session tracking stops
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(this)
|
||||
}
|
||||
|
||||
private fun isNotificationActive(): Boolean {
|
||||
return try {
|
||||
val activeNotifications = notificationManager.activeNotifications
|
||||
activeNotifications.any { it.id == NOTIFICATION_ID }
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateNotification(sessionId: String) {
|
||||
try {
|
||||
createAndShowNotification(sessionId)
|
||||
// Update widget when notification updates
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(this)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(LOG_TAG, e) { "Failed to update notification; retrying in 10s" }
|
||||
|
||||
try {
|
||||
delay(10000L)
|
||||
createAndShowNotification(sessionId)
|
||||
} catch (retryException: Exception) {
|
||||
AppLogger.e(LOG_TAG, retryException) { "Retrying notification update failed" }
|
||||
stopSessionTracking()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun createAndShowNotification(sessionId: String) {
|
||||
try {
|
||||
val session = runBlocking { repository.getSessionById(sessionId) }
|
||||
if (session == null ||
|
||||
session.status != com.atridad.ascently.data.model.SessionStatus.ACTIVE
|
||||
) {
|
||||
stopSessionTracking()
|
||||
return
|
||||
}
|
||||
|
||||
val gym = runBlocking { repository.getGymById(session.gymId) }
|
||||
|
||||
val attempts = runBlocking {
|
||||
repository.getAttemptsBySession(sessionId).firstOrNull() ?: emptyList()
|
||||
}
|
||||
|
||||
val duration =
|
||||
session.startTime?.let { startTime ->
|
||||
try {
|
||||
val start = LocalDateTime.parse(startTime)
|
||||
val now = LocalDateTime.now()
|
||||
val totalSeconds = ChronoUnit.SECONDS.between(start, now)
|
||||
val hours = totalSeconds / 3600
|
||||
val minutes = (totalSeconds % 3600) / 60
|
||||
val seconds = totalSeconds % 60
|
||||
|
||||
when {
|
||||
hours > 0 -> "${hours}h ${minutes}m ${seconds}s"
|
||||
minutes > 0 -> "${minutes}m ${seconds}s"
|
||||
else -> "${totalSeconds}s"
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
"Active"
|
||||
}
|
||||
}
|
||||
?: "Active"
|
||||
|
||||
val notification =
|
||||
NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setContentTitle("Climbing Session Active")
|
||||
.setContentText(
|
||||
"${gym?.name ?: "Gym"} • $duration • ${attempts.size} attempts"
|
||||
)
|
||||
.setSmallIcon(R.drawable.ic_mountains)
|
||||
.setOngoing(true)
|
||||
.setAutoCancel(false)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setCategory(NotificationCompat.CATEGORY_SERVICE)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.setContentIntent(createOpenAppIntent())
|
||||
.addAction(
|
||||
R.drawable.ic_mountains,
|
||||
"Open Session",
|
||||
createOpenAppIntent()
|
||||
)
|
||||
.addAction(
|
||||
android.R.drawable.ic_menu_close_clear_cancel,
|
||||
"End Session",
|
||||
createStopPendingIntent(sessionId)
|
||||
)
|
||||
.build()
|
||||
|
||||
startForeground(NOTIFICATION_ID, notification)
|
||||
|
||||
notificationManager.notify(NOTIFICATION_ID, notification)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e(LOG_TAG, e) { "Failed to build session tracking notification" }
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
private fun createOpenAppIntent(): PendingIntent {
|
||||
val intent =
|
||||
Intent(this, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
action = "OPEN_SESSION"
|
||||
}
|
||||
return PendingIntent.getActivity(
|
||||
this,
|
||||
0,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
}
|
||||
|
||||
private fun createStopPendingIntent(sessionId: String): PendingIntent {
|
||||
val intent = createStopIntent(this, sessionId)
|
||||
return PendingIntent.getService(
|
||||
this,
|
||||
1,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
}
|
||||
|
||||
private fun createNotificationChannel() {
|
||||
val channel =
|
||||
NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"Session Tracking",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
)
|
||||
.apply {
|
||||
description = "Shows active climbing session information"
|
||||
setShowBadge(false)
|
||||
lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
|
||||
enableLights(false)
|
||||
enableVibration(false)
|
||||
setSound(null, null)
|
||||
}
|
||||
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
notificationJob?.cancel()
|
||||
monitoringJob?.cancel()
|
||||
serviceScope.cancel()
|
||||
}
|
||||
}
|
||||
390
android/app/src/main/java/com/atridad/ascently/ui/AscentlyApp.kt
Normal file
@@ -0,0 +1,390 @@
|
||||
package com.atridad.ascently.ui
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.PlayArrow
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.toRoute
|
||||
import com.atridad.ascently.data.database.AscentlyDatabase
|
||||
import com.atridad.ascently.data.repository.ClimbRepository
|
||||
import com.atridad.ascently.data.sync.SyncService
|
||||
import com.atridad.ascently.navigation.Screen
|
||||
import com.atridad.ascently.navigation.bottomNavigationItems
|
||||
import com.atridad.ascently.ui.components.NotificationPermissionDialog
|
||||
import com.atridad.ascently.ui.screens.*
|
||||
import com.atridad.ascently.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.ascently.ui.viewmodel.ClimbViewModelFactory
|
||||
import com.atridad.ascently.utils.AppLogger
|
||||
import com.atridad.ascently.utils.AppShortcutManager
|
||||
import com.atridad.ascently.utils.NotificationPermissionUtils
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun AscentlyApp(
|
||||
shortcutAction: String? = null,
|
||||
lastUsedGymId: String? = null,
|
||||
onShortcutActionProcessed: () -> Unit = {}
|
||||
) {
|
||||
val navController = rememberNavController()
|
||||
val context = LocalContext.current
|
||||
|
||||
var lastUsedGym by remember { mutableStateOf<com.atridad.ascently.data.model.Gym?>(null) }
|
||||
|
||||
val database = remember { AscentlyDatabase.getDatabase(context) }
|
||||
val repository = remember { ClimbRepository(database, context) }
|
||||
val syncService = remember { SyncService(context, repository) }
|
||||
val viewModel: ClimbViewModel =
|
||||
viewModel(factory = ClimbViewModelFactory(repository, syncService, context))
|
||||
|
||||
var showNotificationPermissionDialog by remember { mutableStateOf(false) }
|
||||
var hasCheckedNotificationPermission by remember { mutableStateOf(false) }
|
||||
|
||||
val permissionLauncher =
|
||||
rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.RequestPermission()
|
||||
) { isGranted: Boolean ->
|
||||
if (!isGranted) {
|
||||
showNotificationPermissionDialog = false
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
if (!hasCheckedNotificationPermission) {
|
||||
hasCheckedNotificationPermission = true
|
||||
|
||||
if (NotificationPermissionUtils.shouldRequestNotificationPermission() &&
|
||||
!NotificationPermissionUtils.isNotificationPermissionGranted(context)
|
||||
) {
|
||||
showNotificationPermissionDialog = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) { viewModel.ensureSessionTrackingServiceRunning(context) }
|
||||
|
||||
LaunchedEffect(Unit) { syncService.triggerAutoSync() }
|
||||
|
||||
val activeSession by viewModel.activeSession.collectAsState()
|
||||
val gyms by viewModel.gyms.collectAsState()
|
||||
|
||||
LaunchedEffect(gyms) {
|
||||
if (gyms.isNotEmpty() && lastUsedGym == null) {
|
||||
lastUsedGym = viewModel.getLastUsedGym()
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(activeSession, gyms, lastUsedGym) {
|
||||
AppShortcutManager.updateShortcuts(
|
||||
context = context,
|
||||
hasActiveSession = activeSession != null,
|
||||
hasGyms = gyms.isNotEmpty(),
|
||||
lastUsedGym = if (activeSession == null && gyms.size > 1) lastUsedGym else null
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(shortcutAction) {
|
||||
when (shortcutAction) {
|
||||
AppShortcutManager.ACTION_START_SESSION -> {
|
||||
navController.navigate(Screen.Sessions) {
|
||||
popUpTo(0) { inclusive = true }
|
||||
launchSingleTop = true
|
||||
}
|
||||
}
|
||||
|
||||
AppShortcutManager.ACTION_END_SESSION -> {
|
||||
navController.navigate(Screen.Sessions) {
|
||||
popUpTo(0) { inclusive = true }
|
||||
launchSingleTop = true
|
||||
}
|
||||
|
||||
activeSession?.let { session -> viewModel.endSession(context, session.id) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(shortcutAction, activeSession, gyms, lastUsedGym) {
|
||||
if (shortcutAction == AppShortcutManager.ACTION_START_SESSION && gyms.isNotEmpty()) {
|
||||
AppLogger.d("AscentlyApp") { "Processing shortcut action: activeSession=$activeSession, gyms.size=${gyms.size}, lastUsedGymId=$lastUsedGymId, lastUsedGym=${lastUsedGym?.name}" }
|
||||
|
||||
if (activeSession == null) {
|
||||
if (NotificationPermissionUtils.shouldRequestNotificationPermission() &&
|
||||
!NotificationPermissionUtils.isNotificationPermissionGranted(
|
||||
context
|
||||
)
|
||||
) {
|
||||
AppLogger.d("AscentlyApp") { "Showing notification permission dialog" }
|
||||
showNotificationPermissionDialog = true
|
||||
} else {
|
||||
if (gyms.size == 1) {
|
||||
AppLogger.d("AscentlyApp") { "Starting session with single gym: ${gyms.first().name}" }
|
||||
viewModel.startSession(context, gyms.first().id)
|
||||
} else {
|
||||
val targetGym =
|
||||
lastUsedGymId?.let { gymId -> gyms.find { it.id == gymId } }
|
||||
?: lastUsedGym
|
||||
|
||||
if (targetGym != null) {
|
||||
AppLogger.d("AscentlyApp") { "Starting session with target gym: ${targetGym.name}" }
|
||||
viewModel.startSession(context, targetGym.id)
|
||||
} else {
|
||||
AppLogger.d("AscentlyApp") { "No target gym found, navigating to selection" }
|
||||
navController.navigate(Screen.AddEditSession())
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
AppLogger.d("AscentlyApp") { "Active session already exists: ${activeSession?.id}" }
|
||||
}
|
||||
|
||||
onShortcutActionProcessed()
|
||||
}
|
||||
}
|
||||
|
||||
var fabConfig by remember { mutableStateOf<FabConfig?>(null) }
|
||||
|
||||
Scaffold(
|
||||
bottomBar = { AscentlyBottomNavigation(navController = navController) },
|
||||
floatingActionButton = {
|
||||
fabConfig?.let { config ->
|
||||
FloatingActionButton(
|
||||
onClick = config.onClick,
|
||||
containerColor = MaterialTheme.colorScheme.primary
|
||||
) {
|
||||
Icon(
|
||||
imageVector = config.icon,
|
||||
contentDescription = config.contentDescription
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
) { innerPadding ->
|
||||
NavHost(
|
||||
navController = navController,
|
||||
startDestination = Screen.Sessions,
|
||||
modifier = Modifier.padding(innerPadding)
|
||||
) {
|
||||
composable<Screen.Sessions> {
|
||||
LaunchedEffect(gyms, activeSession) {
|
||||
fabConfig =
|
||||
if (gyms.isNotEmpty() && activeSession == null) {
|
||||
FabConfig(
|
||||
icon = Icons.Default.PlayArrow,
|
||||
contentDescription = "Start Session",
|
||||
onClick = {
|
||||
if (NotificationPermissionUtils
|
||||
.shouldRequestNotificationPermission() &&
|
||||
!NotificationPermissionUtils
|
||||
.isNotificationPermissionGranted(
|
||||
context
|
||||
)
|
||||
) {
|
||||
showNotificationPermissionDialog = true
|
||||
} else {
|
||||
navController.navigate(Screen.AddEditSession())
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
SessionsScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateToSessionDetail = { sessionId ->
|
||||
navController.navigate(Screen.SessionDetail(sessionId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.Problems> {
|
||||
LaunchedEffect(gyms) {
|
||||
fabConfig =
|
||||
if (gyms.isNotEmpty()) {
|
||||
FabConfig(
|
||||
icon = Icons.Default.Add,
|
||||
contentDescription = "Add Problem",
|
||||
onClick = {
|
||||
navController.navigate(Screen.AddEditProblem())
|
||||
}
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
ProblemsScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateToProblemDetail = { problemId ->
|
||||
navController.navigate(Screen.ProblemDetail(problemId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.Analytics> {
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
AnalyticsScreen(viewModel = viewModel)
|
||||
}
|
||||
|
||||
composable<Screen.Gyms> {
|
||||
LaunchedEffect(Unit) {
|
||||
fabConfig =
|
||||
FabConfig(
|
||||
icon = Icons.Default.Add,
|
||||
contentDescription = "Add Gym",
|
||||
onClick = { navController.navigate(Screen.AddEditGym()) }
|
||||
)
|
||||
}
|
||||
GymsScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateToGymDetail = { gymId ->
|
||||
navController.navigate(Screen.GymDetail(gymId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.Settings> {
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
SettingsScreen(viewModel = viewModel)
|
||||
}
|
||||
|
||||
composable<Screen.SessionDetail> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.SessionDetail>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
SessionDetailScreen(
|
||||
sessionId = args.sessionId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() },
|
||||
onNavigateToProblemDetail = { problemId ->
|
||||
navController.navigate(Screen.ProblemDetail(problemId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.ProblemDetail> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.ProblemDetail>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
ProblemDetailScreen(
|
||||
problemId = args.problemId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() },
|
||||
onNavigateToEdit = { problemId ->
|
||||
navController.navigate(Screen.AddEditProblem(problemId = problemId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.GymDetail> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.GymDetail>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
GymDetailScreen(
|
||||
gymId = args.gymId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() },
|
||||
onNavigateToEdit = { gymId ->
|
||||
navController.navigate(Screen.AddEditGym(gymId = gymId))
|
||||
},
|
||||
onNavigateToSessionDetail = { sessionId ->
|
||||
navController.navigate(Screen.SessionDetail(sessionId))
|
||||
},
|
||||
onNavigateToProblemDetail = { problemId ->
|
||||
navController.navigate(Screen.ProblemDetail(problemId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.AddEditGym> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.AddEditGym>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
AddEditGymScreen(
|
||||
gymId = args.gymId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() }
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.AddEditProblem> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.AddEditProblem>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
AddEditProblemScreen(
|
||||
problemId = args.problemId,
|
||||
gymId = args.gymId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() }
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.AddEditSession> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.AddEditSession>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
AddEditSessionScreen(
|
||||
sessionId = args.sessionId,
|
||||
gymId = args.gymId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (showNotificationPermissionDialog) {
|
||||
NotificationPermissionDialog(
|
||||
onDismiss = { showNotificationPermissionDialog = false },
|
||||
onRequestPermission = {
|
||||
permissionLauncher.launch(
|
||||
NotificationPermissionUtils.getNotificationPermissionString()
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun AscentlyBottomNavigation(navController: NavHostController) {
|
||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||
val currentRoute = navBackStackEntry?.destination?.route
|
||||
|
||||
NavigationBar {
|
||||
bottomNavigationItems.forEach { item ->
|
||||
val isSelected =
|
||||
when (item.screen) {
|
||||
is Screen.Sessions -> currentRoute?.contains("Session") == true
|
||||
is Screen.Problems -> currentRoute?.contains("Problem") == true
|
||||
is Screen.Gyms -> currentRoute?.contains("Gym") == true
|
||||
is Screen.Analytics -> currentRoute?.contains("Analytics") == true
|
||||
is Screen.Settings -> currentRoute?.contains("Settings") == true
|
||||
else -> currentRoute?.contains(item.screen::class.simpleName ?: "") == true
|
||||
}
|
||||
|
||||
NavigationBarItem(
|
||||
icon = { Icon(item.icon, contentDescription = item.label) },
|
||||
label = { Text(item.label) },
|
||||
selected = isSelected,
|
||||
onClick = {
|
||||
navController.navigate(item.screen) {
|
||||
popUpTo(0) { inclusive = true }
|
||||
launchSingleTop = true
|
||||
// Don't restore state - always start fresh when switching tabs
|
||||
restoreState = false
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class FabConfig(
|
||||
val icon: androidx.compose.ui.graphics.vector.ImageVector,
|
||||
val contentDescription: String,
|
||||
val onClick: () -> Unit
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.components
|
||||
package com.atridad.ascently.ui.components
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
@@ -10,9 +10,9 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.openclimb.data.model.ClimbSession
|
||||
import com.atridad.openclimb.data.model.Gym
|
||||
import com.atridad.openclimb.ui.theme.CustomIcons
|
||||
import com.atridad.ascently.data.model.ClimbSession
|
||||
import com.atridad.ascently.data.model.Gym
|
||||
import com.atridad.ascently.ui.theme.CustomIcons
|
||||
import kotlinx.coroutines.delay
|
||||
import java.time.LocalDateTime
|
||||
import java.time.temporal.ChronoUnit
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.components
|
||||
package com.atridad.ascently.ui.components
|
||||
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.layout.Box
|
||||
@@ -0,0 +1,161 @@
|
||||
package com.atridad.ascently.ui.components
|
||||
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.pager.HorizontalPager
|
||||
import androidx.compose.foundation.pager.rememberPagerState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
fun FullscreenImageViewer(imagePaths: List<String>, initialIndex: Int = 0, onDismiss: () -> Unit) {
|
||||
val pagerState = rememberPagerState(initialPage = initialIndex, pageCount = { imagePaths.size })
|
||||
val thumbnailListState = rememberLazyListState()
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
// Handle back button press
|
||||
BackHandler { onDismiss() }
|
||||
|
||||
// Auto-scroll thumbnail list to center current image
|
||||
LaunchedEffect(pagerState.currentPage) {
|
||||
if (imagePaths.size > 1) {
|
||||
thumbnailListState.animateScrollToItem(
|
||||
index = pagerState.currentPage,
|
||||
scrollOffset = -200
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Dialog(
|
||||
onDismissRequest = onDismiss,
|
||||
properties =
|
||||
DialogProperties(usePlatformDefaultWidth = false, decorFitsSystemWindows = true)
|
||||
) {
|
||||
Box(modifier = Modifier.fillMaxSize().background(Color.Black).systemBarsPadding()) {
|
||||
// Main image pager
|
||||
HorizontalPager(state = pagerState, modifier = Modifier.fillMaxSize()) { page ->
|
||||
OrientationAwareImage(
|
||||
imagePath = imagePaths[page],
|
||||
contentDescription = "Full screen image",
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentScale = ContentScale.Fit
|
||||
)
|
||||
}
|
||||
|
||||
// Top bar with back button and counter
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxWidth().align(Alignment.TopStart),
|
||||
color = Color.Black.copy(alpha = 0.6f)
|
||||
) {
|
||||
Row(
|
||||
modifier =
|
||||
Modifier.fillMaxWidth().padding(horizontal = 4.dp, vertical = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
// Back button
|
||||
IconButton(onClick = onDismiss) {
|
||||
Icon(
|
||||
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
|
||||
contentDescription = "Close",
|
||||
tint = Color.White
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
|
||||
// Image counter
|
||||
if (imagePaths.size > 1) {
|
||||
Text(
|
||||
text = "${pagerState.currentPage + 1} / ${imagePaths.size}",
|
||||
color = Color.White,
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(16.dp))
|
||||
}
|
||||
}
|
||||
|
||||
// Thumbnail strip at bottom (if multiple images)
|
||||
if (imagePaths.size > 1) {
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxWidth().align(Alignment.BottomCenter),
|
||||
color = Color.Black.copy(alpha = 0.6f)
|
||||
) {
|
||||
LazyRow(
|
||||
state = thumbnailListState,
|
||||
modifier = Modifier.padding(vertical = 12.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
contentPadding = PaddingValues(horizontal = 16.dp)
|
||||
) {
|
||||
itemsIndexed(imagePaths) { index, imagePath ->
|
||||
val isSelected = index == pagerState.currentPage
|
||||
|
||||
Box(
|
||||
modifier =
|
||||
Modifier.size(48.dp)
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.clickable {
|
||||
coroutineScope.launch {
|
||||
pagerState.animateScrollToPage(index)
|
||||
}
|
||||
}
|
||||
) {
|
||||
OrientationAwareImage(
|
||||
imagePath = imagePath,
|
||||
contentDescription = "Thumbnail ${index + 1}",
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
|
||||
// Selection indicator
|
||||
if (isSelected) {
|
||||
Box(
|
||||
modifier =
|
||||
Modifier.fillMaxSize()
|
||||
.background(
|
||||
Color.White.copy(alpha = 0.3f),
|
||||
RoundedCornerShape(8.dp)
|
||||
)
|
||||
)
|
||||
Box(
|
||||
modifier =
|
||||
Modifier.fillMaxSize()
|
||||
.background(
|
||||
Color.Transparent,
|
||||
RoundedCornerShape(8.dp)
|
||||
)
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.background(
|
||||
Color.White.copy(alpha = 0.2f)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.atridad.ascently.ui.components
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
@Composable
|
||||
fun ImageDisplay(
|
||||
imagePaths: List<String>,
|
||||
modifier: Modifier = Modifier,
|
||||
imageSize: Int = 120,
|
||||
onImageClick: ((Int) -> Unit)? = null
|
||||
) {
|
||||
LocalContext.current
|
||||
|
||||
if (imagePaths.isNotEmpty()) {
|
||||
LazyRow(modifier = modifier, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
itemsIndexed(imagePaths) { index, imagePath ->
|
||||
OrientationAwareImage(
|
||||
imagePath = imagePath,
|
||||
contentDescription = "Problem photo",
|
||||
modifier =
|
||||
Modifier.size(imageSize.dp)
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.clickable(enabled = onImageClick != null) {
|
||||
onImageClick?.invoke(index)
|
||||
},
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ImageDisplaySection(
|
||||
imagePaths: List<String>,
|
||||
modifier: Modifier = Modifier,
|
||||
title: String = "Photos",
|
||||
onImageClick: ((Int) -> Unit)? = null
|
||||
) {
|
||||
if (imagePaths.isNotEmpty()) {
|
||||
Column(modifier = modifier) {
|
||||
Text(
|
||||
text = title,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
ImageDisplay(imagePaths = imagePaths, imageSize = 120, onImageClick = onImageClick)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.components
|
||||
package com.atridad.ascently.ui.components
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
@@ -25,8 +25,7 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.FileProvider
|
||||
import coil.compose.AsyncImage
|
||||
import com.atridad.openclimb.utils.ImageUtils
|
||||
import com.atridad.ascently.utils.ImageUtils
|
||||
import java.io.File
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
@@ -256,11 +255,11 @@ private fun createImageFile(context: android.content.Context): File {
|
||||
@Composable
|
||||
private fun ImageItem(imagePath: String, onRemove: () -> Unit, modifier: Modifier = Modifier) {
|
||||
val context = LocalContext.current
|
||||
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
||||
ImageUtils.getImageFile(context, imagePath)
|
||||
|
||||
Box(modifier = modifier.size(80.dp)) {
|
||||
AsyncImage(
|
||||
model = imageFile,
|
||||
OrientationAwareImage(
|
||||
imagePath = imagePath,
|
||||
contentDescription = "Problem photo",
|
||||
modifier = Modifier.fillMaxSize().clip(RoundedCornerShape(8.dp)),
|
||||
contentScale = ContentScale.Crop
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.components
|
||||
package com.atridad.ascently.ui.components
|
||||
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.layout.Box
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.atridad.ascently.ui.components
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Notifications
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
|
||||
@Composable
|
||||
fun NotificationPermissionDialog(onDismiss: () -> Unit, onRequestPermission: () -> Unit) {
|
||||
Dialog(
|
||||
onDismissRequest = onDismiss,
|
||||
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false)
|
||||
) {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
shape = MaterialTheme.shapes.medium
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.padding(24.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Notifications,
|
||||
contentDescription = "Notifications",
|
||||
modifier = Modifier.size(48.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Text(
|
||||
text = "Enable Notifications",
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
fontWeight = MaterialTheme.typography.headlineSmall.fontWeight,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
Text(
|
||||
text =
|
||||
"Ascently needs notification permission to show your active climbing session. This helps you track your progress and ensures the session doesn't get interrupted.",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = TextAlign.Center,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
TextButton(onClick = onDismiss, modifier = Modifier.weight(1f)) {
|
||||
Text("Not Now")
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
onRequestPermission()
|
||||
onDismiss()
|
||||
},
|
||||
modifier = Modifier.weight(1f)
|
||||
) { Text("Enable") }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package com.atridad.ascently.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.foundation.layout.size
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
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.compose.ui.unit.dp
|
||||
import androidx.exifinterface.media.ExifInterface
|
||||
import com.atridad.ascently.utils.ImageUtils
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun OrientationAwareImage(
|
||||
imagePath: String,
|
||||
modifier: Modifier = Modifier,
|
||||
contentDescription: String? = null,
|
||||
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 (_: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
imageBitmap = bitmap
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
Box(modifier = modifier) {
|
||||
if (isLoading) {
|
||||
CircularProgressIndicator(modifier = Modifier.size(32.dp).align(Alignment.Center))
|
||||
} 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 -> {
|
||||
// Default case - no transformation needed
|
||||
}
|
||||
}
|
||||
|
||||
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 (_: Exception) {
|
||||
bitmap
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.components
|
||||
package com.atridad.ascently.ui.components
|
||||
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.fadeIn
|
||||
@@ -0,0 +1,297 @@
|
||||
package com.atridad.ascently.ui.health
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.*
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.ascently.data.health.HealthConnectManager
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun HealthConnectCard(modifier: Modifier = Modifier) {
|
||||
val context = LocalContext.current
|
||||
val healthConnectManager = remember { HealthConnectManager(context) }
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
// State tracking
|
||||
var isHealthConnectAvailable by remember { mutableStateOf(false) }
|
||||
var isLoading by remember { mutableStateOf(true) }
|
||||
var errorMessage by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
// Collect flows
|
||||
val isEnabled by healthConnectManager.isEnabled.collectAsState(initial = false)
|
||||
val hasPermissions by healthConnectManager.hasPermissions.collectAsState(initial = false)
|
||||
|
||||
val isCompatible by healthConnectManager.isCompatible.collectAsState(initial = true)
|
||||
|
||||
// Permission launcher
|
||||
val permissionLauncher =
|
||||
rememberLauncherForActivityResult(
|
||||
contract = healthConnectManager.getPermissionRequestContract()
|
||||
) { _ ->
|
||||
coroutineScope.launch {
|
||||
val allGranted = healthConnectManager.hasAllPermissions()
|
||||
if (!allGranted) {
|
||||
errorMessage =
|
||||
"Some Health Connect permissions were not granted. Please grant all permissions to enable syncing."
|
||||
} else {
|
||||
errorMessage = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check Health Connect availability on first load
|
||||
LaunchedEffect(Unit) {
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
healthConnectManager.isHealthConnectAvailable().collect { available ->
|
||||
isHealthConnectAvailable = available
|
||||
isLoading = false
|
||||
|
||||
if (!available && isCompatible) {
|
||||
errorMessage = "Health Connect is not available on this device"
|
||||
} else if (!isCompatible) {
|
||||
errorMessage =
|
||||
"Health Connect API compatibility issue. Please update your device or the app."
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
isLoading = false
|
||||
errorMessage = "Error checking Health Connect availability: ${e.message}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(16.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceVariant
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth().padding(20.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
// Header with icon and title
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.HealthAndSafety,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(32.dp),
|
||||
tint =
|
||||
if (isHealthConnectAvailable && isEnabled && hasPermissions) {
|
||||
MaterialTheme.colorScheme.primary
|
||||
} else {
|
||||
MaterialTheme.colorScheme.onSurfaceVariant
|
||||
}
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = "Health Connect",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
|
||||
Text(
|
||||
text =
|
||||
when {
|
||||
isLoading -> "Checking availability..."
|
||||
!isCompatible -> "API Issue"
|
||||
!isHealthConnectAvailable -> "Not available"
|
||||
isEnabled && hasPermissions -> "Connected"
|
||||
isEnabled && !hasPermissions -> "Needs permissions"
|
||||
else -> "Disabled"
|
||||
},
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color =
|
||||
when {
|
||||
isLoading ->
|
||||
MaterialTheme.colorScheme.onSurfaceVariant.copy(
|
||||
alpha = 0.7f
|
||||
)
|
||||
|
||||
!isCompatible -> MaterialTheme.colorScheme.error
|
||||
!isHealthConnectAvailable -> MaterialTheme.colorScheme.error
|
||||
isEnabled && hasPermissions ->
|
||||
MaterialTheme.colorScheme.primary
|
||||
|
||||
isEnabled && !hasPermissions ->
|
||||
MaterialTheme.colorScheme.tertiary
|
||||
|
||||
else ->
|
||||
MaterialTheme.colorScheme.onSurfaceVariant.copy(
|
||||
alpha = 0.7f
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Main toggle switch
|
||||
Switch(
|
||||
checked = isEnabled,
|
||||
onCheckedChange = { enabled ->
|
||||
coroutineScope.launch {
|
||||
if (enabled && isHealthConnectAvailable) {
|
||||
healthConnectManager.setEnabled(true)
|
||||
try {
|
||||
val permissionSet =
|
||||
healthConnectManager.getRequiredPermissions()
|
||||
if (permissionSet.isNotEmpty()) {
|
||||
permissionLauncher.launch(permissionSet)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
errorMessage = "Error requesting permissions: ${e.message}"
|
||||
}
|
||||
} else {
|
||||
healthConnectManager.setEnabled(false)
|
||||
errorMessage = null
|
||||
}
|
||||
}
|
||||
},
|
||||
enabled = isHealthConnectAvailable && !isLoading && isCompatible
|
||||
)
|
||||
}
|
||||
|
||||
if (isEnabled) {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Text(
|
||||
text = "Climbing sessions will be automatically added to Health Connect when completed.",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
|
||||
if (!hasPermissions) {
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
Card(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.errorContainer.copy(
|
||||
alpha = 0.3f
|
||||
)
|
||||
)
|
||||
) {
|
||||
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Warning,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(20.dp),
|
||||
tint = MaterialTheme.colorScheme.error
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
|
||||
Text(
|
||||
text = "Permissions needed",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
fontWeight = FontWeight.Medium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Text(
|
||||
text =
|
||||
"Grant Health Connect permissions to sync your climbing sessions",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color =
|
||||
MaterialTheme.colorScheme.onSurfaceVariant.copy(
|
||||
alpha = 0.8f
|
||||
)
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
OutlinedButton(
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
val permissionSet =
|
||||
healthConnectManager
|
||||
.getRequiredPermissions()
|
||||
if (permissionSet.isNotEmpty()) {
|
||||
permissionLauncher.launch(permissionSet)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
errorMessage =
|
||||
"Error requesting permissions: ${e.message}"
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) { Text("Grant Permissions") }
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
Text(
|
||||
text =
|
||||
"Sync your climbing sessions to Samsung Health, Google Fit, and other fitness apps through Health Connect.",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = TextAlign.Center,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.8f)
|
||||
)
|
||||
}
|
||||
errorMessage?.let { error ->
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
Card(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.errorContainer.copy(
|
||||
alpha = 0.5f
|
||||
)
|
||||
)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Warning,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.error
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
|
||||
Text(
|
||||
text = error,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onErrorContainer
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.screens
|
||||
package com.atridad.ascently.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
@@ -17,9 +17,9 @@ import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.openclimb.data.model.*
|
||||
import com.atridad.openclimb.ui.components.ImagePicker
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.ascently.data.model.*
|
||||
import com.atridad.ascently.ui.components.ImagePicker
|
||||
import com.atridad.ascently.ui.viewmodel.ClimbViewModel
|
||||
import java.time.LocalDateTime
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
@@ -40,7 +40,7 @@ fun AddEditGymScreen(gymId: String?, viewModel: ClimbViewModel, onNavigateBack:
|
||||
emptyList()
|
||||
} else {
|
||||
selectedClimbTypes
|
||||
.flatMap { climbType -> DifficultySystem.getSystemsForClimbType(climbType) }
|
||||
.flatMap { climbType -> DifficultySystem.systemsForClimbType(climbType) }
|
||||
.distinct()
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ fun AddEditGymScreen(gymId: String?, viewModel: ClimbViewModel, onNavigateBack:
|
||||
)
|
||||
|
||||
if (isEditing) {
|
||||
viewModel.updateGym(gym.copy(id = gymId!!))
|
||||
viewModel.updateGym(gym.copy(id = gymId))
|
||||
} else {
|
||||
viewModel.addGym(gym)
|
||||
}
|
||||
@@ -164,7 +164,7 @@ fun AddEditGymScreen(gymId: String?, viewModel: ClimbViewModel, onNavigateBack:
|
||||
onCheckedChange = null
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(climbType.getDisplayName())
|
||||
Text(climbType.displayName)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -219,7 +219,7 @@ fun AddEditGymScreen(gymId: String?, viewModel: ClimbViewModel, onNavigateBack:
|
||||
onCheckedChange = null
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(system.getDisplayName())
|
||||
Text(system.displayName)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -248,7 +248,6 @@ fun AddEditProblemScreen(
|
||||
) {
|
||||
val isEditing = problemId != null
|
||||
val gyms by viewModel.gyms.collectAsState()
|
||||
val context = LocalContext.current
|
||||
|
||||
// Problem form state
|
||||
var selectedGym by remember {
|
||||
@@ -295,7 +294,7 @@ fun AddEditProblemScreen(
|
||||
|
||||
val availableClimbTypes = selectedGym?.supportedClimbTypes ?: ClimbType.entries.toList()
|
||||
val availableDifficultySystems =
|
||||
DifficultySystem.getSystemsForClimbType(selectedClimbType).filter { system ->
|
||||
DifficultySystem.systemsForClimbType(selectedClimbType).filter { system ->
|
||||
selectedGym?.difficultySystems?.contains(system) != false
|
||||
}
|
||||
|
||||
@@ -324,7 +323,7 @@ fun AddEditProblemScreen(
|
||||
|
||||
// Reset grade when difficulty system changes (unless it's a valid grade for the new system)
|
||||
LaunchedEffect(selectedDifficultySystem) {
|
||||
val availableGrades = selectedDifficultySystem.getAvailableGrades()
|
||||
val availableGrades = selectedDifficultySystem.availableGrades
|
||||
if (availableGrades.isNotEmpty() && difficultyGrade !in availableGrades) {
|
||||
difficultyGrade = ""
|
||||
}
|
||||
@@ -387,12 +386,11 @@ fun AddEditProblemScreen(
|
||||
)
|
||||
|
||||
if (isEditing) {
|
||||
viewModel.updateProblem(
|
||||
problem.copy(id = problemId),
|
||||
context
|
||||
)
|
||||
problemId.let { id ->
|
||||
viewModel.updateProblem(problem.copy(id = id))
|
||||
}
|
||||
} else {
|
||||
viewModel.addProblem(problem, context)
|
||||
viewModel.addProblem(problem)
|
||||
}
|
||||
onNavigateBack()
|
||||
}
|
||||
@@ -505,7 +503,7 @@ fun AddEditProblemScreen(
|
||||
availableClimbTypes.forEach { climbType ->
|
||||
FilterChip(
|
||||
onClick = { selectedClimbType = climbType },
|
||||
label = { Text(climbType.getDisplayName()) },
|
||||
label = { Text(climbType.displayName) },
|
||||
selected = selectedClimbType == climbType
|
||||
)
|
||||
}
|
||||
@@ -538,7 +536,7 @@ fun AddEditProblemScreen(
|
||||
items(availableDifficultySystems) { system ->
|
||||
FilterChip(
|
||||
onClick = { selectedDifficultySystem = system },
|
||||
label = { Text(system.getDisplayName()) },
|
||||
label = { Text(system.displayName) },
|
||||
selected = selectedDifficultySystem == system
|
||||
)
|
||||
}
|
||||
@@ -570,7 +568,7 @@ fun AddEditProblemScreen(
|
||||
)
|
||||
} else {
|
||||
var expanded by remember { mutableStateOf(false) }
|
||||
val availableGrades = selectedDifficultySystem.getAvailableGrades()
|
||||
val availableGrades = selectedDifficultySystem.availableGrades
|
||||
|
||||
ExposedDropdownMenuBox(
|
||||
expanded = expanded,
|
||||
@@ -592,8 +590,7 @@ fun AddEditProblemScreen(
|
||||
.outlinedTextFieldColors(),
|
||||
modifier =
|
||||
Modifier.menuAnchor(
|
||||
androidx.compose.material3
|
||||
.MenuAnchorType
|
||||
ExposedDropdownMenuAnchorType
|
||||
.PrimaryNotEditable,
|
||||
enabled = true
|
||||
)
|
||||
@@ -765,9 +762,9 @@ fun AddEditSessionScreen(
|
||||
null
|
||||
}
|
||||
)
|
||||
viewModel.updateSession(
|
||||
session.copy(id = sessionId!!)
|
||||
)
|
||||
sessionId.let { id ->
|
||||
viewModel.updateSession(session.copy(id = id))
|
||||
}
|
||||
} else {
|
||||
viewModel.startSession(
|
||||
context,
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.screens
|
||||
package com.atridad.ascently.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
@@ -9,16 +9,16 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.openclimb.R
|
||||
import com.atridad.openclimb.data.model.AttemptResult
|
||||
import com.atridad.openclimb.data.model.ClimbType
|
||||
import com.atridad.openclimb.data.model.DifficultySystem
|
||||
import com.atridad.openclimb.ui.components.BarChart
|
||||
import com.atridad.openclimb.ui.components.BarChartDataPoint
|
||||
import com.atridad.openclimb.ui.components.SyncIndicator
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.ascently.R
|
||||
import com.atridad.ascently.data.model.AttemptResult
|
||||
import com.atridad.ascently.data.model.ClimbType
|
||||
import com.atridad.ascently.data.model.DifficultySystem
|
||||
import com.atridad.ascently.ui.components.BarChart
|
||||
import com.atridad.ascently.ui.components.BarChartDataPoint
|
||||
import com.atridad.ascently.ui.components.SyncIndicator
|
||||
import com.atridad.ascently.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
@Composable
|
||||
fun AnalyticsScreen(viewModel: ClimbViewModel) {
|
||||
@@ -39,7 +39,7 @@ fun AnalyticsScreen(viewModel: ClimbViewModel) {
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(id = R.drawable.ic_mountains),
|
||||
contentDescription = "OpenClimb Logo",
|
||||
contentDescription = "Ascently Logo",
|
||||
modifier = Modifier.size(32.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
@@ -200,7 +200,9 @@ fun GradeDistributionChartCard(gradeDistributionData: List<GradeDistributionData
|
||||
},
|
||||
modifier =
|
||||
Modifier.menuAnchor(
|
||||
type = ExposedDropdownMenuAnchorType.PrimaryNotEditable,
|
||||
type =
|
||||
ExposedDropdownMenuAnchorType
|
||||
.PrimaryNotEditable,
|
||||
enabled = true
|
||||
)
|
||||
.width(120.dp),
|
||||
@@ -251,11 +253,8 @@ fun GradeDistributionChartCard(gradeDistributionData: List<GradeDistributionData
|
||||
systemFiltered.filter { dataPoint ->
|
||||
try {
|
||||
val attemptDate =
|
||||
LocalDateTime.parse(
|
||||
dataPoint.date,
|
||||
DateTimeFormatter.ISO_LOCAL_DATE_TIME
|
||||
)
|
||||
attemptDate.isAfter(sevenDaysAgo)
|
||||
DateFormatUtils.parseToLocalDateTime(dataPoint.date)
|
||||
attemptDate?.isAfter(sevenDaysAgo) == true
|
||||
} catch (_: Exception) {
|
||||
// If date parsing fails, include the data point
|
||||
true
|
||||
@@ -394,9 +393,9 @@ data class GradeDistributionDataPoint(
|
||||
)
|
||||
|
||||
fun calculateGradeDistribution(
|
||||
sessions: List<com.atridad.openclimb.data.model.ClimbSession>,
|
||||
problems: List<com.atridad.openclimb.data.model.Problem>,
|
||||
attempts: List<com.atridad.openclimb.data.model.Attempt>
|
||||
sessions: List<com.atridad.ascently.data.model.ClimbSession>,
|
||||
problems: List<com.atridad.ascently.data.model.Problem>,
|
||||
attempts: List<com.atridad.ascently.data.model.Attempt>
|
||||
): List<GradeDistributionDataPoint> {
|
||||
if (sessions.isEmpty() || problems.isEmpty() || attempts.isEmpty()) {
|
||||
return emptyList()
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.screens
|
||||
package com.atridad.ascently.ui.screens
|
||||
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.clickable
|
||||
@@ -16,10 +16,8 @@ import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.Check
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material.icons.filled.Edit
|
||||
import androidx.compose.material.icons.filled.HealthAndSafety
|
||||
import androidx.compose.material.icons.filled.KeyboardArrowDown
|
||||
import androidx.compose.material.icons.filled.KeyboardArrowUp
|
||||
import androidx.compose.material.icons.filled.Share
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
@@ -30,16 +28,14 @@ import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.atridad.openclimb.data.model.*
|
||||
import com.atridad.openclimb.ui.components.FullscreenImageViewer
|
||||
import com.atridad.openclimb.ui.components.ImageDisplaySection
|
||||
import com.atridad.openclimb.ui.theme.CustomIcons
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import com.atridad.ascently.data.model.*
|
||||
import com.atridad.ascently.ui.components.FullscreenImageViewer
|
||||
import com.atridad.ascently.ui.components.ImageDisplaySection
|
||||
import com.atridad.ascently.ui.components.ImagePicker
|
||||
import com.atridad.ascently.ui.theme.CustomIcons
|
||||
import com.atridad.ascently.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -221,7 +217,6 @@ fun SessionDetailScreen(
|
||||
val problems by viewModel.problems.collectAsState()
|
||||
val gyms by viewModel.gyms.collectAsState()
|
||||
|
||||
var isGeneratingShare by remember { mutableStateOf(false) }
|
||||
var showDeleteDialog by remember { mutableStateOf(false) }
|
||||
var showAddAttemptDialog by remember { mutableStateOf(false) }
|
||||
var showEditAttemptDialog by remember { mutableStateOf<Attempt?>(null) }
|
||||
@@ -234,7 +229,7 @@ fun SessionDetailScreen(
|
||||
val successfulAttempts =
|
||||
attempts.filter { it.result in listOf(AttemptResult.SUCCESS, AttemptResult.FLASH) }
|
||||
val uniqueProblems = attempts.map { it.problemId }.distinct()
|
||||
val attemptedProblems = problems.filter { it.id in uniqueProblems }
|
||||
|
||||
val completedProblems = successfulAttempts.map { it.problemId }.distinct()
|
||||
|
||||
val attemptsWithProblems =
|
||||
@@ -261,64 +256,8 @@ fun SessionDetailScreen(
|
||||
}
|
||||
},
|
||||
actions = {
|
||||
if (session?.duration != null) {
|
||||
val healthConnectManager = viewModel.getHealthConnectManager()
|
||||
val isHealthConnectEnabled by
|
||||
healthConnectManager.isEnabled.collectAsState(
|
||||
initial = false
|
||||
)
|
||||
val hasPermissions by
|
||||
healthConnectManager.hasPermissions.collectAsState(
|
||||
initial = false
|
||||
)
|
||||
|
||||
if (isHealthConnectEnabled && hasPermissions) {
|
||||
IconButton(
|
||||
onClick = {
|
||||
viewModel.manualSyncToHealthConnect(sessionId)
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.HealthAndSafety,
|
||||
contentDescription = "Sync to Health Connect",
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Share button
|
||||
if (session?.duration != null) { // Only show for completed sessions
|
||||
IconButton(
|
||||
onClick = {
|
||||
isGeneratingShare = true
|
||||
viewModel.viewModelScope.launch {
|
||||
val shareFile =
|
||||
viewModel.generateSessionShareCard(
|
||||
context,
|
||||
sessionId
|
||||
)
|
||||
isGeneratingShare = false
|
||||
shareFile?.let { file ->
|
||||
viewModel.shareSessionCard(context, file)
|
||||
}
|
||||
}
|
||||
},
|
||||
enabled = !isGeneratingShare
|
||||
) {
|
||||
if (isGeneratingShare) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(20.dp),
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
} else {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Share,
|
||||
contentDescription = "Share Session"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
// No manual actions needed - Health Connect syncs automatically when
|
||||
// sessions complete
|
||||
|
||||
// Show stop icon for active sessions, delete icon for completed
|
||||
// sessions
|
||||
@@ -564,7 +503,7 @@ fun SessionDetailScreen(
|
||||
viewModel.addAttempt(attempt)
|
||||
showAddAttemptDialog = false
|
||||
},
|
||||
onProblemCreated = { problem -> viewModel.addProblem(problem, context) }
|
||||
onProblemCreated = { problem -> viewModel.addProblem(problem) }
|
||||
)
|
||||
}
|
||||
|
||||
@@ -590,7 +529,7 @@ fun ProblemDetailScreen(
|
||||
onNavigateBack: () -> Unit,
|
||||
onNavigateToEdit: (String) -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
|
||||
var showDeleteDialog by remember { mutableStateOf(false) }
|
||||
var showImageViewer by remember { mutableStateOf(false) }
|
||||
var selectedImageIndex by remember { mutableIntStateOf(0) }
|
||||
@@ -665,7 +604,7 @@ fun ProblemDetailScreen(
|
||||
problem?.let { p ->
|
||||
Text(
|
||||
text =
|
||||
"${p.difficulty.system.getDisplayName()}: ${p.difficulty.grade}",
|
||||
"${p.difficulty.system.displayName}: ${p.difficulty.grade}",
|
||||
style = MaterialTheme.typography.titleLarge,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
fontWeight = FontWeight.Bold
|
||||
@@ -674,7 +613,7 @@ fun ProblemDetailScreen(
|
||||
|
||||
problem?.let { p ->
|
||||
Text(
|
||||
text = p.climbType.getDisplayName(),
|
||||
text = p.climbType.displayName,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
@@ -854,7 +793,7 @@ fun ProblemDetailScreen(
|
||||
TextButton(
|
||||
onClick = {
|
||||
problem?.let { p ->
|
||||
viewModel.deleteProblem(p, context)
|
||||
viewModel.deleteProblem(p)
|
||||
onNavigateBack()
|
||||
}
|
||||
showDeleteDialog = false
|
||||
@@ -1236,19 +1175,10 @@ fun GymDetailScreen(
|
||||
}
|
||||
},
|
||||
supportingContent = {
|
||||
val dateTime =
|
||||
try {
|
||||
LocalDateTime.parse(session.date)
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
val formattedDate =
|
||||
dateTime?.format(
|
||||
DateTimeFormatter.ofPattern(
|
||||
"MMM dd, yyyy"
|
||||
)
|
||||
DateFormatUtils.formatDateForDisplay(
|
||||
session.date
|
||||
)
|
||||
?: session.date
|
||||
|
||||
Text(
|
||||
"$formattedDate • ${sessionAttempts.size} attempts"
|
||||
@@ -1463,7 +1393,7 @@ fun SessionAttemptCard(
|
||||
|
||||
Text(
|
||||
text =
|
||||
"${problem.difficulty.system.getDisplayName()}: ${problem.difficulty.grade}",
|
||||
"${problem.difficulty.system.displayName}: ${problem.difficulty.grade}",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
@@ -1538,14 +1468,7 @@ fun SessionAttemptCard(
|
||||
}
|
||||
|
||||
private fun formatDate(dateString: String): String {
|
||||
return try {
|
||||
val formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME
|
||||
val date = LocalDateTime.parse(dateString, formatter)
|
||||
val displayFormatter = DateTimeFormatter.ofPattern("MMM dd, yyyy")
|
||||
date.format(displayFormatter)
|
||||
} catch (_: Exception) {
|
||||
dateString.take(10) // Fallback to just the date part
|
||||
}
|
||||
return DateFormatUtils.formatDateForDisplay(dateString)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@@ -1567,6 +1490,7 @@ fun EnhancedAddAttemptDialog(
|
||||
// New problem creation state
|
||||
var newProblemName by remember { mutableStateOf("") }
|
||||
var newProblemGrade by remember { mutableStateOf("") }
|
||||
var newProblemImagePaths by remember { mutableStateOf<List<String>>(emptyList()) }
|
||||
var selectedClimbType by remember { mutableStateOf(ClimbType.BOULDER) }
|
||||
var selectedDifficultySystem by remember {
|
||||
mutableStateOf(gym.difficultySystems.firstOrNull() ?: DifficultySystem.V_SCALE)
|
||||
@@ -1584,7 +1508,7 @@ fun EnhancedAddAttemptDialog(
|
||||
// Auto-select difficulty system if there's only one available for the selected climb type
|
||||
LaunchedEffect(selectedClimbType, gym.difficultySystems) {
|
||||
val availableSystems =
|
||||
DifficultySystem.getSystemsForClimbType(selectedClimbType).filter { system ->
|
||||
DifficultySystem.systemsForClimbType(selectedClimbType).filter { system ->
|
||||
gym.difficultySystems.contains(system)
|
||||
}
|
||||
|
||||
@@ -1604,7 +1528,7 @@ fun EnhancedAddAttemptDialog(
|
||||
|
||||
// Reset grade when difficulty system changes
|
||||
LaunchedEffect(selectedDifficultySystem) {
|
||||
val availableGrades = selectedDifficultySystem.getAvailableGrades()
|
||||
val availableGrades = selectedDifficultySystem.availableGrades
|
||||
if (availableGrades.isNotEmpty() && newProblemGrade !in availableGrades) {
|
||||
newProblemGrade = ""
|
||||
}
|
||||
@@ -1721,7 +1645,7 @@ fun EnhancedAddAttemptDialog(
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text =
|
||||
"${problem.difficulty.system.getDisplayName()}: ${problem.difficulty.grade}",
|
||||
"${problem.difficulty.system.displayName}: ${problem.difficulty.grade}",
|
||||
style =
|
||||
MaterialTheme.typography
|
||||
.bodyMedium,
|
||||
@@ -1730,7 +1654,7 @@ fun EnhancedAddAttemptDialog(
|
||||
MaterialTheme
|
||||
.colorScheme
|
||||
.onSurface.copy(
|
||||
alpha = 0.8f
|
||||
alpha = 0.9f
|
||||
)
|
||||
else
|
||||
MaterialTheme
|
||||
@@ -1768,7 +1692,14 @@ fun EnhancedAddAttemptDialog(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
|
||||
IconButton(onClick = { showCreateProblem = false }) {
|
||||
IconButton(
|
||||
onClick = {
|
||||
showCreateProblem = false
|
||||
newProblemName = ""
|
||||
newProblemGrade = ""
|
||||
newProblemImagePaths = emptyList()
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
Icons.AutoMirrored.Filled.ArrowBack,
|
||||
contentDescription = "Back",
|
||||
@@ -1807,7 +1738,7 @@ fun EnhancedAddAttemptDialog(
|
||||
onClick = { selectedClimbType = climbType },
|
||||
label = {
|
||||
Text(
|
||||
climbType.getDisplayName(),
|
||||
climbType.displayName,
|
||||
fontWeight = FontWeight.Medium
|
||||
)
|
||||
},
|
||||
@@ -1838,7 +1769,7 @@ fun EnhancedAddAttemptDialog(
|
||||
)
|
||||
LazyRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
val availableSystems =
|
||||
DifficultySystem.getSystemsForClimbType(
|
||||
DifficultySystem.systemsForClimbType(
|
||||
selectedClimbType
|
||||
)
|
||||
.filter { system ->
|
||||
@@ -1849,7 +1780,7 @@ fun EnhancedAddAttemptDialog(
|
||||
onClick = { selectedDifficultySystem = system },
|
||||
label = {
|
||||
Text(
|
||||
system.getDisplayName(),
|
||||
system.displayName,
|
||||
fontWeight = FontWeight.Medium
|
||||
)
|
||||
},
|
||||
@@ -1926,8 +1857,7 @@ fun EnhancedAddAttemptDialog(
|
||||
)
|
||||
} else {
|
||||
var expanded by remember { mutableStateOf(false) }
|
||||
val availableGrades =
|
||||
selectedDifficultySystem.getAvailableGrades()
|
||||
val availableGrades = selectedDifficultySystem.availableGrades
|
||||
|
||||
ExposedDropdownMenuBox(
|
||||
expanded = expanded,
|
||||
@@ -1949,8 +1879,7 @@ fun EnhancedAddAttemptDialog(
|
||||
.outlinedTextFieldColors(),
|
||||
modifier =
|
||||
Modifier.menuAnchor(
|
||||
androidx.compose.material3
|
||||
.MenuAnchorType
|
||||
ExposedDropdownMenuAnchorType
|
||||
.PrimaryNotEditable,
|
||||
enabled = true
|
||||
)
|
||||
@@ -1985,6 +1914,21 @@ fun EnhancedAddAttemptDialog(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Photos Section
|
||||
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Text(
|
||||
text = "Photos (Optional)",
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
fontWeight = FontWeight.Medium,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
ImagePicker(
|
||||
imageUris = newProblemImagePaths,
|
||||
onImagesChanged = { newProblemImagePaths = it },
|
||||
maxImages = 5
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2149,7 +2093,9 @@ fun EnhancedAddAttemptDialog(
|
||||
null
|
||||
},
|
||||
climbType = selectedClimbType,
|
||||
difficulty = difficulty
|
||||
difficulty = difficulty,
|
||||
imagePaths =
|
||||
newProblemImagePaths
|
||||
)
|
||||
|
||||
onProblemCreated(newProblem)
|
||||
@@ -2167,6 +2113,12 @@ fun EnhancedAddAttemptDialog(
|
||||
notes = notes.ifBlank { null }
|
||||
)
|
||||
onAttemptAdded(attempt)
|
||||
|
||||
// Reset form
|
||||
newProblemName = ""
|
||||
newProblemGrade = ""
|
||||
newProblemImagePaths = emptyList()
|
||||
showCreateProblem = false
|
||||
}
|
||||
} else {
|
||||
// Create attempt for selected problem
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.screens
|
||||
package com.atridad.ascently.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
@@ -10,10 +10,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.openclimb.R
|
||||
import com.atridad.openclimb.data.model.Gym
|
||||
import com.atridad.openclimb.ui.components.SyncIndicator
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.ascently.R
|
||||
import com.atridad.ascently.data.model.Gym
|
||||
import com.atridad.ascently.ui.components.SyncIndicator
|
||||
import com.atridad.ascently.ui.viewmodel.ClimbViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -28,7 +28,7 @@ fun GymsScreen(viewModel: ClimbViewModel, onNavigateToGymDetail: (String) -> Uni
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(id = R.drawable.ic_mountains),
|
||||
contentDescription = "OpenClimb Logo",
|
||||
contentDescription = "Ascently Logo",
|
||||
modifier = Modifier.size(32.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
@@ -87,7 +87,7 @@ fun GymCard(gym: Gym, onClick: () -> Unit) {
|
||||
gym.supportedClimbTypes.forEach { climbType ->
|
||||
AssistChip(
|
||||
onClick = {},
|
||||
label = { Text(climbType.getDisplayName()) },
|
||||
label = { Text(climbType.displayName) },
|
||||
modifier = Modifier.padding(end = 4.dp)
|
||||
)
|
||||
}
|
||||
@@ -97,7 +97,7 @@ fun GymCard(gym: Gym, onClick: () -> Unit) {
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text =
|
||||
"Systems: ${gym.difficultySystems.joinToString(", ") { it.getDisplayName() }}",
|
||||
"Systems: ${gym.difficultySystems.joinToString(", ") { it.displayName }}",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
@@ -1,35 +1,34 @@
|
||||
package com.atridad.openclimb.ui.screens
|
||||
package com.atridad.ascently.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.CheckCircle
|
||||
import androidx.compose.material.icons.filled.Image
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.openclimb.R
|
||||
import com.atridad.openclimb.data.model.ClimbType
|
||||
import com.atridad.openclimb.data.model.Gym
|
||||
import com.atridad.openclimb.data.model.Problem
|
||||
import com.atridad.openclimb.ui.components.FullscreenImageViewer
|
||||
import com.atridad.openclimb.ui.components.ImageDisplay
|
||||
import com.atridad.openclimb.ui.components.SyncIndicator
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.ascently.R
|
||||
import com.atridad.ascently.data.model.Attempt
|
||||
import com.atridad.ascently.data.model.AttemptResult
|
||||
import com.atridad.ascently.data.model.ClimbType
|
||||
import com.atridad.ascently.data.model.Gym
|
||||
import com.atridad.ascently.data.model.Problem
|
||||
import com.atridad.ascently.ui.components.SyncIndicator
|
||||
import com.atridad.ascently.ui.viewmodel.ClimbViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String) -> Unit) {
|
||||
val problems by viewModel.problems.collectAsState()
|
||||
val gyms by viewModel.gyms.collectAsState()
|
||||
val context = LocalContext.current
|
||||
var showImageViewer by remember { mutableStateOf(false) }
|
||||
var selectedImagePaths by remember { mutableStateOf<List<String>>(emptyList()) }
|
||||
var selectedImageIndex by remember { mutableIntStateOf(0) }
|
||||
val attempts by viewModel.attempts.collectAsState()
|
||||
|
||||
// Filter state
|
||||
var selectedClimbType by remember { mutableStateOf<ClimbType?>(null) }
|
||||
@@ -56,7 +55,7 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(id = R.drawable.ic_mountains),
|
||||
contentDescription = "OpenClimb Logo",
|
||||
contentDescription = "Ascently Logo",
|
||||
modifier = Modifier.size(32.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
@@ -103,7 +102,7 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
||||
items(ClimbType.entries) { climbType ->
|
||||
FilterChip(
|
||||
onClick = { selectedClimbType = climbType },
|
||||
label = { Text(climbType.getDisplayName()) },
|
||||
label = { Text(climbType.displayName) },
|
||||
selected = selectedClimbType == climbType
|
||||
)
|
||||
}
|
||||
@@ -178,15 +177,11 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
||||
ProblemCard(
|
||||
problem = problem,
|
||||
gymName = gyms.find { it.id == problem.gymId }?.name ?: "Unknown Gym",
|
||||
attempts = attempts,
|
||||
onClick = { onNavigateToProblemDetail(problem.id) },
|
||||
onImageClick = { imagePaths, index ->
|
||||
selectedImagePaths = imagePaths
|
||||
selectedImageIndex = index
|
||||
showImageViewer = true
|
||||
},
|
||||
onToggleActive = {
|
||||
val updatedProblem = problem.copy(isActive = !problem.isActive)
|
||||
viewModel.updateProblem(updatedProblem, context)
|
||||
viewModel.updateProblem(updatedProblem)
|
||||
}
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
@@ -194,15 +189,6 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fullscreen Image Viewer
|
||||
if (showImageViewer && selectedImagePaths.isNotEmpty()) {
|
||||
FullscreenImageViewer(
|
||||
imagePaths = selectedImagePaths,
|
||||
initialIndex = selectedImageIndex,
|
||||
onDismiss = { showImageViewer = false }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@@ -210,10 +196,17 @@ fun ProblemsScreen(viewModel: ClimbViewModel, onNavigateToProblemDetail: (String
|
||||
fun ProblemCard(
|
||||
problem: Problem,
|
||||
gymName: String,
|
||||
attempts: List<Attempt>,
|
||||
onClick: () -> Unit,
|
||||
onImageClick: ((List<String>, Int) -> Unit)? = null,
|
||||
onToggleActive: (() -> Unit)? = null
|
||||
) {
|
||||
val isCompleted =
|
||||
attempts.any { attempt ->
|
||||
attempt.problemId == problem.id &&
|
||||
(attempt.result == AttemptResult.SUCCESS ||
|
||||
attempt.result == AttemptResult.FLASH)
|
||||
}
|
||||
|
||||
Card(onClick = onClick, modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
|
||||
Row(
|
||||
@@ -242,15 +235,38 @@ fun ProblemCard(
|
||||
}
|
||||
|
||||
Column(horizontalAlignment = Alignment.End) {
|
||||
Text(
|
||||
text = problem.difficulty.grade,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
if (problem.imagePaths.isNotEmpty()) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Image,
|
||||
contentDescription = "Has images",
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
|
||||
if (isCompleted) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CheckCircle,
|
||||
contentDescription = "Completed",
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.tertiary
|
||||
)
|
||||
}
|
||||
|
||||
Text(
|
||||
text = problem.difficulty.grade,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
|
||||
Text(
|
||||
text = problem.climbType.getDisplayName(),
|
||||
text = problem.climbType.displayName,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
@@ -279,16 +295,6 @@ fun ProblemCard(
|
||||
}
|
||||
}
|
||||
|
||||
// Display images if any
|
||||
if (problem.imagePaths.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
ImageDisplay(
|
||||
imagePaths = problem.imagePaths.take(3), // Show max 3 images in list
|
||||
imageSize = 60,
|
||||
onImageClick = { index -> onImageClick?.invoke(problem.imagePaths, index) }
|
||||
)
|
||||
}
|
||||
|
||||
if (!problem.isActive) {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Text(
|
||||
@@ -0,0 +1,536 @@
|
||||
package com.atridad.ascently.ui.screens
|
||||
|
||||
import android.content.Context
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.List
|
||||
import androidx.compose.material.icons.filled.CalendarMonth
|
||||
import androidx.compose.material.icons.filled.CheckCircle
|
||||
import androidx.compose.material.icons.filled.Warning
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.ascently.R
|
||||
import com.atridad.ascently.data.model.ClimbSession
|
||||
import com.atridad.ascently.data.model.SessionStatus
|
||||
import com.atridad.ascently.ui.components.ActiveSessionBanner
|
||||
import com.atridad.ascently.ui.components.SyncIndicator
|
||||
import com.atridad.ascently.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.ascently.utils.DateFormatUtils
|
||||
import java.time.LocalDate
|
||||
import java.time.YearMonth
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.format.TextStyle
|
||||
import java.util.Locale
|
||||
|
||||
enum class ViewMode {
|
||||
LIST,
|
||||
CALENDAR
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SessionsScreen(viewModel: ClimbViewModel, onNavigateToSessionDetail: (String) -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val sessions by viewModel.sessions.collectAsState()
|
||||
val gyms by viewModel.gyms.collectAsState()
|
||||
val activeSession by viewModel.activeSession.collectAsState()
|
||||
val uiState by viewModel.uiState.collectAsState()
|
||||
|
||||
val sharedPreferences =
|
||||
context.getSharedPreferences("SessionsPreferences", Context.MODE_PRIVATE)
|
||||
val savedViewMode = sharedPreferences.getString("view_mode", "LIST")
|
||||
var viewMode by remember {
|
||||
mutableStateOf(if (savedViewMode == "CALENDAR") ViewMode.CALENDAR else ViewMode.LIST)
|
||||
}
|
||||
var selectedMonth by remember { mutableStateOf(YearMonth.now()) }
|
||||
var selectedDate by remember { mutableStateOf<LocalDate?>(null) }
|
||||
|
||||
val completedSessions = sessions.filter { it.status == SessionStatus.COMPLETED }
|
||||
val activeSessionGym = activeSession?.let { session -> gyms.find { it.id == session.gymId } }
|
||||
|
||||
Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(id = R.drawable.ic_mountains),
|
||||
contentDescription = "Ascently Logo",
|
||||
modifier = Modifier.size(32.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Text(
|
||||
text = "Climbing Sessions",
|
||||
style = MaterialTheme.typography.headlineMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
|
||||
IconButton(
|
||||
onClick = {
|
||||
viewMode =
|
||||
if (viewMode == ViewMode.LIST) ViewMode.CALENDAR else ViewMode.LIST
|
||||
selectedDate = null
|
||||
sharedPreferences.edit().putString("view_mode", viewMode.name).apply()
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
imageVector =
|
||||
if (viewMode == ViewMode.LIST) Icons.Default.CalendarMonth
|
||||
else Icons.AutoMirrored.Filled.List,
|
||||
contentDescription =
|
||||
if (viewMode == ViewMode.LIST) "Calendar View" else "List View",
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
|
||||
SyncIndicator(isSyncing = viewModel.syncService.isSyncing)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
ActiveSessionBanner(
|
||||
activeSession = activeSession,
|
||||
gym = activeSessionGym,
|
||||
onSessionClick = { activeSession?.let { onNavigateToSessionDetail(it.id) } },
|
||||
onEndSession = { activeSession?.let { viewModel.endSession(context, it.id) } }
|
||||
)
|
||||
|
||||
if (activeSession != null) {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
}
|
||||
|
||||
if (completedSessions.isEmpty() && activeSession == null) {
|
||||
EmptyStateMessage(
|
||||
title = if (gyms.isEmpty()) "No Gyms Available" else "No Sessions Yet",
|
||||
message =
|
||||
if (gyms.isEmpty())
|
||||
"Add a gym first to start tracking your climbing sessions!"
|
||||
else "Start your first climbing session!",
|
||||
onActionClick = {},
|
||||
actionText = ""
|
||||
)
|
||||
} else {
|
||||
when (viewMode) {
|
||||
ViewMode.LIST -> {
|
||||
LazyColumn {
|
||||
items(completedSessions) { session ->
|
||||
SessionCard(
|
||||
session = session,
|
||||
gymName = gyms.find { it.id == session.gymId }?.name
|
||||
?: "Unknown Gym",
|
||||
onClick = { onNavigateToSessionDetail(session.id) }
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
ViewMode.CALENDAR -> {
|
||||
CalendarView(
|
||||
sessions = completedSessions,
|
||||
gyms = gyms,
|
||||
activeSession = activeSession,
|
||||
activeSessionGym = activeSessionGym,
|
||||
selectedMonth = selectedMonth,
|
||||
onMonthChange = { selectedMonth = it },
|
||||
selectedDate = selectedDate,
|
||||
onDateSelected = { selectedDate = it },
|
||||
onNavigateToSessionDetail = onNavigateToSessionDetail,
|
||||
onEndSession = {
|
||||
activeSession?.let { viewModel.endSession(context, it.id) }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uiState.message?.let { message ->
|
||||
LaunchedEffect(message) {
|
||||
kotlinx.coroutines.delay(5000)
|
||||
viewModel.clearMessage()
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.primaryContainer
|
||||
),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.CheckCircle,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = message,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onPrimaryContainer
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uiState.error?.let { error ->
|
||||
LaunchedEffect(error) {
|
||||
kotlinx.coroutines.delay(5000)
|
||||
viewModel.clearError()
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.errorContainer
|
||||
),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.Warning,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.error
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = error,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onErrorContainer
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SessionCard(session: ClimbSession, gymName: String, onClick: () -> Unit) {
|
||||
Card(onClick = onClick, modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Text(
|
||||
text = gymName,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = formatDate(session.date),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
|
||||
session.duration?.let { duration ->
|
||||
Text(
|
||||
text = "Duration: $duration minutes",
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
|
||||
session.notes?.let { notes ->
|
||||
if (notes.isNotBlank()) {
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = notes,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 2
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun EmptyStateMessage(
|
||||
title: String,
|
||||
message: String,
|
||||
onActionClick: () -> Unit,
|
||||
actionText: String
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Text(
|
||||
text = title,
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
fontWeight = FontWeight.Bold,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Text(
|
||||
text = message,
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
if (actionText.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Button(onClick = onActionClick) { Text(actionText) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun CalendarView(
|
||||
sessions: List<ClimbSession>,
|
||||
gyms: List<com.atridad.ascently.data.model.Gym>,
|
||||
activeSession: ClimbSession?,
|
||||
activeSessionGym: com.atridad.ascently.data.model.Gym?,
|
||||
selectedMonth: YearMonth,
|
||||
onMonthChange: (YearMonth) -> Unit,
|
||||
selectedDate: LocalDate?,
|
||||
onDateSelected: (LocalDate?) -> Unit,
|
||||
onNavigateToSessionDetail: (String) -> Unit,
|
||||
onEndSession: () -> Unit
|
||||
) {
|
||||
val sessionsByDate =
|
||||
remember(sessions) {
|
||||
sessions.groupBy {
|
||||
try {
|
||||
java.time.Instant.parse(it.date)
|
||||
.atZone(java.time.ZoneId.systemDefault())
|
||||
.toLocalDate()
|
||||
} catch (e: Exception) {
|
||||
LocalDate.parse(it.date, DateTimeFormatter.ISO_LOCAL_DATE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Column(modifier = Modifier.fillMaxSize()) {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceVariant
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 8.dp, vertical = 12.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
IconButton(onClick = { onMonthChange(selectedMonth.minusMonths(1)) }) {
|
||||
Text("‹", style = MaterialTheme.typography.headlineMedium)
|
||||
}
|
||||
|
||||
Text(
|
||||
text =
|
||||
"${selectedMonth.month.getDisplayName(TextStyle.FULL, Locale.getDefault())} ${selectedMonth.year}",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
IconButton(onClick = { onMonthChange(selectedMonth.plusMonths(1)) }) {
|
||||
Text("›", style = MaterialTheme.typography.headlineMedium)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
val today = LocalDate.now()
|
||||
onMonthChange(YearMonth.from(today))
|
||||
onDateSelected(today)
|
||||
},
|
||||
shape = RoundedCornerShape(50),
|
||||
colors =
|
||||
ButtonDefaults.buttonColors(
|
||||
containerColor = MaterialTheme.colorScheme.primary
|
||||
),
|
||||
contentPadding = PaddingValues(horizontal = 20.dp, vertical = 8.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "Today",
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
fontWeight = FontWeight.SemiBold
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Row(modifier = Modifier.fillMaxWidth()) {
|
||||
listOf("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat").forEach { day ->
|
||||
Text(
|
||||
text = day,
|
||||
modifier = Modifier.weight(1f),
|
||||
textAlign = TextAlign.Center,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
val firstDayOfMonth = selectedMonth.atDay(1)
|
||||
val daysInMonth = selectedMonth.lengthOfMonth()
|
||||
val firstDayOfWeek = firstDayOfMonth.dayOfWeek.value % 7
|
||||
val totalCells =
|
||||
((firstDayOfWeek + daysInMonth) / 7.0).let {
|
||||
if (it == it.toInt().toDouble()) it.toInt() * 7 else (it.toInt() + 1) * 7
|
||||
}
|
||||
|
||||
LazyVerticalGrid(columns = GridCells.Fixed(7), modifier = Modifier.fillMaxWidth()) {
|
||||
items(totalCells) { index ->
|
||||
val dayNumber = index - firstDayOfWeek + 1
|
||||
|
||||
if (dayNumber in 1..daysInMonth) {
|
||||
val date = selectedMonth.atDay(dayNumber)
|
||||
val sessionsOnDate = sessionsByDate[date] ?: emptyList()
|
||||
val isSelected = date == selectedDate
|
||||
val isToday = date == LocalDate.now()
|
||||
|
||||
CalendarDay(
|
||||
day = dayNumber,
|
||||
hasSession = sessionsOnDate.isNotEmpty(),
|
||||
isSelected = isSelected,
|
||||
isToday = isToday,
|
||||
onClick = {
|
||||
if (sessionsOnDate.isNotEmpty()) {
|
||||
onDateSelected(if (isSelected) null else date)
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
Spacer(modifier = Modifier.aspectRatio(1f))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedDate != null) {
|
||||
val sessionsOnSelectedDate = sessionsByDate[selectedDate] ?: emptyList()
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Text(
|
||||
text =
|
||||
"Sessions on ${selectedDate.format(DateTimeFormatter.ofPattern("MMMM d, yyyy"))}",
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier.padding(vertical = 8.dp)
|
||||
)
|
||||
|
||||
LazyColumn(modifier = Modifier.fillMaxWidth()) {
|
||||
items(sessionsOnSelectedDate) { session ->
|
||||
SessionCard(
|
||||
session = session,
|
||||
gymName = gyms.find { it.id == session.gymId }?.name ?: "Unknown Gym",
|
||||
onClick = { onNavigateToSessionDetail(session.id) }
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun CalendarDay(
|
||||
day: Int,
|
||||
hasSession: Boolean,
|
||||
isSelected: Boolean,
|
||||
isToday: Boolean,
|
||||
onClick: () -> Unit
|
||||
) {
|
||||
Box(
|
||||
modifier =
|
||||
Modifier.aspectRatio(1f)
|
||||
.padding(2.dp)
|
||||
.clip(CircleShape)
|
||||
.background(
|
||||
when {
|
||||
isSelected -> MaterialTheme.colorScheme.primaryContainer
|
||||
isToday -> MaterialTheme.colorScheme.secondaryContainer
|
||||
else -> Color.Transparent
|
||||
}
|
||||
)
|
||||
.clickable(enabled = hasSession, onClick = onClick),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Text(
|
||||
text = day.toString(),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color =
|
||||
when {
|
||||
isSelected -> MaterialTheme.colorScheme.onPrimaryContainer
|
||||
isToday -> MaterialTheme.colorScheme.onSecondaryContainer
|
||||
!hasSession -> MaterialTheme.colorScheme.onSurfaceVariant
|
||||
else -> MaterialTheme.colorScheme.onSurface
|
||||
},
|
||||
fontWeight = if (hasSession || isToday) FontWeight.Bold else FontWeight.Normal
|
||||
)
|
||||
|
||||
if (hasSession) {
|
||||
Box(
|
||||
modifier =
|
||||
Modifier.size(6.dp)
|
||||
.clip(CircleShape)
|
||||
.background(
|
||||
if (isSelected) MaterialTheme.colorScheme.primary
|
||||
else
|
||||
MaterialTheme.colorScheme.primary.copy(
|
||||
alpha = 0.7f
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatDate(dateString: String): String {
|
||||
return DateFormatUtils.formatDateForDisplay(dateString)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.screens
|
||||
package com.atridad.ascently.ui.screens
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
@@ -15,10 +15,10 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.openclimb.R
|
||||
import com.atridad.openclimb.ui.components.SyncIndicator
|
||||
import com.atridad.openclimb.ui.health.HealthConnectCard
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.ascently.R
|
||||
import com.atridad.ascently.ui.components.SyncIndicator
|
||||
import com.atridad.ascently.ui.health.HealthConnectCard
|
||||
import com.atridad.ascently.ui.viewmodel.ClimbViewModel
|
||||
import java.io.File
|
||||
import java.time.Instant
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -38,26 +38,27 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
val isTesting by syncService.isTesting.collectAsState()
|
||||
val lastSyncTime by syncService.lastSyncTime.collectAsState()
|
||||
val syncError by syncService.syncError.collectAsState()
|
||||
val isAutoSyncEnabled by syncService.isAutoSyncEnabled.collectAsState()
|
||||
|
||||
// State for dialogs
|
||||
var showResetDialog by remember { mutableStateOf(false) }
|
||||
var showSyncConfigDialog by remember { mutableStateOf(false) }
|
||||
var showDisconnectDialog by remember { mutableStateOf(false) }
|
||||
var showFixImagesDialog by remember { mutableStateOf(false) }
|
||||
|
||||
var showDeleteImagesDialog by remember { mutableStateOf(false) }
|
||||
var isFixingImages by remember { mutableStateOf(false) }
|
||||
|
||||
var isDeletingImages by remember { mutableStateOf(false) }
|
||||
|
||||
// Sync configuration state
|
||||
var serverUrl by remember { mutableStateOf(syncService.serverURL) }
|
||||
var serverUrl by remember { mutableStateOf(syncService.serverUrl) }
|
||||
var authToken by remember { mutableStateOf(syncService.authToken) }
|
||||
|
||||
val packageInfo = remember { context.packageManager.getPackageInfo(context.packageName, 0) }
|
||||
val appVersion = packageInfo.versionName
|
||||
|
||||
// Update local state when sync service configuration changes
|
||||
LaunchedEffect(syncService.serverURL, syncService.authToken) {
|
||||
serverUrl = syncService.serverURL
|
||||
LaunchedEffect(syncService.serverUrl, syncService.authToken) {
|
||||
serverUrl = syncService.serverUrl
|
||||
authToken = syncService.authToken
|
||||
}
|
||||
|
||||
@@ -85,7 +86,7 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
// Only allow ZIP files
|
||||
if (!fileName.lowercase().endsWith(".zip")) {
|
||||
viewModel.setError(
|
||||
"Only ZIP files are supported for import. Please use a ZIP file exported from OpenClimb."
|
||||
"Only ZIP files are supported for import. Please use a ZIP file exported from Ascently."
|
||||
)
|
||||
return@let
|
||||
}
|
||||
@@ -128,7 +129,7 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(id = R.drawable.ic_mountains),
|
||||
contentDescription = "OpenClimb Logo",
|
||||
contentDescription = "Ascently Logo",
|
||||
modifier = Modifier.size(32.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
@@ -182,7 +183,7 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
},
|
||||
supportingContent = {
|
||||
Column {
|
||||
Text("Server: ${syncService.serverURL}")
|
||||
Text("Server: ${syncService.serverUrl}")
|
||||
lastSyncTime?.let { time ->
|
||||
Text(
|
||||
"Last sync: ${
|
||||
@@ -280,8 +281,10 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
}
|
||||
Spacer(modifier = Modifier.width(16.dp))
|
||||
Switch(
|
||||
checked = syncService.isAutoSyncEnabled,
|
||||
onCheckedChange = { syncService.isAutoSyncEnabled = it }
|
||||
checked = isAutoSyncEnabled,
|
||||
onCheckedChange = { enabled ->
|
||||
syncService.setAutoSyncEnabled(enabled)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -333,7 +336,7 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
ListItem(
|
||||
headlineContent = { Text("Setup Sync") },
|
||||
supportingContent = {
|
||||
Text("Connect to your OpenClimb sync server")
|
||||
Text("Connect to your Ascently sync server")
|
||||
},
|
||||
leadingContent = {
|
||||
Icon(Icons.Default.CloudSync, contentDescription = null)
|
||||
@@ -418,7 +421,7 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
TextButton(
|
||||
onClick = {
|
||||
val defaultFileName =
|
||||
"openclimb_export_${
|
||||
"ascently_export_${
|
||||
java.time.LocalDateTime.now()
|
||||
.toString()
|
||||
.replace(":", "-")
|
||||
@@ -481,46 +484,6 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Card(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.surfaceVariant.copy(
|
||||
alpha = 0.3f
|
||||
)
|
||||
)
|
||||
) {
|
||||
ListItem(
|
||||
headlineContent = { Text("Fix Image Names") },
|
||||
supportingContent = {
|
||||
Text(
|
||||
"Rename all images to use consistent naming across devices"
|
||||
)
|
||||
},
|
||||
leadingContent = {
|
||||
Icon(Icons.Default.Build, contentDescription = null)
|
||||
},
|
||||
trailingContent = {
|
||||
TextButton(
|
||||
onClick = { showFixImagesDialog = true },
|
||||
enabled = !isFixingImages && !uiState.isLoading
|
||||
) {
|
||||
if (isFixingImages) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(16.dp),
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
} else {
|
||||
Text("Fix Names")
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Card(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors =
|
||||
@@ -641,11 +604,11 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
painterResource(
|
||||
id = R.drawable.ic_mountains
|
||||
),
|
||||
contentDescription = "OpenClimb Logo",
|
||||
contentDescription = "Ascently Logo",
|
||||
modifier = Modifier.size(24.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Text("OpenClimb")
|
||||
Text("Ascently")
|
||||
}
|
||||
},
|
||||
supportingContent = { Text("Track your climbing progress") },
|
||||
@@ -900,7 +863,7 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
syncService.serverURL = serverUrl.trim()
|
||||
syncService.serverUrl = serverUrl.trim()
|
||||
syncService.authToken = authToken.trim()
|
||||
viewModel.testSyncConnection()
|
||||
while (syncService.isTesting.value) {
|
||||
@@ -942,7 +905,7 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
syncService.serverURL = serverUrl.trim()
|
||||
syncService.serverUrl = serverUrl.trim()
|
||||
syncService.authToken = authToken.trim()
|
||||
viewModel.testSyncConnection()
|
||||
while (syncService.isTesting.value) {
|
||||
@@ -969,7 +932,7 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
dismissButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
serverUrl = syncService.serverURL
|
||||
serverUrl = syncService.serverUrl
|
||||
authToken = syncService.authToken
|
||||
showSyncConfigDialog = false
|
||||
}
|
||||
@@ -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
|
||||
if (showDeleteImagesDialog) {
|
||||
AlertDialog(
|
||||
@@ -1047,7 +981,7 @@ fun SettingsScreen(viewModel: ClimbViewModel) {
|
||||
isDeletingImages = true
|
||||
showDeleteImagesDialog = false
|
||||
coroutineScope.launch {
|
||||
viewModel.deleteAllImages(context)
|
||||
viewModel.deleteAllImages()
|
||||
isDeletingImages = false
|
||||
viewModel.setMessage("All images deleted successfully!")
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.theme
|
||||
package com.atridad.ascently.ui.theme
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.theme
|
||||
package com.atridad.ascently.ui.theme
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.SolidColor
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.theme
|
||||
package com.atridad.ascently.ui.theme
|
||||
|
||||
import android.app.Activity
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
@@ -88,7 +88,7 @@ private val LightColorScheme = lightColorScheme(
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun OpenClimbTheme(
|
||||
fun AscentlyTheme(
|
||||
darkTheme: Boolean = isSystemInDarkTheme(),
|
||||
// Dynamic color is available on Android 12+ and provides full Material You theming
|
||||
// When enabled, it adapts to the user's system wallpaper colors
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.ui.theme
|
||||
package com.atridad.ascently.ui.theme
|
||||
|
||||
import androidx.compose.material3.Typography
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
@@ -0,0 +1,491 @@
|
||||
package com.atridad.ascently.ui.viewmodel
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.atridad.ascently.data.health.HealthConnectManager
|
||||
import com.atridad.ascently.data.model.*
|
||||
import com.atridad.ascently.data.repository.ClimbRepository
|
||||
import com.atridad.ascently.data.sync.SyncService
|
||||
import com.atridad.ascently.service.SessionTrackingService
|
||||
import com.atridad.ascently.utils.AppLogger
|
||||
import com.atridad.ascently.utils.ImageUtils
|
||||
import com.atridad.ascently.widget.ClimbStatsWidgetProvider
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class ClimbViewModel(
|
||||
private val repository: ClimbRepository,
|
||||
val syncService: SyncService,
|
||||
private val context: Context
|
||||
) : ViewModel() {
|
||||
|
||||
// Health Connect manager
|
||||
private val healthConnectManager = HealthConnectManager(context)
|
||||
|
||||
// UI State flows
|
||||
private val _uiState = MutableStateFlow(ClimbUiState())
|
||||
val uiState: StateFlow<ClimbUiState> = _uiState.asStateFlow()
|
||||
|
||||
// Data flows
|
||||
val gyms =
|
||||
repository
|
||||
.getAllGyms()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = emptyList()
|
||||
)
|
||||
|
||||
val problems =
|
||||
repository
|
||||
.getAllProblems()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = emptyList()
|
||||
)
|
||||
|
||||
val sessions =
|
||||
repository
|
||||
.getAllSessions()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = emptyList()
|
||||
)
|
||||
|
||||
val activeSession =
|
||||
repository
|
||||
.getActiveSessionFlow()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = null
|
||||
)
|
||||
|
||||
val attempts =
|
||||
repository
|
||||
.getAllAttempts()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = emptyList()
|
||||
)
|
||||
|
||||
// Gym operations
|
||||
fun addGym(gym: Gym, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
repository.insertGym(gym)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateGym(gym: Gym, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
repository.updateGym(gym)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteGym(gym: Gym, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
repository.deleteGym(gym)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getGymById(id: String): Flow<Gym?> = flow { emit(repository.getGymById(id)) }
|
||||
|
||||
// Problem operations
|
||||
fun addProblem(problem: Problem, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
val finalProblem = renameTemporaryImages(problem)
|
||||
repository.insertProblem(finalProblem)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun renameTemporaryImages(problem: Problem): Problem {
|
||||
if (problem.imagePaths.isEmpty()) {
|
||||
return problem
|
||||
}
|
||||
|
||||
val finalImagePaths = mutableListOf<String>()
|
||||
|
||||
problem.imagePaths.forEachIndexed { index, tempPath ->
|
||||
if (tempPath.startsWith("temp_")) {
|
||||
val finalPath =
|
||||
ImageUtils.renameTemporaryImage(context, tempPath, problem.id, index)
|
||||
finalImagePaths.add(finalPath ?: tempPath)
|
||||
} else {
|
||||
finalImagePaths.add(tempPath)
|
||||
}
|
||||
}
|
||||
|
||||
return problem.copy(imagePaths = finalImagePaths)
|
||||
}
|
||||
|
||||
fun updateProblem(problem: Problem, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
val finalProblem = renameTemporaryImages(problem)
|
||||
repository.updateProblem(finalProblem)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteProblem(problem: Problem, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
problem.imagePaths.forEach { imagePath -> ImageUtils.deleteImage(context, imagePath) }
|
||||
repository.deleteProblem(problem)
|
||||
cleanupOrphanedImages()
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun cleanupOrphanedImages() {
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
val referencedImagePaths = allProblems.flatMap { it.imagePaths }.toSet()
|
||||
ImageUtils.cleanupOrphanedImages(context, referencedImagePaths)
|
||||
}
|
||||
|
||||
fun deleteAllImages() {
|
||||
viewModelScope.launch {
|
||||
val imagesDir = ImageUtils.getImagesDirectory(context)
|
||||
var deletedCount = 0
|
||||
|
||||
imagesDir.listFiles()?.forEach { file ->
|
||||
if (file.isFile && file.extension.lowercase() == "jpg") {
|
||||
if (file.delete()) {
|
||||
deletedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
val updatedProblems =
|
||||
allProblems.map { problem ->
|
||||
if (problem.imagePaths.isNotEmpty()) {
|
||||
problem.copy(imagePaths = emptyList())
|
||||
} else {
|
||||
problem
|
||||
}
|
||||
}
|
||||
|
||||
for (updatedProblem in updatedProblems) {
|
||||
if (updatedProblem.imagePaths !=
|
||||
allProblems.find { it.id == updatedProblem.id }?.imagePaths
|
||||
) {
|
||||
repository.insertProblemWithoutSync(updatedProblem)
|
||||
}
|
||||
}
|
||||
|
||||
AppLogger.i("ClimbViewModel") { "Deleted $deletedCount image files and cleared image references" }
|
||||
}
|
||||
}
|
||||
|
||||
fun getProblemById(id: String): Flow<Problem?> = flow { emit(repository.getProblemById(id)) }
|
||||
|
||||
fun getProblemsByGym(gymId: String): Flow<List<Problem>> = repository.getProblemsByGym(gymId)
|
||||
|
||||
// Session operations
|
||||
|
||||
fun updateSession(session: ClimbSession, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
repository.updateSession(session)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteSession(session: ClimbSession, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
repository.deleteSession(session)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getSessionById(id: String): Flow<ClimbSession?> = flow {
|
||||
emit(repository.getSessionById(id))
|
||||
}
|
||||
|
||||
fun getSessionsByGym(gymId: String): Flow<List<ClimbSession>> =
|
||||
repository.getSessionsByGym(gymId)
|
||||
|
||||
// Get last used gym for shortcut functionality
|
||||
suspend fun getLastUsedGym(): Gym? = repository.getLastUsedGym()
|
||||
|
||||
// Active session management
|
||||
fun startSession(context: Context, gymId: String, notes: String? = null) {
|
||||
viewModelScope.launch {
|
||||
AppLogger.d("ClimbViewModel") { "startSession called with gymId: $gymId" }
|
||||
|
||||
if (!com.atridad.ascently.utils.NotificationPermissionUtils
|
||||
.isNotificationPermissionGranted(context)
|
||||
) {
|
||||
AppLogger.d("ClimbViewModel") { "Notification permission not granted" }
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
error =
|
||||
"Notification permission is required to track your climbing session. Please enable notifications in settings."
|
||||
)
|
||||
return@launch
|
||||
}
|
||||
|
||||
val existingActive = repository.getActiveSession()
|
||||
if (existingActive != null) {
|
||||
AppLogger.d("ClimbViewModel") { "Active session already exists: ${existingActive.id}" }
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
error = "There's already an active session. Please end it first."
|
||||
)
|
||||
return@launch
|
||||
}
|
||||
|
||||
AppLogger.d("ClimbViewModel") { "Creating new session" }
|
||||
val newSession = ClimbSession.create(gymId = gymId, notes = notes)
|
||||
repository.insertSession(newSession)
|
||||
|
||||
AppLogger.d("ClimbViewModel") { "Starting tracking service for session: ${newSession.id}" }
|
||||
// Start the tracking service
|
||||
val serviceIntent = SessionTrackingService.createStartIntent(context, newSession.id)
|
||||
context.startForegroundService(serviceIntent)
|
||||
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
|
||||
_uiState.value = _uiState.value.copy(message = "Session started successfully!")
|
||||
}
|
||||
}
|
||||
|
||||
fun endSession(context: Context, sessionId: String) {
|
||||
viewModelScope.launch {
|
||||
if (!com.atridad.ascently.utils.NotificationPermissionUtils
|
||||
.isNotificationPermissionGranted(context)
|
||||
) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
error =
|
||||
"Notification permission is required to manage your climbing session. Please enable notifications in settings."
|
||||
)
|
||||
return@launch
|
||||
}
|
||||
|
||||
val session = repository.getSessionById(sessionId)
|
||||
if (session != null && session.status == SessionStatus.ACTIVE) {
|
||||
val completedSession = with(ClimbSession) { session.complete() }
|
||||
repository.updateSession(completedSession)
|
||||
|
||||
val serviceIntent = SessionTrackingService.createStopIntent(context, sessionId)
|
||||
context.startService(serviceIntent)
|
||||
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
|
||||
syncToHealthConnect(completedSession)
|
||||
|
||||
_uiState.value = _uiState.value.copy(message = "Session completed!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ensureSessionTrackingServiceRunning(context: Context) {
|
||||
viewModelScope.launch {
|
||||
val activeSession = repository.getActiveSession()
|
||||
if (activeSession != null && activeSession.status == SessionStatus.ACTIVE) {
|
||||
val serviceIntent =
|
||||
SessionTrackingService.createStartIntent(context, activeSession.id)
|
||||
context.startForegroundService(serviceIntent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt operations
|
||||
fun addAttempt(attempt: Attempt, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
repository.insertAttempt(attempt)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteAttempt(attempt: Attempt, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
repository.deleteAttempt(attempt)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateAttempt(attempt: Attempt, updateWidgets: Boolean = true) {
|
||||
viewModelScope.launch {
|
||||
repository.updateAttempt(attempt)
|
||||
if (updateWidgets) {
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getAttemptsBySession(sessionId: String): Flow<List<Attempt>> =
|
||||
repository.getAttemptsBySession(sessionId)
|
||||
|
||||
fun getAttemptsByProblem(problemId: String): Flow<List<Attempt>> =
|
||||
repository.getAttemptsByProblem(problemId)
|
||||
|
||||
fun exportDataToZipUri(context: Context, uri: android.net.Uri) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = true,
|
||||
message = "Creating ZIP file with images..."
|
||||
)
|
||||
repository.exportAllDataToZipUri(context, uri)
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
message =
|
||||
"Export complete! Your climbing data and images have been saved."
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
error = "Export failed: ${e.message}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun importData(file: File) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_uiState.value = _uiState.value.copy(isLoading = true)
|
||||
|
||||
if (!file.name.lowercase().endsWith(".zip")) {
|
||||
throw Exception(
|
||||
"Only ZIP files are supported for import. Please use a ZIP file exported from Ascently."
|
||||
)
|
||||
}
|
||||
|
||||
repository.importDataFromZip(file)
|
||||
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
message = "Data imported successfully from ${file.name}"
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
error = "Import failed: ${e.message}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UI state operations
|
||||
fun clearMessage() {
|
||||
_uiState.value = _uiState.value.copy(message = null)
|
||||
}
|
||||
|
||||
fun clearError() {
|
||||
_uiState.value = _uiState.value.copy(error = null)
|
||||
}
|
||||
|
||||
// Sync-related methods
|
||||
suspend fun performManualSync() {
|
||||
try {
|
||||
syncService.syncWithServer()
|
||||
} catch (e: Exception) {
|
||||
setError("Sync failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun testSyncConnection() {
|
||||
try {
|
||||
syncService.testConnection()
|
||||
} catch (e: Exception) {
|
||||
setError("Connection test failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
fun setError(message: String) {
|
||||
_uiState.value = _uiState.value.copy(error = message)
|
||||
}
|
||||
|
||||
fun setMessage(message: String) {
|
||||
_uiState.value = _uiState.value.copy(message = message)
|
||||
}
|
||||
|
||||
fun resetAllData() {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_uiState.value = _uiState.value.copy(isLoading = true)
|
||||
|
||||
repository.resetAllData()
|
||||
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
message = "All data has been reset successfully"
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(isLoading = false, error = "Reset failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun syncToHealthConnect(session: ClimbSession) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val gym = repository.getGymById(session.gymId)
|
||||
val gymName = gym?.name ?: "Unknown Gym"
|
||||
|
||||
val attempts = repository.getAttemptsBySession(session.id).first()
|
||||
val attemptCount = attempts.size
|
||||
|
||||
val result =
|
||||
healthConnectManager.autoSyncCompletedSession(
|
||||
session,
|
||||
gymName,
|
||||
attemptCount
|
||||
)
|
||||
|
||||
result.onFailure { error ->
|
||||
if (healthConnectManager.isReadySync()) {
|
||||
AppLogger.w("ClimbViewModel") { "Health Connect sync failed: ${error.message}" }
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (healthConnectManager.isReadySync()) {
|
||||
AppLogger.w("ClimbViewModel") { "Health Connect sync error: ${e.message}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class ClimbUiState(
|
||||
val isLoading: Boolean = false,
|
||||
val message: String? = null,
|
||||
val error: String? = null
|
||||
)
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.atridad.openclimb.ui.viewmodel
|
||||
package com.atridad.ascently.ui.viewmodel
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.atridad.openclimb.data.repository.ClimbRepository
|
||||
import com.atridad.openclimb.data.sync.SyncService
|
||||
import com.atridad.ascently.data.repository.ClimbRepository
|
||||
import com.atridad.ascently.data.sync.SyncService
|
||||
|
||||
class ClimbViewModelFactory(
|
||||
private val repository: ClimbRepository,
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.atridad.ascently.utils
|
||||
|
||||
import android.util.Log
|
||||
import com.atridad.ascently.BuildConfig
|
||||
|
||||
object AppLogger {
|
||||
|
||||
private const val DEFAULT_TAG = "Ascently"
|
||||
|
||||
enum class Level(val androidLevel: Int) {
|
||||
DEBUG(Log.DEBUG),
|
||||
INFO(Log.INFO),
|
||||
WARN(Log.WARN),
|
||||
ERROR(Log.ERROR)
|
||||
}
|
||||
|
||||
fun d(tag: String = DEFAULT_TAG, messageProvider: () -> String) {
|
||||
log(Level.DEBUG, tag, messageProvider)
|
||||
}
|
||||
|
||||
fun i(tag: String = DEFAULT_TAG, messageProvider: () -> String) {
|
||||
log(Level.INFO, tag, messageProvider)
|
||||
}
|
||||
|
||||
fun w(tag: String = DEFAULT_TAG, throwable: Throwable? = null, messageProvider: () -> String) {
|
||||
log(Level.WARN, tag, messageProvider, throwable)
|
||||
}
|
||||
|
||||
fun e(tag: String = DEFAULT_TAG, throwable: Throwable? = null, messageProvider: () -> String) {
|
||||
log(Level.ERROR, tag, messageProvider, throwable)
|
||||
}
|
||||
|
||||
private fun log(
|
||||
level: Level,
|
||||
tag: String,
|
||||
messageProvider: () -> String,
|
||||
throwable: Throwable? = null
|
||||
) {
|
||||
if (!BuildConfig.DEBUG) return
|
||||
|
||||
val message = messageProvider()
|
||||
if (throwable != null) {
|
||||
Log.println(level.androidLevel, tag, "$message\n${Log.getStackTraceString(throwable)}")
|
||||
} else {
|
||||
Log.println(level.androidLevel, tag, message)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.atridad.ascently.utils
|
||||
|
||||
import java.time.Instant
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
import java.time.ZoneOffset
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
object DateFormatUtils {
|
||||
|
||||
// ISO 8601 formatter matching iOS date format exactly
|
||||
private val ISO_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX").withZone(ZoneOffset.UTC)
|
||||
|
||||
/** Get current timestamp in iOS-compatible ISO 8601 format */
|
||||
fun nowISO8601(): String {
|
||||
return ISO_FORMATTER.format(Instant.now())
|
||||
}
|
||||
|
||||
/** Parse an iOS-compatible ISO 8601 date string back to Instant */
|
||||
fun parseISO8601(dateString: String): Instant? {
|
||||
return try {
|
||||
Instant.from(ISO_FORMATTER.parse(dateString))
|
||||
} catch (_: Exception) {
|
||||
|
||||
try {
|
||||
Instant.parse(dateString)
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a UTC ISO 8601 date string for display in local timezone This fixes the timezone
|
||||
* display issue where UTC dates were shown as local dates
|
||||
*/
|
||||
fun formatDateForDisplay(dateString: String): String {
|
||||
val pattern = "MMM dd, yyyy"
|
||||
return try {
|
||||
val instant = parseISO8601(dateString)
|
||||
if (instant != null) {
|
||||
val localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault())
|
||||
localDateTime.format(DateTimeFormatter.ofPattern(pattern))
|
||||
} else {
|
||||
// Fallback for malformed dates
|
||||
dateString.take(10)
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
dateString.take(10)
|
||||
}
|
||||
}
|
||||
|
||||
/** Parse a UTC ISO 8601 date string to LocalDateTime in system timezone */
|
||||
fun parseToLocalDateTime(dateString: String): LocalDateTime? {
|
||||
return try {
|
||||
val instant = parseISO8601(dateString)
|
||||
instant?.let { LocalDateTime.ofInstant(it, ZoneId.systemDefault()) }
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.atridad.ascently.utils
|
||||
|
||||
import java.security.MessageDigest
|
||||
|
||||
/**
|
||||
* Utility for creating consistent image filenames across iOS and Android platforms. Uses
|
||||
* deterministic naming based on problem ID and timestamp to ensure sync compatibility.
|
||||
*/
|
||||
object ImageNamingUtils {
|
||||
|
||||
private const val IMAGE_EXTENSION = ".jpg"
|
||||
private const val HASH_LENGTH = 12 // First 12 chars of SHA-256
|
||||
|
||||
/** Generates a deterministic filename for a problem image */
|
||||
fun generateImageFilename(problemId: String, imageIndex: Int): String {
|
||||
val input = "${problemId}_${imageIndex}"
|
||||
val hash = createHash(input)
|
||||
|
||||
return "problem_${hash}_${imageIndex}${IMAGE_EXTENSION}"
|
||||
}
|
||||
|
||||
/** Legacy method for backward compatibility */
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
fun generateImageFilename(problemId: String, timestamp: String, imageIndex: Int): String {
|
||||
return generateImageFilename(problemId, imageIndex)
|
||||
}
|
||||
|
||||
/** Creates a deterministic hash from input string */
|
||||
private fun createHash(input: String): String {
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
val hashBytes = digest.digest(input.toByteArray(Charsets.UTF_8))
|
||||
val hashHex = hashBytes.joinToString("") { "%02x".format(it) }
|
||||
return hashHex.take(HASH_LENGTH)
|
||||
}
|
||||
|
||||
/** Creates a mapping of existing server filenames to canonical filenames */
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
package com.atridad.ascently.utils
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.ImageDecoder
|
||||
import android.net.Uri
|
||||
import androidx.core.graphics.scale
|
||||
import androidx.exifinterface.media.ExifInterface
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.util.UUID
|
||||
|
||||
object ImageUtils {
|
||||
|
||||
private const val IMAGES_DIR = "problem_images"
|
||||
private const val MAX_IMAGE_SIZE = 1024
|
||||
private const val IMAGE_QUALITY = 85
|
||||
|
||||
// Creates the images directory if it doesn't exist
|
||||
fun getImagesDirectory(context: Context): File {
|
||||
val imagesDir = File(context.filesDir, IMAGES_DIR)
|
||||
if (!imagesDir.exists()) {
|
||||
imagesDir.mkdirs()
|
||||
}
|
||||
return imagesDir
|
||||
}
|
||||
|
||||
/** 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) {
|
||||
AppLogger.e("ImageUtils", e) { "Error saving image with EXIF data" }
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/** Compresses and resizes an image bitmap */
|
||||
@SuppressLint("UseKtx")
|
||||
private fun compressImage(original: Bitmap): Bitmap {
|
||||
val width = original.width
|
||||
val height = original.height
|
||||
|
||||
// Calculate the scaling factor
|
||||
val scaleFactor =
|
||||
if (width > height) {
|
||||
if (width > MAX_IMAGE_SIZE) MAX_IMAGE_SIZE.toFloat() / width else 1f
|
||||
} else {
|
||||
if (height > MAX_IMAGE_SIZE) MAX_IMAGE_SIZE.toFloat() / height else 1f
|
||||
}
|
||||
|
||||
return if (scaleFactor < 1f) {
|
||||
val newWidth = (width * scaleFactor).toInt()
|
||||
val newHeight = (height * scaleFactor).toInt()
|
||||
original.scale(newWidth, newHeight)
|
||||
} else {
|
||||
original
|
||||
}
|
||||
}
|
||||
|
||||
/** Gets the full file path for an image */
|
||||
fun getImageFile(context: Context, relativePath: String): File {
|
||||
// If relativePath already contains the directory, use it as-is
|
||||
// Otherwise, assume it's just a filename and add the images directory
|
||||
return if (relativePath.contains("/")) {
|
||||
File(context.filesDir, relativePath)
|
||||
} else {
|
||||
// Just a filename - look in the images directory
|
||||
File(getImagesDirectory(context), relativePath)
|
||||
}
|
||||
}
|
||||
|
||||
/** Deletes an image file */
|
||||
fun deleteImage(context: Context, relativePath: String): Boolean {
|
||||
return try {
|
||||
val file = getImageFile(context, relativePath)
|
||||
file.delete()
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e("ImageUtils", e) { "Failed to delete image: $relativePath" }
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/** Imports an image file from the import directory */
|
||||
fun importImageFile(context: Context, sourceFile: File): String? {
|
||||
return try {
|
||||
if (!sourceFile.exists()) return null
|
||||
|
||||
// Generate new filename to avoid conflicts
|
||||
val extension = sourceFile.extension.ifEmpty { "jpg" }
|
||||
val filename = "${UUID.randomUUID()}.$extension"
|
||||
val destFile = File(getImagesDirectory(context), filename)
|
||||
|
||||
sourceFile.copyTo(destFile, overwrite = true)
|
||||
"$IMAGES_DIR/$filename"
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e("ImageUtils", e) { "Failed to import image from source: ${sourceFile.name}" }
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Gets all image files in the images directory */
|
||||
fun getAllImages(context: Context): List<String> {
|
||||
return try {
|
||||
val imagesDir = getImagesDirectory(context)
|
||||
imagesDir.listFiles()?.mapNotNull { file ->
|
||||
if (file.isFile &&
|
||||
(file.extension == "jpg" ||
|
||||
file.extension == "jpeg" ||
|
||||
file.extension == "png")
|
||||
) {
|
||||
"$IMAGES_DIR/${file.name}"
|
||||
} else null
|
||||
}
|
||||
?: emptyList()
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e("ImageUtils", e) { "Failed to enumerate images directory" }
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
/** Temporarily saves an image during selection process */
|
||||
fun saveTemporaryImageFromUri(context: Context, imageUri: Uri): String? {
|
||||
return try {
|
||||
val source = ImageDecoder.createSource(context.contentResolver, imageUri)
|
||||
val originalBitmap = ImageDecoder.decodeBitmap(source)
|
||||
|
||||
val tempFilename = "temp_${UUID.randomUUID()}.jpg"
|
||||
val imageFile = File(getImagesDirectory(context), tempFilename)
|
||||
|
||||
val success = saveImageWithExif(context, imageUri, originalBitmap, imageFile)
|
||||
originalBitmap.recycle()
|
||||
|
||||
if (!success) return null
|
||||
|
||||
tempFilename
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e("ImageUtils", e) { "Error saving temporary image from URI" }
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Renames a temporary image */
|
||||
fun renameTemporaryImage(
|
||||
context: Context,
|
||||
tempFilename: String,
|
||||
problemId: String,
|
||||
imageIndex: Int
|
||||
): String? {
|
||||
return try {
|
||||
val tempFile = File(getImagesDirectory(context), tempFilename)
|
||||
if (!tempFile.exists()) {
|
||||
AppLogger.e("ImageUtils") { "Temporary file does not exist: $tempFilename" }
|
||||
return null
|
||||
}
|
||||
|
||||
val deterministicFilename =
|
||||
ImageNamingUtils.generateImageFilename(problemId, imageIndex)
|
||||
val finalFile = File(getImagesDirectory(context), deterministicFilename)
|
||||
|
||||
if (tempFile.renameTo(finalFile)) {
|
||||
AppLogger.d("ImageUtils") { "Renamed temporary image: $tempFilename -> $deterministicFilename" }
|
||||
deterministicFilename
|
||||
} else {
|
||||
AppLogger.e("ImageUtils") { "Failed to rename temporary image: $tempFilename" }
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e("ImageUtils", e) { "Error renaming temporary image" }
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Saves image data with a specific filename */
|
||||
fun saveImageFromBytesWithFilename(
|
||||
context: Context,
|
||||
imageData: ByteArray,
|
||||
filename: String
|
||||
): String? {
|
||||
return try {
|
||||
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
|
||||
FileOutputStream(imageFile).use { output ->
|
||||
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
||||
}
|
||||
|
||||
// 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
|
||||
AppLogger.w("ImageUtils") { "Failed to preserve EXIF data: ${e.message}" }
|
||||
}
|
||||
|
||||
bitmap.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
|
||||
"$IMAGES_DIR/$filename"
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e("ImageUtils", e) { "Failed to save image from bytes: $filename" }
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Cleans up orphaned images that are not referenced by any problems */
|
||||
fun cleanupOrphanedImages(context: Context, referencedPaths: Set<String>) {
|
||||
try {
|
||||
val allImages = getAllImages(context)
|
||||
val orphanedImages = allImages.filter { it !in referencedPaths }
|
||||
|
||||
orphanedImages.forEach { path -> deleteImage(context, path) }
|
||||
} catch (e: Exception) {
|
||||
AppLogger.e("ImageUtils", e) { "Failed to clean up orphaned images" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package com.atridad.ascently.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import androidx.core.content.edit
|
||||
|
||||
class MigrationManager(private val context: Context) {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "MigrationManager"
|
||||
private const val MIGRATION_PREFS = "ascently_migration_state"
|
||||
private const val MIGRATION_COMPLETED_KEY = "openclimb_to_ascently_completed"
|
||||
}
|
||||
|
||||
private val migrationPrefs: SharedPreferences =
|
||||
context.getSharedPreferences(MIGRATION_PREFS, Context.MODE_PRIVATE)
|
||||
|
||||
/**
|
||||
* Perform migration from OpenClimb to Ascently if needed This should be called early in app
|
||||
* startup
|
||||
*/
|
||||
fun migrateIfNeeded() {
|
||||
if (migrationPrefs.getBoolean(MIGRATION_COMPLETED_KEY, false)) {
|
||||
AppLogger.d(TAG) { "Migration already completed, skipping" }
|
||||
return
|
||||
}
|
||||
|
||||
AppLogger.i(TAG) { "🔄 Starting migration from OpenClimb to Ascently..." }
|
||||
var migrationCount = 0
|
||||
|
||||
// Migrate SharedPreferences
|
||||
migrationCount += migrateSharedPreferences()
|
||||
|
||||
// Mark migration as completed
|
||||
migrationPrefs.edit { putBoolean(MIGRATION_COMPLETED_KEY, true) }
|
||||
|
||||
if (migrationCount > 0) {
|
||||
AppLogger.i(TAG) { "🎉 Migration completed! Migrated $migrationCount items from OpenClimb to Ascently" }
|
||||
} else {
|
||||
AppLogger.i(TAG) { "ℹ️ No OpenClimb data found to migrate" }
|
||||
}
|
||||
}
|
||||
|
||||
/** Migrate SharedPreferences from OpenClimb naming to Ascently naming */
|
||||
private fun migrateSharedPreferences(): Int {
|
||||
var count = 0
|
||||
|
||||
// Define preference file migrations
|
||||
val preferenceFileMigrations =
|
||||
listOf(
|
||||
"openclimb_data_state" to "ascently_data_state",
|
||||
"health_connect_prefs" to "health_connect_prefs", // Keep same name
|
||||
"deleted_items" to "deleted_items", // Keep same name
|
||||
"sync_preferences" to "sync_preferences" // Keep same name
|
||||
)
|
||||
|
||||
for ((oldFileName, newFileName) in preferenceFileMigrations) {
|
||||
if (oldFileName != newFileName) {
|
||||
count += migratePreferenceFile(oldFileName, newFileName)
|
||||
}
|
||||
}
|
||||
|
||||
// Migrate specific keys within preference files
|
||||
count += migratePreferenceKeys()
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
/** Migrate an entire SharedPreferences file */
|
||||
private fun migratePreferenceFile(oldFileName: String, newFileName: String): Int {
|
||||
val oldPrefs = context.getSharedPreferences(oldFileName, Context.MODE_PRIVATE)
|
||||
val newPrefs = context.getSharedPreferences(newFileName, Context.MODE_PRIVATE)
|
||||
|
||||
// If old prefs exist and new prefs are empty, migrate
|
||||
if (oldPrefs.all.isNotEmpty() && newPrefs.all.isEmpty()) {
|
||||
newPrefs.edit {
|
||||
oldPrefs.all.forEach { (key, value) ->
|
||||
when (value) {
|
||||
is String -> putString(key, value)
|
||||
is Int -> putInt(key, value)
|
||||
is Long -> putLong(key, value)
|
||||
is Float -> putFloat(key, value)
|
||||
is Boolean -> putBoolean(key, value)
|
||||
is Set<*> -> {
|
||||
@Suppress("UNCHECKED_CAST") putStringSet(key, value as Set<String>)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear old preferences
|
||||
oldPrefs.edit { clear() }
|
||||
|
||||
AppLogger.d(TAG) { "Migrated preference file: $oldFileName → $newFileName (${oldPrefs.all.size} keys)" }
|
||||
return oldPrefs.all.size
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
/** Migrate specific keys within preference files that might contain openclimb references */
|
||||
private fun migratePreferenceKeys(): Int {
|
||||
var count = 0
|
||||
|
||||
// Check for any openclimb-prefixed keys across all preference files
|
||||
val preferencesToCheck =
|
||||
listOf(
|
||||
"ascently_data_state",
|
||||
"health_connect_prefs",
|
||||
"deleted_items",
|
||||
"sync_preferences"
|
||||
)
|
||||
|
||||
for (prefFileName in preferencesToCheck) {
|
||||
val prefs = context.getSharedPreferences(prefFileName, Context.MODE_PRIVATE)
|
||||
val keysToMigrate = mutableListOf<Pair<String, String>>()
|
||||
|
||||
// Find keys that start with openclimb_ and should be ascently_
|
||||
prefs.all.keys.forEach { key ->
|
||||
if (key.startsWith("openclimb_")) {
|
||||
val newKey = key.replace("openclimb_", "ascently_")
|
||||
keysToMigrate.add(key to newKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Perform the key migrations
|
||||
if (keysToMigrate.isNotEmpty()) {
|
||||
prefs.edit {
|
||||
keysToMigrate.forEach { (oldKey, newKey) ->
|
||||
val value = prefs.all[oldKey]
|
||||
when (value) {
|
||||
is String -> putString(newKey, value)
|
||||
is Int -> putInt(newKey, value)
|
||||
is Long -> putLong(newKey, value)
|
||||
is Float -> putFloat(newKey, value)
|
||||
is Boolean -> putBoolean(newKey, value)
|
||||
is Set<*> -> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
putStringSet(newKey, value as Set<String>)
|
||||
}
|
||||
}
|
||||
remove(oldKey)
|
||||
}
|
||||
}
|
||||
|
||||
AppLogger.d(TAG) { "Migrated ${keysToMigrate.size} keys in $prefFileName" }
|
||||
count += keysToMigrate.size
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
/** Check if migration has been completed */
|
||||
fun isMigrationCompleted(): Boolean {
|
||||
return migrationPrefs.getBoolean(MIGRATION_COMPLETED_KEY, false)
|
||||
}
|
||||
|
||||
/** Reset migration state (for testing purposes) */
|
||||
fun resetMigrationState() {
|
||||
migrationPrefs.edit { putBoolean(MIGRATION_COMPLETED_KEY, false) }
|
||||
AppLogger.d(TAG) { "Migration state reset" }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.utils
|
||||
package com.atridad.ascently.utils
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.utils
|
||||
package com.atridad.ascently.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
@@ -7,23 +7,23 @@ import android.content.pm.ShortcutManager
|
||||
import android.graphics.drawable.Icon
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import com.atridad.openclimb.MainActivity
|
||||
import com.atridad.openclimb.R
|
||||
import com.atridad.ascently.MainActivity
|
||||
import com.atridad.ascently.R
|
||||
|
||||
object AppShortcutManager {
|
||||
|
||||
const val SHORTCUT_START_SESSION = "start_session"
|
||||
const val SHORTCUT_END_SESSION = "end_session"
|
||||
|
||||
const val ACTION_START_SESSION = "com.atridad.openclimb.action.START_SESSION"
|
||||
const val ACTION_END_SESSION = "com.atridad.openclimb.action.END_SESSION"
|
||||
const val ACTION_START_SESSION = "com.atridad.ascently.action.START_SESSION"
|
||||
const val ACTION_END_SESSION = "com.atridad.ascently.action.END_SESSION"
|
||||
|
||||
/** Updates the app shortcuts based on current session state */
|
||||
fun updateShortcuts(
|
||||
context: Context,
|
||||
hasActiveSession: Boolean,
|
||||
hasGyms: Boolean,
|
||||
lastUsedGym: com.atridad.openclimb.data.model.Gym? = null
|
||||
lastUsedGym: com.atridad.ascently.data.model.Gym? = null
|
||||
) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||
val shortcutManager = context.getSystemService(ShortcutManager::class.java)
|
||||
@@ -45,7 +45,7 @@ object AppShortcutManager {
|
||||
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
||||
private fun createStartSessionShortcut(
|
||||
context: Context,
|
||||
lastUsedGym: com.atridad.openclimb.data.model.Gym? = null
|
||||
lastUsedGym: com.atridad.ascently.data.model.Gym? = null
|
||||
): ShortcutInfo {
|
||||
val startIntent =
|
||||
Intent(context, MainActivity::class.java).apply {
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.atridad.openclimb.utils
|
||||
package com.atridad.ascently.utils
|
||||
|
||||
import android.content.Context
|
||||
import com.atridad.openclimb.data.format.BackupProblem
|
||||
import com.atridad.openclimb.data.format.ClimbDataBackup
|
||||
import com.atridad.ascently.data.format.BackupProblem
|
||||
import com.atridad.ascently.data.format.ClimbDataBackup
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
@@ -22,25 +22,25 @@ object ZipExportImportUtils {
|
||||
|
||||
/** Creates a ZIP file containing the JSON data and all referenced images */
|
||||
fun createExportZip(
|
||||
context: Context,
|
||||
exportData: ClimbDataBackup,
|
||||
referencedImagePaths: Set<String>,
|
||||
directory: File? = null
|
||||
context: Context,
|
||||
exportData: ClimbDataBackup,
|
||||
referencedImagePaths: Set<String>,
|
||||
directory: File? = null
|
||||
): File {
|
||||
val exportDir =
|
||||
directory
|
||||
?: File(
|
||||
context.getExternalFilesDir(
|
||||
android.os.Environment.DIRECTORY_DOCUMENTS
|
||||
),
|
||||
"OpenClimb"
|
||||
)
|
||||
directory
|
||||
?: File(
|
||||
context.getExternalFilesDir(
|
||||
android.os.Environment.DIRECTORY_DOCUMENTS
|
||||
),
|
||||
"Ascently"
|
||||
)
|
||||
if (!exportDir.exists()) {
|
||||
exportDir.mkdirs()
|
||||
}
|
||||
|
||||
val timestamp = LocalDateTime.now().toString().replace(":", "-").replace(".", "-")
|
||||
val zipFile = File(exportDir, "openclimb_export_$timestamp.zip")
|
||||
val zipFile = File(exportDir, "ascently_export_$timestamp.zip")
|
||||
|
||||
try {
|
||||
ZipOutputStream(FileOutputStream(zipFile)).use { zipOut ->
|
||||
@@ -52,10 +52,11 @@ object ZipExportImportUtils {
|
||||
zipOut.closeEntry()
|
||||
|
||||
// Add JSON data file
|
||||
val json = Json {
|
||||
prettyPrint = true
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
val json =
|
||||
Json {
|
||||
prettyPrint = true
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
val jsonString = json.encodeToString(exportData)
|
||||
|
||||
val jsonEntry = ZipEntry(DATA_JSON_FILENAME)
|
||||
@@ -78,24 +79,21 @@ object ZipExportImportUtils {
|
||||
zipOut.closeEntry()
|
||||
successfulImages++
|
||||
} else {
|
||||
android.util.Log.w(
|
||||
"ZipExportImportUtils",
|
||||
"Image file not found or empty: $imagePath"
|
||||
)
|
||||
AppLogger.w("ZipExportImportUtils") {
|
||||
"Image file not found or empty: $imagePath"
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e(
|
||||
"ZipExportImportUtils",
|
||||
"Failed to add image $imagePath: ${e.message}"
|
||||
)
|
||||
AppLogger.e("ZipExportImportUtils", e) {
|
||||
"Failed to add image $imagePath: ${e.message}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Log export summary
|
||||
android.util.Log.i(
|
||||
"ZipExportImportUtils",
|
||||
"Export completed: ${successfulImages}/${referencedImagePaths.size} images included"
|
||||
)
|
||||
AppLogger.i("ZipExportImportUtils") {
|
||||
"Export completed: ${successfulImages}/${referencedImagePaths.size} images included"
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the created ZIP file
|
||||
@@ -115,10 +113,10 @@ object ZipExportImportUtils {
|
||||
|
||||
/** Creates a ZIP file and writes it to a provided URI */
|
||||
fun createExportZipToUri(
|
||||
context: Context,
|
||||
uri: android.net.Uri,
|
||||
exportData: ClimbDataBackup,
|
||||
referencedImagePaths: Set<String>
|
||||
context: Context,
|
||||
uri: android.net.Uri,
|
||||
exportData: ClimbDataBackup,
|
||||
referencedImagePaths: Set<String>
|
||||
) {
|
||||
try {
|
||||
context.contentResolver.openOutputStream(uri)?.use { outputStream ->
|
||||
@@ -131,10 +129,11 @@ object ZipExportImportUtils {
|
||||
zipOut.closeEntry()
|
||||
|
||||
// Add JSON data file
|
||||
val json = Json {
|
||||
prettyPrint = true
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
val json =
|
||||
Json {
|
||||
prettyPrint = true
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
val jsonString = json.encodeToString(exportData)
|
||||
|
||||
val jsonEntry = ZipEntry(DATA_JSON_FILENAME)
|
||||
@@ -158,32 +157,30 @@ object ZipExportImportUtils {
|
||||
successfulImages++
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e(
|
||||
"ZipExportImportUtils",
|
||||
"Failed to add image $imagePath: ${e.message}"
|
||||
)
|
||||
AppLogger.e("ZipExportImportUtils", e) {
|
||||
"Failed to add image $imagePath: ${e.message}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
android.util.Log.i(
|
||||
"ZipExportImportUtils",
|
||||
"Export to URI completed: ${successfulImages}/${referencedImagePaths.size} images included"
|
||||
)
|
||||
AppLogger.i("ZipExportImportUtils") {
|
||||
"Export to URI completed: ${successfulImages}/${referencedImagePaths.size} images included"
|
||||
}
|
||||
}
|
||||
}
|
||||
?: throw IOException("Could not open output stream")
|
||||
?: throw IOException("Could not open output stream")
|
||||
} catch (e: Exception) {
|
||||
throw IOException("Failed to create export ZIP to URI: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun createMetadata(
|
||||
exportData: ClimbDataBackup,
|
||||
referencedImagePaths: Set<String>
|
||||
exportData: ClimbDataBackup,
|
||||
referencedImagePaths: Set<String>
|
||||
): String {
|
||||
return buildString {
|
||||
appendLine("OpenClimb Export Metadata")
|
||||
appendLine("=======================")
|
||||
appendLine("Ascently Export Metadata")
|
||||
appendLine("========================")
|
||||
appendLine("Export Date: ${exportData.exportedAt}")
|
||||
appendLine("Version: ${exportData.version}")
|
||||
appendLine("Gyms: ${exportData.gyms.size}")
|
||||
@@ -197,8 +194,8 @@ object ZipExportImportUtils {
|
||||
|
||||
/** Data class to hold extraction results */
|
||||
data class ImportResult(
|
||||
val jsonContent: String,
|
||||
val importedImagePaths: Map<String, String> // original filename -> new relative path
|
||||
val jsonContent: String,
|
||||
val importedImagePaths: Map<String, String> // original filename -> new relative path
|
||||
)
|
||||
|
||||
/** Extracts a ZIP file and returns the JSON content and imported image paths */
|
||||
@@ -217,16 +214,17 @@ object ZipExportImportUtils {
|
||||
// Read metadata for validation
|
||||
val metadataContent = zipIn.readBytes().toString(Charsets.UTF_8)
|
||||
foundRequiredFiles.add("metadata")
|
||||
android.util.Log.i(
|
||||
"ZipExportImportUtils",
|
||||
"Found metadata: ${metadataContent.lines().take(3).joinToString()}"
|
||||
)
|
||||
AppLogger.i("ZipExportImportUtils") {
|
||||
"Found metadata: ${metadataContent.lines().take(3).joinToString()}"
|
||||
}
|
||||
}
|
||||
|
||||
entry.name == DATA_JSON_FILENAME -> {
|
||||
// Read JSON data
|
||||
jsonContent = zipIn.readBytes().toString(Charsets.UTF_8)
|
||||
foundRequiredFiles.add("data")
|
||||
}
|
||||
|
||||
entry.name.startsWith("$IMAGES_DIR_NAME/") && !entry.isDirectory -> {
|
||||
// Extract image file
|
||||
val originalFilename = entry.name.substringAfter("$IMAGES_DIR_NAME/")
|
||||
@@ -234,11 +232,11 @@ object ZipExportImportUtils {
|
||||
try {
|
||||
// Create temporary file to hold the extracted image
|
||||
val tempFile =
|
||||
File.createTempFile(
|
||||
"import_image_",
|
||||
"_$originalFilename",
|
||||
context.cacheDir
|
||||
)
|
||||
File.createTempFile(
|
||||
"import_image_",
|
||||
"_$originalFilename",
|
||||
context.cacheDir
|
||||
)
|
||||
|
||||
FileOutputStream(tempFile).use { output -> zipIn.copyTo(output) }
|
||||
|
||||
@@ -248,37 +246,33 @@ object ZipExportImportUtils {
|
||||
val newPath = ImageUtils.importImageFile(context, tempFile)
|
||||
if (newPath != null) {
|
||||
importedImagePaths[originalFilename] = newPath
|
||||
android.util.Log.d(
|
||||
"ZipExportImportUtils",
|
||||
"Successfully imported image: $originalFilename -> $newPath"
|
||||
)
|
||||
AppLogger.d("ZipExportImportUtils") {
|
||||
"Successfully imported image: $originalFilename -> $newPath"
|
||||
}
|
||||
} else {
|
||||
android.util.Log.w(
|
||||
"ZipExportImportUtils",
|
||||
"Failed to import image: $originalFilename"
|
||||
)
|
||||
AppLogger.w("ZipExportImportUtils") {
|
||||
"Failed to import image: $originalFilename"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
android.util.Log.w(
|
||||
"ZipExportImportUtils",
|
||||
"Extracted image is empty: $originalFilename"
|
||||
)
|
||||
AppLogger.w("ZipExportImportUtils") {
|
||||
"Extracted image is empty: $originalFilename"
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up temp file
|
||||
tempFile.delete()
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e(
|
||||
"ZipExportImportUtils",
|
||||
"Failed to process image $originalFilename: ${e.message}"
|
||||
)
|
||||
AppLogger.e("ZipExportImportUtils", e) {
|
||||
"Failed to process image $originalFilename: ${e.message}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
android.util.Log.d(
|
||||
"ZipExportImportUtils",
|
||||
"Skipping ZIP entry: ${entry.name}"
|
||||
)
|
||||
AppLogger.d("ZipExportImportUtils") {
|
||||
"Skipping ZIP entry: ${entry.name}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,10 +290,9 @@ object ZipExportImportUtils {
|
||||
throw IOException("Invalid ZIP file: data.json is empty")
|
||||
}
|
||||
|
||||
android.util.Log.i(
|
||||
"ZipExportImportUtils",
|
||||
"Import extraction completed: ${importedImagePaths.size} images processed"
|
||||
)
|
||||
AppLogger.i("ZipExportImportUtils") {
|
||||
"Import extraction completed: ${importedImagePaths.size} images processed"
|
||||
}
|
||||
|
||||
return ImportResult(jsonContent, importedImagePaths)
|
||||
} catch (e: Exception) {
|
||||
@@ -312,16 +305,16 @@ object ZipExportImportUtils {
|
||||
* the new ones after import
|
||||
*/
|
||||
fun updateProblemImagePaths(
|
||||
problems: List<BackupProblem>,
|
||||
imagePathMapping: Map<String, String>
|
||||
problems: List<BackupProblem>,
|
||||
imagePathMapping: Map<String, String>
|
||||
): List<BackupProblem> {
|
||||
return problems.map { problem ->
|
||||
val updatedImagePaths =
|
||||
(problem.imagePaths ?: emptyList()).mapNotNull { oldPath ->
|
||||
// Extract filename from the old path
|
||||
val filename = oldPath.substringAfterLast("/")
|
||||
imagePathMapping[filename]
|
||||
}
|
||||
(problem.imagePaths ?: emptyList()).mapNotNull { oldPath ->
|
||||
// Extract filename from the old path
|
||||
val filename = oldPath.substringAfterLast("/")
|
||||
imagePathMapping[filename]
|
||||
}
|
||||
problem.withUpdatedImagePaths(updatedImagePaths)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.atridad.openclimb.widget
|
||||
package com.atridad.ascently.widget
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
@@ -7,10 +7,11 @@ import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.widget.RemoteViews
|
||||
import com.atridad.openclimb.MainActivity
|
||||
import com.atridad.openclimb.R
|
||||
import com.atridad.openclimb.data.database.OpenClimbDatabase
|
||||
import com.atridad.openclimb.data.repository.ClimbRepository
|
||||
import com.atridad.ascently.MainActivity
|
||||
import com.atridad.ascently.R
|
||||
import com.atridad.ascently.data.database.AscentlyDatabase
|
||||
import com.atridad.ascently.data.repository.ClimbRepository
|
||||
import java.time.LocalDate
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
@@ -45,57 +46,50 @@ class ClimbStatsWidgetProvider : AppWidgetProvider() {
|
||||
) {
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
val database = OpenClimbDatabase.getDatabase(context)
|
||||
val database = AscentlyDatabase.getDatabase(context)
|
||||
val repository = ClimbRepository(database, context)
|
||||
|
||||
// Fetch stats data
|
||||
// Get last 7 days date range (rolling period)
|
||||
val today = LocalDate.now()
|
||||
val sevenDaysAgo = today.minusDays(6) // Today + 6 days ago = 7 days total
|
||||
|
||||
// Fetch all sessions and attempts
|
||||
val sessions = repository.getAllSessions().first()
|
||||
val problems = repository.getAllProblems().first()
|
||||
val attempts = repository.getAllAttempts().first()
|
||||
val gyms = repository.getAllGyms().first()
|
||||
val activeSession = repository.getActiveSession()
|
||||
|
||||
// Calculate stats
|
||||
val completedSessions = sessions.filter { it.endTime != null }
|
||||
|
||||
// Count problems that have been completed (have at least one successful attempt)
|
||||
val completedProblems =
|
||||
problems
|
||||
.filter { problem ->
|
||||
attempts.any { attempt ->
|
||||
attempt.problemId == problem.id &&
|
||||
(attempt.result ==
|
||||
com.atridad.openclimb.data.model
|
||||
.AttemptResult.SUCCESS ||
|
||||
attempt.result ==
|
||||
com.atridad.openclimb.data.model
|
||||
.AttemptResult.FLASH)
|
||||
}
|
||||
}
|
||||
.size
|
||||
|
||||
val favoriteGym =
|
||||
sessions.groupBy { it.gymId }.maxByOrNull { it.value.size }?.let {
|
||||
(gymId, _) ->
|
||||
gyms.find { it.id == gymId }?.name
|
||||
// Filter for last 7 days across all gyms
|
||||
val weekSessions =
|
||||
sessions.filter { session ->
|
||||
try {
|
||||
val sessionDate = LocalDate.parse(session.date.substring(0, 10))
|
||||
!sessionDate.isBefore(sevenDaysAgo) && !sessionDate.isAfter(today)
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
?: "No sessions yet"
|
||||
|
||||
val weekSessionIds = weekSessions.map { it.id }.toSet()
|
||||
|
||||
// Count total attempts this week
|
||||
val totalAttempts =
|
||||
attempts.count { attempt -> weekSessionIds.contains(attempt.sessionId) }
|
||||
|
||||
// Count sessions this week
|
||||
val totalSessions = weekSessions.size
|
||||
|
||||
launch(Dispatchers.Main) {
|
||||
val views = RemoteViews(context.packageName, R.layout.widget_climb_stats)
|
||||
|
||||
views.setTextViewText(
|
||||
R.id.widget_total_sessions,
|
||||
completedSessions.size.toString()
|
||||
)
|
||||
views.setTextViewText(
|
||||
R.id.widget_problems_completed,
|
||||
completedProblems.toString()
|
||||
)
|
||||
views.setTextViewText(R.id.widget_total_problems, problems.size.toString())
|
||||
views.setTextViewText(R.id.widget_favorite_gym, favoriteGym)
|
||||
// Set weekly stats
|
||||
views.setTextViewText(R.id.widget_attempts_value, totalAttempts.toString())
|
||||
views.setTextViewText(R.id.widget_sessions_value, totalSessions.toString())
|
||||
|
||||
val intent = Intent(context, MainActivity::class.java)
|
||||
val intent =
|
||||
Intent(context, MainActivity::class.java).apply {
|
||||
flags =
|
||||
Intent.FLAG_ACTIVITY_NEW_TASK or
|
||||
Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
}
|
||||
val pendingIntent =
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
@@ -108,13 +102,11 @@ class ClimbStatsWidgetProvider : AppWidgetProvider() {
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
} catch (_: Exception) {
|
||||
launch(Dispatchers.Main) {
|
||||
val views = RemoteViews(context.packageName, R.layout.widget_climb_stats)
|
||||
views.setTextViewText(R.id.widget_total_sessions, "0")
|
||||
views.setTextViewText(R.id.widget_problems_completed, "0")
|
||||
views.setTextViewText(R.id.widget_total_problems, "0")
|
||||
views.setTextViewText(R.id.widget_favorite_gym, "No data")
|
||||
views.setTextViewText(R.id.widget_attempts_value, "0")
|
||||
views.setTextViewText(R.id.widget_sessions_value, "0")
|
||||
|
||||
val intent = Intent(context, MainActivity::class.java)
|
||||
val pendingIntent =
|
||||
@@ -1,426 +0,0 @@
|
||||
package com.atridad.openclimb.data.health
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.util.Log
|
||||
import androidx.activity.result.contract.ActivityResultContract
|
||||
import androidx.health.connect.client.HealthConnectClient
|
||||
import androidx.health.connect.client.PermissionController
|
||||
import androidx.health.connect.client.permission.HealthPermission
|
||||
import androidx.health.connect.client.records.ExerciseSessionRecord
|
||||
import androidx.health.connect.client.records.HeartRateRecord
|
||||
import androidx.health.connect.client.records.TotalCaloriesBurnedRecord
|
||||
import androidx.health.connect.client.units.Energy
|
||||
import com.atridad.openclimb.data.model.ClimbSession
|
||||
import com.atridad.openclimb.data.model.SessionStatus
|
||||
import com.atridad.openclimb.utils.DateFormatUtils
|
||||
import java.time.Duration
|
||||
import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
|
||||
/**
|
||||
* Health Connect manager for OpenClimb that syncs climbing sessions to Samsung Health, Google Fit,
|
||||
* and other health apps.
|
||||
*/
|
||||
@SuppressLint("RestrictedApi")
|
||||
class HealthConnectManager(private val context: Context) {
|
||||
|
||||
private val preferences: SharedPreferences =
|
||||
context.getSharedPreferences("health_connect_prefs", Context.MODE_PRIVATE)
|
||||
|
||||
private val _isEnabled = MutableStateFlow(preferences.getBoolean("enabled", false))
|
||||
private val _hasPermissions = MutableStateFlow(preferences.getBoolean("permissions", false))
|
||||
private val _autoSync = MutableStateFlow(preferences.getBoolean("auto_sync", true))
|
||||
private val _isCompatible = MutableStateFlow(true)
|
||||
|
||||
val isEnabled: Flow<Boolean> = _isEnabled.asStateFlow()
|
||||
val hasPermissions: Flow<Boolean> = _hasPermissions.asStateFlow()
|
||||
val autoSyncEnabled: Flow<Boolean> = _autoSync.asStateFlow()
|
||||
val isCompatible: Flow<Boolean> = _isCompatible.asStateFlow()
|
||||
|
||||
companion object {
|
||||
private const val TAG = "HealthConnectManager"
|
||||
|
||||
val REQUIRED_PERMISSIONS =
|
||||
setOf(
|
||||
HealthPermission.getReadPermission(ExerciseSessionRecord::class),
|
||||
HealthPermission.getWritePermission(ExerciseSessionRecord::class),
|
||||
HealthPermission.getReadPermission(HeartRateRecord::class),
|
||||
HealthPermission.getWritePermission(HeartRateRecord::class),
|
||||
HealthPermission.getReadPermission(TotalCaloriesBurnedRecord::class),
|
||||
HealthPermission.getWritePermission(TotalCaloriesBurnedRecord::class)
|
||||
)
|
||||
}
|
||||
|
||||
private val healthConnectClient by lazy {
|
||||
try {
|
||||
HealthConnectClient.getOrCreate(context)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to create Health Connect client", e)
|
||||
_isCompatible.value = false
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Check if Health Connect is available on this device */
|
||||
fun isHealthConnectAvailable(): Flow<Boolean> = flow {
|
||||
try {
|
||||
if (!_isCompatible.value) {
|
||||
emit(false)
|
||||
return@flow
|
||||
}
|
||||
val status = HealthConnectClient.getSdkStatus(context)
|
||||
emit(status == HealthConnectClient.SDK_AVAILABLE)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error checking Health Connect availability", e)
|
||||
_isCompatible.value = false
|
||||
emit(false)
|
||||
}
|
||||
}
|
||||
|
||||
/** Enable or disable Health Connect integration */
|
||||
fun setEnabled(enabled: Boolean) {
|
||||
preferences.edit().putBoolean("enabled", enabled).apply()
|
||||
_isEnabled.value = enabled
|
||||
|
||||
if (!enabled) {
|
||||
setPermissionsGranted(false)
|
||||
}
|
||||
}
|
||||
|
||||
/** Update the permissions granted state */
|
||||
fun setPermissionsGranted(granted: Boolean) {
|
||||
preferences.edit().putBoolean("permissions", granted).apply()
|
||||
_hasPermissions.value = granted
|
||||
}
|
||||
|
||||
/** Enable or disable auto-sync */
|
||||
fun setAutoSyncEnabled(enabled: Boolean) {
|
||||
preferences.edit().putBoolean("auto_sync", enabled).apply()
|
||||
_autoSync.value = enabled
|
||||
}
|
||||
|
||||
/** Check if all required permissions are granted */
|
||||
suspend fun hasAllPermissions(): Boolean {
|
||||
return try {
|
||||
if (!_isCompatible.value || healthConnectClient == null) {
|
||||
return false
|
||||
}
|
||||
val grantedPermissions =
|
||||
healthConnectClient!!.permissionController.getGrantedPermissions()
|
||||
val hasAll =
|
||||
REQUIRED_PERMISSIONS.all { permission ->
|
||||
grantedPermissions.contains(permission)
|
||||
}
|
||||
setPermissionsGranted(hasAll)
|
||||
hasAll
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error checking permissions", e)
|
||||
setPermissionsGranted(false)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/** Check if Health Connect is ready for use */
|
||||
suspend fun isReady(): Boolean {
|
||||
return try {
|
||||
if (!_isEnabled.value || !_isCompatible.value || healthConnectClient == null)
|
||||
return false
|
||||
|
||||
val isAvailable =
|
||||
HealthConnectClient.getSdkStatus(context) == HealthConnectClient.SDK_AVAILABLE
|
||||
val hasPerms = if (isAvailable) hasAllPermissions() else false
|
||||
isAvailable && hasPerms
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error checking Health Connect readiness", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/** Get permission request contract */
|
||||
fun getPermissionRequestContract(): ActivityResultContract<Set<String>, Set<String>> {
|
||||
return PermissionController.createRequestPermissionResultContract()
|
||||
}
|
||||
|
||||
/** Test Health Connect functionality */
|
||||
fun testHealthConnectSync(): String {
|
||||
val results = mutableListOf<String>()
|
||||
|
||||
results.add("=== Health Connect Debug Test ===")
|
||||
|
||||
try {
|
||||
// Check availability synchronously
|
||||
val packageManager = context.packageManager
|
||||
val healthConnectPackages =
|
||||
listOf(
|
||||
"com.google.android.apps.healthdata",
|
||||
"com.android.health.connect",
|
||||
"androidx.health.connect"
|
||||
)
|
||||
|
||||
val available =
|
||||
healthConnectPackages.any { packageName ->
|
||||
try {
|
||||
packageManager.getPackageInfo(packageName, 0)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
results.add("Available: $available")
|
||||
|
||||
// Check enabled state
|
||||
results.add("Enabled in settings: ${_isEnabled.value}")
|
||||
|
||||
// Check permissions (simplified)
|
||||
val hasPerms = _hasPermissions.value
|
||||
results.add("Has permissions: $hasPerms")
|
||||
|
||||
// Check compatibility
|
||||
results.add("API Compatible: ${_isCompatible.value}")
|
||||
|
||||
val ready = _isEnabled.value && _isCompatible.value && available && hasPerms
|
||||
results.add("Ready to sync: $ready")
|
||||
|
||||
if (ready) {
|
||||
results.add("Health Connect is connected!")
|
||||
} else {
|
||||
results.add("❌ Health Connect not ready")
|
||||
if (!available) results.add("- Health Connect not available on device")
|
||||
if (!_isEnabled.value) results.add("- Not enabled in OpenClimb settings")
|
||||
if (!hasPerms) results.add("- Permissions not granted")
|
||||
if (!_isCompatible.value) results.add("- API compatibility issues")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
results.add("Test failed with error: ${e.message}")
|
||||
Log.e(TAG, "Health Connect test failed", e)
|
||||
}
|
||||
|
||||
return results.joinToString("\n")
|
||||
}
|
||||
|
||||
/** Get required permissions as strings */
|
||||
fun getRequiredPermissions(): Set<String> {
|
||||
return try {
|
||||
REQUIRED_PERMISSIONS.map { it.toString() }.toSet()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error getting required permissions", e)
|
||||
emptySet()
|
||||
}
|
||||
}
|
||||
|
||||
/** Sync a completed climbing session to Health Connect */
|
||||
@SuppressLint("RestrictedApi")
|
||||
suspend fun syncClimbingSession(
|
||||
session: ClimbSession,
|
||||
gymName: String,
|
||||
attemptCount: Int = 0
|
||||
): Result<Unit> {
|
||||
return try {
|
||||
if (!isReady()) {
|
||||
return Result.failure(IllegalStateException("Health Connect not ready"))
|
||||
}
|
||||
|
||||
if (session.status != SessionStatus.COMPLETED) {
|
||||
return Result.failure(
|
||||
IllegalArgumentException("Only completed sessions can be synced")
|
||||
)
|
||||
}
|
||||
|
||||
val startTime = session.startTime?.let { DateFormatUtils.parseISO8601(it) }
|
||||
val endTime = session.endTime?.let { DateFormatUtils.parseISO8601(it) }
|
||||
|
||||
if (startTime == null || endTime == null) {
|
||||
return Result.failure(
|
||||
IllegalArgumentException("Session must have valid start and end times")
|
||||
)
|
||||
}
|
||||
|
||||
Log.d(TAG, "Attempting to sync session '${session.id}' to Health Connect...")
|
||||
|
||||
val records = mutableListOf<androidx.health.connect.client.records.Record>()
|
||||
|
||||
try {
|
||||
val exerciseSession =
|
||||
ExerciseSessionRecord(
|
||||
startTime = startTime,
|
||||
startZoneOffset =
|
||||
ZoneOffset.systemDefault().rules.getOffset(startTime),
|
||||
endTime = endTime,
|
||||
endZoneOffset = ZoneOffset.systemDefault().rules.getOffset(endTime),
|
||||
exerciseType =
|
||||
ExerciseSessionRecord.EXERCISE_TYPE_STRENGTH_TRAINING,
|
||||
title = "Rock Climbing at $gymName"
|
||||
)
|
||||
records.add(exerciseSession)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Failed to create exercise session record", e)
|
||||
}
|
||||
|
||||
try {
|
||||
val durationMinutes = Duration.between(startTime, endTime).toMinutes()
|
||||
val estimatedCalories = estimateCaloriesForClimbing(durationMinutes, attemptCount)
|
||||
|
||||
if (estimatedCalories > 0) {
|
||||
val caloriesRecord =
|
||||
TotalCaloriesBurnedRecord(
|
||||
startTime = startTime,
|
||||
startZoneOffset =
|
||||
ZoneOffset.systemDefault().rules.getOffset(startTime),
|
||||
endTime = endTime,
|
||||
endZoneOffset =
|
||||
ZoneOffset.systemDefault().rules.getOffset(endTime),
|
||||
energy = Energy.calories(estimatedCalories)
|
||||
)
|
||||
records.add(caloriesRecord)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Failed to create calories record", e)
|
||||
}
|
||||
|
||||
try {
|
||||
val heartRateRecord = createHeartRateRecord(startTime, endTime, attemptCount)
|
||||
heartRateRecord?.let { records.add(it) }
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Failed to create heart rate record", e)
|
||||
}
|
||||
|
||||
if (records.isNotEmpty() && healthConnectClient != null) {
|
||||
Log.d(TAG, "Writing ${records.size} records to Health Connect...")
|
||||
healthConnectClient!!.insertRecords(records)
|
||||
Log.i(
|
||||
TAG,
|
||||
"Successfully synced ${records.size} records for session '${session.id}' to Health Connect"
|
||||
)
|
||||
|
||||
preferences
|
||||
.edit()
|
||||
.putString("last_sync_success", DateFormatUtils.nowISO8601())
|
||||
.apply()
|
||||
} else {
|
||||
val reason =
|
||||
when {
|
||||
records.isEmpty() -> "No records created"
|
||||
healthConnectClient == null -> "Health Connect client unavailable"
|
||||
else -> "Unknown reason"
|
||||
}
|
||||
Log.w(TAG, "Sync failed for session '${session.id}': $reason")
|
||||
return Result.failure(Exception("Sync failed: $reason"))
|
||||
}
|
||||
|
||||
Result.success(Unit)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error syncing climbing session to Health Connect", e)
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
|
||||
/** Auto-sync a session if enabled */
|
||||
suspend fun autoSyncSession(
|
||||
session: ClimbSession,
|
||||
gymName: String,
|
||||
attemptCount: Int = 0
|
||||
): Result<Unit> {
|
||||
return if (_autoSync.value && isReady()) {
|
||||
Log.d(TAG, "Auto-syncing session '${session.id}' to Health Connect...")
|
||||
syncClimbingSession(session, gymName, attemptCount)
|
||||
} else {
|
||||
val reason =
|
||||
when {
|
||||
!_autoSync.value -> "auto-sync disabled"
|
||||
!isReady() -> "Health Connect not ready"
|
||||
else -> "unknown reason"
|
||||
}
|
||||
Log.d(TAG, "Auto-sync skipped for session '${session.id}': $reason")
|
||||
Result.success(Unit)
|
||||
}
|
||||
}
|
||||
|
||||
/** Estimate calories burned during climbing */
|
||||
private fun estimateCaloriesForClimbing(durationMinutes: Long, attemptCount: Int): Double {
|
||||
val baseCaloriesPerMinute = 8.0
|
||||
val intensityMultiplier =
|
||||
when {
|
||||
attemptCount >= 20 -> 1.3
|
||||
attemptCount >= 10 -> 1.1
|
||||
else -> 0.9
|
||||
}
|
||||
return durationMinutes * baseCaloriesPerMinute * intensityMultiplier
|
||||
}
|
||||
|
||||
/** Create heart rate data */
|
||||
@SuppressLint("RestrictedApi")
|
||||
private fun createHeartRateRecord(
|
||||
startTime: Instant,
|
||||
endTime: Instant,
|
||||
attemptCount: Int
|
||||
): HeartRateRecord? {
|
||||
return try {
|
||||
val samples = mutableListOf<HeartRateRecord.Sample>()
|
||||
val intervalMinutes = 5L
|
||||
|
||||
val baseHeartRate =
|
||||
when {
|
||||
attemptCount >= 20 -> 155L
|
||||
attemptCount >= 10 -> 145L
|
||||
else -> 135L
|
||||
}
|
||||
|
||||
var currentTime = startTime
|
||||
while (currentTime.isBefore(endTime)) {
|
||||
val variation = (-15..15).random()
|
||||
val heartRate = (baseHeartRate + variation).coerceIn(110L, 180L)
|
||||
|
||||
samples.add(HeartRateRecord.Sample(time = currentTime, beatsPerMinute = heartRate))
|
||||
currentTime = currentTime.plusSeconds(intervalMinutes * 60)
|
||||
}
|
||||
|
||||
if (samples.isEmpty()) return null
|
||||
|
||||
HeartRateRecord(
|
||||
startTime = startTime,
|
||||
startZoneOffset = ZoneOffset.systemDefault().rules.getOffset(startTime),
|
||||
endTime = endTime,
|
||||
endZoneOffset = ZoneOffset.systemDefault().rules.getOffset(endTime),
|
||||
samples = samples
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error creating heart rate record", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Reset all preferences */
|
||||
fun reset() {
|
||||
preferences.edit().clear().apply()
|
||||
_isEnabled.value = false
|
||||
_hasPermissions.value = false
|
||||
_autoSync.value = true
|
||||
}
|
||||
|
||||
/** Check if ready for use */
|
||||
fun isReadySync(): Boolean {
|
||||
return _isEnabled.value && _hasPermissions.value
|
||||
}
|
||||
|
||||
/** Get last successful sync timestamp */
|
||||
fun getLastSyncSuccess(): String? {
|
||||
return preferences.getString("last_sync_success", null)
|
||||
}
|
||||
|
||||
/** Get detailed status */
|
||||
fun getDetailedStatus(): Map<String, String> {
|
||||
return mapOf(
|
||||
"enabled" to _isEnabled.value.toString(),
|
||||
"hasPermissions" to _hasPermissions.value.toString(),
|
||||
"autoSync" to _autoSync.value.toString(),
|
||||
"compatible" to _isCompatible.value.toString(),
|
||||
"lastSyncSuccess" to (getLastSyncSuccess() ?: "never")
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.atridad.openclimb.data.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
enum class ClimbType {
|
||||
ROPE,
|
||||
BOULDER;
|
||||
|
||||
/**
|
||||
* Get the display name
|
||||
*/
|
||||
fun getDisplayName(): String = when (this) {
|
||||
ROPE -> "Rope"
|
||||
BOULDER -> "Bouldering"
|
||||
}
|
||||
}
|
||||
@@ -1,224 +0,0 @@
|
||||
package com.atridad.openclimb.data.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
enum class DifficultySystem {
|
||||
// Bouldering
|
||||
V_SCALE,
|
||||
FONT,
|
||||
|
||||
// Rope
|
||||
YDS,
|
||||
CUSTOM;
|
||||
|
||||
/** Get the display name for the UI */
|
||||
fun getDisplayName(): String =
|
||||
when (this) {
|
||||
V_SCALE -> "V Scale"
|
||||
FONT -> "Font Scale"
|
||||
YDS -> "YDS (Yosemite)"
|
||||
CUSTOM -> "Custom"
|
||||
}
|
||||
|
||||
/** Check if this system is for bouldering */
|
||||
fun isBoulderingSystem(): Boolean =
|
||||
when (this) {
|
||||
V_SCALE, FONT -> true
|
||||
YDS -> false
|
||||
CUSTOM -> true
|
||||
}
|
||||
|
||||
/** Check if this system is for rope climbing */
|
||||
fun isRopeSystem(): Boolean =
|
||||
when (this) {
|
||||
YDS -> true
|
||||
V_SCALE, FONT -> false
|
||||
CUSTOM -> true
|
||||
}
|
||||
|
||||
/** Get available grades for this system */
|
||||
fun getAvailableGrades(): List<String> =
|
||||
when (this) {
|
||||
V_SCALE ->
|
||||
listOf(
|
||||
"VB",
|
||||
"V0",
|
||||
"V1",
|
||||
"V2",
|
||||
"V3",
|
||||
"V4",
|
||||
"V5",
|
||||
"V6",
|
||||
"V7",
|
||||
"V8",
|
||||
"V9",
|
||||
"V10",
|
||||
"V11",
|
||||
"V12",
|
||||
"V13",
|
||||
"V14",
|
||||
"V15",
|
||||
"V16",
|
||||
"V17"
|
||||
)
|
||||
FONT ->
|
||||
listOf(
|
||||
"3",
|
||||
"4A",
|
||||
"4B",
|
||||
"4C",
|
||||
"5A",
|
||||
"5B",
|
||||
"5C",
|
||||
"6A",
|
||||
"6A+",
|
||||
"6B",
|
||||
"6B+",
|
||||
"6C",
|
||||
"6C+",
|
||||
"7A",
|
||||
"7A+",
|
||||
"7B",
|
||||
"7B+",
|
||||
"7C",
|
||||
"7C+",
|
||||
"8A",
|
||||
"8A+",
|
||||
"8B",
|
||||
"8B+",
|
||||
"8C",
|
||||
"8C+"
|
||||
)
|
||||
YDS ->
|
||||
listOf(
|
||||
"5.0",
|
||||
"5.1",
|
||||
"5.2",
|
||||
"5.3",
|
||||
"5.4",
|
||||
"5.5",
|
||||
"5.6",
|
||||
"5.7",
|
||||
"5.8",
|
||||
"5.9",
|
||||
"5.10a",
|
||||
"5.10b",
|
||||
"5.10c",
|
||||
"5.10d",
|
||||
"5.11a",
|
||||
"5.11b",
|
||||
"5.11c",
|
||||
"5.11d",
|
||||
"5.12a",
|
||||
"5.12b",
|
||||
"5.12c",
|
||||
"5.12d",
|
||||
"5.13a",
|
||||
"5.13b",
|
||||
"5.13c",
|
||||
"5.13d",
|
||||
"5.14a",
|
||||
"5.14b",
|
||||
"5.14c",
|
||||
"5.14d",
|
||||
"5.15a",
|
||||
"5.15b",
|
||||
"5.15c",
|
||||
"5.15d"
|
||||
)
|
||||
CUSTOM -> emptyList()
|
||||
}
|
||||
|
||||
companion object {
|
||||
/** Get all difficulty systems based on type */
|
||||
fun getSystemsForClimbType(climbType: ClimbType): List<DifficultySystem> =
|
||||
when (climbType) {
|
||||
ClimbType.BOULDER -> entries.filter { it.isBoulderingSystem() }
|
||||
ClimbType.ROPE -> entries.filter { it.isRopeSystem() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class DifficultyGrade(val system: DifficultySystem, val grade: String, val numericValue: Int) {
|
||||
|
||||
constructor(
|
||||
system: DifficultySystem,
|
||||
grade: String
|
||||
) : this(system = system, grade = grade, numericValue = calculateNumericValue(system, grade))
|
||||
|
||||
companion object {
|
||||
private fun calculateNumericValue(system: DifficultySystem, grade: String): Int {
|
||||
return when (system) {
|
||||
DifficultySystem.V_SCALE -> {
|
||||
if (grade == "VB") 0 else grade.removePrefix("V").toIntOrNull() ?: 0
|
||||
}
|
||||
DifficultySystem.YDS -> {
|
||||
when {
|
||||
grade.startsWith("5.10") ->
|
||||
10 + (grade.getOrNull(4)?.toString()?.hashCode()?.rem(4) ?: 0)
|
||||
grade.startsWith("5.11") ->
|
||||
14 + (grade.getOrNull(4)?.toString()?.hashCode()?.rem(4) ?: 0)
|
||||
grade.startsWith("5.12") ->
|
||||
18 + (grade.getOrNull(4)?.toString()?.hashCode()?.rem(4) ?: 0)
|
||||
grade.startsWith("5.13") ->
|
||||
22 + (grade.getOrNull(4)?.toString()?.hashCode()?.rem(4) ?: 0)
|
||||
grade.startsWith("5.14") ->
|
||||
26 + (grade.getOrNull(4)?.toString()?.hashCode()?.rem(4) ?: 0)
|
||||
grade.startsWith("5.15") ->
|
||||
30 + (grade.getOrNull(4)?.toString()?.hashCode()?.rem(4) ?: 0)
|
||||
else -> grade.removePrefix("5.").toIntOrNull() ?: 0
|
||||
}
|
||||
}
|
||||
DifficultySystem.FONT -> {
|
||||
when {
|
||||
grade.startsWith("6A") -> 6
|
||||
grade.startsWith("6B") -> 7
|
||||
grade.startsWith("6C") -> 8
|
||||
grade.startsWith("7A") -> 9
|
||||
grade.startsWith("7B") -> 10
|
||||
grade.startsWith("7C") -> 11
|
||||
grade.startsWith("8A") -> 12
|
||||
grade.startsWith("8B") -> 13
|
||||
grade.startsWith("8C") -> 14
|
||||
else -> grade.toIntOrNull() ?: 0
|
||||
}
|
||||
}
|
||||
DifficultySystem.CUSTOM -> grade.hashCode().rem(100)
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Compare this grade with another grade of the same system Returns negative if this grade is
|
||||
* easier, positive if harder, 0 if equal
|
||||
*/
|
||||
fun compareTo(other: DifficultyGrade): Int {
|
||||
if (system != other.system) return 0
|
||||
|
||||
return when (system) {
|
||||
DifficultySystem.V_SCALE -> compareVScaleGrades(grade, other.grade)
|
||||
DifficultySystem.FONT -> compareFontGrades(grade, other.grade)
|
||||
DifficultySystem.YDS -> compareYDSGrades(grade, other.grade)
|
||||
DifficultySystem.CUSTOM -> grade.compareTo(other.grade)
|
||||
}
|
||||
}
|
||||
|
||||
private fun compareVScaleGrades(grade1: String, grade2: String): Int {
|
||||
if (grade1 == "VB" && grade2 != "VB") return -1
|
||||
if (grade2 == "VB" && grade1 != "VB") return 1
|
||||
if (grade1 == "VB" && grade2 == "VB") return 0
|
||||
|
||||
val num1 = grade1.removePrefix("V").toIntOrNull() ?: 0
|
||||
val num2 = grade2.removePrefix("V").toIntOrNull() ?: 0
|
||||
return num1.compareTo(num2)
|
||||
}
|
||||
|
||||
private fun compareFontGrades(grade1: String, grade2: String): Int {
|
||||
return grade1.compareTo(grade2)
|
||||
}
|
||||
|
||||
private fun compareYDSGrades(grade1: String, grade2: String): Int {
|
||||
return grade1.compareTo(grade2)
|
||||
}
|
||||
}
|
||||
@@ -1,292 +0,0 @@
|
||||
package com.atridad.openclimb.service
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.IBinder
|
||||
import androidx.core.app.NotificationCompat
|
||||
import com.atridad.openclimb.MainActivity
|
||||
import com.atridad.openclimb.R
|
||||
import com.atridad.openclimb.data.database.OpenClimbDatabase
|
||||
import com.atridad.openclimb.data.repository.ClimbRepository
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import java.time.LocalDateTime
|
||||
import java.time.temporal.ChronoUnit
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
class SessionTrackingService : Service() {
|
||||
|
||||
private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
private var notificationJob: Job? = null
|
||||
private var monitoringJob: Job? = null
|
||||
|
||||
private lateinit var repository: ClimbRepository
|
||||
private lateinit var notificationManager: NotificationManager
|
||||
|
||||
companion object {
|
||||
const val NOTIFICATION_ID = 1001
|
||||
const val CHANNEL_ID = "session_tracking_channel"
|
||||
const val ACTION_START_SESSION = "start_session"
|
||||
const val ACTION_STOP_SESSION = "stop_session"
|
||||
const val EXTRA_SESSION_ID = "session_id"
|
||||
|
||||
fun createStartIntent(context: Context, sessionId: String): Intent {
|
||||
return Intent(context, SessionTrackingService::class.java).apply {
|
||||
action = ACTION_START_SESSION
|
||||
putExtra(EXTRA_SESSION_ID, sessionId)
|
||||
}
|
||||
}
|
||||
|
||||
fun createStopIntent(context: Context, sessionId: String): Intent {
|
||||
return Intent(context, SessionTrackingService::class.java).apply {
|
||||
action = ACTION_STOP_SESSION
|
||||
putExtra(EXTRA_SESSION_ID, sessionId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
val database = OpenClimbDatabase.getDatabase(this)
|
||||
repository = ClimbRepository(database, this)
|
||||
notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
createNotificationChannel()
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
when (intent?.action) {
|
||||
ACTION_START_SESSION -> {
|
||||
val sessionId = intent.getStringExtra(EXTRA_SESSION_ID)
|
||||
if (sessionId != null) {
|
||||
startSessionTracking(sessionId)
|
||||
}
|
||||
}
|
||||
ACTION_STOP_SESSION -> {
|
||||
val sessionId = intent.getStringExtra(EXTRA_SESSION_ID)
|
||||
serviceScope.launch {
|
||||
try {
|
||||
val targetSession = when {
|
||||
sessionId != null -> repository.getSessionById(sessionId)
|
||||
else -> repository.getActiveSession()
|
||||
}
|
||||
if (targetSession != null && targetSession.status == com.atridad.openclimb.data.model.SessionStatus.ACTIVE) {
|
||||
val completed = with(com.atridad.openclimb.data.model.ClimbSession) { targetSession.complete() }
|
||||
repository.updateSession(completed)
|
||||
}
|
||||
} finally {
|
||||
stopSessionTracking()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return START_REDELIVER_INTENT
|
||||
}
|
||||
|
||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||
super.onTaskRemoved(rootIntent)
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? = null
|
||||
|
||||
private fun startSessionTracking(sessionId: String) {
|
||||
notificationJob?.cancel()
|
||||
monitoringJob?.cancel()
|
||||
|
||||
try {
|
||||
createAndShowNotification(sessionId)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
notificationJob = serviceScope.launch {
|
||||
try {
|
||||
if (!isNotificationActive()) {
|
||||
delay(1000L)
|
||||
createAndShowNotification(sessionId)
|
||||
}
|
||||
|
||||
while (isActive) {
|
||||
delay(5000L)
|
||||
updateNotification(sessionId)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
monitoringJob = serviceScope.launch {
|
||||
try {
|
||||
while (isActive) {
|
||||
delay(10000L)
|
||||
|
||||
if (!isNotificationActive()) {
|
||||
updateNotification(sessionId)
|
||||
}
|
||||
|
||||
val session = repository.getSessionById(sessionId)
|
||||
if (session == null || session.status != com.atridad.openclimb.data.model.SessionStatus.ACTIVE) {
|
||||
stopSessionTracking()
|
||||
break
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopSessionTracking() {
|
||||
notificationJob?.cancel()
|
||||
monitoringJob?.cancel()
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
private fun isNotificationActive(): Boolean {
|
||||
return try {
|
||||
val activeNotifications = notificationManager.activeNotifications
|
||||
activeNotifications.any { it.id == NOTIFICATION_ID }
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateNotification(sessionId: String) {
|
||||
try {
|
||||
createAndShowNotification(sessionId)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
|
||||
try {
|
||||
delay(10000L)
|
||||
createAndShowNotification(sessionId)
|
||||
} catch (retryException: Exception) {
|
||||
retryException.printStackTrace()
|
||||
stopSessionTracking()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun createAndShowNotification(sessionId: String) {
|
||||
try {
|
||||
val session = runBlocking {
|
||||
repository.getSessionById(sessionId)
|
||||
}
|
||||
if (session == null || session.status != com.atridad.openclimb.data.model.SessionStatus.ACTIVE) {
|
||||
stopSessionTracking()
|
||||
return
|
||||
}
|
||||
|
||||
val gym = runBlocking {
|
||||
repository.getGymById(session.gymId)
|
||||
}
|
||||
|
||||
val attempts = runBlocking {
|
||||
repository.getAttemptsBySession(sessionId).firstOrNull() ?: emptyList()
|
||||
}
|
||||
|
||||
val duration = session.startTime?.let { startTime ->
|
||||
try {
|
||||
val start = LocalDateTime.parse(startTime)
|
||||
val now = LocalDateTime.now()
|
||||
val totalSeconds = ChronoUnit.SECONDS.between(start, now)
|
||||
val hours = totalSeconds / 3600
|
||||
val minutes = (totalSeconds % 3600) / 60
|
||||
val seconds = totalSeconds % 60
|
||||
|
||||
when {
|
||||
hours > 0 -> "${hours}h ${minutes}m ${seconds}s"
|
||||
minutes > 0 -> "${minutes}m ${seconds}s"
|
||||
else -> "${totalSeconds}s"
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
"Active"
|
||||
}
|
||||
} ?: "Active"
|
||||
|
||||
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setContentTitle("Climbing Session Active")
|
||||
.setContentText("${gym?.name ?: "Gym"} • $duration • ${attempts.size} attempts")
|
||||
.setSmallIcon(R.drawable.ic_mountains)
|
||||
.setOngoing(true)
|
||||
.setAutoCancel(false)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setCategory(NotificationCompat.CATEGORY_SERVICE)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.setContentIntent(createOpenAppIntent())
|
||||
.addAction(
|
||||
R.drawable.ic_mountains,
|
||||
"Open Session",
|
||||
createOpenAppIntent()
|
||||
)
|
||||
.addAction(
|
||||
android.R.drawable.ic_menu_close_clear_cancel,
|
||||
"End Session",
|
||||
createStopPendingIntent(sessionId)
|
||||
)
|
||||
.build()
|
||||
|
||||
startForeground(NOTIFICATION_ID, notification)
|
||||
|
||||
notificationManager.notify(NOTIFICATION_ID, notification)
|
||||
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
private fun createOpenAppIntent(): PendingIntent {
|
||||
val intent = Intent(this, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
action = "OPEN_SESSION"
|
||||
}
|
||||
return PendingIntent.getActivity(
|
||||
this,
|
||||
0,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
}
|
||||
|
||||
private fun createStopPendingIntent(sessionId: String): PendingIntent {
|
||||
val intent = createStopIntent(this, sessionId)
|
||||
return PendingIntent.getService(
|
||||
this,
|
||||
1,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
}
|
||||
|
||||
private fun createNotificationChannel() {
|
||||
val channel = NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"Session Tracking",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
).apply {
|
||||
description = "Shows active climbing session information"
|
||||
setShowBadge(false)
|
||||
lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
|
||||
enableLights(false)
|
||||
enableVibration(false)
|
||||
setSound(null, null)
|
||||
}
|
||||
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
notificationJob?.cancel()
|
||||
monitoringJob?.cancel()
|
||||
serviceScope.cancel()
|
||||
}
|
||||
}
|
||||
@@ -1,403 +0,0 @@
|
||||
package com.atridad.openclimb.ui
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.PlayArrow
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.toRoute
|
||||
import com.atridad.openclimb.data.database.OpenClimbDatabase
|
||||
import com.atridad.openclimb.data.repository.ClimbRepository
|
||||
import com.atridad.openclimb.data.sync.SyncService
|
||||
import com.atridad.openclimb.navigation.Screen
|
||||
import com.atridad.openclimb.navigation.bottomNavigationItems
|
||||
import com.atridad.openclimb.ui.components.NotificationPermissionDialog
|
||||
import com.atridad.openclimb.ui.screens.*
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModelFactory
|
||||
import com.atridad.openclimb.utils.AppShortcutManager
|
||||
import com.atridad.openclimb.utils.NotificationPermissionUtils
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun OpenClimbApp(
|
||||
shortcutAction: String? = null,
|
||||
lastUsedGymId: String? = null,
|
||||
onShortcutActionProcessed: () -> Unit = {}
|
||||
) {
|
||||
val navController = rememberNavController()
|
||||
val context = LocalContext.current
|
||||
|
||||
var lastUsedGym by remember { mutableStateOf<com.atridad.openclimb.data.model.Gym?>(null) }
|
||||
|
||||
val database = remember { OpenClimbDatabase.getDatabase(context) }
|
||||
val repository = remember { ClimbRepository(database, context) }
|
||||
val syncService = remember { SyncService(context, repository) }
|
||||
val viewModel: ClimbViewModel =
|
||||
viewModel(factory = ClimbViewModelFactory(repository, syncService, context))
|
||||
|
||||
var showNotificationPermissionDialog by remember { mutableStateOf(false) }
|
||||
var hasCheckedNotificationPermission by remember { mutableStateOf(false) }
|
||||
|
||||
val permissionLauncher =
|
||||
rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.RequestPermission()
|
||||
) { isGranted: Boolean ->
|
||||
if (!isGranted) {
|
||||
showNotificationPermissionDialog = false
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
if (!hasCheckedNotificationPermission) {
|
||||
hasCheckedNotificationPermission = true
|
||||
|
||||
if (NotificationPermissionUtils.shouldRequestNotificationPermission() &&
|
||||
!NotificationPermissionUtils.isNotificationPermissionGranted(context)
|
||||
) {
|
||||
showNotificationPermissionDialog = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) { viewModel.ensureSessionTrackingServiceRunning(context) }
|
||||
|
||||
LaunchedEffect(Unit) { syncService.triggerAutoSync() }
|
||||
|
||||
val activeSession by viewModel.activeSession.collectAsState()
|
||||
val gyms by viewModel.gyms.collectAsState()
|
||||
|
||||
LaunchedEffect(gyms) {
|
||||
if (gyms.isNotEmpty() && lastUsedGym == null) {
|
||||
lastUsedGym = viewModel.getLastUsedGym()
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(activeSession, gyms, lastUsedGym) {
|
||||
AppShortcutManager.updateShortcuts(
|
||||
context = context,
|
||||
hasActiveSession = activeSession != null,
|
||||
hasGyms = gyms.isNotEmpty(),
|
||||
lastUsedGym = if (activeSession == null && gyms.size > 1) lastUsedGym else null
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(shortcutAction) {
|
||||
when (shortcutAction) {
|
||||
AppShortcutManager.ACTION_START_SESSION -> {
|
||||
navController.navigate(Screen.Sessions) {
|
||||
popUpTo(0) { inclusive = true }
|
||||
launchSingleTop = true
|
||||
}
|
||||
}
|
||||
AppShortcutManager.ACTION_END_SESSION -> {
|
||||
navController.navigate(Screen.Sessions) {
|
||||
popUpTo(0) { inclusive = true }
|
||||
launchSingleTop = true
|
||||
}
|
||||
|
||||
activeSession?.let { session -> viewModel.endSession(context, session.id) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(shortcutAction, activeSession, gyms, lastUsedGym) {
|
||||
if (shortcutAction == AppShortcutManager.ACTION_START_SESSION && gyms.isNotEmpty()) {
|
||||
android.util.Log.d(
|
||||
"OpenClimbApp",
|
||||
"Processing shortcut action: activeSession=$activeSession, gyms.size=${gyms.size}, lastUsedGymId=$lastUsedGymId, lastUsedGym=${lastUsedGym?.name}"
|
||||
)
|
||||
|
||||
if (activeSession == null) {
|
||||
if (NotificationPermissionUtils.shouldRequestNotificationPermission() &&
|
||||
!NotificationPermissionUtils.isNotificationPermissionGranted(
|
||||
context
|
||||
)
|
||||
) {
|
||||
android.util.Log.d("OpenClimbApp", "Showing notification permission dialog")
|
||||
showNotificationPermissionDialog = true
|
||||
} else {
|
||||
if (gyms.size == 1) {
|
||||
android.util.Log.d(
|
||||
"OpenClimbApp",
|
||||
"Starting session with single gym: ${gyms.first().name}"
|
||||
)
|
||||
viewModel.startSession(context, gyms.first().id)
|
||||
} else {
|
||||
val targetGym =
|
||||
lastUsedGymId?.let { gymId -> gyms.find { it.id == gymId } }
|
||||
?: lastUsedGym
|
||||
|
||||
if (targetGym != null) {
|
||||
android.util.Log.d(
|
||||
"OpenClimbApp",
|
||||
"Starting session with target gym: ${targetGym.name}"
|
||||
)
|
||||
viewModel.startSession(context, targetGym.id)
|
||||
} else {
|
||||
android.util.Log.d(
|
||||
"OpenClimbApp",
|
||||
"No target gym found, navigating to selection"
|
||||
)
|
||||
navController.navigate(Screen.AddEditSession())
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
android.util.Log.d(
|
||||
"OpenClimbApp",
|
||||
"Active session already exists: ${activeSession?.id}"
|
||||
)
|
||||
}
|
||||
|
||||
onShortcutActionProcessed()
|
||||
}
|
||||
}
|
||||
|
||||
var fabConfig by remember { mutableStateOf<FabConfig?>(null) }
|
||||
|
||||
Scaffold(
|
||||
bottomBar = { OpenClimbBottomNavigation(navController = navController) },
|
||||
floatingActionButton = {
|
||||
fabConfig?.let { config ->
|
||||
FloatingActionButton(
|
||||
onClick = config.onClick,
|
||||
containerColor = MaterialTheme.colorScheme.primary
|
||||
) {
|
||||
Icon(
|
||||
imageVector = config.icon,
|
||||
contentDescription = config.contentDescription
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
) { innerPadding ->
|
||||
NavHost(
|
||||
navController = navController,
|
||||
startDestination = Screen.Sessions,
|
||||
modifier = Modifier.padding(innerPadding)
|
||||
) {
|
||||
composable<Screen.Sessions> {
|
||||
LaunchedEffect(gyms, activeSession) {
|
||||
fabConfig =
|
||||
if (gyms.isNotEmpty() && activeSession == null) {
|
||||
FabConfig(
|
||||
icon = Icons.Default.PlayArrow,
|
||||
contentDescription = "Start Session",
|
||||
onClick = {
|
||||
if (NotificationPermissionUtils
|
||||
.shouldRequestNotificationPermission() &&
|
||||
!NotificationPermissionUtils
|
||||
.isNotificationPermissionGranted(
|
||||
context
|
||||
)
|
||||
) {
|
||||
showNotificationPermissionDialog = true
|
||||
} else {
|
||||
navController.navigate(Screen.AddEditSession())
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
SessionsScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateToSessionDetail = { sessionId ->
|
||||
navController.navigate(Screen.SessionDetail(sessionId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.Problems> {
|
||||
LaunchedEffect(gyms) {
|
||||
fabConfig =
|
||||
if (gyms.isNotEmpty()) {
|
||||
FabConfig(
|
||||
icon = Icons.Default.Add,
|
||||
contentDescription = "Add Problem",
|
||||
onClick = {
|
||||
navController.navigate(Screen.AddEditProblem())
|
||||
}
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
ProblemsScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateToProblemDetail = { problemId ->
|
||||
navController.navigate(Screen.ProblemDetail(problemId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.Analytics> {
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
AnalyticsScreen(viewModel = viewModel)
|
||||
}
|
||||
|
||||
composable<Screen.Gyms> {
|
||||
LaunchedEffect(Unit) {
|
||||
fabConfig =
|
||||
FabConfig(
|
||||
icon = Icons.Default.Add,
|
||||
contentDescription = "Add Gym",
|
||||
onClick = { navController.navigate(Screen.AddEditGym()) }
|
||||
)
|
||||
}
|
||||
GymsScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateToGymDetail = { gymId ->
|
||||
navController.navigate(Screen.GymDetail(gymId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.Settings> {
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
SettingsScreen(viewModel = viewModel)
|
||||
}
|
||||
|
||||
composable<Screen.SessionDetail> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.SessionDetail>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
SessionDetailScreen(
|
||||
sessionId = args.sessionId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() },
|
||||
onNavigateToProblemDetail = { problemId ->
|
||||
navController.navigate(Screen.ProblemDetail(problemId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.ProblemDetail> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.ProblemDetail>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
ProblemDetailScreen(
|
||||
problemId = args.problemId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() },
|
||||
onNavigateToEdit = { problemId ->
|
||||
navController.navigate(Screen.AddEditProblem(problemId = problemId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.GymDetail> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.GymDetail>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
GymDetailScreen(
|
||||
gymId = args.gymId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() },
|
||||
onNavigateToEdit = { gymId ->
|
||||
navController.navigate(Screen.AddEditGym(gymId = gymId))
|
||||
},
|
||||
onNavigateToSessionDetail = { sessionId ->
|
||||
navController.navigate(Screen.SessionDetail(sessionId))
|
||||
},
|
||||
onNavigateToProblemDetail = { problemId ->
|
||||
navController.navigate(Screen.ProblemDetail(problemId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.AddEditGym> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.AddEditGym>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
AddEditGymScreen(
|
||||
gymId = args.gymId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() }
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.AddEditProblem> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.AddEditProblem>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
AddEditProblemScreen(
|
||||
problemId = args.problemId,
|
||||
gymId = args.gymId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() }
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.AddEditSession> { backStackEntry ->
|
||||
val args = backStackEntry.toRoute<Screen.AddEditSession>()
|
||||
LaunchedEffect(Unit) { fabConfig = null }
|
||||
AddEditSessionScreen(
|
||||
sessionId = args.sessionId,
|
||||
gymId = args.gymId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navController.popBackStack() }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (showNotificationPermissionDialog) {
|
||||
NotificationPermissionDialog(
|
||||
onDismiss = { showNotificationPermissionDialog = false },
|
||||
onRequestPermission = {
|
||||
permissionLauncher.launch(
|
||||
NotificationPermissionUtils.getNotificationPermissionString()
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun OpenClimbBottomNavigation(navController: NavHostController) {
|
||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||
val currentRoute = navBackStackEntry?.destination?.route
|
||||
|
||||
NavigationBar {
|
||||
bottomNavigationItems.forEach { item ->
|
||||
val isSelected =
|
||||
when (item.screen) {
|
||||
is Screen.Sessions -> currentRoute?.contains("Session") == true
|
||||
is Screen.Problems -> currentRoute?.contains("Problem") == true
|
||||
is Screen.Gyms -> currentRoute?.contains("Gym") == true
|
||||
is Screen.Analytics -> currentRoute?.contains("Analytics") == true
|
||||
is Screen.Settings -> currentRoute?.contains("Settings") == true
|
||||
else -> currentRoute?.contains(item.screen::class.simpleName ?: "") == true
|
||||
}
|
||||
|
||||
NavigationBarItem(
|
||||
icon = { Icon(item.icon, contentDescription = item.label) },
|
||||
label = { Text(item.label) },
|
||||
selected = isSelected,
|
||||
onClick = {
|
||||
navController.navigate(item.screen) {
|
||||
popUpTo(0) { inclusive = true }
|
||||
launchSingleTop = true
|
||||
// Don't restore state - always start fresh when switching tabs
|
||||
restoreState = false
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class FabConfig(
|
||||
val icon: androidx.compose.ui.graphics.vector.ImageVector,
|
||||
val contentDescription: String,
|
||||
val onClick: () -> Unit
|
||||
)
|
||||
@@ -1,209 +0,0 @@
|
||||
package com.atridad.openclimb.ui.components
|
||||
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.gestures.detectTransformGestures
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.pager.HorizontalPager
|
||||
import androidx.compose.foundation.pager.rememberPagerState
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
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.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import coil.compose.AsyncImage
|
||||
import com.atridad.openclimb.utils.ImageUtils
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
fun FullscreenImageViewer(
|
||||
imagePaths: List<String>,
|
||||
initialIndex: Int = 0,
|
||||
onDismiss: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val pagerState = rememberPagerState(
|
||||
initialPage = initialIndex,
|
||||
pageCount = { imagePaths.size }
|
||||
)
|
||||
val thumbnailListState = rememberLazyListState()
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
// Auto-scroll thumbnail list to center current image
|
||||
LaunchedEffect(pagerState.currentPage) {
|
||||
thumbnailListState.animateScrollToItem(
|
||||
index = pagerState.currentPage,
|
||||
scrollOffset = -200
|
||||
)
|
||||
}
|
||||
|
||||
Dialog(
|
||||
onDismissRequest = onDismiss,
|
||||
properties = DialogProperties(
|
||||
usePlatformDefaultWidth = false,
|
||||
decorFitsSystemWindows = false
|
||||
)
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.Black)
|
||||
) {
|
||||
// Main image pager
|
||||
HorizontalPager(
|
||||
state = pagerState,
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) { page ->
|
||||
ZoomableImage(
|
||||
imagePath = imagePaths[page],
|
||||
modifier = Modifier.fillMaxSize()
|
||||
)
|
||||
}
|
||||
|
||||
// Close button
|
||||
IconButton(
|
||||
onClick = onDismiss,
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopEnd)
|
||||
.padding(16.dp)
|
||||
.background(
|
||||
Color.Black.copy(alpha = 0.5f),
|
||||
CircleShape
|
||||
)
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.Close,
|
||||
contentDescription = "Close",
|
||||
tint = Color.White
|
||||
)
|
||||
}
|
||||
|
||||
// Image counter
|
||||
if (imagePaths.size > 1) {
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopCenter)
|
||||
.padding(16.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = Color.Black.copy(alpha = 0.7f)
|
||||
)
|
||||
) {
|
||||
Text(
|
||||
text = "${pagerState.currentPage + 1} / ${imagePaths.size}",
|
||||
color = Color.White,
|
||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Thumbnail strip (if multiple images)
|
||||
if (imagePaths.size > 1) {
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomCenter)
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = Color.Black.copy(alpha = 0.7f)
|
||||
)
|
||||
) {
|
||||
LazyRow(
|
||||
state = thumbnailListState,
|
||||
modifier = Modifier.padding(8.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
contentPadding = PaddingValues(horizontal = 8.dp)
|
||||
) {
|
||||
itemsIndexed(imagePaths) { index, imagePath ->
|
||||
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
||||
val isSelected = index == pagerState.currentPage
|
||||
|
||||
AsyncImage(
|
||||
model = imageFile,
|
||||
contentDescription = "Thumbnail ${index + 1}",
|
||||
modifier = Modifier
|
||||
.size(60.dp)
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.clickable {
|
||||
coroutineScope.launch {
|
||||
pagerState.animateScrollToPage(index)
|
||||
}
|
||||
}
|
||||
.then(
|
||||
if (isSelected) {
|
||||
Modifier.background(
|
||||
Color.White.copy(alpha = 0.3f),
|
||||
RoundedCornerShape(8.dp)
|
||||
)
|
||||
} else Modifier
|
||||
),
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package com.atridad.openclimb.ui.components
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import coil.compose.AsyncImage
|
||||
import com.atridad.openclimb.utils.ImageUtils
|
||||
|
||||
@Composable
|
||||
fun ImageDisplay(
|
||||
imagePaths: List<String>,
|
||||
modifier: Modifier = Modifier,
|
||||
imageSize: Int = 120,
|
||||
onImageClick: ((Int) -> Unit)? = null
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
|
||||
if (imagePaths.isNotEmpty()) {
|
||||
LazyRow(
|
||||
modifier = modifier,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
itemsIndexed(imagePaths) { index, imagePath ->
|
||||
val imageFile = ImageUtils.getImageFile(context, imagePath)
|
||||
|
||||
AsyncImage(
|
||||
model = imageFile,
|
||||
contentDescription = "Problem photo",
|
||||
modifier = Modifier
|
||||
.size(imageSize.dp)
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.clickable(enabled = onImageClick != null) {
|
||||
onImageClick?.invoke(index)
|
||||
},
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ImageDisplaySection(
|
||||
imagePaths: List<String>,
|
||||
modifier: Modifier = Modifier,
|
||||
title: String = "Photos",
|
||||
onImageClick: ((Int) -> Unit)? = null
|
||||
) {
|
||||
if (imagePaths.isNotEmpty()) {
|
||||
Column(modifier = modifier) {
|
||||
Text(
|
||||
text = title,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
ImageDisplay(
|
||||
imagePaths = imagePaths,
|
||||
imageSize = 120,
|
||||
onImageClick = onImageClick
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
package com.atridad.openclimb.ui.components
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Notifications
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
|
||||
@Composable
|
||||
fun NotificationPermissionDialog(
|
||||
onDismiss: () -> Unit,
|
||||
onRequestPermission: () -> Unit
|
||||
) {
|
||||
Dialog(
|
||||
onDismissRequest = onDismiss,
|
||||
properties = DialogProperties(
|
||||
dismissOnBackPress = false,
|
||||
dismissOnClickOutside = false
|
||||
)
|
||||
) {
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
shape = MaterialTheme.shapes.medium
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.padding(24.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Notifications,
|
||||
contentDescription = "Notifications",
|
||||
modifier = Modifier.size(48.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Text(
|
||||
text = "Enable Notifications",
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
fontWeight = MaterialTheme.typography.headlineSmall.fontWeight,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
Text(
|
||||
text = "OpenClimb needs notification permission to show your active climbing session. This helps you track your progress and ensures the session doesn't get interrupted.",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = TextAlign.Center,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
TextButton(
|
||||
onClick = onDismiss,
|
||||
modifier = Modifier.weight(1f)
|
||||
) {
|
||||
Text("Not Now")
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
onRequestPermission()
|
||||
onDismiss()
|
||||
},
|
||||
modifier = Modifier.weight(1f)
|
||||
) {
|
||||
Text("Enable")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,440 +0,0 @@
|
||||
package com.atridad.openclimb.ui.health
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.*
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.openclimb.data.health.HealthConnectManager
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun HealthConnectCard(modifier: Modifier = Modifier) {
|
||||
val context = LocalContext.current
|
||||
val healthConnectManager = remember { HealthConnectManager(context) }
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
// State tracking
|
||||
var isHealthConnectAvailable by remember { mutableStateOf(false) }
|
||||
var isLoading by remember { mutableStateOf(true) }
|
||||
var errorMessage by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
// Collect flows
|
||||
val isEnabled by healthConnectManager.isEnabled.collectAsState(initial = false)
|
||||
val hasPermissions by healthConnectManager.hasPermissions.collectAsState(initial = false)
|
||||
val autoSyncEnabled by healthConnectManager.autoSyncEnabled.collectAsState(initial = true)
|
||||
val isCompatible by healthConnectManager.isCompatible.collectAsState(initial = true)
|
||||
|
||||
// Permission launcher
|
||||
val permissionLauncher =
|
||||
rememberLauncherForActivityResult(
|
||||
contract = healthConnectManager.getPermissionRequestContract()
|
||||
) { grantedPermissions ->
|
||||
coroutineScope.launch {
|
||||
val allGranted = healthConnectManager.hasAllPermissions()
|
||||
if (!allGranted) {
|
||||
errorMessage =
|
||||
"Some Health Connect permissions were not granted. Please grant all permissions to enable syncing."
|
||||
} else {
|
||||
errorMessage = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check Health Connect availability on first load
|
||||
LaunchedEffect(Unit) {
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
healthConnectManager.isHealthConnectAvailable().collect { available ->
|
||||
isHealthConnectAvailable = available
|
||||
isLoading = false
|
||||
|
||||
if (!available && isCompatible) {
|
||||
errorMessage = "Health Connect is not available on this device"
|
||||
} else if (!isCompatible) {
|
||||
errorMessage =
|
||||
"Health Connect API compatibility issue. Please update your device or the app."
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
isLoading = false
|
||||
errorMessage = "Error checking Health Connect availability: ${e.message}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(16.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceVariant
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth().padding(20.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
// Header with icon and title
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.HealthAndSafety,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(32.dp),
|
||||
tint =
|
||||
if (isHealthConnectAvailable && isEnabled && hasPermissions) {
|
||||
MaterialTheme.colorScheme.primary
|
||||
} else {
|
||||
MaterialTheme.colorScheme.onSurfaceVariant
|
||||
}
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = "Health Connect",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
|
||||
Text(
|
||||
text =
|
||||
when {
|
||||
isLoading -> "Checking availability..."
|
||||
!isCompatible -> "API Issue"
|
||||
!isHealthConnectAvailable -> "Not available"
|
||||
isEnabled && hasPermissions -> "Connected"
|
||||
isEnabled && !hasPermissions -> "Needs permissions"
|
||||
else -> "Disabled"
|
||||
},
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color =
|
||||
when {
|
||||
isLoading ->
|
||||
MaterialTheme.colorScheme.onSurfaceVariant.copy(
|
||||
alpha = 0.7f
|
||||
)
|
||||
!isCompatible -> MaterialTheme.colorScheme.error
|
||||
!isHealthConnectAvailable -> MaterialTheme.colorScheme.error
|
||||
isEnabled && hasPermissions ->
|
||||
MaterialTheme.colorScheme.primary
|
||||
isEnabled && !hasPermissions ->
|
||||
MaterialTheme.colorScheme.tertiary
|
||||
else ->
|
||||
MaterialTheme.colorScheme.onSurfaceVariant.copy(
|
||||
alpha = 0.7f
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Main toggle switch
|
||||
Switch(
|
||||
checked = isEnabled,
|
||||
onCheckedChange = { enabled ->
|
||||
if (enabled && isHealthConnectAvailable) {
|
||||
healthConnectManager.setEnabled(true)
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
val permissionSet =
|
||||
healthConnectManager.getRequiredPermissions()
|
||||
if (permissionSet.isNotEmpty()) {
|
||||
permissionLauncher.launch(permissionSet)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
errorMessage = "Error requesting permissions: ${e.message}"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
healthConnectManager.setEnabled(false)
|
||||
errorMessage = null
|
||||
}
|
||||
},
|
||||
enabled = isHealthConnectAvailable && !isLoading && isCompatible
|
||||
)
|
||||
}
|
||||
|
||||
if (isEnabled) {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
Card(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor =
|
||||
if (hasPermissions) {
|
||||
MaterialTheme.colorScheme.primaryContainer.copy(
|
||||
alpha = 0.3f
|
||||
)
|
||||
} else {
|
||||
MaterialTheme.colorScheme.errorContainer.copy(
|
||||
alpha = 0.3f
|
||||
)
|
||||
}
|
||||
)
|
||||
) {
|
||||
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
imageVector =
|
||||
if (hasPermissions) Icons.Default.CheckCircle
|
||||
else Icons.Default.Warning,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(20.dp),
|
||||
tint =
|
||||
if (hasPermissions) {
|
||||
MaterialTheme.colorScheme.primary
|
||||
} else {
|
||||
MaterialTheme.colorScheme.error
|
||||
}
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
|
||||
Text(
|
||||
text =
|
||||
if (hasPermissions) "Ready to sync"
|
||||
else "Permissions needed",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
fontWeight = FontWeight.Medium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
if (!hasPermissions) {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Text(
|
||||
text =
|
||||
"Grant Health Connect permissions to sync your climbing sessions",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color =
|
||||
MaterialTheme.colorScheme.onSurfaceVariant.copy(
|
||||
alpha = 0.8f
|
||||
)
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
OutlinedButton(
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
val permissionSet =
|
||||
healthConnectManager
|
||||
.getRequiredPermissions()
|
||||
if (permissionSet.isNotEmpty()) {
|
||||
permissionLauncher.launch(permissionSet)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
errorMessage =
|
||||
"Error requesting permissions: ${e.message}"
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) { Text("Grant Permissions") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hasPermissions) {
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
Card(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.surfaceVariant.copy(
|
||||
alpha = 0.3f
|
||||
)
|
||||
)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = "Auto-sync sessions",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
fontWeight = FontWeight.Medium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
Text(
|
||||
text = "Automatically sync completed climbing sessions",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color =
|
||||
MaterialTheme.colorScheme.onSurfaceVariant.copy(
|
||||
alpha = 0.7f
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Switch(
|
||||
checked = autoSyncEnabled,
|
||||
onCheckedChange = { enabled ->
|
||||
healthConnectManager.setAutoSyncEnabled(enabled)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
Text(
|
||||
text =
|
||||
"Sync your climbing sessions to Samsung Health, Google Fit, and other fitness apps through Health Connect.",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = TextAlign.Center,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.8f)
|
||||
)
|
||||
}
|
||||
errorMessage?.let { error ->
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
Card(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.errorContainer.copy(
|
||||
alpha = 0.5f
|
||||
)
|
||||
)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Warning,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.error
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
|
||||
Text(
|
||||
text = error,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onErrorContainer
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isEnabled) {
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
var testResult by remember { mutableStateOf<String?>(null) }
|
||||
var isTestRunning by remember { mutableStateOf(false) }
|
||||
|
||||
OutlinedButton(
|
||||
onClick = {
|
||||
isTestRunning = true
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
testResult = healthConnectManager.testHealthConnectSync()
|
||||
} catch (e: Exception) {
|
||||
testResult = "Test failed: ${e.message}"
|
||||
} finally {
|
||||
isTestRunning = false
|
||||
}
|
||||
}
|
||||
},
|
||||
enabled = !isTestRunning,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
if (isTestRunning) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(16.dp),
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
}
|
||||
Text(if (isTestRunning) "Testing..." else "Test Connection")
|
||||
}
|
||||
testResult?.let { result ->
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Card(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.surfaceVariant.copy(
|
||||
alpha = 0.5f
|
||||
)
|
||||
)
|
||||
) {
|
||||
Column(modifier = Modifier.fillMaxWidth().padding(12.dp)) {
|
||||
Text(
|
||||
text = "Debug Results:",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = result,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
fontFamily = androidx.compose.ui.text.font.FontFamily.Monospace
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun HealthConnectStatusBanner(isConnected: Boolean, modifier: Modifier = Modifier) {
|
||||
if (isConnected) {
|
||||
Card(
|
||||
modifier = modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.primaryContainer.copy(
|
||||
alpha = 0.5f
|
||||
)
|
||||
)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CloudDone,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
|
||||
Text(
|
||||
text = "Health Connect active - sessions will sync automatically",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onPrimaryContainer
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,256 +0,0 @@
|
||||
package com.atridad.openclimb.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.CheckCircle
|
||||
import androidx.compose.material.icons.filled.Warning
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.atridad.openclimb.R
|
||||
import com.atridad.openclimb.data.model.ClimbSession
|
||||
import com.atridad.openclimb.data.model.SessionStatus
|
||||
import com.atridad.openclimb.ui.components.ActiveSessionBanner
|
||||
import com.atridad.openclimb.ui.components.SyncIndicator
|
||||
import com.atridad.openclimb.ui.viewmodel.ClimbViewModel
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SessionsScreen(viewModel: ClimbViewModel, onNavigateToSessionDetail: (String) -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val sessions by viewModel.sessions.collectAsState()
|
||||
val gyms by viewModel.gyms.collectAsState()
|
||||
val activeSession by viewModel.activeSession.collectAsState()
|
||||
val uiState by viewModel.uiState.collectAsState()
|
||||
|
||||
// Filter out active sessions from regular session list
|
||||
val completedSessions = sessions.filter { it.status == SessionStatus.COMPLETED }
|
||||
val activeSessionGym = activeSession?.let { session -> gyms.find { it.id == session.gymId } }
|
||||
|
||||
Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(id = R.drawable.ic_mountains),
|
||||
contentDescription = "OpenClimb Logo",
|
||||
modifier = Modifier.size(32.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Text(
|
||||
text = "Climbing Sessions",
|
||||
style = MaterialTheme.typography.headlineMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
SyncIndicator(isSyncing = viewModel.syncService.isSyncing)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
// Active session banner
|
||||
ActiveSessionBanner(
|
||||
activeSession = activeSession,
|
||||
gym = activeSessionGym,
|
||||
onSessionClick = { activeSession?.let { onNavigateToSessionDetail(it.id) } },
|
||||
onEndSession = { activeSession?.let { viewModel.endSession(context, it.id) } }
|
||||
)
|
||||
|
||||
if (activeSession != null) {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
}
|
||||
|
||||
if (completedSessions.isEmpty() && activeSession == null) {
|
||||
EmptyStateMessage(
|
||||
title = if (gyms.isEmpty()) "No Gyms Available" else "No Sessions Yet",
|
||||
message =
|
||||
if (gyms.isEmpty())
|
||||
"Add a gym first to start tracking your climbing sessions!"
|
||||
else "Start your first climbing session!",
|
||||
onActionClick = {},
|
||||
actionText = ""
|
||||
)
|
||||
} else {
|
||||
LazyColumn {
|
||||
items(completedSessions) { session ->
|
||||
SessionCard(
|
||||
session = session,
|
||||
gymName = gyms.find { it.id == session.gymId }?.name ?: "Unknown Gym",
|
||||
onClick = { onNavigateToSessionDetail(session.id) }
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Show UI state messages and errors
|
||||
uiState.message?.let { message ->
|
||||
LaunchedEffect(message) {
|
||||
kotlinx.coroutines.delay(5000)
|
||||
viewModel.clearMessage()
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.primaryContainer
|
||||
),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.CheckCircle,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = message,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onPrimaryContainer
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uiState.error?.let { error ->
|
||||
LaunchedEffect(error) {
|
||||
kotlinx.coroutines.delay(5000)
|
||||
viewModel.clearError()
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
colors =
|
||||
CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.errorContainer
|
||||
),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.Warning,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.error
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = error,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onErrorContainer
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SessionCard(session: ClimbSession, gymName: String, onClick: () -> Unit) {
|
||||
Card(onClick = onClick, modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.fillMaxWidth().padding(16.dp)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Text(
|
||||
text = gymName,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = formatDate(session.date),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
|
||||
session.duration?.let { duration ->
|
||||
Text(
|
||||
text = "Duration: $duration minutes",
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
}
|
||||
|
||||
session.notes?.let { notes ->
|
||||
if (notes.isNotBlank()) {
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = notes,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 2
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun EmptyStateMessage(
|
||||
title: String,
|
||||
message: String,
|
||||
onActionClick: () -> Unit,
|
||||
actionText: String
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Text(
|
||||
text = title,
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
fontWeight = FontWeight.Bold,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Text(
|
||||
text = message,
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
if (actionText.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Button(onClick = onActionClick) { Text(actionText) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatDate(dateString: String): String {
|
||||
return try {
|
||||
val date = LocalDateTime.parse(dateString.split("T")[0] + "T00:00:00")
|
||||
date.format(DateTimeFormatter.ofPattern("MMM dd, yyyy"))
|
||||
} catch (_: Exception) {
|
||||
dateString
|
||||
}
|
||||
}
|
||||
@@ -1,667 +0,0 @@
|
||||
package com.atridad.openclimb.ui.viewmodel
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.atridad.openclimb.data.health.HealthConnectManager
|
||||
import com.atridad.openclimb.data.model.*
|
||||
import com.atridad.openclimb.data.repository.ClimbRepository
|
||||
import com.atridad.openclimb.data.sync.SyncService
|
||||
import com.atridad.openclimb.service.SessionTrackingService
|
||||
import com.atridad.openclimb.utils.ImageNamingUtils
|
||||
import com.atridad.openclimb.utils.ImageUtils
|
||||
import com.atridad.openclimb.utils.SessionShareUtils
|
||||
import com.atridad.openclimb.widget.ClimbStatsWidgetProvider
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ClimbViewModel(
|
||||
private val repository: ClimbRepository,
|
||||
val syncService: SyncService,
|
||||
private val context: Context
|
||||
) : ViewModel() {
|
||||
|
||||
// Health Connect manager
|
||||
private val healthConnectManager = HealthConnectManager(context)
|
||||
|
||||
// UI State flows
|
||||
private val _uiState = MutableStateFlow(ClimbUiState())
|
||||
val uiState: StateFlow<ClimbUiState> = _uiState.asStateFlow()
|
||||
|
||||
// Data flows
|
||||
val gyms =
|
||||
repository
|
||||
.getAllGyms()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = emptyList()
|
||||
)
|
||||
|
||||
val problems =
|
||||
repository
|
||||
.getAllProblems()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = emptyList()
|
||||
)
|
||||
|
||||
val sessions =
|
||||
repository
|
||||
.getAllSessions()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = emptyList()
|
||||
)
|
||||
|
||||
val activeSession =
|
||||
repository
|
||||
.getActiveSessionFlow()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = null
|
||||
)
|
||||
|
||||
val attempts =
|
||||
repository
|
||||
.getAllAttempts()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(),
|
||||
initialValue = emptyList()
|
||||
)
|
||||
|
||||
// Gym operations
|
||||
fun addGym(gym: Gym) {
|
||||
viewModelScope.launch { repository.insertGym(gym) }
|
||||
}
|
||||
|
||||
fun addGym(gym: Gym, context: Context) {
|
||||
viewModelScope.launch {
|
||||
repository.insertGym(gym)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateGym(gym: Gym) {
|
||||
viewModelScope.launch { repository.updateGym(gym) }
|
||||
}
|
||||
|
||||
fun updateGym(gym: Gym, context: Context) {
|
||||
viewModelScope.launch {
|
||||
repository.updateGym(gym)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteGym(gym: Gym) {
|
||||
viewModelScope.launch { repository.deleteGym(gym) }
|
||||
}
|
||||
|
||||
fun deleteGym(gym: Gym, context: Context) {
|
||||
viewModelScope.launch {
|
||||
repository.deleteGym(gym)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun getGymById(id: String): Flow<Gym?> = flow { emit(repository.getGymById(id)) }
|
||||
|
||||
// Problem operations
|
||||
fun addProblem(problem: Problem, context: Context) {
|
||||
viewModelScope.launch {
|
||||
val finalProblem = renameTemporaryImages(problem, context)
|
||||
repository.insertProblem(finalProblem)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
// Auto-sync now happens automatically via repository callback
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun renameTemporaryImages(problem: Problem, context: Context? = null): Problem {
|
||||
if (problem.imagePaths.isEmpty()) {
|
||||
return problem
|
||||
}
|
||||
|
||||
val appContext = context ?: return problem
|
||||
val finalImagePaths = mutableListOf<String>()
|
||||
|
||||
problem.imagePaths.forEachIndexed { index, tempPath ->
|
||||
if (tempPath.startsWith("temp_")) {
|
||||
val deterministicName = ImageNamingUtils.generateImageFilename(problem.id, index)
|
||||
val finalPath =
|
||||
ImageUtils.renameTemporaryImage(appContext, tempPath, problem.id, index)
|
||||
finalImagePaths.add(finalPath ?: tempPath)
|
||||
} else {
|
||||
finalImagePaths.add(tempPath)
|
||||
}
|
||||
}
|
||||
|
||||
return problem.copy(imagePaths = finalImagePaths)
|
||||
}
|
||||
|
||||
fun updateProblem(problem: Problem, context: Context) {
|
||||
viewModelScope.launch {
|
||||
val finalProblem = renameTemporaryImages(problem, context)
|
||||
repository.updateProblem(finalProblem)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteProblem(problem: Problem, context: Context) {
|
||||
viewModelScope.launch {
|
||||
// Delete associated images
|
||||
problem.imagePaths.forEach { imagePath -> ImageUtils.deleteImage(context, imagePath) }
|
||||
|
||||
repository.deleteProblem(problem)
|
||||
|
||||
cleanupOrphanedImages(context)
|
||||
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun cleanupOrphanedImages(context: Context) {
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
val referencedImagePaths = allProblems.flatMap { it.imagePaths }.toSet()
|
||||
ImageUtils.cleanupOrphanedImages(context, referencedImagePaths)
|
||||
}
|
||||
fun migrateImageNamesToDeterministic(context: Context) {
|
||||
viewModelScope.launch {
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
var migrationCount = 0
|
||||
val updatedProblems = mutableListOf<Problem>()
|
||||
|
||||
for (problem in allProblems) {
|
||||
if (problem.imagePaths.isEmpty()) continue
|
||||
|
||||
var newImagePaths = mutableListOf<String>()
|
||||
var problemNeedsUpdate = false
|
||||
|
||||
for ((index, imagePath) in problem.imagePaths.withIndex()) {
|
||||
val currentFilename = File(imagePath).name
|
||||
|
||||
if (ImageNamingUtils.isValidImageFilename(currentFilename)) {
|
||||
newImagePaths.add(imagePath)
|
||||
continue
|
||||
}
|
||||
|
||||
val deterministicName =
|
||||
ImageNamingUtils.generateImageFilename(problem.id, index)
|
||||
|
||||
val imagesDir = ImageUtils.getImagesDirectory(context)
|
||||
val oldFile = File(imagesDir, currentFilename)
|
||||
val newFile = File(imagesDir, deterministicName)
|
||||
|
||||
if (oldFile.exists()) {
|
||||
if (oldFile.renameTo(newFile)) {
|
||||
newImagePaths.add(deterministicName)
|
||||
problemNeedsUpdate = true
|
||||
migrationCount++
|
||||
println("Migrated: $currentFilename → $deterministicName")
|
||||
} else {
|
||||
println("Failed to migrate $currentFilename")
|
||||
newImagePaths.add(imagePath)
|
||||
}
|
||||
} else {
|
||||
println("Warning: Image file not found: $currentFilename")
|
||||
newImagePaths.add(imagePath)
|
||||
}
|
||||
}
|
||||
|
||||
if (problemNeedsUpdate) {
|
||||
val updatedProblem = problem.copy(imagePaths = newImagePaths)
|
||||
updatedProblems.add(updatedProblem)
|
||||
}
|
||||
}
|
||||
|
||||
for (updatedProblem in updatedProblems) {
|
||||
repository.insertProblemWithoutSync(updatedProblem)
|
||||
}
|
||||
|
||||
println(
|
||||
"Migration completed: $migrationCount images renamed, ${updatedProblems.size} problems updated"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteAllImages(context: Context) {
|
||||
viewModelScope.launch {
|
||||
val imagesDir = ImageUtils.getImagesDirectory(context)
|
||||
var deletedCount = 0
|
||||
|
||||
imagesDir.listFiles()?.forEach { file ->
|
||||
if (file.isFile && file.extension.lowercase() == "jpg") {
|
||||
if (file.delete()) {
|
||||
deletedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val allProblems = repository.getAllProblems().first()
|
||||
val updatedProblems =
|
||||
allProblems.map { problem ->
|
||||
if (problem.imagePaths.isNotEmpty()) {
|
||||
problem.copy(imagePaths = emptyList())
|
||||
} else {
|
||||
problem
|
||||
}
|
||||
}
|
||||
|
||||
for (updatedProblem in updatedProblems) {
|
||||
if (updatedProblem.imagePaths !=
|
||||
allProblems.find { it.id == updatedProblem.id }?.imagePaths
|
||||
) {
|
||||
repository.insertProblemWithoutSync(updatedProblem)
|
||||
}
|
||||
}
|
||||
|
||||
println("Deleted $deletedCount image files and cleared image references")
|
||||
}
|
||||
}
|
||||
|
||||
fun getProblemById(id: String): Flow<Problem?> = flow { emit(repository.getProblemById(id)) }
|
||||
|
||||
fun getProblemsByGym(gymId: String): Flow<List<Problem>> = repository.getProblemsByGym(gymId)
|
||||
|
||||
// Session operations
|
||||
fun addSession(session: ClimbSession) {
|
||||
viewModelScope.launch { repository.insertSession(session) }
|
||||
}
|
||||
|
||||
fun addSession(session: ClimbSession, context: Context) {
|
||||
viewModelScope.launch {
|
||||
repository.insertSession(session)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateSession(session: ClimbSession) {
|
||||
viewModelScope.launch { repository.updateSession(session) }
|
||||
}
|
||||
|
||||
fun updateSession(session: ClimbSession, context: Context) {
|
||||
viewModelScope.launch {
|
||||
repository.updateSession(session)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteSession(session: ClimbSession) {
|
||||
viewModelScope.launch { repository.deleteSession(session) }
|
||||
}
|
||||
|
||||
fun deleteSession(session: ClimbSession, context: Context) {
|
||||
viewModelScope.launch {
|
||||
repository.deleteSession(session)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun getSessionById(id: String): Flow<ClimbSession?> = flow {
|
||||
emit(repository.getSessionById(id))
|
||||
}
|
||||
|
||||
fun getSessionsByGym(gymId: String): Flow<List<ClimbSession>> =
|
||||
repository.getSessionsByGym(gymId)
|
||||
|
||||
// Get last used gym for shortcut functionality
|
||||
suspend fun getLastUsedGym(): Gym? = repository.getLastUsedGym()
|
||||
|
||||
// Active session management
|
||||
fun startSession(context: Context, gymId: String, notes: String? = null) {
|
||||
viewModelScope.launch {
|
||||
android.util.Log.d("ClimbViewModel", "startSession called with gymId: $gymId")
|
||||
|
||||
if (!com.atridad.openclimb.utils.NotificationPermissionUtils
|
||||
.isNotificationPermissionGranted(context)
|
||||
) {
|
||||
android.util.Log.d("ClimbViewModel", "Notification permission not granted")
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
error =
|
||||
"Notification permission is required to track your climbing session. Please enable notifications in settings."
|
||||
)
|
||||
return@launch
|
||||
}
|
||||
|
||||
val existingActive = repository.getActiveSession()
|
||||
if (existingActive != null) {
|
||||
android.util.Log.d(
|
||||
"ClimbViewModel",
|
||||
"Active session already exists: ${existingActive.id}"
|
||||
)
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
error = "There's already an active session. Please end it first."
|
||||
)
|
||||
return@launch
|
||||
}
|
||||
|
||||
android.util.Log.d("ClimbViewModel", "Creating new session")
|
||||
val newSession = ClimbSession.create(gymId = gymId, notes = notes)
|
||||
repository.insertSession(newSession)
|
||||
|
||||
android.util.Log.d(
|
||||
"ClimbViewModel",
|
||||
"Starting tracking service for session: ${newSession.id}"
|
||||
)
|
||||
// Start the tracking service
|
||||
val serviceIntent = SessionTrackingService.createStartIntent(context, newSession.id)
|
||||
context.startForegroundService(serviceIntent)
|
||||
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
|
||||
_uiState.value = _uiState.value.copy(message = "Session started successfully!")
|
||||
}
|
||||
}
|
||||
|
||||
fun endSession(context: Context, sessionId: String) {
|
||||
viewModelScope.launch {
|
||||
if (!com.atridad.openclimb.utils.NotificationPermissionUtils
|
||||
.isNotificationPermissionGranted(context)
|
||||
) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
error =
|
||||
"Notification permission is required to manage your climbing session. Please enable notifications in settings."
|
||||
)
|
||||
return@launch
|
||||
}
|
||||
|
||||
val session = repository.getSessionById(sessionId)
|
||||
if (session != null && session.status == SessionStatus.ACTIVE) {
|
||||
val completedSession = with(ClimbSession) { session.complete() }
|
||||
repository.updateSession(completedSession)
|
||||
|
||||
val serviceIntent = SessionTrackingService.createStopIntent(context, sessionId)
|
||||
context.startService(serviceIntent)
|
||||
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
|
||||
syncToHealthConnect(completedSession)
|
||||
|
||||
_uiState.value = _uiState.value.copy(message = "Session completed!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ensureSessionTrackingServiceRunning(context: Context) {
|
||||
viewModelScope.launch {
|
||||
val activeSession = repository.getActiveSession()
|
||||
if (activeSession != null && activeSession.status == SessionStatus.ACTIVE) {
|
||||
val serviceIntent =
|
||||
SessionTrackingService.createStartIntent(context, activeSession.id)
|
||||
context.startForegroundService(serviceIntent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt operations
|
||||
fun addAttempt(attempt: Attempt) {
|
||||
viewModelScope.launch { repository.insertAttempt(attempt) }
|
||||
}
|
||||
|
||||
fun addAttempt(attempt: Attempt, context: Context) {
|
||||
viewModelScope.launch {
|
||||
repository.insertAttempt(attempt)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteAttempt(attempt: Attempt) {
|
||||
viewModelScope.launch { repository.deleteAttempt(attempt) }
|
||||
}
|
||||
|
||||
fun deleteAttempt(attempt: Attempt, context: Context) {
|
||||
viewModelScope.launch {
|
||||
repository.deleteAttempt(attempt)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateAttempt(attempt: Attempt) {
|
||||
viewModelScope.launch { repository.updateAttempt(attempt) }
|
||||
}
|
||||
|
||||
fun updateAttempt(attempt: Attempt, context: Context) {
|
||||
viewModelScope.launch {
|
||||
repository.updateAttempt(attempt)
|
||||
ClimbStatsWidgetProvider.updateAllWidgets(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun getAttemptsBySession(sessionId: String): Flow<List<Attempt>> =
|
||||
repository.getAttemptsBySession(sessionId)
|
||||
|
||||
fun getAttemptsByProblem(problemId: String): Flow<List<Attempt>> =
|
||||
repository.getAttemptsByProblem(problemId)
|
||||
|
||||
fun exportDataToZipUri(context: Context, uri: android.net.Uri) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = true,
|
||||
message = "Creating ZIP file with images..."
|
||||
)
|
||||
repository.exportAllDataToZipUri(context, uri)
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
message =
|
||||
"Export complete! Your climbing data and images have been saved."
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
error = "Export failed: ${e.message}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun importData(file: File) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_uiState.value = _uiState.value.copy(isLoading = true)
|
||||
|
||||
if (!file.name.lowercase().endsWith(".zip")) {
|
||||
throw Exception(
|
||||
"Only ZIP files are supported for import. Please use a ZIP file exported from OpenClimb."
|
||||
)
|
||||
}
|
||||
|
||||
repository.importDataFromZip(file)
|
||||
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
message = "Data imported successfully from ${file.name}"
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
error = "Import failed: ${e.message}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UI state operations
|
||||
fun clearMessage() {
|
||||
_uiState.value = _uiState.value.copy(message = null)
|
||||
}
|
||||
|
||||
fun clearError() {
|
||||
_uiState.value = _uiState.value.copy(error = null)
|
||||
}
|
||||
|
||||
// Sync-related methods
|
||||
suspend fun performManualSync() {
|
||||
try {
|
||||
syncService.syncWithServer()
|
||||
} catch (e: Exception) {
|
||||
setError("Sync failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun testSyncConnection() {
|
||||
try {
|
||||
syncService.testConnection()
|
||||
} catch (e: Exception) {
|
||||
setError("Connection test failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
fun setError(message: String) {
|
||||
_uiState.value = _uiState.value.copy(error = message)
|
||||
}
|
||||
|
||||
fun setMessage(message: String) {
|
||||
_uiState.value = _uiState.value.copy(message = message)
|
||||
}
|
||||
|
||||
fun resetAllData() {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_uiState.value = _uiState.value.copy(isLoading = true)
|
||||
|
||||
repository.resetAllData()
|
||||
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
isLoading = false,
|
||||
message = "All data has been reset successfully"
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(isLoading = false, error = "Reset failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun syncToHealthConnect(session: ClimbSession) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val gym = repository.getGymById(session.gymId)
|
||||
val gymName = gym?.name ?: "Unknown Gym"
|
||||
|
||||
val attempts = repository.getAttemptsBySession(session.id).first()
|
||||
val attemptCount = attempts.size
|
||||
|
||||
val result = healthConnectManager.autoSyncSession(session, gymName, attemptCount)
|
||||
|
||||
result
|
||||
.onSuccess {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
message =
|
||||
"Session synced to Health Connect successfully!"
|
||||
)
|
||||
}
|
||||
.onFailure { error ->
|
||||
if (healthConnectManager.isReadySync()) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
error =
|
||||
"Failed to sync to Health Connect: ${error.message}"
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (healthConnectManager.isReadySync()) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(error = "Health Connect sync error: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun manualSyncToHealthConnect(sessionId: String) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val session = repository.getSessionById(sessionId)
|
||||
if (session == null) {
|
||||
_uiState.value = _uiState.value.copy(error = "Session not found")
|
||||
return@launch
|
||||
}
|
||||
|
||||
if (session.status != SessionStatus.COMPLETED) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(error = "Only completed sessions can be synced")
|
||||
return@launch
|
||||
}
|
||||
|
||||
val gym = repository.getGymById(session.gymId)
|
||||
val gymName = gym?.name ?: "Unknown Gym"
|
||||
val attempts = repository.getAttemptsBySession(session.id).first()
|
||||
val attemptCount = attempts.size
|
||||
|
||||
val result =
|
||||
healthConnectManager.syncClimbingSession(session, gymName, attemptCount)
|
||||
|
||||
result
|
||||
.onSuccess {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
message =
|
||||
"Session synced to Health Connect successfully!"
|
||||
)
|
||||
}
|
||||
.onFailure { error ->
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
error =
|
||||
"Failed to sync to Health Connect: ${error.message}"
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(error = "Health Connect sync error: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getHealthConnectManager(): HealthConnectManager = healthConnectManager
|
||||
|
||||
// Share operations
|
||||
suspend fun generateSessionShareCard(context: Context, sessionId: String): File? =
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val session = repository.getSessionById(sessionId) ?: return@withContext null
|
||||
val attempts = repository.getAttemptsBySession(sessionId).first()
|
||||
val problems =
|
||||
repository.getAllProblems().first().filter { problem ->
|
||||
attempts.any { it.problemId == problem.id }
|
||||
}
|
||||
val gym = repository.getGymById(session.gymId) ?: return@withContext null
|
||||
|
||||
val stats = SessionShareUtils.calculateSessionStats(session, attempts, problems)
|
||||
SessionShareUtils.generateShareCard(context, session, gym, stats)
|
||||
} catch (e: Exception) {
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
error = "Failed to generate share card: ${e.message}"
|
||||
)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun shareSessionCard(context: Context, imageFile: File) {
|
||||
SessionShareUtils.shareSessionCard(context, imageFile)
|
||||
}
|
||||
}
|
||||
|
||||
data class ClimbUiState(
|
||||
val isLoading: Boolean = false,
|
||||
val message: String? = null,
|
||||
val error: String? = null
|
||||
)
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.atridad.openclimb.utils
|
||||
|
||||
import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
object DateFormatUtils {
|
||||
|
||||
// ISO 8601 formatter matching iOS date format exactly
|
||||
private val ISO_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX").withZone(ZoneOffset.UTC)
|
||||
|
||||
/** Get current timestamp in iOS-compatible ISO 8601 format */
|
||||
fun nowISO8601(): String {
|
||||
return ISO_FORMATTER.format(Instant.now())
|
||||
}
|
||||
|
||||
/** Format an Instant to iOS-compatible ISO 8601 format */
|
||||
fun formatISO8601(instant: Instant): String {
|
||||
return ISO_FORMATTER.format(instant)
|
||||
}
|
||||
|
||||
/** Parse an iOS-compatible ISO 8601 date string back to Instant */
|
||||
fun parseISO8601(dateString: String): Instant? {
|
||||
return try {
|
||||
Instant.from(ISO_FORMATTER.parse(dateString))
|
||||
} catch (e: Exception) {
|
||||
|
||||
try {
|
||||
Instant.parse(dateString)
|
||||
} catch (e2: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Validate that a date string matches the expected iOS format */
|
||||
fun isValidISO8601(dateString: String): Boolean {
|
||||
return parseISO8601(dateString) != null
|
||||
}
|
||||
|
||||
/** Convert milliseconds timestamp to iOS-compatible ISO 8601 format */
|
||||
fun millisToISO8601(millis: Long): String {
|
||||
return ISO_FORMATTER.format(Instant.ofEpochMilli(millis))
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package com.atridad.openclimb.utils
|
||||
|
||||
import java.security.MessageDigest
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Utility for creating consistent image filenames across iOS and Android platforms. Uses
|
||||
* deterministic naming based on problem ID and timestamp to ensure sync compatibility.
|
||||
*/
|
||||
object ImageNamingUtils {
|
||||
|
||||
private const val IMAGE_EXTENSION = ".jpg"
|
||||
private const val HASH_LENGTH = 12 // First 12 chars of SHA-256
|
||||
|
||||
/** Generates a deterministic filename for a problem image */
|
||||
fun generateImageFilename(problemId: String, imageIndex: Int): String {
|
||||
val input = "${problemId}_${imageIndex}"
|
||||
val hash = createHash(input)
|
||||
|
||||
return "problem_${hash}_${imageIndex}${IMAGE_EXTENSION}"
|
||||
}
|
||||
|
||||
/** Legacy method for backward compatibility */
|
||||
fun generateImageFilename(problemId: String, timestamp: String, imageIndex: Int): String {
|
||||
return generateImageFilename(problemId, imageIndex)
|
||||
}
|
||||
|
||||
/** Extracts problem ID from an image filename */
|
||||
fun extractProblemIdFromFilename(filename: String): String? {
|
||||
if (!filename.startsWith("problem_") || !filename.endsWith(IMAGE_EXTENSION)) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Format: problem_{hash}_{index}.jpg
|
||||
val nameWithoutExtension = filename.substring(0, filename.length - IMAGE_EXTENSION.length)
|
||||
val parts = nameWithoutExtension.split("_")
|
||||
|
||||
if (parts.size != 3 || parts[0] != "problem") {
|
||||
return null
|
||||
}
|
||||
|
||||
return parts[1]
|
||||
}
|
||||
|
||||
/** Validates if a filename follows our naming convention */
|
||||
fun isValidImageFilename(filename: String): Boolean {
|
||||
if (!filename.startsWith("problem_") || !filename.endsWith(IMAGE_EXTENSION)) {
|
||||
return false
|
||||
}
|
||||
|
||||
val nameWithoutExtension = filename.substring(0, filename.length - IMAGE_EXTENSION.length)
|
||||
val parts = nameWithoutExtension.split("_")
|
||||
|
||||
return parts.size == 3 &&
|
||||
parts[0] == "problem" &&
|
||||
parts[1].length == HASH_LENGTH &&
|
||||
parts[2].toIntOrNull() != null
|
||||
}
|
||||
|
||||
/** Migrates an existing filename to our naming convention */
|
||||
fun migrateFilename(oldFilename: String, problemId: String, imageIndex: Int): String {
|
||||
if (isValidImageFilename(oldFilename)) {
|
||||
return oldFilename
|
||||
}
|
||||
|
||||
return generateImageFilename(problemId, imageIndex)
|
||||
}
|
||||
|
||||
/** Creates a deterministic hash from input string */
|
||||
private fun createHash(input: String): String {
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
val hashBytes = digest.digest(input.toByteArray(Charsets.UTF_8))
|
||||
val hashHex = hashBytes.joinToString("") { "%02x".format(it) }
|
||||
return hashHex.take(HASH_LENGTH)
|
||||
}
|
||||
|
||||
/** Batch renames images for a problem to use our naming convention */
|
||||
fun batchRenameForProblem(
|
||||
problemId: String,
|
||||
existingFilenames: List<String>
|
||||
): Map<String, String> {
|
||||
val renameMap = mutableMapOf<String, String>()
|
||||
|
||||
existingFilenames.forEachIndexed { index, oldFilename ->
|
||||
val newFilename = generateImageFilename(problemId, index)
|
||||
if (newFilename != oldFilename) {
|
||||
renameMap[oldFilename] = newFilename
|
||||
}
|
||||
}
|
||||
|
||||
return renameMap
|
||||
}
|
||||
|
||||
/** Generates the canonical filename for a problem image */
|
||||
fun getCanonicalImageFilename(problemId: String, imageIndex: Int): String {
|
||||
return generateImageFilename(problemId, imageIndex)
|
||||
}
|
||||
|
||||
/** Creates a mapping of existing server filenames to canonical filenames */
|
||||
fun createServerMigrationMap(
|
||||
problemId: String,
|
||||
serverImageFilenames: List<String>,
|
||||
localImageCount: Int
|
||||
): Map<String, String> {
|
||||
val migrationMap = mutableMapOf<String, String>()
|
||||
|
||||
for (imageIndex in 0 until localImageCount) {
|
||||
val canonicalName = getCanonicalImageFilename(problemId, imageIndex)
|
||||
|
||||
if (serverImageFilenames.contains(canonicalName)) {
|
||||
continue
|
||||
}
|
||||
|
||||
for (serverFilename in serverImageFilenames) {
|
||||
if (isValidImageFilename(serverFilename) &&
|
||||
!migrationMap.values.contains(serverFilename)
|
||||
) {
|
||||
migrationMap[serverFilename] = canonicalName
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return migrationMap
|
||||
}
|
||||
}
|
||||
@@ -1,399 +0,0 @@
|
||||
package com.atridad.openclimb.utils
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
import androidx.core.graphics.scale
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.util.UUID
|
||||
|
||||
object ImageUtils {
|
||||
|
||||
private const val IMAGES_DIR = "problem_images"
|
||||
private const val MAX_IMAGE_SIZE = 1024
|
||||
private const val IMAGE_QUALITY = 85
|
||||
|
||||
// Creates the images directory if it doesn't exist
|
||||
fun getImagesDirectory(context: Context): File {
|
||||
val imagesDir = File(context.filesDir, IMAGES_DIR)
|
||||
if (!imagesDir.exists()) {
|
||||
imagesDir.mkdirs()
|
||||
}
|
||||
return imagesDir
|
||||
}
|
||||
|
||||
/** Saves an image from a URI with compression and proper orientation */
|
||||
fun saveImageFromUri(
|
||||
context: Context,
|
||||
imageUri: Uri,
|
||||
problemId: String? = null,
|
||||
imageIndex: Int? = null
|
||||
): String? {
|
||||
return try {
|
||||
|
||||
val originalBitmap =
|
||||
context.contentResolver.openInputStream(imageUri)?.use { input ->
|
||||
BitmapFactory.decodeStream(input)
|
||||
}
|
||||
?: return null
|
||||
|
||||
val orientedBitmap = correctImageOrientation(context, imageUri, originalBitmap)
|
||||
val compressedBitmap = compressImage(orientedBitmap)
|
||||
|
||||
// Always require deterministic naming - no UUID fallback
|
||||
require(problemId != null && imageIndex != null) {
|
||||
"Problem ID and image index are required for deterministic image naming"
|
||||
}
|
||||
|
||||
val filename = ImageNamingUtils.generateImageFilename(problemId, imageIndex)
|
||||
val imageFile = File(getImagesDirectory(context), filename)
|
||||
|
||||
FileOutputStream(imageFile).use { output ->
|
||||
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
||||
}
|
||||
|
||||
originalBitmap.recycle()
|
||||
if (orientedBitmap != originalBitmap) {
|
||||
orientedBitmap.recycle()
|
||||
}
|
||||
compressedBitmap.recycle()
|
||||
|
||||
"$IMAGES_DIR/$filename"
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Corrects image orientation based on EXIF data */
|
||||
private fun correctImageOrientation(context: Context, imageUri: Uri, bitmap: Bitmap): Bitmap {
|
||||
return try {
|
||||
val inputStream = context.contentResolver.openInputStream(imageUri)
|
||||
inputStream?.use { input ->
|
||||
val exif = androidx.exifinterface.media.ExifInterface(input)
|
||||
val orientation =
|
||||
exif.getAttributeInt(
|
||||
androidx.exifinterface.media.ExifInterface.TAG_ORIENTATION,
|
||||
androidx.exifinterface.media.ExifInterface.ORIENTATION_NORMAL
|
||||
)
|
||||
|
||||
val matrix = android.graphics.Matrix()
|
||||
when (orientation) {
|
||||
androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_90 -> {
|
||||
matrix.postRotate(90f)
|
||||
}
|
||||
androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_180 -> {
|
||||
matrix.postRotate(180f)
|
||||
}
|
||||
androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_270 -> {
|
||||
matrix.postRotate(270f)
|
||||
}
|
||||
androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> {
|
||||
matrix.postScale(-1f, 1f)
|
||||
}
|
||||
androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_VERTICAL -> {
|
||||
matrix.postScale(1f, -1f)
|
||||
}
|
||||
androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSPOSE -> {
|
||||
matrix.postRotate(90f)
|
||||
matrix.postScale(-1f, 1f)
|
||||
}
|
||||
androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSVERSE -> {
|
||||
matrix.postRotate(-90f)
|
||||
matrix.postScale(-1f, 1f)
|
||||
}
|
||||
}
|
||||
|
||||
if (matrix.isIdentity) {
|
||||
bitmap
|
||||
} else {
|
||||
android.graphics.Bitmap.createBitmap(
|
||||
bitmap,
|
||||
0,
|
||||
0,
|
||||
bitmap.width,
|
||||
bitmap.height,
|
||||
matrix,
|
||||
true
|
||||
)
|
||||
}
|
||||
}
|
||||
?: bitmap
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
bitmap
|
||||
}
|
||||
}
|
||||
|
||||
/** Compresses and resizes an image bitmap */
|
||||
@SuppressLint("UseKtx")
|
||||
private fun compressImage(original: Bitmap): Bitmap {
|
||||
val width = original.width
|
||||
val height = original.height
|
||||
|
||||
// Calculate the scaling factor
|
||||
val scaleFactor =
|
||||
if (width > height) {
|
||||
if (width > MAX_IMAGE_SIZE) MAX_IMAGE_SIZE.toFloat() / width else 1f
|
||||
} else {
|
||||
if (height > MAX_IMAGE_SIZE) MAX_IMAGE_SIZE.toFloat() / height else 1f
|
||||
}
|
||||
|
||||
return if (scaleFactor < 1f) {
|
||||
val newWidth = (width * scaleFactor).toInt()
|
||||
val newHeight = (height * scaleFactor).toInt()
|
||||
original.scale(newWidth, newHeight)
|
||||
} else {
|
||||
original
|
||||
}
|
||||
}
|
||||
|
||||
/** Gets the full file path for an image */
|
||||
fun getImageFile(context: Context, relativePath: String): File {
|
||||
// If relativePath already contains the directory, use it as-is
|
||||
// Otherwise, assume it's just a filename and add the images directory
|
||||
return if (relativePath.contains("/")) {
|
||||
File(context.filesDir, relativePath)
|
||||
} else {
|
||||
// Just a filename - look in the images directory
|
||||
File(getImagesDirectory(context), relativePath)
|
||||
}
|
||||
}
|
||||
|
||||
/** Deletes an image file */
|
||||
fun deleteImage(context: Context, relativePath: String): Boolean {
|
||||
return try {
|
||||
val file = getImageFile(context, relativePath)
|
||||
file.delete()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/** Imports an image file from the import directory */
|
||||
fun importImageFile(context: Context, sourceFile: File): String? {
|
||||
return try {
|
||||
if (!sourceFile.exists()) return null
|
||||
|
||||
// Generate new filename to avoid conflicts
|
||||
val extension = sourceFile.extension.ifEmpty { "jpg" }
|
||||
val filename = "${UUID.randomUUID()}.$extension"
|
||||
val destFile = File(getImagesDirectory(context), filename)
|
||||
|
||||
sourceFile.copyTo(destFile, overwrite = true)
|
||||
"$IMAGES_DIR/$filename"
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Gets all image files in the images directory */
|
||||
fun getAllImages(context: Context): List<String> {
|
||||
return try {
|
||||
val imagesDir = getImagesDirectory(context)
|
||||
imagesDir.listFiles()?.mapNotNull { file ->
|
||||
if (file.isFile &&
|
||||
(file.extension == "jpg" ||
|
||||
file.extension == "jpeg" ||
|
||||
file.extension == "png")
|
||||
) {
|
||||
"$IMAGES_DIR/${file.name}"
|
||||
} else null
|
||||
}
|
||||
?: emptyList()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
/** Temporarily saves an image during selection process */
|
||||
fun saveTemporaryImageFromUri(context: Context, imageUri: Uri): String? {
|
||||
return try {
|
||||
val originalBitmap =
|
||||
MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)
|
||||
?: return null
|
||||
|
||||
val orientedBitmap = correctImageOrientation(context, imageUri, originalBitmap)
|
||||
val compressedBitmap = compressImage(orientedBitmap)
|
||||
|
||||
val tempFilename = "temp_${UUID.randomUUID()}.jpg"
|
||||
val imageFile = File(getImagesDirectory(context), tempFilename)
|
||||
|
||||
FileOutputStream(imageFile).use { output ->
|
||||
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
||||
}
|
||||
|
||||
originalBitmap.recycle()
|
||||
if (orientedBitmap != originalBitmap) {
|
||||
orientedBitmap.recycle()
|
||||
}
|
||||
if (compressedBitmap != orientedBitmap) {
|
||||
compressedBitmap.recycle()
|
||||
}
|
||||
|
||||
tempFilename
|
||||
} catch (e: Exception) {
|
||||
Log.e("ImageUtils", "Error saving temporary image from URI", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Renames a temporary image */
|
||||
fun renameTemporaryImage(
|
||||
context: Context,
|
||||
tempFilename: String,
|
||||
problemId: String,
|
||||
imageIndex: Int
|
||||
): String? {
|
||||
return try {
|
||||
val tempFile = File(getImagesDirectory(context), tempFilename)
|
||||
if (!tempFile.exists()) {
|
||||
Log.e("ImageUtils", "Temporary file does not exist: $tempFilename")
|
||||
return null
|
||||
}
|
||||
|
||||
val deterministicFilename =
|
||||
ImageNamingUtils.generateImageFilename(problemId, imageIndex)
|
||||
val finalFile = File(getImagesDirectory(context), deterministicFilename)
|
||||
|
||||
if (tempFile.renameTo(finalFile)) {
|
||||
Log.d(
|
||||
"ImageUtils",
|
||||
"Renamed temporary image: $tempFilename -> $deterministicFilename"
|
||||
)
|
||||
deterministicFilename
|
||||
} else {
|
||||
Log.e("ImageUtils", "Failed to rename temporary image: $tempFilename")
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("ImageUtils", "Error renaming temporary image", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Saves an image from byte array to app's private storage */
|
||||
fun saveImageFromBytes(context: Context, imageData: ByteArray): String? {
|
||||
return try {
|
||||
val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) ?: return null
|
||||
|
||||
val compressedBitmap = compressImage(bitmap)
|
||||
|
||||
// Generate unique filename
|
||||
val filename = "${UUID.randomUUID()}.jpg"
|
||||
val imageFile = File(getImagesDirectory(context), filename)
|
||||
|
||||
// Save compressed image
|
||||
FileOutputStream(imageFile).use { output ->
|
||||
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
||||
}
|
||||
|
||||
// Clean up bitmaps
|
||||
bitmap.recycle()
|
||||
compressedBitmap.recycle()
|
||||
|
||||
// Return relative path
|
||||
"$IMAGES_DIR/$filename"
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Saves image data with a specific filename */
|
||||
fun saveImageFromBytesWithFilename(
|
||||
context: Context,
|
||||
imageData: ByteArray,
|
||||
filename: String
|
||||
): String? {
|
||||
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)
|
||||
|
||||
// Save compressed image
|
||||
FileOutputStream(imageFile).use { output ->
|
||||
compressedBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, output)
|
||||
}
|
||||
|
||||
// Clean up bitmaps
|
||||
bitmap.recycle()
|
||||
compressedBitmap.recycle()
|
||||
|
||||
// Return relative path
|
||||
"$IMAGES_DIR/$filename"
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/** Migrates existing images to use consistent naming convention */
|
||||
fun migrateImageNaming(
|
||||
context: Context,
|
||||
problemId: String,
|
||||
currentImagePaths: List<String>
|
||||
): Map<String, String> {
|
||||
val migrationMap = mutableMapOf<String, String>()
|
||||
|
||||
currentImagePaths.forEachIndexed { index, oldPath ->
|
||||
val oldFilename = oldPath.substringAfterLast('/')
|
||||
val newFilename = ImageNamingUtils.migrateFilename(oldFilename, problemId, index)
|
||||
|
||||
if (oldFilename != newFilename) {
|
||||
try {
|
||||
val oldFile = getImageFile(context, oldPath)
|
||||
val newFile = File(getImagesDirectory(context), newFilename)
|
||||
|
||||
if (oldFile.exists() && oldFile.renameTo(newFile)) {
|
||||
val newPath = "$IMAGES_DIR/$newFilename"
|
||||
migrationMap[oldPath] = newPath
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// Log error but continue with other images
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return migrationMap
|
||||
}
|
||||
|
||||
/** Batch migrates all images in the system to use consistent naming */
|
||||
fun batchMigrateAllImages(
|
||||
context: Context,
|
||||
problemImageMap: Map<String, List<String>>
|
||||
): Map<String, String> {
|
||||
val allMigrations = mutableMapOf<String, String>()
|
||||
|
||||
problemImageMap.forEach { (problemId, imagePaths) ->
|
||||
val migrations = migrateImageNaming(context, problemId, imagePaths)
|
||||
allMigrations.putAll(migrations)
|
||||
}
|
||||
|
||||
return allMigrations
|
||||
}
|
||||
|
||||
/** Cleans up orphaned images that are not referenced by any problems */
|
||||
fun cleanupOrphanedImages(context: Context, referencedPaths: Set<String>) {
|
||||
try {
|
||||
val allImages = getAllImages(context)
|
||||
val orphanedImages = allImages.filter { it !in referencedPaths }
|
||||
|
||||
orphanedImages.forEach { path -> deleteImage(context, path) }
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,543 +0,0 @@
|
||||
package com.atridad.openclimb.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.*
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.core.graphics.createBitmap
|
||||
import androidx.core.graphics.toColorInt
|
||||
import com.atridad.openclimb.data.model.*
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
object SessionShareUtils {
|
||||
|
||||
data class SessionStats(
|
||||
val totalAttempts: Int,
|
||||
val successfulAttempts: Int,
|
||||
val problems: List<Problem>,
|
||||
val uniqueProblemsAttempted: Int,
|
||||
val uniqueProblemsCompleted: Int,
|
||||
val averageGrade: String?,
|
||||
val sessionDuration: String,
|
||||
val topResult: AttemptResult?,
|
||||
val topGrade: String?
|
||||
)
|
||||
|
||||
fun calculateSessionStats(
|
||||
session: ClimbSession,
|
||||
attempts: List<Attempt>,
|
||||
problems: List<Problem>
|
||||
): SessionStats {
|
||||
val successfulResults = listOf(AttemptResult.SUCCESS, AttemptResult.FLASH)
|
||||
|
||||
val successfulAttempts = attempts.filter { it.result in successfulResults }
|
||||
val uniqueProblems = attempts.map { it.problemId }.distinct()
|
||||
val uniqueCompletedProblems = successfulAttempts.map { it.problemId }.distinct()
|
||||
|
||||
val attemptedProblems = problems.filter { it.id in uniqueProblems }
|
||||
|
||||
// Calculate separate averages for different climbing types and difficulty systems
|
||||
val boulderProblems = attemptedProblems.filter { it.climbType == ClimbType.BOULDER }
|
||||
val ropeProblems = attemptedProblems.filter { it.climbType == ClimbType.ROPE }
|
||||
|
||||
val boulderAverage = calculateAverageGrade(boulderProblems, "Boulder")
|
||||
val ropeAverage = calculateAverageGrade(ropeProblems, "Rope")
|
||||
|
||||
// Combine averages for display
|
||||
val averageGrade =
|
||||
when {
|
||||
boulderAverage != null && ropeAverage != null ->
|
||||
"$boulderAverage / $ropeAverage"
|
||||
boulderAverage != null -> boulderAverage
|
||||
ropeAverage != null -> ropeAverage
|
||||
else -> null
|
||||
}
|
||||
|
||||
// Determine highest achieved grade (only from completed problems: SUCCESS or FLASH)
|
||||
val completedProblems = problems.filter { it.id in uniqueCompletedProblems }
|
||||
val completedBoulder = completedProblems.filter { it.climbType == ClimbType.BOULDER }
|
||||
val completedRope = completedProblems.filter { it.climbType == ClimbType.ROPE }
|
||||
val topBoulder = highestGradeForProblems(completedBoulder)
|
||||
val topRope = highestGradeForProblems(completedRope)
|
||||
val topGrade =
|
||||
when {
|
||||
topBoulder != null && topRope != null -> "$topBoulder / $topRope"
|
||||
topBoulder != null -> topBoulder
|
||||
topRope != null -> topRope
|
||||
else -> null
|
||||
}
|
||||
|
||||
val duration = if (session.duration != null) "${session.duration}m" else "Unknown"
|
||||
val topResult =
|
||||
attempts
|
||||
.maxByOrNull {
|
||||
when (it.result) {
|
||||
AttemptResult.FLASH -> 3
|
||||
AttemptResult.SUCCESS -> 2
|
||||
AttemptResult.FALL -> 1
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
?.result
|
||||
|
||||
return SessionStats(
|
||||
totalAttempts = attempts.size,
|
||||
successfulAttempts = successfulAttempts.size,
|
||||
problems = attemptedProblems,
|
||||
uniqueProblemsAttempted = uniqueProblems.size,
|
||||
uniqueProblemsCompleted = uniqueCompletedProblems.size,
|
||||
averageGrade = averageGrade,
|
||||
sessionDuration = duration,
|
||||
topResult = topResult,
|
||||
topGrade = topGrade
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate average grade for a specific set of problems, respecting their difficulty systems
|
||||
*/
|
||||
private fun calculateAverageGrade(problems: List<Problem>, climbingType: String): String? {
|
||||
if (problems.isEmpty()) return null
|
||||
|
||||
// Group problems by difficulty system
|
||||
val problemsBySystem = problems.groupBy { it.difficulty.system }
|
||||
|
||||
val averages = mutableListOf<String>()
|
||||
|
||||
problemsBySystem.forEach { (system, systemProblems) ->
|
||||
when (system) {
|
||||
DifficultySystem.V_SCALE -> {
|
||||
val gradeValues =
|
||||
systemProblems.mapNotNull { problem ->
|
||||
when {
|
||||
problem.difficulty.grade == "VB" -> 0
|
||||
else -> problem.difficulty.grade.removePrefix("V").toIntOrNull()
|
||||
}
|
||||
}
|
||||
if (gradeValues.isNotEmpty()) {
|
||||
val avg = gradeValues.average().roundToInt()
|
||||
averages.add(if (avg == 0) "VB" else "V$avg")
|
||||
}
|
||||
}
|
||||
DifficultySystem.FONT -> {
|
||||
val gradeValues =
|
||||
systemProblems.mapNotNull { problem ->
|
||||
// Extract numeric part from Font grades (e.g., "6A" -> 6, "7C+" ->
|
||||
// 7)
|
||||
problem.difficulty.grade.filter { it.isDigit() }.toIntOrNull()
|
||||
}
|
||||
if (gradeValues.isNotEmpty()) {
|
||||
val avg = gradeValues.average().roundToInt()
|
||||
averages.add("$avg")
|
||||
}
|
||||
}
|
||||
DifficultySystem.YDS -> {
|
||||
val gradeValues =
|
||||
systemProblems.mapNotNull { problem ->
|
||||
// Extract numeric part from YDS grades (e.g., "5.10a" -> 5.10)
|
||||
val grade = problem.difficulty.grade
|
||||
if (grade.startsWith("5.")) {
|
||||
grade.substring(2).toDoubleOrNull()
|
||||
} else null
|
||||
}
|
||||
if (gradeValues.isNotEmpty()) {
|
||||
val avg = gradeValues.average()
|
||||
averages.add("5.${String.format("%.1f", avg)}")
|
||||
}
|
||||
}
|
||||
DifficultySystem.CUSTOM -> {
|
||||
// For custom systems, try to extract numeric values
|
||||
val gradeValues =
|
||||
systemProblems.mapNotNull { problem ->
|
||||
problem.difficulty
|
||||
.grade
|
||||
.filter { it.isDigit() || it == '.' || it == '-' }
|
||||
.toDoubleOrNull()
|
||||
}
|
||||
if (gradeValues.isNotEmpty()) {
|
||||
val avg = gradeValues.average()
|
||||
averages.add(String.format("%.1f", avg))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return if (averages.isNotEmpty()) {
|
||||
if (averages.size == 1) {
|
||||
averages.first()
|
||||
} else {
|
||||
averages.joinToString(" / ")
|
||||
}
|
||||
} else null
|
||||
}
|
||||
|
||||
fun generateShareCard(
|
||||
context: Context,
|
||||
session: ClimbSession,
|
||||
gym: Gym,
|
||||
stats: SessionStats
|
||||
): File? {
|
||||
return try {
|
||||
val width = 1242 // 3:4 aspect at higher resolution for better fit
|
||||
val height = 1656
|
||||
|
||||
val bitmap = createBitmap(width, height)
|
||||
val canvas = Canvas(bitmap)
|
||||
|
||||
val gradientDrawable =
|
||||
GradientDrawable(
|
||||
GradientDrawable.Orientation.TOP_BOTTOM,
|
||||
intArrayOf("#667eea".toColorInt(), "#764ba2".toColorInt())
|
||||
)
|
||||
gradientDrawable.setBounds(0, 0, width, height)
|
||||
gradientDrawable.draw(canvas)
|
||||
|
||||
// Setup paint objects
|
||||
val titlePaint =
|
||||
Paint().apply {
|
||||
color = Color.WHITE
|
||||
textSize = 72f
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
isAntiAlias = true
|
||||
textAlign = Paint.Align.CENTER
|
||||
}
|
||||
|
||||
val subtitlePaint =
|
||||
Paint().apply {
|
||||
color = "#E8E8E8".toColorInt()
|
||||
textSize = 48f
|
||||
typeface = Typeface.DEFAULT
|
||||
isAntiAlias = true
|
||||
textAlign = Paint.Align.CENTER
|
||||
}
|
||||
|
||||
val statLabelPaint =
|
||||
Paint().apply {
|
||||
color = "#B8B8B8".toColorInt()
|
||||
textSize = 36f
|
||||
typeface = Typeface.DEFAULT
|
||||
isAntiAlias = true
|
||||
textAlign = Paint.Align.CENTER
|
||||
}
|
||||
|
||||
val statValuePaint =
|
||||
Paint().apply {
|
||||
color = Color.WHITE
|
||||
textSize = 64f
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
isAntiAlias = true
|
||||
textAlign = Paint.Align.CENTER
|
||||
}
|
||||
|
||||
val cardPaint =
|
||||
Paint().apply {
|
||||
color = "#40FFFFFF".toColorInt()
|
||||
isAntiAlias = true
|
||||
}
|
||||
|
||||
// Draw main card background
|
||||
val cardRect = RectF(60f, 200f, width - 60f, height - 120f)
|
||||
canvas.drawRoundRect(cardRect, 40f, 40f, cardPaint)
|
||||
|
||||
// Draw content
|
||||
var yPosition = 300f
|
||||
|
||||
// Title
|
||||
canvas.drawText("Climbing Session", width / 2f, yPosition, titlePaint)
|
||||
yPosition += 80f
|
||||
|
||||
// Gym and date
|
||||
canvas.drawText(gym.name, width / 2f, yPosition, subtitlePaint)
|
||||
yPosition += 60f
|
||||
|
||||
val dateText = formatSessionDate(session.date)
|
||||
canvas.drawText(dateText, width / 2f, yPosition, subtitlePaint)
|
||||
yPosition += 120f
|
||||
|
||||
// Stats grid
|
||||
val statsStartY = yPosition
|
||||
val columnWidth = width / 2f
|
||||
val columnMaxTextWidth = columnWidth - 120f
|
||||
|
||||
// Left column stats
|
||||
var leftY = statsStartY
|
||||
drawStatItemFitting(
|
||||
canvas,
|
||||
columnWidth / 2f,
|
||||
leftY,
|
||||
"Attempts",
|
||||
stats.totalAttempts.toString(),
|
||||
statLabelPaint,
|
||||
statValuePaint,
|
||||
columnMaxTextWidth
|
||||
)
|
||||
leftY += 120f
|
||||
drawStatItemFitting(
|
||||
canvas,
|
||||
columnWidth / 2f,
|
||||
leftY,
|
||||
"Problems",
|
||||
stats.uniqueProblemsAttempted.toString(),
|
||||
statLabelPaint,
|
||||
statValuePaint,
|
||||
columnMaxTextWidth
|
||||
)
|
||||
leftY += 120f
|
||||
drawStatItemFitting(
|
||||
canvas,
|
||||
columnWidth / 2f,
|
||||
leftY,
|
||||
"Duration",
|
||||
stats.sessionDuration,
|
||||
statLabelPaint,
|
||||
statValuePaint,
|
||||
columnMaxTextWidth
|
||||
)
|
||||
|
||||
// Right column stats
|
||||
var rightY = statsStartY
|
||||
drawStatItemFitting(
|
||||
canvas,
|
||||
width - columnWidth / 2f,
|
||||
rightY,
|
||||
"Completed",
|
||||
stats.uniqueProblemsCompleted.toString(),
|
||||
statLabelPaint,
|
||||
statValuePaint,
|
||||
columnMaxTextWidth
|
||||
)
|
||||
rightY += 120f
|
||||
|
||||
var rightYAfter = rightY
|
||||
stats.topGrade?.let { grade ->
|
||||
drawStatItemFitting(
|
||||
canvas,
|
||||
width - columnWidth / 2f,
|
||||
rightY,
|
||||
"Top Grade",
|
||||
grade,
|
||||
statLabelPaint,
|
||||
statValuePaint,
|
||||
columnMaxTextWidth
|
||||
)
|
||||
rightYAfter += 120f
|
||||
}
|
||||
|
||||
// Grade range(s)
|
||||
val boulderRange =
|
||||
gradeRangeForProblems(
|
||||
stats.problems.filter { it.climbType == ClimbType.BOULDER }
|
||||
)
|
||||
val ropeRange =
|
||||
gradeRangeForProblems(stats.problems.filter { it.climbType == ClimbType.ROPE })
|
||||
val rangesY = kotlin.math.max(leftY, rightYAfter) + 120f
|
||||
if (boulderRange != null && ropeRange != null) {
|
||||
// Two evenly spaced items
|
||||
drawStatItemFitting(
|
||||
canvas,
|
||||
columnWidth / 2f,
|
||||
rangesY,
|
||||
"Boulder Range",
|
||||
boulderRange,
|
||||
statLabelPaint,
|
||||
statValuePaint,
|
||||
columnMaxTextWidth
|
||||
)
|
||||
drawStatItemFitting(
|
||||
canvas,
|
||||
width - columnWidth / 2f,
|
||||
rangesY,
|
||||
"Rope Range",
|
||||
ropeRange,
|
||||
statLabelPaint,
|
||||
statValuePaint,
|
||||
columnMaxTextWidth
|
||||
)
|
||||
} else if (boulderRange != null || ropeRange != null) {
|
||||
// Single centered item
|
||||
val singleRange = boulderRange ?: ropeRange ?: ""
|
||||
drawStatItemFitting(
|
||||
canvas,
|
||||
width / 2f,
|
||||
rangesY,
|
||||
"Grade Range",
|
||||
singleRange,
|
||||
statLabelPaint,
|
||||
statValuePaint,
|
||||
width - 200f
|
||||
)
|
||||
}
|
||||
|
||||
// App branding
|
||||
val brandingPaint =
|
||||
Paint().apply {
|
||||
color = "#80FFFFFF".toColorInt()
|
||||
textSize = 32f
|
||||
typeface = Typeface.DEFAULT
|
||||
isAntiAlias = true
|
||||
textAlign = Paint.Align.CENTER
|
||||
}
|
||||
canvas.drawText("OpenClimb", width / 2f, height - 40f, brandingPaint)
|
||||
|
||||
// Save to file
|
||||
val shareDir = File(context.cacheDir, "shares")
|
||||
if (!shareDir.exists()) {
|
||||
shareDir.mkdirs()
|
||||
}
|
||||
|
||||
val filename = "session_${session.id}_${System.currentTimeMillis()}.png"
|
||||
val file = File(shareDir, filename)
|
||||
|
||||
val outputStream = FileOutputStream(file)
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
|
||||
outputStream.flush()
|
||||
outputStream.close()
|
||||
|
||||
bitmap.recycle()
|
||||
|
||||
file
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun drawStatItem(
|
||||
canvas: Canvas,
|
||||
x: Float,
|
||||
y: Float,
|
||||
label: String,
|
||||
value: String,
|
||||
labelPaint: Paint,
|
||||
valuePaint: Paint
|
||||
) {
|
||||
canvas.drawText(value, x, y, valuePaint)
|
||||
canvas.drawText(label, x, y + 50f, labelPaint)
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a stat item while fitting the value text to a max width by reducing text size if
|
||||
* needed.
|
||||
*/
|
||||
private fun drawStatItemFitting(
|
||||
canvas: Canvas,
|
||||
x: Float,
|
||||
y: Float,
|
||||
label: String,
|
||||
value: String,
|
||||
labelPaint: Paint,
|
||||
valuePaint: Paint,
|
||||
maxTextWidth: Float
|
||||
) {
|
||||
val tempPaint = Paint(valuePaint)
|
||||
var textSize = tempPaint.textSize
|
||||
var textWidth = tempPaint.measureText(value)
|
||||
while (textWidth > maxTextWidth && textSize > 36f) {
|
||||
textSize -= 2f
|
||||
tempPaint.textSize = textSize
|
||||
textWidth = tempPaint.measureText(value)
|
||||
}
|
||||
canvas.drawText(value, x, y, tempPaint)
|
||||
canvas.drawText(label, x, y + 50f, labelPaint)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a range string like "X - Y" for the given problems, based on their difficulty grades.
|
||||
*/
|
||||
private fun gradeRangeForProblems(problems: List<Problem>): String? {
|
||||
if (problems.isEmpty()) return null
|
||||
val grades = problems.map { it.difficulty }
|
||||
val sorted = grades.sortedWith { a, b -> a.compareTo(b) }
|
||||
return "${sorted.first().grade} - ${sorted.last().grade}"
|
||||
}
|
||||
|
||||
private fun formatSessionDate(dateString: String): String {
|
||||
return try {
|
||||
val formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME
|
||||
val date = LocalDateTime.parse(dateString, formatter)
|
||||
val displayFormatter = DateTimeFormatter.ofPattern("MMMM dd, yyyy")
|
||||
date.format(displayFormatter)
|
||||
} catch (_: Exception) {
|
||||
dateString.take(10)
|
||||
}
|
||||
}
|
||||
|
||||
fun shareSessionCard(context: Context, imageFile: File) {
|
||||
try {
|
||||
val uri =
|
||||
FileProvider.getUriForFile(
|
||||
context,
|
||||
"${context.packageName}.fileprovider",
|
||||
imageFile
|
||||
)
|
||||
|
||||
val shareIntent =
|
||||
Intent().apply {
|
||||
action = Intent.ACTION_SEND
|
||||
type = "image/png"
|
||||
putExtra(Intent.EXTRA_STREAM, uri)
|
||||
putExtra(Intent.EXTRA_TEXT, "Check out my climbing session! #OpenClimb")
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
}
|
||||
|
||||
val chooser = Intent.createChooser(shareIntent, "Share Session")
|
||||
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
context.startActivity(chooser)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the highest grade string among the given problems, respecting their difficulty
|
||||
* system.
|
||||
*/
|
||||
private fun highestGradeForProblems(problems: List<Problem>): String? {
|
||||
if (problems.isEmpty()) return null
|
||||
return problems
|
||||
.maxByOrNull { p -> gradeRank(p.difficulty.system, p.difficulty.grade) }
|
||||
?.difficulty
|
||||
?.grade
|
||||
}
|
||||
|
||||
/** Produces a comparable numeric rank for grades across supported systems. */
|
||||
private fun gradeRank(system: DifficultySystem, grade: String): Double {
|
||||
return when (system) {
|
||||
DifficultySystem.V_SCALE -> {
|
||||
if (grade == "VB") 0.0 else grade.removePrefix("V").toDoubleOrNull() ?: -1.0
|
||||
}
|
||||
DifficultySystem.FONT -> {
|
||||
val list = DifficultySystem.FONT.getAvailableGrades()
|
||||
val idx = list.indexOf(grade.uppercase())
|
||||
if (idx >= 0) idx.toDouble()
|
||||
else grade.filter { it.isDigit() }.toDoubleOrNull() ?: -1.0
|
||||
}
|
||||
DifficultySystem.YDS -> {
|
||||
// Parse 5.X with optional letter a-d
|
||||
val s = grade.lowercase()
|
||||
if (!s.startsWith("5.")) return -1.0
|
||||
val tail = s.removePrefix("5.")
|
||||
val numberPart = tail.takeWhile { it.isDigit() || it == '.' }
|
||||
val letterPart = tail.drop(numberPart.length).firstOrNull()
|
||||
val base = numberPart.toDoubleOrNull() ?: return -1.0
|
||||
val letterWeight =
|
||||
when (letterPart) {
|
||||
'a' -> 0.0
|
||||
'b' -> 0.1
|
||||
'c' -> 0.2
|
||||
'd' -> 0.3
|
||||
else -> 0.0
|
||||
}
|
||||
base + letterWeight
|
||||
}
|
||||
DifficultySystem.CUSTOM -> {
|
||||
grade.filter { it.isDigit() || it == '.' || it == '-' }.toDoubleOrNull() ?: -1.0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="#000000"
|
||||
android:pathData="M12,2C6.48,2 2,6.48 2,12C2,17.52 6.48,22 12,22C17.52,22 22,17.52 22,12C22,6.48 17.52,2 12,2ZM10,17L5,12L6.41,10.59L10,14.17L17.59,6.58L19,8L10,17Z"/>
|
||||
</vector>
|
||||
9
android/app/src/main/res/drawable/ic_circle_filled.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="#000000"
|
||||
android:pathData="M9,11.24V7.5C9,6.12 10.12,5 11.5,5S14,6.12 14,7.5v3.74c1.21,-0.81 2,-2.18 2,-3.74C16,5.01 13.99,3 11.5,3S7,5.01 7,7.5C7,9.06 7.79,10.43 9,11.24zM18.84,15.87l-4.54,-2.26c-0.17,-0.07 -0.35,-0.11 -0.54,-0.11H13v-6C13,6.67 12.33,6 11.5,6S10,6.67 10,7.5v10.74l-3.43,-0.72c-0.08,-0.01 -0.15,-0.03 -0.24,-0.03c-0.31,0 -0.59,0.13 -0.79,0.33l-0.79,0.8l4.94,4.94C9.96,23.83 10.34,24 10.75,24h6.79c0.75,0 1.33,-0.55 1.44,-1.28l0.75,-5.27c0.01,-0.07 0.02,-0.14 0.02,-0.2C19.75,16.63 19.37,16.09 18.84,15.87z"/>
|
||||
</vector>
|
||||
@@ -4,27 +4,6 @@
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
|
||||
<group
|
||||
android:scaleX="0.7"
|
||||
android:scaleY="0.7"
|
||||
android:translateX="16.2"
|
||||
android:translateY="20">
|
||||
|
||||
<!-- Left mountain (yellow/amber) -->
|
||||
<path
|
||||
android:fillColor="#FFC107"
|
||||
android:strokeColor="#1C1C1C"
|
||||
android:strokeWidth="3"
|
||||
android:strokeLineJoin="round"
|
||||
android:pathData="M15,70 L35,25 L55,70 Z" />
|
||||
|
||||
<!-- Right mountain (red) -->
|
||||
<path
|
||||
android:fillColor="#F44336"
|
||||
android:strokeColor="#1C1C1C"
|
||||
android:strokeWidth="3"
|
||||
android:strokeLineJoin="round"
|
||||
android:pathData="M40,70 L65,15 L90,70 Z" />
|
||||
</group>
|
||||
<path android:fillColor="#FFC107" android:pathData="M24.000,78.545 L41.851,38.380 L59.702,78.545 Z" />
|
||||
<path android:fillColor="#F44336" android:pathData="M39.372,78.545 L61.686,29.455 L84.000,78.545 Z" />
|
||||
</vector>
|
||||
@@ -4,29 +4,6 @@
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
|
||||
<!-- Left mountain (yellow/amber) -->
|
||||
<path
|
||||
android:fillColor="#FFC107"
|
||||
android:pathData="M3,18 L8,9 L13,18 Z" />
|
||||
|
||||
<!-- Right mountain (red) -->
|
||||
<path
|
||||
android:fillColor="#F44336"
|
||||
android:pathData="M11,18 L16,7 L21,18 Z" />
|
||||
|
||||
<!-- Black outlines -->
|
||||
<path
|
||||
android:fillColor="@android:color/transparent"
|
||||
android:strokeColor="#1C1C1C"
|
||||
android:strokeWidth="1"
|
||||
android:strokeLineJoin="round"
|
||||
android:pathData="M3,18 L8,9 L13,18" />
|
||||
|
||||
<path
|
||||
android:fillColor="@android:color/transparent"
|
||||
android:strokeColor="#1C1C1C"
|
||||
android:strokeWidth="1"
|
||||
android:strokeLineJoin="round"
|
||||
android:pathData="M11,18 L16,7 L21,18" />
|
||||
</vector>
|
||||
<path android:fillColor="#FFC107" android:pathData="M2.000,20.182 L7.950,6.793 L13.901,20.182 Z" />
|
||||
<path android:fillColor="#F44336" android:pathData="M7.124,20.182 L14.562,3.818 L22.000,20.182 Z" />
|
||||
</vector>
|
||||
19
android/app/src/main/res/drawable/ic_splash.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:pathData="M0,0 L108,0 L108,108 L0,108 Z" />
|
||||
|
||||
<path
|
||||
android:fillColor="#FFC107"
|
||||
android:pathData="M24,74 L42,34 L60,74 Z" />
|
||||
|
||||
<path
|
||||
android:fillColor="#F44336"
|
||||
android:pathData="M41,74 L59,24 L84,74 Z" />
|
||||
</vector>
|
||||
@@ -5,190 +5,84 @@
|
||||
android:layout_height="match_parent"
|
||||
android:background="@drawable/widget_background"
|
||||
android:orientation="vertical"
|
||||
android:padding="12dp">
|
||||
android:padding="12dp"
|
||||
android:gravity="center">
|
||||
|
||||
<!-- Header -->
|
||||
<!-- Header with icon and "Weekly" text -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_marginBottom="12dp">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_width="28dp"
|
||||
android:layout_height="28dp"
|
||||
android:src="@drawable/ic_mountains"
|
||||
android:tint="@color/widget_primary"
|
||||
android:layout_marginEnd="8dp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="OpenClimb"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/widget_text_primary" />
|
||||
android:layout_marginEnd="8dp"
|
||||
android:contentDescription="@string/ascently_icon" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Climbing Stats"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/widget_text_secondary" />
|
||||
android:text="@string/weekly"
|
||||
android:textSize="18sp"
|
||||
android:textColor="@color/widget_text_primary" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- Stats Grid -->
|
||||
<!-- Attempts Row -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center">
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_marginBottom="12dp">
|
||||
|
||||
<!-- Top Row -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:orientation="horizontal"
|
||||
android:layout_marginBottom="8dp">
|
||||
<ImageView
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="32dp"
|
||||
android:src="@drawable/ic_circle_filled"
|
||||
android:tint="@color/widget_primary"
|
||||
android:layout_marginEnd="12dp"
|
||||
android:contentDescription="Attempts icon" />
|
||||
|
||||
<!-- Sessions Card -->
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/widget_stat_card_background"
|
||||
android:layout_marginEnd="4dp"
|
||||
android:padding="12dp">
|
||||
<TextView
|
||||
android:id="@+id/widget_attempts_value"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="0"
|
||||
android:textSize="40sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/widget_text_primary" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/widget_total_sessions"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="0"
|
||||
android:textSize="22sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/widget_primary" />
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Sessions"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/widget_text_secondary"
|
||||
android:layout_marginTop="2dp" />
|
||||
<!-- Sessions Row -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical">
|
||||
|
||||
</LinearLayout>
|
||||
<ImageView
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="32dp"
|
||||
android:src="@drawable/ic_play_arrow_24"
|
||||
android:tint="@color/widget_primary"
|
||||
android:layout_marginEnd="12dp"
|
||||
android:contentDescription="@string/sessions_icon" />
|
||||
|
||||
<!-- Problems Card -->
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/widget_stat_card_background"
|
||||
android:layout_marginStart="4dp"
|
||||
android:padding="12dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/widget_problems_completed"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="0"
|
||||
android:textSize="22sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/widget_primary" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Completed"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/widget_text_secondary"
|
||||
android:layout_marginTop="2dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- Bottom Row -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<!-- Success Rate Card -->
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/widget_stat_card_background"
|
||||
android:layout_marginEnd="4dp"
|
||||
android:padding="12dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/widget_total_problems"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="0"
|
||||
android:textSize="22sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/widget_secondary" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Problems"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/widget_text_secondary"
|
||||
android:layout_marginTop="2dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- Favorite Gym Card -->
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/widget_stat_card_background"
|
||||
android:layout_marginStart="4dp"
|
||||
android:padding="12dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/widget_favorite_gym"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="No gyms"
|
||||
android:textSize="13sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/widget_accent"
|
||||
android:gravity="center"
|
||||
android:maxLines="2"
|
||||
android:ellipsize="end" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Favorite"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/widget_text_secondary"
|
||||
android:layout_marginTop="2dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
<TextView
|
||||
android:id="@+id/widget_sessions_value"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/_0"
|
||||
android:textSize="40sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/widget_text_primary" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 550 B |
|
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 730 B |
|
Before Width: | Height: | Size: 982 B After Width: | Height: | Size: 388 B |
|
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 514 B |
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 628 B |
|
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 854 B |
|
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 970 B |
|
Before Width: | Height: | Size: 5.8 KiB After Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 7.6 KiB After Width: | Height: | Size: 1.6 KiB |
@@ -1,5 +1,5 @@
|
||||
<resources>
|
||||
<string name="app_name">OpenClimb</string>
|
||||
<string name="app_name">Ascently</string>
|
||||
<string name="session_tracking_service_description">Tracks active climbing sessions and displays session information in the notification area</string>
|
||||
|
||||
<!-- App Shortcuts -->
|
||||
@@ -12,5 +12,9 @@
|
||||
<string name="shortcut_end_session_disabled">No active session to end</string>
|
||||
|
||||
<!-- Widget -->
|
||||
<string name="widget_description">View your climbing stats at a glance</string>
|
||||
<string name="widget_description">View your weekly climbing stats</string>
|
||||
<string name="ascently_icon">Ascently icon</string>
|
||||
<string name="weekly">Weekly</string>
|
||||
<string name="sessions_icon">Sessions icon</string>
|
||||
<string name="_0">0</string>
|
||||
</resources>
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="Theme.OpenClimb" parent="android:Theme.Material.Light.NoActionBar" />
|
||||
<style name="Theme.Ascently" parent="android:Theme.Material.Light.NoActionBar" />
|
||||
|
||||
<style name="Theme.OpenClimb.Splash" parent="Theme.OpenClimb">
|
||||
<style name="Theme.Ascently.Splash" parent="Theme.Ascently">
|
||||
<item name="android:windowSplashScreenBackground">@color/splash_background</item>
|
||||
<item name="android:windowSplashScreenAnimatedIcon">@drawable/ic_mountains</item>
|
||||
<item name="android:windowSplashScreenAnimatedIcon">@drawable/ic_splash</item>
|
||||
<item name="android:windowSplashScreenAnimationDuration">200</item>
|
||||
</style>
|
||||
</resources>
|
||||
</resources>
|
||||
|
||||
@@ -3,15 +3,14 @@
|
||||
android:description="@string/widget_description"
|
||||
android:initialKeyguardLayout="@layout/widget_climb_stats"
|
||||
android:initialLayout="@layout/widget_climb_stats"
|
||||
android:minWidth="250dp"
|
||||
android:minHeight="180dp"
|
||||
android:minWidth="110dp"
|
||||
android:minHeight="110dp"
|
||||
android:maxResizeWidth="110dp"
|
||||
android:maxResizeHeight="110dp"
|
||||
android:previewImage="@drawable/ic_mountains"
|
||||
android:previewLayout="@layout/widget_climb_stats"
|
||||
android:resizeMode="horizontal|vertical"
|
||||
android:targetCellWidth="4"
|
||||
android:resizeMode="none"
|
||||
android:targetCellWidth="2"
|
||||
android:targetCellHeight="2"
|
||||
android:updatePeriodMillis="1800000"
|
||||
android:widgetCategory="home_screen"
|
||||
android:widgetFeatures="reconfigurable"
|
||||
android:maxResizeWidth="320dp"
|
||||
android:maxResizeHeight="240dp" />
|
||||
android:widgetCategory="home_screen" />
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.atridad.openclimb
|
||||
package com.atridad.ascently
|
||||
|
||||
import com.atridad.openclimb.data.format.*
|
||||
import com.atridad.openclimb.data.model.*
|
||||
import com.atridad.ascently.data.format.*
|
||||
import com.atridad.ascently.data.model.*
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import org.junit.Assert.*
|
||||
|
||||