4 Commits

Author SHA1 Message Date
genki
2b5f761d25 Oh yes - Thats how we do
No default params for KSP complainer fuck

UI sweeps
2026-01-10 00:08:04 -05:00
genki
52ea64f29a Oh yes - Thats how we do
No default params for KSP complainer fuck

UI sweeps
2026-01-09 19:59:44 -05:00
genki
51fdfbf3d6 Improved Training Screen and underlying
Added diagnostic view model with flag for picture detection but broke fucking everything meassing with tagDAO. au demain
2026-01-08 00:02:27 -05:00
genki
6ce115baa9 Bradeth_v1
UI improvement sweep
Underlying 'train models' backend functionality, dao and room db.
Mlmodule Hilt DI
2026-01-07 00:44:11 -05:00
49 changed files with 8269 additions and 578 deletions

View File

@@ -4,6 +4,14 @@
<selectionStates> <selectionStates>
<SelectionState runConfigName="app"> <SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" /> <option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2026-01-08T02:44:48.809354959Z">
<Target type="DEFAULT_BOOT">
<handle>
<DeviceId pluginId="LocalEmulator" identifier="path=/home/genki/.android/avd/Medium_Phone.avd" />
</handle>
</Target>
</DropdownSelection>
<DialogSelection />
</SelectionState> </SelectionState>
</selectionStates> </selectionStates>
</component> </component>

0
app/PersonEntity Normal file
View File

View File

@@ -77,5 +77,12 @@ dependencies {
implementation(libs.mlkit.face.detection) implementation(libs.mlkit.face.detection)
implementation(libs.kotlinx.coroutines.play.services) implementation(libs.kotlinx.coroutines.play.services)
//Face Rec
implementation(libs.tensorflow.lite)
implementation(libs.tensorflow.lite.support)
// Optional: GPU acceleration
implementation(libs.tensorflow.lite.gpu)
// Gson for storing FloatArrays in Room
implementation(libs.gson)
} }

View File

@@ -10,6 +10,7 @@ import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
@@ -19,7 +20,6 @@ import com.placeholder.sherpai2.domain.repository.ImageRepository
import com.placeholder.sherpai2.ui.presentation.MainScreen import com.placeholder.sherpai2.ui.presentation.MainScreen
import com.placeholder.sherpai2.ui.theme.SherpAI2Theme import com.placeholder.sherpai2.ui.theme.SherpAI2Theme
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
@@ -31,11 +31,9 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// Determine storage permission based on Android version
val storagePermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { val storagePermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Manifest.permission.READ_MEDIA_IMAGES Manifest.permission.READ_MEDIA_IMAGES
} else { } else {
@Suppress("DEPRECATION")
Manifest.permission.READ_EXTERNAL_STORAGE Manifest.permission.READ_EXTERNAL_STORAGE
} }
@@ -43,44 +41,53 @@ class MainActivity : ComponentActivity() {
SherpAI2Theme { SherpAI2Theme {
var hasPermission by remember { var hasPermission by remember {
mutableStateOf( mutableStateOf(
ContextCompat.checkSelfPermission(this, storagePermission) == ContextCompat.checkSelfPermission(this@MainActivity, storagePermission) ==
PackageManager.PERMISSION_GRANTED PackageManager.PERMISSION_GRANTED
) )
} }
// Track ingestion completion var isIngesting by remember { mutableStateOf(false) }
var imagesIngested by remember { mutableStateOf(false) } var imagesIngested by remember { mutableStateOf(false) }
// Launcher for permission request
val permissionLauncher = rememberLauncherForActivityResult( val permissionLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission() ActivityResultContracts.RequestPermission()
) { granted -> ) { granted ->
hasPermission = granted hasPermission = granted
} }
// Trigger ingestion once permission is granted // Logic: Handle the flow of Permission -> Ingestion
LaunchedEffect(hasPermission) { LaunchedEffect(hasPermission) {
if (hasPermission) { if (hasPermission) {
// Suspend until ingestion completes if (!imagesIngested && !isIngesting) {
imageRepository.ingestImages() isIngesting = true
imagesIngested = true imageRepository.ingestImages()
imagesIngested = true
isIngesting = false
}
} else { } else {
permissionLauncher.launch(storagePermission) permissionLauncher.launch(storagePermission)
} }
} }
// Gate UI until permission granted AND ingestion completed // UI State Mapping
if (hasPermission && imagesIngested) { Box(
MainScreen() modifier = Modifier.fillMaxSize(),
} else { contentAlignment = Alignment.Center
Box( ) {
modifier = Modifier.fillMaxSize(), when {
contentAlignment = Alignment.Center hasPermission && imagesIngested -> {
) { MainScreen()
Text("Please grant storage permission to continue.") }
hasPermission && isIngesting -> {
// Show a loader so you know it's working!
CircularProgressIndicator()
}
else -> {
Text("Please grant storage permission to continue.")
}
} }
} }
} }
} }
} }
} }

View File

@@ -2,46 +2,48 @@ package com.placeholder.sherpai2.data.local
import androidx.room.Database import androidx.room.Database
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import com.placeholder.sherpai2.data.local.dao.EventDao import com.placeholder.sherpai2.data.local.dao.*
import com.placeholder.sherpai2.data.local.dao.ImageAggregateDao import com.placeholder.sherpai2.data.local.entity.*
import com.placeholder.sherpai2.data.local.dao.ImageDao
import com.placeholder.sherpai2.data.local.dao.ImageEventDao
import com.placeholder.sherpai2.data.local.dao.ImagePersonDao
import com.placeholder.sherpai2.data.local.dao.ImageTagDao
import com.placeholder.sherpai2.data.local.dao.PersonDao
import com.placeholder.sherpai2.data.local.dao.TagDao
import com.placeholder.sherpai2.data.local.entity.EventEntity
import com.placeholder.sherpai2.data.local.entity.ImageEntity
import com.placeholder.sherpai2.data.local.entity.ImageEventEntity
import com.placeholder.sherpai2.data.local.entity.ImagePersonEntity
import com.placeholder.sherpai2.data.local.entity.ImageTagEntity
import com.placeholder.sherpai2.data.local.entity.PersonEntity
import com.placeholder.sherpai2.data.local.entity.TagEntity
/**
* AppDatabase - Complete database for SherpAI2
*
* ENTITIES:
* - YOUR EXISTING: Image, Tag, Event, junction tables
* - NEW: PersonEntity (people in your app)
* - NEW: FaceModelEntity (face embeddings, links to PersonEntity)
* - NEW: PhotoFaceTagEntity (face detections, links to ImageEntity + FaceModelEntity)
*/
@Database( @Database(
entities = [ entities = [
// ===== YOUR EXISTING ENTITIES =====
ImageEntity::class, ImageEntity::class,
TagEntity::class, TagEntity::class,
PersonEntity::class,
EventEntity::class, EventEntity::class,
ImageTagEntity::class, ImageTagEntity::class,
ImagePersonEntity::class, ImageEventEntity::class,
ImageEventEntity::class
],
version = 1,
exportSchema = true
)
// ===== NEW ENTITIES =====
PersonEntity::class, // NEW: People
FaceModelEntity::class, // NEW: Face embeddings
PhotoFaceTagEntity::class // NEW: Face tags
],
version = 5,
exportSchema = false
)
// No TypeConverters needed - embeddings stored as strings
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
// ===== YOUR EXISTING DAOs =====
abstract fun imageDao(): ImageDao abstract fun imageDao(): ImageDao
abstract fun tagDao(): TagDao abstract fun tagDao(): TagDao
abstract fun personDao(): PersonDao
abstract fun eventDao(): EventDao abstract fun eventDao(): EventDao
abstract fun imageTagDao(): ImageTagDao abstract fun imageTagDao(): ImageTagDao
abstract fun imagePersonDao(): ImagePersonDao
abstract fun imageEventDao(): ImageEventDao abstract fun imageEventDao(): ImageEventDao
abstract fun imageAggregateDao(): ImageAggregateDao abstract fun imageAggregateDao(): ImageAggregateDao
// ===== NEW DAOs =====
abstract fun personDao(): PersonDao // NEW: Manage people
abstract fun faceModelDao(): FaceModelDao // NEW: Manage face embeddings
abstract fun photoFaceTagDao(): PhotoFaceTagDao // NEW: Manage face tags
} }

View File

@@ -0,0 +1,44 @@
package com.placeholder.sherpai2.data.local.dao
import androidx.room.*
import kotlinx.coroutines.flow.Flow
import com.placeholder.sherpai2.data.local.entity.FaceModelEntity
/**
* FaceModelDao - Manages face recognition models
*
* PRIMARY KEY TYPE: String (UUID)
* FOREIGN KEY: personId (String)
*/
@Dao
interface FaceModelDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertFaceModel(faceModel: FaceModelEntity): Long // Row ID
@Update
suspend fun updateFaceModel(faceModel: FaceModelEntity)
@Query("UPDATE face_models SET lastUsed = :timestamp WHERE id = :faceModelId")
suspend fun updateLastUsed(faceModelId: String, timestamp: Long)
@Query("SELECT * FROM face_models WHERE id = :faceModelId")
suspend fun getFaceModelById(faceModelId: String): FaceModelEntity?
@Query("SELECT * FROM face_models WHERE personId = :personId AND isActive = 1")
suspend fun getFaceModelByPersonId(personId: String): FaceModelEntity?
@Query("SELECT * FROM face_models WHERE isActive = 1 ORDER BY lastUsed DESC")
suspend fun getAllActiveFaceModels(): List<FaceModelEntity>
@Query("SELECT * FROM face_models WHERE isActive = 1 ORDER BY lastUsed DESC")
fun getAllActiveFaceModelsFlow(): Flow<List<FaceModelEntity>>
@Query("DELETE FROM face_models WHERE id = :faceModelId")
suspend fun deleteFaceModelById(faceModelId: String)
@Query("UPDATE face_models SET isActive = 0 WHERE id = :faceModelId")
suspend fun deactivateFaceModel(faceModelId: String)
@Query("SELECT COUNT(*) FROM face_models WHERE isActive = 1")
suspend fun getActiveFaceModelCount(): Int
}

View File

@@ -65,4 +65,11 @@ interface ImageDao {
@Insert(onConflict = OnConflictStrategy.IGNORE) @Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(image: ImageEntity) suspend fun insert(image: ImageEntity)
}
/**
* Get images by list of IDs.
* FIXED: Changed from List<Long> to List<String> to match ImageEntity.imageId type
*/
@Query("SELECT * FROM images WHERE imageId IN (:imageIds)")
suspend fun getImagesByIds(imageIds: List<String>): List<ImageEntity>
}

View File

@@ -1,25 +0,0 @@
package com.placeholder.sherpai2.data.local.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.placeholder.sherpai2.data.local.entity.ImagePersonEntity
@Dao
interface ImagePersonDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun upsert(entity: ImagePersonEntity)
/**
* All images containing a specific person.
*/
@Query("""
SELECT imageId FROM image_persons
WHERE personId = :personId
AND visibility = 'PUBLIC'
AND confirmed = 1
""")
suspend fun findImagesForPerson(personId: String): List<String>
}

View File

@@ -15,9 +15,6 @@ interface ImageTagDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun upsert(imageTag: ImageTagEntity) suspend fun upsert(imageTag: ImageTagEntity)
/**
* Observe tags for an image.
*/
@Query(""" @Query("""
SELECT * FROM image_tags SELECT * FROM image_tags
WHERE imageId = :imageId WHERE imageId = :imageId
@@ -26,9 +23,7 @@ interface ImageTagDao {
fun observeTagsForImage(imageId: String): Flow<List<ImageTagEntity>> fun observeTagsForImage(imageId: String): Flow<List<ImageTagEntity>>
/** /**
* Find images by tag. * FIXED: Removed default parameter
*
* This is your primary tag-search query.
*/ */
@Query(""" @Query("""
SELECT imageId FROM image_tags SELECT imageId FROM image_tags
@@ -38,7 +33,7 @@ interface ImageTagDao {
""") """)
suspend fun findImagesByTag( suspend fun findImagesByTag(
tagId: String, tagId: String,
minConfidence: Float = 0.5f minConfidence: Float
): List<String> ): List<String>
@Transaction @Transaction
@@ -49,5 +44,4 @@ interface ImageTagDao {
WHERE it.imageId = :imageId AND it.visibility = 'PUBLIC' WHERE it.imageId = :imageId AND it.visibility = 'PUBLIC'
""") """)
fun getTagsForImage(imageId: String): Flow<List<TagEntity>> fun getTagsForImage(imageId: String): Flow<List<TagEntity>>
}
}

View File

@@ -1,24 +1,51 @@
package com.placeholder.sherpai2.data.local.dao package com.placeholder.sherpai2.data.local.dao
import androidx.room.Dao import androidx.room.*
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.placeholder.sherpai2.data.local.entity.PersonEntity import com.placeholder.sherpai2.data.local.entity.PersonEntity
import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface PersonDao { interface PersonDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(person: PersonEntity) suspend fun insert(person: PersonEntity): Long
@Query("SELECT * FROM persons WHERE personId = :personId") @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun getById(personId: String): PersonEntity? suspend fun insertAll(persons: List<PersonEntity>)
@Query(""" @Update
SELECT * FROM persons suspend fun update(person: PersonEntity)
WHERE isHidden = 0
ORDER BY displayName /**
""") * FIXED: Removed default parameter
suspend fun getVisiblePeople(): List<PersonEntity> */
@Query("UPDATE persons SET updatedAt = :timestamp WHERE id = :personId")
suspend fun updateTimestamp(personId: String, timestamp: Long)
@Delete
suspend fun delete(person: PersonEntity)
@Query("DELETE FROM persons WHERE id = :personId")
suspend fun deleteById(personId: String)
@Query("SELECT * FROM persons WHERE id = :personId")
suspend fun getPersonById(personId: String): PersonEntity?
@Query("SELECT * FROM persons WHERE id IN (:personIds)")
suspend fun getPersonsByIds(personIds: List<String>): List<PersonEntity>
@Query("SELECT * FROM persons ORDER BY name ASC")
suspend fun getAllPersons(): List<PersonEntity>
@Query("SELECT * FROM persons ORDER BY name ASC")
fun getAllPersonsFlow(): Flow<List<PersonEntity>>
@Query("SELECT * FROM persons WHERE name LIKE '%' || :query || '%' ORDER BY name ASC")
suspend fun searchByName(query: String): List<PersonEntity>
@Query("SELECT COUNT(*) FROM persons")
suspend fun getPersonCount(): Int
@Query("SELECT EXISTS(SELECT 1 FROM persons WHERE id = :personId)")
suspend fun personExists(personId: String): Boolean
} }

View File

@@ -0,0 +1,91 @@
package com.placeholder.sherpai2.data.local.dao
import androidx.room.*
import kotlinx.coroutines.flow.Flow
import com.placeholder.sherpai2.data.local.entity.PhotoFaceTagEntity
@Dao
interface PhotoFaceTagDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertTag(tag: PhotoFaceTagEntity): Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertTags(tags: List<PhotoFaceTagEntity>)
@Update
suspend fun updateTag(tag: PhotoFaceTagEntity)
/**
* FIXED: Removed default parameter
*/
@Query("UPDATE photo_face_tags SET verifiedByUser = 1, verifiedAt = :timestamp WHERE id = :tagId")
suspend fun markTagAsVerified(tagId: String, timestamp: Long)
// ===== QUERY BY IMAGE =====
@Query("SELECT * FROM photo_face_tags WHERE imageId = :imageId")
suspend fun getTagsForImage(imageId: String): List<PhotoFaceTagEntity>
@Query("SELECT COUNT(*) FROM photo_face_tags WHERE imageId = :imageId")
suspend fun getFaceCountForImage(imageId: String): Int
@Query("SELECT EXISTS(SELECT 1 FROM photo_face_tags WHERE imageId = :imageId AND faceModelId = :faceModelId)")
suspend fun imageHasPerson(imageId: String, faceModelId: String): Boolean
// ===== QUERY BY FACE MODEL =====
@Query("SELECT DISTINCT imageId FROM photo_face_tags WHERE faceModelId = :faceModelId ORDER BY detectedAt DESC")
suspend fun getImageIdsForFaceModel(faceModelId: String): List<String>
@Query("SELECT DISTINCT imageId FROM photo_face_tags WHERE faceModelId = :faceModelId ORDER BY detectedAt DESC")
fun getImageIdsForFaceModelFlow(faceModelId: String): Flow<List<String>>
@Query("SELECT faceModelId, COUNT(DISTINCT imageId) as photoCount FROM photo_face_tags GROUP BY faceModelId")
suspend fun getPhotoCountPerFaceModel(): List<FaceModelPhotoCount>
@Query("SELECT * FROM photo_face_tags WHERE faceModelId = :faceModelId ORDER BY detectedAt DESC")
suspend fun getAllTagsForFaceModel(faceModelId: String): List<PhotoFaceTagEntity>
// ===== DELETE =====
@Delete
suspend fun deleteTag(tag: PhotoFaceTagEntity)
@Query("DELETE FROM photo_face_tags WHERE id = :tagId")
suspend fun deleteTagById(tagId: String)
@Query("DELETE FROM photo_face_tags WHERE faceModelId = :faceModelId")
suspend fun deleteTagsForFaceModel(faceModelId: String)
@Query("DELETE FROM photo_face_tags WHERE imageId = :imageId")
suspend fun deleteTagsForImage(imageId: String)
// ===== STATISTICS =====
/**
* FIXED: Removed default parameter
*/
@Query("SELECT * FROM photo_face_tags WHERE confidence < :threshold ORDER BY confidence ASC")
suspend fun getLowConfidenceTags(threshold: Float): List<PhotoFaceTagEntity>
@Query("SELECT * FROM photo_face_tags WHERE verifiedByUser = 0 ORDER BY detectedAt DESC")
suspend fun getUnverifiedTags(): List<PhotoFaceTagEntity>
@Query("SELECT COUNT(*) FROM photo_face_tags WHERE verifiedByUser = 0")
suspend fun getUnverifiedTagCount(): Int
@Query("SELECT AVG(confidence) FROM photo_face_tags WHERE faceModelId = :faceModelId")
suspend fun getAverageConfidenceForFaceModel(faceModelId: String): Float?
/**
* FIXED: Removed default parameter
*/
@Query("SELECT * FROM photo_face_tags ORDER BY detectedAt DESC LIMIT :limit")
suspend fun getRecentlyDetectedFaces(limit: Int): List<PhotoFaceTagEntity>
}
data class FaceModelPhotoCount(
val faceModelId: String,
val photoCount: Int
)

View File

@@ -4,21 +4,218 @@ import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.OnConflictStrategy import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import com.placeholder.sherpai2.data.local.entity.ImageEntity
import com.placeholder.sherpai2.data.local.entity.TagEntity import com.placeholder.sherpai2.data.local.entity.TagEntity
import com.placeholder.sherpai2.data.local.entity.TagWithUsage
import kotlinx.coroutines.flow.Flow
/**
* TagDao - Tag management with face recognition integration
*
* NO DEFAULT PARAMETERS - Room doesn't support them in @Query methods
*/
@Dao @Dao
interface TagDao { interface TagDao {
@Insert(onConflict = OnConflictStrategy.IGNORE) // ======================
suspend fun insert(tag: TagEntity) // BASIC OPERATIONS
// ======================
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(tag: TagEntity): Long
/**
* Resolve a tag by value.
* Example: "park"
*/
@Query("SELECT * FROM tags WHERE value = :value LIMIT 1") @Query("SELECT * FROM tags WHERE value = :value LIMIT 1")
suspend fun getByValue(value: String): TagEntity? suspend fun getByValue(value: String): TagEntity?
@Query("SELECT * FROM tags") @Query("SELECT * FROM tags WHERE tagId = :tagId")
suspend fun getById(tagId: String): TagEntity?
@Query("SELECT * FROM tags ORDER BY value ASC")
suspend fun getAll(): List<TagEntity> suspend fun getAll(): List<TagEntity>
}
@Query("SELECT * FROM tags ORDER BY value ASC")
fun getAllFlow(): Flow<List<TagEntity>>
@Query("SELECT * FROM tags WHERE type = :type ORDER BY value ASC")
suspend fun getByType(type: String): List<TagEntity>
@Query("DELETE FROM tags WHERE tagId = :tagId")
suspend fun delete(tagId: String)
// ======================
// STATISTICS (returns TagWithUsage)
// ======================
/**
* Get most used tags WITH usage counts
*
* @param limit Maximum number of tags to return
*/
@Query("""
SELECT t.tagId, t.type, t.value, t.createdAt,
COUNT(it.imageId) as usage_count
FROM tags t
LEFT JOIN image_tags it ON t.tagId = it.tagId
GROUP BY t.tagId
ORDER BY usage_count DESC
LIMIT :limit
""")
suspend fun getMostUsedTags(limit: Int): List<TagWithUsage>
/**
* Get tag usage count
*/
@Query("""
SELECT COUNT(DISTINCT it.imageId)
FROM image_tags it
WHERE it.tagId = :tagId
""")
suspend fun getTagUsageCount(tagId: String): Int
// ======================
// PERSON INTEGRATION
// ======================
/**
* Get all tags used for images containing a specific person
*/
@Query("""
SELECT DISTINCT t.* FROM tags t
INNER JOIN image_tags it ON t.tagId = it.tagId
INNER JOIN photo_face_tags pft ON it.imageId = pft.imageId
INNER JOIN face_models fm ON pft.faceModelId = fm.id
WHERE fm.personId = :personId
ORDER BY t.value ASC
""")
suspend fun getTagsForPerson(personId: String): List<TagEntity>
/**
* Get images that have both a specific tag AND contain a specific person
*/
@Query("""
SELECT DISTINCT i.* FROM images i
INNER JOIN image_tags it ON i.imageId = it.imageId
INNER JOIN photo_face_tags pft ON i.imageId = pft.imageId
INNER JOIN face_models fm ON pft.faceModelId = fm.id
WHERE it.tagId = :tagId AND fm.personId = :personId
ORDER BY i.capturedAt DESC
""")
suspend fun getImagesWithTagAndPerson(
tagId: String,
personId: String
): List<ImageEntity>
/**
* Get images with tag and person as Flow
*/
@Query("""
SELECT DISTINCT i.* FROM images i
INNER JOIN image_tags it ON i.imageId = it.imageId
INNER JOIN photo_face_tags pft ON i.imageId = pft.imageId
INNER JOIN face_models fm ON pft.faceModelId = fm.id
WHERE it.tagId = :tagId AND fm.personId = :personId
ORDER BY i.capturedAt DESC
""")
fun getImagesWithTagAndPersonFlow(
tagId: String,
personId: String
): Flow<List<ImageEntity>>
/**
* Count images with tag and person
*/
@Query("""
SELECT COUNT(DISTINCT i.imageId) FROM images i
INNER JOIN image_tags it ON i.imageId = it.imageId
INNER JOIN photo_face_tags pft ON i.imageId = pft.imageId
INNER JOIN face_models fm ON pft.faceModelId = fm.id
WHERE it.tagId = :tagId AND fm.personId = :personId
""")
suspend fun countImagesWithTagAndPerson(
tagId: String,
personId: String
): Int
// ======================
// AUTO-SUGGESTIONS
// ======================
/**
* Suggest tags based on person's relationship
*
* @param limit Maximum number of suggestions
*/
@Query("""
SELECT DISTINCT t.* FROM tags t
INNER JOIN image_tags it ON t.tagId = it.tagId
INNER JOIN photo_face_tags pft ON it.imageId = pft.imageId
INNER JOIN face_models fm ON pft.faceModelId = fm.id
INNER JOIN persons p ON fm.personId = p.id
WHERE p.relationship = :relationship
AND p.id != :excludePersonId
GROUP BY t.tagId
ORDER BY COUNT(it.imageId) DESC
LIMIT :limit
""")
suspend fun suggestTagsBasedOnRelationship(
relationship: String,
excludePersonId: String,
limit: Int
): List<TagEntity>
/**
* Get tags commonly used with this tag
*
* @param limit Maximum number of related tags
*/
@Query("""
SELECT DISTINCT t2.* FROM tags t2
INNER JOIN image_tags it2 ON t2.tagId = it2.tagId
WHERE it2.imageId IN (
SELECT it1.imageId FROM image_tags it1
WHERE it1.tagId = :tagId
)
AND t2.tagId != :tagId
GROUP BY t2.tagId
ORDER BY COUNT(it2.imageId) DESC
LIMIT :limit
""")
suspend fun getRelatedTags(
tagId: String,
limit: Int
): List<TagEntity>
// ======================
// SEARCH
// ======================
/**
* Search tags by value (partial match)
*
* @param limit Maximum number of results
*/
@Query("""
SELECT * FROM tags
WHERE value LIKE '%' || :query || '%'
ORDER BY value ASC
LIMIT :limit
""")
suspend fun searchTags(query: String, limit: Int): List<TagEntity>
/**
* Search tags with usage count
*
* @param limit Maximum number of results
*/
@Query("""
SELECT t.tagId, t.type, t.value, t.createdAt,
COUNT(it.imageId) as usage_count
FROM tags t
LEFT JOIN image_tags it ON t.tagId = it.tagId
WHERE t.value LIKE '%' || :query || '%'
GROUP BY t.tagId
ORDER BY usage_count DESC, t.value ASC
LIMIT :limit
""")
suspend fun searchTagsWithUsage(query: String, limit: Int): List<TagWithUsage>
}

View File

@@ -0,0 +1,231 @@
package com.placeholder.sherpai2.data.local.entity
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import java.util.UUID
/**
* PersonEntity - NO DEFAULT VALUES for KSP compatibility
*/
@Entity(
tableName = "persons",
indices = [Index(value = ["name"])]
)
data class PersonEntity(
@PrimaryKey
@ColumnInfo(name = "id")
val id: String, // ← No default
@ColumnInfo(name = "name")
val name: String,
@ColumnInfo(name = "dateOfBirth")
val dateOfBirth: Long?,
@ColumnInfo(name = "relationship")
val relationship: String?,
@ColumnInfo(name = "createdAt")
val createdAt: Long, // ← No default
@ColumnInfo(name = "updatedAt")
val updatedAt: Long // ← No default
) {
companion object {
fun create(
name: String,
dateOfBirth: Long? = null,
relationship: String? = null
): PersonEntity {
val now = System.currentTimeMillis()
return PersonEntity(
id = UUID.randomUUID().toString(),
name = name,
dateOfBirth = dateOfBirth,
relationship = relationship,
createdAt = now,
updatedAt = now
)
}
}
fun getAge(): Int? {
if (dateOfBirth == null) return null
val now = System.currentTimeMillis()
val ageInMillis = now - dateOfBirth
return (ageInMillis / (1000L * 60 * 60 * 24 * 365)).toInt()
}
fun getRelationshipEmoji(): String {
return when (relationship) {
"Family" -> "👨‍👩‍👧‍👦"
"Friend" -> "🤝"
"Partner" -> "❤️"
"Child" -> "👶"
"Parent" -> "👪"
"Sibling" -> "👫"
"Colleague" -> "💼"
else -> "👤"
}
}
}
/**
* FaceModelEntity - NO DEFAULT VALUES
*/
@Entity(
tableName = "face_models",
foreignKeys = [
ForeignKey(
entity = PersonEntity::class,
parentColumns = ["id"],
childColumns = ["personId"],
onDelete = ForeignKey.CASCADE
)
],
indices = [Index(value = ["personId"], unique = true)]
)
data class FaceModelEntity(
@PrimaryKey
@ColumnInfo(name = "id")
val id: String, // ← No default
@ColumnInfo(name = "personId")
val personId: String,
@ColumnInfo(name = "embedding")
val embedding: String,
@ColumnInfo(name = "trainingImageCount")
val trainingImageCount: Int,
@ColumnInfo(name = "averageConfidence")
val averageConfidence: Float,
@ColumnInfo(name = "createdAt")
val createdAt: Long, // ← No default
@ColumnInfo(name = "updatedAt")
val updatedAt: Long, // ← No default
@ColumnInfo(name = "lastUsed")
val lastUsed: Long?,
@ColumnInfo(name = "isActive")
val isActive: Boolean
) {
companion object {
fun create(
personId: String,
embeddingArray: FloatArray,
trainingImageCount: Int,
averageConfidence: Float
): FaceModelEntity {
val now = System.currentTimeMillis()
return FaceModelEntity(
id = UUID.randomUUID().toString(),
personId = personId,
embedding = embeddingArray.joinToString(","),
trainingImageCount = trainingImageCount,
averageConfidence = averageConfidence,
createdAt = now,
updatedAt = now,
lastUsed = null,
isActive = true
)
}
}
fun getEmbeddingArray(): FloatArray {
return embedding.split(",").map { it.toFloat() }.toFloatArray()
}
}
/**
* PhotoFaceTagEntity - NO DEFAULT VALUES
*/
@Entity(
tableName = "photo_face_tags",
foreignKeys = [
ForeignKey(
entity = ImageEntity::class,
parentColumns = ["imageId"],
childColumns = ["imageId"],
onDelete = ForeignKey.CASCADE
),
ForeignKey(
entity = FaceModelEntity::class,
parentColumns = ["id"],
childColumns = ["faceModelId"],
onDelete = ForeignKey.CASCADE
)
],
indices = [
Index(value = ["imageId"]),
Index(value = ["faceModelId"]),
Index(value = ["imageId", "faceModelId"])
]
)
data class PhotoFaceTagEntity(
@PrimaryKey
@ColumnInfo(name = "id")
val id: String, // ← No default
@ColumnInfo(name = "imageId")
val imageId: String,
@ColumnInfo(name = "faceModelId")
val faceModelId: String,
@ColumnInfo(name = "boundingBox")
val boundingBox: String,
@ColumnInfo(name = "confidence")
val confidence: Float,
@ColumnInfo(name = "embedding")
val embedding: String,
@ColumnInfo(name = "detectedAt")
val detectedAt: Long, // ← No default
@ColumnInfo(name = "verifiedByUser")
val verifiedByUser: Boolean,
@ColumnInfo(name = "verifiedAt")
val verifiedAt: Long?
) {
companion object {
fun create(
imageId: String,
faceModelId: String,
boundingBox: android.graphics.Rect,
confidence: Float,
faceEmbedding: FloatArray
): PhotoFaceTagEntity {
return PhotoFaceTagEntity(
id = UUID.randomUUID().toString(),
imageId = imageId,
faceModelId = faceModelId,
boundingBox = "${boundingBox.left},${boundingBox.top},${boundingBox.right},${boundingBox.bottom}",
confidence = confidence,
embedding = faceEmbedding.joinToString(","),
detectedAt = System.currentTimeMillis(),
verifiedByUser = false,
verifiedAt = null
)
}
}
fun getBoundingBox(): android.graphics.Rect {
val parts = boundingBox.split(",").map { it.toInt() }
return android.graphics.Rect(parts[0], parts[1], parts[2], parts[3])
}
fun getEmbeddingArray(): FloatArray {
return embedding.split(",").map { it.toFloat() }.toFloatArray()
}
}

View File

@@ -1,40 +0,0 @@
package com.placeholder.sherpai2.data.local.entity
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
@Entity(
tableName = "image_persons",
primaryKeys = ["imageId", "personId"],
foreignKeys = [
ForeignKey(
entity = ImageEntity::class,
parentColumns = ["imageId"],
childColumns = ["imageId"],
onDelete = ForeignKey.CASCADE
),
ForeignKey(
entity = PersonEntity::class,
parentColumns = ["personId"],
childColumns = ["personId"],
onDelete = ForeignKey.CASCADE
)
],
indices = [
Index("personId")
]
)
data class ImagePersonEntity(
val imageId: String,
val personId: String,
val confidence: Float,
val confirmed: Boolean,
/**
* PUBLIC | PRIVATE
*/
val visibility: String
)

View File

@@ -1,30 +0,0 @@
package com.placeholder.sherpai2.data.local.entity
import androidx.room.Entity
import androidx.room.PrimaryKey
/**
* Represents a known person.
*
* People are separate from generic tags because:
* - face embeddings
* - privacy rules
* - identity merging
*/
@Entity(tableName = "persons")
data class PersonEntity(
@PrimaryKey
val personId: String,
val displayName: String,
/**
* Reference to face embedding storage (ML layer).
*/
val faceEmbeddingId: String?,
val isHidden: Boolean,
val createdAt: Long
)

View File

@@ -1,30 +1,143 @@
package com.placeholder.sherpai2.data.local.entity package com.placeholder.sherpai2.data.local.entity
import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import java.util.UUID
/** /**
* Represents a conceptual tag. * Tag type constants - MUST be defined BEFORE TagEntity
* to avoid KSP initialization order issues
*/
object TagType {
const val GENERIC = "GENERIC" // User tags
const val SYSTEM = "SYSTEM" // AI/auto tags
const val HIDDEN = "HIDDEN" // Internal
}
/**
* Common system tag values
*/
object SystemTags {
const val HAS_FACES = "has_faces"
const val MULTIPLE_PEOPLE = "multiple_people"
const val LANDSCAPE = "landscape"
const val PORTRAIT = "portrait"
const val LOW_QUALITY = "low_quality"
const val BLURRY = "blurry"
}
/**
* TagEntity - Normalized tag storage
* *
* Tags are normalized so that: * EXPLICIT COLUMN MAPPINGS for KSP compatibility
* - "park" exists once
* - many images can reference it
*/ */
@Entity(tableName = "tags") @Entity(tableName = "tags")
data class TagEntity( data class TagEntity(
@PrimaryKey @PrimaryKey
@ColumnInfo(name = "tagId")
val tagId: String, val tagId: String,
/** @ColumnInfo(name = "type")
* GENERIC | SYSTEM | HIDDEN
*/
val type: String, val type: String,
/** @ColumnInfo(name = "value")
* Human-readable value, e.g. "park", "sunset"
*/
val value: String, val value: String,
@ColumnInfo(name = "createdAt")
val createdAt: Long val createdAt: Long
) ) {
companion object {
/**
* Create a generic user tag
*/
fun createUserTag(value: String): TagEntity {
return TagEntity(
tagId = UUID.randomUUID().toString(),
type = TagType.GENERIC,
value = value.trim().lowercase(),
createdAt = System.currentTimeMillis()
)
}
/**
* Create a system tag (auto-generated)
*/
fun createSystemTag(value: String): TagEntity {
return TagEntity(
tagId = UUID.randomUUID().toString(),
type = TagType.SYSTEM,
value = value.trim().lowercase(),
createdAt = System.currentTimeMillis()
)
}
/**
* Create hidden tag (internal use)
*/
fun createHiddenTag(value: String): TagEntity {
return TagEntity(
tagId = UUID.randomUUID().toString(),
type = TagType.HIDDEN,
value = value.trim().lowercase(),
createdAt = System.currentTimeMillis()
)
}
}
/**
* Check if this is a user-created tag
*/
fun isUserTag(): Boolean = type == TagType.GENERIC
/**
* Check if this is a system tag
*/
fun isSystemTag(): Boolean = type == TagType.SYSTEM
/**
* Check if this is a hidden tag
*/
fun isHiddenTag(): Boolean = type == TagType.HIDDEN
/**
* Get display value (capitalized for UI)
*/
fun getDisplayValue(): String = value.replaceFirstChar { it.uppercase() }
}
/**
* TagWithUsage - For queries that include usage count
*
* NOT AN ENTITY - just a POJO for query results
* Do NOT add this to @Database entities list!
*/
data class TagWithUsage(
@ColumnInfo(name = "tagId")
val tagId: String,
@ColumnInfo(name = "type")
val type: String,
@ColumnInfo(name = "value")
val value: String,
@ColumnInfo(name = "createdAt")
val createdAt: Long,
@ColumnInfo(name = "usage_count")
val usageCount: Int
) {
/**
* Convert to TagEntity (without usage count)
*/
fun toTagEntity(): TagEntity {
return TagEntity(
tagId = tagId,
type = type,
value = value,
createdAt = createdAt
)
}
}

View File

@@ -15,12 +15,6 @@ data class ImageWithEverything(
) )
val tags: List<ImageTagEntity>, val tags: List<ImageTagEntity>,
@Relation(
parentColumn = "imageId",
entityColumn = "imageId"
)
val persons: List<ImagePersonEntity>,
@Relation( @Relation(
parentColumn = "imageId", parentColumn = "imageId",
entityColumn = "imageId" entityColumn = "imageId"

View File

@@ -0,0 +1,400 @@
package com.placeholder.sherpai2.data.repository
import android.content.Context
import android.graphics.Bitmap
import com.placeholder.sherpai2.data.local.dao.FaceModelDao
import com.placeholder.sherpai2.data.local.dao.ImageDao
import com.placeholder.sherpai2.data.local.dao.PersonDao
import com.placeholder.sherpai2.data.local.dao.PhotoFaceTagDao
import com.placeholder.sherpai2.data.local.entity.*
import com.placeholder.sherpai2.ml.FaceNetModel
import com.placeholder.sherpai2.ui.trainingprep.TrainingSanityChecker
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton
/**
* FaceRecognitionRepository - Complete face recognition system
*
* USES STRING IDs TO MATCH YOUR SCHEMA:
* - PersonEntity.id: String (UUID)
* - ImageEntity.imageId: String
* - FaceModelEntity.id: String (UUID)
* - PhotoFaceTagEntity.id: String (UUID)
*/
@Singleton
class FaceRecognitionRepository @Inject constructor(
private val context: Context,
private val personDao: PersonDao,
private val imageDao: ImageDao,
private val faceModelDao: FaceModelDao,
private val photoFaceTagDao: PhotoFaceTagDao
) {
private val faceNetModel by lazy { FaceNetModel(context) }
// ======================
// TRAINING OPERATIONS
// ======================
/**
* Create a new person with face model in one operation.
*
* @return PersonId (String UUID)
*/
suspend fun createPersonWithFaceModel(
personName: String,
validImages: List<TrainingSanityChecker.ValidTrainingImage>,
onProgress: (Int, Int) -> Unit = { _, _ -> }
): String = withContext(Dispatchers.IO) {
// Create PersonEntity with UUID
val person = PersonEntity.create(name = personName)
personDao.insert(person)
// Train face model
trainPerson(
personId = person.id,
validImages = validImages,
onProgress = onProgress
)
person.id
}
/**
* Train a face recognition model for an existing person.
*
* @param personId String UUID
* @return Face model ID (String UUID)
*/
suspend fun trainPerson(
personId: String,
validImages: List<TrainingSanityChecker.ValidTrainingImage>,
onProgress: (Int, Int) -> Unit = { _, _ -> }
): String = withContext(Dispatchers.Default) {
val person = personDao.getPersonById(personId)
?: throw IllegalArgumentException("Person with ID $personId not found")
val embeddings = faceNetModel.generateEmbeddingsBatch(
faceBitmaps = validImages.map { it.croppedFaceBitmap },
onProgress = onProgress
)
val personEmbedding = faceNetModel.createPersonModel(embeddings)
val confidences = embeddings.map { embedding ->
faceNetModel.calculateSimilarity(personEmbedding, embedding)
}
val avgConfidence = confidences.average().toFloat()
val faceModel = FaceModelEntity.create(
personId = personId,
embeddingArray = personEmbedding,
trainingImageCount = validImages.size,
averageConfidence = avgConfidence
)
faceModelDao.insertFaceModel(faceModel)
faceModel.id
}
/**
* Retrain face model with additional images.
*/
suspend fun retrainFaceModel(
faceModelId: String,
newFaceImages: List<Bitmap>
) = withContext(Dispatchers.Default) {
val faceModel = faceModelDao.getFaceModelById(faceModelId)
?: throw IllegalArgumentException("Face model $faceModelId not found")
val existingEmbedding = faceModel.getEmbeddingArray()
val newEmbeddings = faceNetModel.generateEmbeddingsBatch(newFaceImages)
val allEmbeddings = listOf(existingEmbedding) + newEmbeddings
val updatedEmbedding = faceNetModel.createPersonModel(allEmbeddings)
val confidences = allEmbeddings.map { embedding ->
faceNetModel.calculateSimilarity(updatedEmbedding, embedding)
}
val avgConfidence = confidences.average().toFloat()
faceModelDao.updateFaceModel(
FaceModelEntity.create(
personId = faceModel.personId,
embeddingArray = updatedEmbedding,
trainingImageCount = faceModel.trainingImageCount + newFaceImages.size,
averageConfidence = avgConfidence
).copy(
id = faceModelId,
createdAt = faceModel.createdAt,
updatedAt = System.currentTimeMillis()
)
)
}
// ======================
// SCANNING / RECOGNITION
// ======================
/**
* Scan an image for faces and tag recognized persons.
*
* @param imageId String (from ImageEntity.imageId)
*/
suspend fun scanImage(
imageId: String,
detectedFaces: List<DetectedFace>,
threshold: Float = FaceNetModel.SIMILARITY_THRESHOLD_HIGH
): List<PhotoFaceTagEntity> = withContext(Dispatchers.Default) {
val faceModels = faceModelDao.getAllActiveFaceModels()
if (faceModels.isEmpty()) {
return@withContext emptyList()
}
val tags = mutableListOf<PhotoFaceTagEntity>()
for (detectedFace in detectedFaces) {
val faceEmbedding = faceNetModel.generateEmbedding(detectedFace.croppedBitmap)
var bestMatch: Pair<String, Float>? = null
var highestSimilarity = threshold
for (faceModel in faceModels) {
val modelEmbedding = faceModel.getEmbeddingArray()
val similarity = faceNetModel.calculateSimilarity(faceEmbedding, modelEmbedding)
if (similarity > highestSimilarity) {
highestSimilarity = similarity
bestMatch = Pair(faceModel.id, similarity)
}
}
if (bestMatch != null) {
val (faceModelId, confidence) = bestMatch
val tag = PhotoFaceTagEntity.create(
imageId = imageId,
faceModelId = faceModelId,
boundingBox = detectedFace.boundingBox,
confidence = confidence,
faceEmbedding = faceEmbedding
)
tags.add(tag)
faceModelDao.updateLastUsed(faceModelId, System.currentTimeMillis())
}
}
if (tags.isNotEmpty()) {
photoFaceTagDao.insertTags(tags)
}
tags
}
/**
* Recognize a single face bitmap (without saving).
*/
suspend fun recognizeFace(
faceBitmap: Bitmap,
threshold: Float = FaceNetModel.SIMILARITY_THRESHOLD_HIGH
): Pair<String, Float>? = withContext(Dispatchers.Default) {
val faceEmbedding = faceNetModel.generateEmbedding(faceBitmap)
val faceModels = faceModelDao.getAllActiveFaceModels()
val modelEmbeddings = faceModels.map { it.id to it.getEmbeddingArray() }
faceNetModel.findBestMatch(faceEmbedding, modelEmbeddings, threshold)
}
// ======================
// SEARCH / QUERY
// ======================
/**
* Get all images containing a specific person.
*
* @param personId String UUID
*/
suspend fun getImagesForPerson(personId: String): List<ImageEntity> = withContext(Dispatchers.IO) {
val faceModel = faceModelDao.getFaceModelByPersonId(personId)
?: return@withContext emptyList()
val imageIds = photoFaceTagDao.getImageIdsForFaceModel(faceModel.id)
imageDao.getImagesByIds(imageIds)
}
/**
* Get images for person as Flow (reactive).
*/
fun getImagesForPersonFlow(personId: String): Flow<List<ImageEntity>> {
return photoFaceTagDao.getImageIdsForFaceModelFlow(personId)
.map { imageIds ->
imageDao.getImagesByIds(imageIds)
}
}
/**
* Get all persons with face models.
*/
suspend fun getPersonsWithFaceModels(): List<PersonEntity> = withContext(Dispatchers.IO) {
val faceModels = faceModelDao.getAllActiveFaceModels()
val personIds = faceModels.map { it.personId }
personDao.getPersonsByIds(personIds)
}
/**
* Get face detection stats for a person.
*/
suspend fun getPersonFaceStats(personId: String): PersonFaceStats? = withContext(Dispatchers.IO) {
val person = personDao.getPersonById(personId) ?: return@withContext null
val faceModel = faceModelDao.getFaceModelByPersonId(personId) ?: return@withContext null
val imageIds = photoFaceTagDao.getImageIdsForFaceModel(faceModel.id)
val allTags = photoFaceTagDao.getAllTagsForFaceModel(faceModel.id)
val avgConfidence = if (allTags.isNotEmpty()) {
allTags.map { it.confidence }.average().toFloat()
} else {
0f
}
val lastDetected = allTags.maxOfOrNull { it.detectedAt }
PersonFaceStats(
personId = person.id,
personName = person.name,
faceModelId = faceModel.id,
trainingImageCount = faceModel.trainingImageCount,
taggedPhotoCount = imageIds.size,
averageConfidence = avgConfidence,
lastDetectedAt = lastDetected
)
}
/**
* Get face tags for an image.
*/
suspend fun getFaceTagsForImage(imageId: String): List<PhotoFaceTagEntity> {
return photoFaceTagDao.getTagsForImage(imageId)
}
/**
* Get person from a face tag.
*/
suspend fun getPersonForFaceTag(tag: PhotoFaceTagEntity): PersonEntity? = withContext(Dispatchers.IO) {
val faceModel = faceModelDao.getFaceModelById(tag.faceModelId) ?: return@withContext null
personDao.getPersonById(faceModel.personId)
}
/**
* Get face tags with person info for an image.
*/
suspend fun getFaceTagsWithPersons(imageId: String): List<Pair<PhotoFaceTagEntity, PersonEntity>> = withContext(Dispatchers.IO) {
val tags = photoFaceTagDao.getTagsForImage(imageId)
tags.mapNotNull { tag ->
val person = getPersonForFaceTag(tag)
if (person != null) tag to person else null
}
}
// ======================
// VERIFICATION / QUALITY
// ======================
suspend fun verifyFaceTag(tagId: String) {
photoFaceTagDao.markTagAsVerified(
tagId = tagId,
timestamp = System.currentTimeMillis()
)
}
suspend fun getUnverifiedTags(): List<PhotoFaceTagEntity> {
return photoFaceTagDao.getUnverifiedTags()
}
suspend fun getLowConfidenceTags(threshold: Float = 0.7f): List<PhotoFaceTagEntity> {
return photoFaceTagDao.getLowConfidenceTags(threshold)
}
// ======================
// MANAGEMENT
// ======================
suspend fun deleteFaceModel(faceModelId: String) = withContext(Dispatchers.IO) {
photoFaceTagDao.deleteTagsForFaceModel(faceModelId)
faceModelDao.deleteFaceModelById(faceModelId)
}
// Add this method to FaceRecognitionRepository_StringIds.kt
// Replace the existing createPersonWithFaceModel method with this version:
/**
* Create a new person with face model in one operation.
* Now supports full PersonEntity with optional fields.
*
* @param person PersonEntity with name, DOB, relationship, etc.
* @return PersonId (String UUID)
*/
suspend fun createPersonWithFaceModel(
person: PersonEntity,
validImages: List<TrainingSanityChecker.ValidTrainingImage>,
onProgress: (Int, Int) -> Unit = { _, _ -> }
): String = withContext(Dispatchers.IO) {
// Insert person with all fields
personDao.insert(person)
// Train face model
trainPerson(
personId = person.id,
validImages = validImages,
onProgress = onProgress
)
person.id
}
/**
* Get face model by ID
*/
suspend fun getFaceModelById(faceModelId: String): FaceModelEntity? = withContext(Dispatchers.IO) {
faceModelDao.getFaceModelById(faceModelId)
}
suspend fun deleteTagsForImage(imageId: String) {
photoFaceTagDao.deleteTagsForImage(imageId)
}
fun cleanup() {
faceNetModel.close()
}
}
data class DetectedFace(
val croppedBitmap: Bitmap,
val boundingBox: android.graphics.Rect
)
data class PersonFaceStats(
val personId: String,
val personName: String,
val faceModelId: String,
val trainingImageCount: Int,
val taggedPhotoCount: Int,
val averageConfidence: Float,
val lastDetectedAt: Long?
)

View File

@@ -0,0 +1,380 @@
package com.placeholder.sherpai2.data.service
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import com.placeholder.sherpai2.data.local.dao.ImageTagDao
import com.placeholder.sherpai2.data.local.dao.PersonDao
import com.placeholder.sherpai2.data.local.dao.PhotoFaceTagDao
import com.placeholder.sherpai2.data.local.dao.TagDao
import com.placeholder.sherpai2.data.local.entity.ImageEntity
import com.placeholder.sherpai2.data.local.entity.ImageTagEntity
import com.placeholder.sherpai2.data.local.entity.TagEntity
import com.placeholder.sherpai2.data.repository.DetectedFace
import com.placeholder.sherpai2.util.DiagnosticLogger
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.Calendar
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.math.abs
/**
* AutoTaggingService - Intelligent auto-tagging system
*
* Capabilities:
* - Face-based tags (group_photo, selfie, couple)
* - Scene tags (portrait, landscape, square orientation)
* - Time tags (morning, afternoon, evening, night)
* - Quality tags (high_res, low_res)
* - Relationship tags (family, friend, colleague from PersonEntity)
* - Birthday tags (from PersonEntity DOB)
* - Indoor/Outdoor estimation (basic heuristic)
*/
@Singleton
class AutoTaggingService @Inject constructor(
@ApplicationContext private val context: Context,
private val tagDao: TagDao,
private val imageTagDao: ImageTagDao,
private val photoFaceTagDao: PhotoFaceTagDao,
private val personDao: PersonDao
) {
// ======================
// MAIN AUTO-TAGGING
// ======================
/**
* Auto-tag an image with all applicable system tags
*
* @return Number of tags applied
*/
suspend fun autoTagImage(
imageEntity: ImageEntity,
bitmap: Bitmap,
detectedFaces: List<DetectedFace>
): Int = withContext(Dispatchers.Default) {
val tagsToApply = mutableListOf<String>()
// Face-count based tags
when (detectedFaces.size) {
0 -> { /* No face tags */ }
1 -> {
if (isSelfie(detectedFaces[0], bitmap)) {
tagsToApply.add("selfie")
} else {
tagsToApply.add("single_person")
}
}
2 -> tagsToApply.add("couple")
in 3..5 -> tagsToApply.add("group_photo")
in 6..10 -> {
tagsToApply.add("group_photo")
tagsToApply.add("large_group")
}
else -> {
tagsToApply.add("group_photo")
tagsToApply.add("large_group")
tagsToApply.add("crowd")
}
}
// Orientation tags
val aspectRatio = bitmap.width.toFloat() / bitmap.height.toFloat()
when {
aspectRatio > 1.3f -> tagsToApply.add("landscape")
aspectRatio < 0.77f -> tagsToApply.add("portrait")
else -> tagsToApply.add("square")
}
// Resolution tags
val megapixels = (bitmap.width * bitmap.height) / 1_000_000f
when {
megapixels > 2.0f -> tagsToApply.add("high_res")
megapixels < 0.5f -> tagsToApply.add("low_res")
}
// Time-based tags
val hourOfDay = getHourFromTimestamp(imageEntity.capturedAt)
tagsToApply.add(when (hourOfDay) {
in 5..10 -> "morning"
in 11..16 -> "afternoon"
in 17..20 -> "evening"
else -> "night"
})
// Indoor/Outdoor estimation (only if image is large enough)
if (bitmap.width >= 200 && bitmap.height >= 200) {
val isIndoor = estimateIndoorOutdoor(bitmap)
tagsToApply.add(if (isIndoor) "indoor" else "outdoor")
}
// Apply all tags
var tagsApplied = 0
tagsToApply.forEach { tagName ->
if (applySystemTag(imageEntity.imageId, tagName)) {
tagsApplied++
}
}
DiagnosticLogger.d("AutoTag: Applied $tagsApplied tags to image ${imageEntity.imageId}")
tagsApplied
}
// ======================
// RELATIONSHIP TAGS
// ======================
/**
* Tag all images with a person using their relationship tag
*
* @param personId Person to tag images for
* @return Number of tags applied
*/
suspend fun autoTagRelationshipForPerson(personId: String): Int = withContext(Dispatchers.IO) {
val person = personDao.getPersonById(personId) ?: return@withContext 0
val relationship = person.relationship?.lowercase() ?: return@withContext 0
// Get face model for this person
val faceModels = photoFaceTagDao.getAllTagsForFaceModel(personId)
if (faceModels.isEmpty()) return@withContext 0
val imageIds = faceModels.map { it.imageId }.distinct()
var tagsApplied = 0
imageIds.forEach { imageId ->
if (applySystemTag(imageId, relationship)) {
tagsApplied++
}
}
DiagnosticLogger.i("AutoTag: Applied '$relationship' tag to $tagsApplied images for ${person.name}")
tagsApplied
}
/**
* Tag relationships for ALL persons in database
*/
suspend fun autoTagAllRelationships(): Int = withContext(Dispatchers.IO) {
val persons = personDao.getAllPersons()
var totalTags = 0
persons.forEach { person ->
totalTags += autoTagRelationshipForPerson(person.id)
}
DiagnosticLogger.i("AutoTag: Applied $totalTags relationship tags across ${persons.size} persons")
totalTags
}
// ======================
// BIRTHDAY TAGS
// ======================
/**
* Tag images near a person's birthday
*
* @param personId Person whose birthday to check
* @param daysRange Days before/after birthday to consider (default: 3)
* @return Number of tags applied
*/
suspend fun autoTagBirthdaysForPerson(
personId: String,
daysRange: Int = 3
): Int = withContext(Dispatchers.IO) {
val person = personDao.getPersonById(personId) ?: return@withContext 0
val dateOfBirth = person.dateOfBirth ?: return@withContext 0
// Get all face tags for this person
val faceTags = photoFaceTagDao.getAllTagsForFaceModel(personId)
if (faceTags.isEmpty()) return@withContext 0
var tagsApplied = 0
faceTags.forEach { faceTag ->
// Get the image to check its timestamp
val imageId = faceTag.imageId
// Check if image was captured near birthday
if (isNearBirthday(faceTag.detectedAt, dateOfBirth, daysRange)) {
if (applySystemTag(imageId, "birthday")) {
tagsApplied++
}
}
}
DiagnosticLogger.i("AutoTag: Applied 'birthday' tag to $tagsApplied images for ${person.name}")
tagsApplied
}
/**
* Tag birthdays for ALL persons with DOB
*/
suspend fun autoTagAllBirthdays(daysRange: Int = 3): Int = withContext(Dispatchers.IO) {
val persons = personDao.getAllPersons()
var totalTags = 0
persons.forEach { person ->
if (person.dateOfBirth != null) {
totalTags += autoTagBirthdaysForPerson(person.id, daysRange)
}
}
DiagnosticLogger.i("AutoTag: Applied $totalTags birthday tags")
totalTags
}
// ======================
// HELPER METHODS
// ======================
/**
* Check if an image is a selfie based on face size
*/
private fun isSelfie(face: DetectedFace, bitmap: Bitmap): Boolean {
val boundingBox = face.boundingBox
val faceArea = boundingBox.width() * boundingBox.height()
val imageArea = bitmap.width * bitmap.height
val faceRatio = faceArea.toFloat() / imageArea.toFloat()
// Selfie = face takes up significant portion (>15% of image)
return faceRatio > 0.15f
}
/**
* Get hour of day from timestamp (0-23)
*/
private fun getHourFromTimestamp(timestamp: Long): Int {
return Calendar.getInstance().apply {
timeInMillis = timestamp
}.get(Calendar.HOUR_OF_DAY)
}
/**
* Check if a timestamp is near a birthday
*/
private fun isNearBirthday(
capturedTimestamp: Long,
dobTimestamp: Long,
daysRange: Int
): Boolean {
val capturedCal = Calendar.getInstance().apply { timeInMillis = capturedTimestamp }
val dobCal = Calendar.getInstance().apply { timeInMillis = dobTimestamp }
val capturedMonth = capturedCal.get(Calendar.MONTH)
val capturedDay = capturedCal.get(Calendar.DAY_OF_MONTH)
val dobMonth = dobCal.get(Calendar.MONTH)
val dobDay = dobCal.get(Calendar.DAY_OF_MONTH)
if (capturedMonth == dobMonth) {
return abs(capturedDay - dobDay) <= daysRange
}
// Handle edge case: birthday near end/start of month
// e.g., DOB = Jan 2, captured = Dec 31 (within 3 days)
if (abs(capturedMonth - dobMonth) == 1 || abs(capturedMonth - dobMonth) == 11) {
val daysInCapturedMonth = capturedCal.getActualMaximum(Calendar.DAY_OF_MONTH)
val daysInDobMonth = dobCal.getActualMaximum(Calendar.DAY_OF_MONTH)
if (capturedMonth < dobMonth || (capturedMonth == 11 && dobMonth == 0)) {
// Captured before DOB month
val dayDiff = (daysInCapturedMonth - capturedDay) + dobDay
return dayDiff <= daysRange
} else {
// Captured after DOB month
val dayDiff = (daysInDobMonth - dobDay) + capturedDay
return dayDiff <= daysRange
}
}
return false
}
/**
* Basic indoor/outdoor estimation using brightness and saturation
*
* Heuristic:
* - Outdoor: Higher brightness (>120), Higher saturation (>0.25)
* - Indoor: Lower brightness, Lower saturation
*/
private fun estimateIndoorOutdoor(bitmap: Bitmap): Boolean {
// Sample pixels for analysis (don't process entire image)
val sampleSize = 100
val sampledPixels = mutableListOf<Int>()
val stepX = bitmap.width / sampleSize.coerceAtMost(bitmap.width)
val stepY = bitmap.height / sampleSize.coerceAtMost(bitmap.height)
for (x in 0 until sampleSize.coerceAtMost(bitmap.width)) {
for (y in 0 until sampleSize.coerceAtMost(bitmap.height)) {
val px = (x * stepX).coerceIn(0, bitmap.width - 1)
val py = (y * stepY).coerceIn(0, bitmap.height - 1)
sampledPixels.add(bitmap.getPixel(px, py))
}
}
if (sampledPixels.isEmpty()) return true // Default to indoor if sampling fails
// Calculate average brightness
val avgBrightness = sampledPixels.map { pixel ->
val r = Color.red(pixel)
val g = Color.green(pixel)
val b = Color.blue(pixel)
(r + g + b) / 3.0f
}.average()
// Calculate color saturation
val avgSaturation = sampledPixels.map { pixel ->
val hsv = FloatArray(3)
Color.colorToHSV(pixel, hsv)
hsv[1] // Saturation
}.average()
// Heuristic: Indoor if low brightness OR low saturation
return avgBrightness < 120 || avgSaturation < 0.25
}
/**
* Apply a system tag to an image (helper to avoid duplicates)
*
* @return true if tag was applied, false if already exists
*/
private suspend fun applySystemTag(imageId: String, tagName: String): Boolean {
return withContext(Dispatchers.IO) {
try {
// Get or create tag
val tag = getOrCreateSystemTag(tagName)
// Create image-tag link
val imageTag = ImageTagEntity(
imageId = imageId,
tagId = tag.tagId,
source = "AUTO",
confidence = 1.0f,
visibility = "PUBLIC",
createdAt = System.currentTimeMillis()
)
imageTagDao.upsert(imageTag)
true
} catch (e: Exception) {
DiagnosticLogger.e("Failed to apply tag '$tagName' to image $imageId", e)
false
}
}
}
/**
* Get existing system tag or create new one
*/
private suspend fun getOrCreateSystemTag(tagName: String): TagEntity {
return withContext(Dispatchers.IO) {
tagDao.getByValue(tagName) ?: run {
val newTag = TagEntity.createSystemTag(tagName)
tagDao.insert(newTag)
newTag
}
}
}
}

View File

@@ -3,10 +3,7 @@ package com.placeholder.sherpai2.di
import android.content.Context import android.content.Context
import androidx.room.Room import androidx.room.Room
import com.placeholder.sherpai2.data.local.AppDatabase import com.placeholder.sherpai2.data.local.AppDatabase
import com.placeholder.sherpai2.data.local.dao.ImageAggregateDao import com.placeholder.sherpai2.data.local.dao.*
import com.placeholder.sherpai2.data.local.dao.ImageEventDao
import com.placeholder.sherpai2.data.local.dao.ImageTagDao
import com.placeholder.sherpai2.data.local.dao.TagDao
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
@@ -14,48 +11,69 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton import javax.inject.Singleton
/**
* DatabaseModule - Provides database and ALL DAOs
*
* DEVELOPMENT CONFIGURATION:
* - fallbackToDestructiveMigration enabled
* - No migrations required
*/
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
object DatabaseModule { object DatabaseModule {
// ===== DATABASE =====
@Provides @Provides
@Singleton @Singleton
fun provideDatabase( fun provideDatabase(
@ApplicationContext context: Context @ApplicationContext context: Context
): AppDatabase { ): AppDatabase =
return Room.databaseBuilder( Room.databaseBuilder(
context, context,
AppDatabase::class.java, AppDatabase::class.java,
"sherpai.db" "sherpai.db"
).build() )
} .fallbackToDestructiveMigration()
.build()
// --- Add these DAO providers --- // ===== CORE DAOs =====
@Provides @Provides
fun provideTagDao(database: AppDatabase): TagDao { fun provideImageDao(db: AppDatabase): ImageDao =
return database.tagDao() db.imageDao()
}
@Provides @Provides
fun provideImageTagDao(database: AppDatabase): ImageTagDao { fun provideTagDao(db: AppDatabase): TagDao =
return database.imageTagDao() db.tagDao()
}
// Add providers for your other DAOs now to avoid future errors
@Provides
fun provideImageDao(database: AppDatabase) = database.imageDao()
@Provides @Provides
fun providePersonDao(database: AppDatabase) = database.personDao() fun provideEventDao(db: AppDatabase): EventDao =
db.eventDao()
@Provides @Provides
fun provideEventDao(database: AppDatabase) = database.eventDao() fun provideImageEventDao(db: AppDatabase): ImageEventDao =
db.imageEventDao()
@Provides @Provides
fun provideImageEventDao(database: AppDatabase): ImageEventDao = database.imageEventDao() fun provideImageAggregateDao(db: AppDatabase): ImageAggregateDao =
db.imageAggregateDao()
@Provides @Provides
fun provideImageAggregateDao(database: AppDatabase): ImageAggregateDao = database.imageAggregateDao() fun provideImageTagDao(db: AppDatabase): ImageTagDao =
db.imageTagDao()
// ===== FACE RECOGNITION DAOs =====
@Provides
fun providePersonDao(db: AppDatabase): PersonDao =
db.personDao()
@Provides
fun provideFaceModelDao(db: AppDatabase): FaceModelDao =
db.faceModelDao()
@Provides
fun providePhotoFaceTagDao(db: AppDatabase): PhotoFaceTagDao =
db.photoFaceTagDao()
} }

View File

@@ -0,0 +1,34 @@
package com.placeholder.sherpai2.di
import android.content.Context
import com.placeholder.sherpai2.ml.FaceNetModel
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
/**
* MLModule - Provides ML-related dependencies
*
* This module provides FaceNetModel for dependency injection
*/
@Module
@InstallIn(SingletonComponent::class)
object MLModule {
/**
* Provide FaceNetModel singleton
*
* FaceNetModel loads the MobileFaceNet TFLite model and manages
* face embedding generation for recognition.
*/
@Provides
@Singleton
fun provideFaceNetModel(
@ApplicationContext context: Context
): FaceNetModel {
return FaceNetModel(context)
}
}

View File

@@ -1,20 +1,35 @@
package com.placeholder.sherpai2.di package com.placeholder.sherpai2.di
import android.content.Context
import com.placeholder.sherpai2.data.local.dao.FaceModelDao
import com.placeholder.sherpai2.data.local.dao.ImageDao
import com.placeholder.sherpai2.data.local.dao.PersonDao
import com.placeholder.sherpai2.data.local.dao.PhotoFaceTagDao
import com.placeholder.sherpai2.data.repository.FaceRecognitionRepository
import com.placeholder.sherpai2.data.repository.TaggingRepositoryImpl import com.placeholder.sherpai2.data.repository.TaggingRepositoryImpl
import com.placeholder.sherpai2.domain.repository.ImageRepository import com.placeholder.sherpai2.domain.repository.ImageRepository
import com.placeholder.sherpai2.domain.repository.ImageRepositoryImpl import com.placeholder.sherpai2.domain.repository.ImageRepositoryImpl
import com.placeholder.sherpai2.domain.repository.TaggingRepository import com.placeholder.sherpai2.domain.repository.TaggingRepository
import dagger.Binds import dagger.Binds
import dagger.Module import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton import javax.inject.Singleton
/**
* RepositoryModule - Provides repository implementations
*
* UPDATED TO INCLUDE:
* - FaceRecognitionRepository for face recognition operations
*/
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
abstract class RepositoryModule { abstract class RepositoryModule {
// ===== EXISTING REPOSITORY BINDINGS =====
@Binds @Binds
@Singleton @Singleton
abstract fun bindImageRepository( abstract fun bindImageRepository(
@@ -26,4 +41,50 @@ abstract class RepositoryModule {
abstract fun bindTaggingRepository( abstract fun bindTaggingRepository(
impl: TaggingRepositoryImpl impl: TaggingRepositoryImpl
): TaggingRepository ): TaggingRepository
}
// ===== COMPANION OBJECT FOR PROVIDES =====
companion object {
/**
* Provide FaceRecognitionRepository
*
* Uses @Provides instead of @Binds because it needs Context parameter
* and multiple DAO dependencies
*
* INJECTED DEPENDENCIES:
* - Context: For FaceNetModel initialization
* - PersonDao: Access existing persons
* - ImageDao: Access existing images
* - FaceModelDao: Manage face models
* - PhotoFaceTagDao: Manage photo tags
*
* USAGE IN VIEWMODEL:
* ```
* @HiltViewModel
* class MyViewModel @Inject constructor(
* private val faceRecognitionRepository: FaceRecognitionRepository
* ) : ViewModel() {
* // Use repository methods
* }
* ```
*/
@Provides
@Singleton
fun provideFaceRecognitionRepository(
@ApplicationContext context: Context,
personDao: PersonDao,
imageDao: ImageDao,
faceModelDao: FaceModelDao,
photoFaceTagDao: PhotoFaceTagDao
): FaceRecognitionRepository {
return FaceRecognitionRepository(
context = context,
personDao = personDao,
imageDao = imageDao,
faceModelDao = faceModelDao,
photoFaceTagDao = photoFaceTagDao
)
}
}
}

View File

@@ -0,0 +1,204 @@
package com.placeholder.sherpai2.ml
import android.content.Context
import android.graphics.Bitmap
import org.tensorflow.lite.Interpreter
import java.io.FileInputStream
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.MappedByteBuffer
import java.nio.channels.FileChannel
import kotlin.math.sqrt
/**
* FaceNetModel - MobileFaceNet wrapper for face recognition
*
* CLEAN IMPLEMENTATION:
* - All IDs are Strings (matching your schema)
* - Generates 192-dimensional embeddings
* - Cosine similarity for matching
*/
class FaceNetModel(private val context: Context) {
companion object {
private const val MODEL_FILE = "mobilefacenet.tflite"
private const val INPUT_SIZE = 112
private const val EMBEDDING_SIZE = 192
const val SIMILARITY_THRESHOLD_HIGH = 0.7f
const val SIMILARITY_THRESHOLD_MEDIUM = 0.6f
const val SIMILARITY_THRESHOLD_LOW = 0.5f
}
private var interpreter: Interpreter? = null
init {
try {
val model = loadModelFile()
interpreter = Interpreter(model)
} catch (e: Exception) {
throw RuntimeException("Failed to load FaceNet model", e)
}
}
/**
* Load TFLite model from assets
*/
private fun loadModelFile(): MappedByteBuffer {
val fileDescriptor = context.assets.openFd(MODEL_FILE)
val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
val fileChannel = inputStream.channel
val startOffset = fileDescriptor.startOffset
val declaredLength = fileDescriptor.declaredLength
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
}
/**
* Generate embedding for a single face
*
* @param faceBitmap Cropped face image (will be resized to 112x112)
* @return 192-dimensional embedding
*/
fun generateEmbedding(faceBitmap: Bitmap): FloatArray {
val resized = Bitmap.createScaledBitmap(faceBitmap, INPUT_SIZE, INPUT_SIZE, true)
val inputBuffer = preprocessImage(resized)
val output = Array(1) { FloatArray(EMBEDDING_SIZE) }
interpreter?.run(inputBuffer, output)
return normalizeEmbedding(output[0])
}
/**
* Generate embeddings for multiple faces (batch processing)
*/
fun generateEmbeddingsBatch(
faceBitmaps: List<Bitmap>,
onProgress: (Int, Int) -> Unit = { _, _ -> }
): List<FloatArray> {
return faceBitmaps.mapIndexed { index, bitmap ->
onProgress(index + 1, faceBitmaps.size)
generateEmbedding(bitmap)
}
}
/**
* Create person model by averaging multiple embeddings
*/
fun createPersonModel(embeddings: List<FloatArray>): FloatArray {
require(embeddings.isNotEmpty()) { "Need at least one embedding" }
val averaged = FloatArray(EMBEDDING_SIZE) { 0f }
embeddings.forEach { embedding ->
for (i in embedding.indices) {
averaged[i] += embedding[i]
}
}
val count = embeddings.size.toFloat()
for (i in averaged.indices) {
averaged[i] /= count
}
return normalizeEmbedding(averaged)
}
/**
* Calculate cosine similarity between two embeddings
* Returns value between -1.0 and 1.0 (higher = more similar)
*/
fun calculateSimilarity(embedding1: FloatArray, embedding2: FloatArray): Float {
require(embedding1.size == EMBEDDING_SIZE && embedding2.size == EMBEDDING_SIZE) {
"Invalid embedding size"
}
var dotProduct = 0f
var norm1 = 0f
var norm2 = 0f
for (i in embedding1.indices) {
dotProduct += embedding1[i] * embedding2[i]
norm1 += embedding1[i] * embedding1[i]
norm2 += embedding2[i] * embedding2[i]
}
return dotProduct / (sqrt(norm1) * sqrt(norm2))
}
/**
* Find best matching face model from a list
*
* @param faceEmbedding Embedding to match
* @param modelEmbeddings List of (modelId: String, embedding: FloatArray)
* @param threshold Minimum similarity threshold
* @return Pair of (modelId: String, confidence: Float) or null
*/
fun findBestMatch(
faceEmbedding: FloatArray,
modelEmbeddings: List<Pair<String, FloatArray>>,
threshold: Float = SIMILARITY_THRESHOLD_HIGH
): Pair<String, Float>? {
var bestMatch: Pair<String, Float>? = null
var highestSimilarity = threshold
for ((modelId, modelEmbedding) in modelEmbeddings) {
val similarity = calculateSimilarity(faceEmbedding, modelEmbedding)
if (similarity > highestSimilarity) {
highestSimilarity = similarity
bestMatch = Pair(modelId, similarity)
}
}
return bestMatch
}
/**
* Preprocess image for model input
*/
private fun preprocessImage(bitmap: Bitmap): ByteBuffer {
val buffer = ByteBuffer.allocateDirect(4 * INPUT_SIZE * INPUT_SIZE * 3)
buffer.order(ByteOrder.nativeOrder())
val pixels = IntArray(INPUT_SIZE * INPUT_SIZE)
bitmap.getPixels(pixels, 0, INPUT_SIZE, 0, 0, INPUT_SIZE, INPUT_SIZE)
for (pixel in pixels) {
val r = ((pixel shr 16) and 0xFF) / 255.0f
val g = ((pixel shr 8) and 0xFF) / 255.0f
val b = (pixel and 0xFF) / 255.0f
buffer.putFloat((r - 0.5f) / 0.5f)
buffer.putFloat((g - 0.5f) / 0.5f)
buffer.putFloat((b - 0.5f) / 0.5f)
}
return buffer
}
/**
* Normalize embedding to unit length
*/
private fun normalizeEmbedding(embedding: FloatArray): FloatArray {
var norm = 0f
for (value in embedding) {
norm += value * value
}
norm = sqrt(norm)
return if (norm > 0) {
FloatArray(embedding.size) { i -> embedding[i] / norm }
} else {
embedding
}
}
/**
* Clean up resources
*/
fun close() {
interpreter?.close()
interpreter = null
}
}

View File

@@ -0,0 +1,127 @@
package com.placeholder.sherpai2.ml
/**
* ThresholdStrategy - Smart threshold selection for face recognition
*
* Considers:
* - Training image count
* - Image quality
* - Detection context (group photo, selfie, etc.)
*/
object ThresholdStrategy {
/**
* Get optimal threshold for face recognition
*
* @param trainingCount Number of images used to train the model
* @param imageQuality Quality assessment of the image being scanned
* @param detectionContext Context of the detection (group, selfie, etc.)
* @return Similarity threshold (0.0 - 1.0)
*/
fun getOptimalThreshold(
trainingCount: Int,
imageQuality: ImageQuality = ImageQuality.UNKNOWN,
detectionContext: DetectionContext = DetectionContext.GENERAL
): Float {
// Base threshold from training count
val baseThreshold = when {
trainingCount >= 40 -> 0.68f // High confidence - strict
trainingCount >= 30 -> 0.62f // Good confidence - moderate-strict
trainingCount >= 20 -> 0.56f // Moderate confidence
trainingCount >= 15 -> 0.50f // Acceptable confidence - lenient
else -> 0.48f // Sparse training - very lenient
}
// Adjust based on image quality
val qualityAdjustment = when (imageQuality) {
ImageQuality.HIGH -> -0.02f // Can be stricter with good quality
ImageQuality.MEDIUM -> 0f // No change
ImageQuality.LOW -> +0.03f // Be more lenient with poor quality
ImageQuality.UNKNOWN -> 0f // No change
}
// Adjust based on detection context
val contextAdjustment = when (detectionContext) {
DetectionContext.GROUP_PHOTO -> +0.02f // More lenient in groups (faces smaller)
DetectionContext.SELFIE -> -0.03f // Stricter for close-ups (more detail)
DetectionContext.PROFILE -> +0.02f // More lenient for side profiles
DetectionContext.DISTANT -> +0.03f // More lenient for far away faces
DetectionContext.GENERAL -> 0f // No change
}
// Combine adjustments and clamp to safe range
return (baseThreshold + qualityAdjustment + contextAdjustment).coerceIn(0.40f, 0.75f)
}
/**
* Get threshold for liberal matching (e.g., during testing)
*/
fun getLiberalThreshold(trainingCount: Int): Float {
return when {
trainingCount >= 30 -> 0.52f
trainingCount >= 20 -> 0.48f
else -> 0.45f
}.coerceIn(0.40f, 0.65f)
}
/**
* Get threshold for conservative matching (minimize false positives)
*/
fun getConservativeThreshold(trainingCount: Int): Float {
return when {
trainingCount >= 40 -> 0.72f
trainingCount >= 30 -> 0.68f
trainingCount >= 20 -> 0.62f
else -> 0.58f
}.coerceIn(0.55f, 0.75f)
}
/**
* Estimate image quality from bitmap properties
*/
fun estimateImageQuality(width: Int, height: Int, fileSize: Long = 0): ImageQuality {
val megapixels = (width * height) / 1_000_000f
return when {
megapixels > 4.0f -> ImageQuality.HIGH
megapixels > 1.0f -> ImageQuality.MEDIUM
else -> ImageQuality.LOW
}
}
/**
* Estimate detection context from face count and face size
*/
fun estimateDetectionContext(
faceCount: Int,
faceAreaRatio: Float = 0f
): DetectionContext {
return when {
faceCount == 1 && faceAreaRatio > 0.15f -> DetectionContext.SELFIE
faceCount == 1 && faceAreaRatio < 0.05f -> DetectionContext.DISTANT
faceCount >= 3 -> DetectionContext.GROUP_PHOTO
else -> DetectionContext.GENERAL
}
}
}
/**
* Image quality assessment
*/
enum class ImageQuality {
HIGH, // > 4MP, good lighting
MEDIUM, // 1-4MP
LOW, // < 1MP, poor quality
UNKNOWN // Cannot determine
}
/**
* Detection context
*/
enum class DetectionContext {
GROUP_PHOTO, // Multiple faces (3+)
SELFIE, // Single face, close-up
PROFILE, // Side view
DISTANT, // Face is small in frame
GENERAL // Default
}

View File

@@ -0,0 +1,336 @@
package com.placeholder.sherpai2.ui.album
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.placeholder.sherpai2.data.local.dao.ImageDao
import com.placeholder.sherpai2.data.local.dao.ImageTagDao
import com.placeholder.sherpai2.data.local.dao.PersonDao
import com.placeholder.sherpai2.data.local.dao.TagDao
import com.placeholder.sherpai2.data.local.entity.ImageEntity
import com.placeholder.sherpai2.data.local.entity.PersonEntity
import com.placeholder.sherpai2.data.local.entity.PhotoFaceTagEntity
import com.placeholder.sherpai2.data.repository.FaceRecognitionRepository
import com.placeholder.sherpai2.ui.search.DateRange
import com.placeholder.sherpai2.ui.search.DisplayMode
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import java.util.Calendar
import javax.inject.Inject
/**
* AlbumViewModel - Display photos from a specific album (tag, person, or time range)
*
* Features:
* - Search within album
* - Date filtering
* - Simple/Verbose toggle
* - Album stats
*/
@HiltViewModel
class AlbumViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val tagDao: TagDao,
private val imageTagDao: ImageTagDao,
private val imageDao: ImageDao,
private val personDao: PersonDao,
private val faceRecognitionRepository: FaceRecognitionRepository
) : ViewModel() {
// Album parameters from navigation
private val albumType: String = savedStateHandle["albumType"] ?: "tag"
private val albumId: String = savedStateHandle["albumId"] ?: ""
// UI state
private val _uiState = MutableStateFlow<AlbumUiState>(AlbumUiState.Loading)
val uiState: StateFlow<AlbumUiState> = _uiState.asStateFlow()
// Search query within album
private val _searchQuery = MutableStateFlow("")
val searchQuery: StateFlow<String> = _searchQuery.asStateFlow()
// Date range filter
private val _dateRange = MutableStateFlow(DateRange.ALL_TIME)
val dateRange: StateFlow<DateRange> = _dateRange.asStateFlow()
// Display mode
private val _displayMode = MutableStateFlow(DisplayMode.SIMPLE)
val displayMode: StateFlow<DisplayMode> = _displayMode.asStateFlow()
init {
loadAlbumData()
}
/**
* Load album data based on type
*/
private fun loadAlbumData() {
viewModelScope.launch {
try {
_uiState.value = AlbumUiState.Loading
when (albumType) {
"tag" -> loadTagAlbum()
"person" -> loadPersonAlbum()
"time" -> loadTimeAlbum()
else -> _uiState.value = AlbumUiState.Error("Unknown album type")
}
} catch (e: Exception) {
_uiState.value = AlbumUiState.Error(e.message ?: "Failed to load album")
}
}
}
private suspend fun loadTagAlbum() {
val tag = tagDao.getByValue(albumId)
if (tag == null) {
_uiState.value = AlbumUiState.Error("Tag not found")
return
}
combine(
_searchQuery,
_dateRange
) { query, dateRange ->
Pair(query, dateRange)
}.collectLatest { (query, dateRange) ->
val imageIds = imageTagDao.findImagesByTag(tag.tagId, 0.5f)
val images = imageDao.getImagesByIds(imageIds)
val filteredImages = images
.filter { isInDateRange(it.capturedAt, dateRange) }
.filter {
query.isBlank() || containsQuery(it, query)
}
val imagesWithFaces = filteredImages.map { image ->
val tagsWithPersons = faceRecognitionRepository.getFaceTagsWithPersons(image.imageId)
AlbumPhoto(
image = image,
faceTags = tagsWithPersons.map { it.first },
persons = tagsWithPersons.map { it.second }
)
}
val uniquePersons = imagesWithFaces
.flatMap { it.persons }
.distinctBy { it.id }
_uiState.value = AlbumUiState.Success(
albumName = tag.value.replace("_", " ").capitalize(),
albumType = "Tag",
photos = imagesWithFaces,
personCount = uniquePersons.size,
totalFaces = imagesWithFaces.sumOf { it.faceTags.size }
)
}
}
private suspend fun loadPersonAlbum() {
val person = personDao.getPersonById(albumId)
if (person == null) {
_uiState.value = AlbumUiState.Error("Person not found")
return
}
combine(
_searchQuery,
_dateRange
) { query, dateRange ->
Pair(query, dateRange)
}.collectLatest { (query, dateRange) ->
val images = faceRecognitionRepository.getImagesForPerson(albumId)
val filteredImages = images
.filter { isInDateRange(it.capturedAt, dateRange) }
.filter {
query.isBlank() || containsQuery(it, query)
}
val imagesWithFaces = filteredImages.map { image ->
val tagsWithPersons = faceRecognitionRepository.getFaceTagsWithPersons(image.imageId)
AlbumPhoto(
image = image,
faceTags = tagsWithPersons.map { it.first },
persons = tagsWithPersons.map { it.second }
)
}
_uiState.value = AlbumUiState.Success(
albumName = person.name,
albumType = "Person",
photos = imagesWithFaces,
personCount = 1,
totalFaces = imagesWithFaces.sumOf { it.faceTags.size }
)
}
}
private suspend fun loadTimeAlbum() {
// Time-based albums (Today, This Week, etc)
val (startTime, endTime, albumName) = when (albumId) {
"today" -> Triple(getStartOfDay(), System.currentTimeMillis(), "Today")
"week" -> Triple(getStartOfWeek(), System.currentTimeMillis(), "This Week")
"month" -> Triple(getStartOfMonth(), System.currentTimeMillis(), "This Month")
"year" -> Triple(getStartOfYear(), System.currentTimeMillis(), "This Year")
else -> {
_uiState.value = AlbumUiState.Error("Unknown time range")
return
}
}
combine(
_searchQuery,
_dateRange
) { query, _ ->
query
}.collectLatest { query ->
val images = imageDao.getImagesInRange(startTime, endTime)
val filteredImages = images.filter {
query.isBlank() || containsQuery(it, query)
}
val imagesWithFaces = filteredImages.map { image ->
val tagsWithPersons = faceRecognitionRepository.getFaceTagsWithPersons(image.imageId)
AlbumPhoto(
image = image,
faceTags = tagsWithPersons.map { it.first },
persons = tagsWithPersons.map { it.second }
)
}
val uniquePersons = imagesWithFaces
.flatMap { it.persons }
.distinctBy { it.id }
_uiState.value = AlbumUiState.Success(
albumName = albumName,
albumType = "Time",
photos = imagesWithFaces,
personCount = uniquePersons.size,
totalFaces = imagesWithFaces.sumOf { it.faceTags.size }
)
}
}
fun setSearchQuery(query: String) {
_searchQuery.value = query
}
fun setDateRange(range: DateRange) {
_dateRange.value = range
}
fun toggleDisplayMode() {
_displayMode.value = when (_displayMode.value) {
DisplayMode.SIMPLE -> DisplayMode.VERBOSE
DisplayMode.VERBOSE -> DisplayMode.SIMPLE
}
}
private fun isInDateRange(timestamp: Long, range: DateRange): Boolean {
return when (range) {
DateRange.ALL_TIME -> true
DateRange.TODAY -> isToday(timestamp)
DateRange.THIS_WEEK -> isThisWeek(timestamp)
DateRange.THIS_MONTH -> isThisMonth(timestamp)
DateRange.THIS_YEAR -> isThisYear(timestamp)
}
}
private fun containsQuery(image: ImageEntity, query: String): Boolean {
// Could expand to search by person names, tags, etc.
return true
}
private fun isToday(timestamp: Long): Boolean {
val today = Calendar.getInstance()
val date = Calendar.getInstance().apply { timeInMillis = timestamp }
return today.get(Calendar.YEAR) == date.get(Calendar.YEAR) &&
today.get(Calendar.DAY_OF_YEAR) == date.get(Calendar.DAY_OF_YEAR)
}
private fun isThisWeek(timestamp: Long): Boolean {
val today = Calendar.getInstance()
val date = Calendar.getInstance().apply { timeInMillis = timestamp }
return today.get(Calendar.YEAR) == date.get(Calendar.YEAR) &&
today.get(Calendar.WEEK_OF_YEAR) == date.get(Calendar.WEEK_OF_YEAR)
}
private fun isThisMonth(timestamp: Long): Boolean {
val today = Calendar.getInstance()
val date = Calendar.getInstance().apply { timeInMillis = timestamp }
return today.get(Calendar.YEAR) == date.get(Calendar.YEAR) &&
today.get(Calendar.MONTH) == date.get(Calendar.MONTH)
}
private fun isThisYear(timestamp: Long): Boolean {
val today = Calendar.getInstance()
val date = Calendar.getInstance().apply { timeInMillis = timestamp }
return today.get(Calendar.YEAR) == date.get(Calendar.YEAR)
}
private fun getStartOfDay(): Long {
return Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}.timeInMillis
}
private fun getStartOfWeek(): Long {
return Calendar.getInstance().apply {
set(Calendar.DAY_OF_WEEK, firstDayOfWeek)
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}.timeInMillis
}
private fun getStartOfMonth(): Long {
return Calendar.getInstance().apply {
set(Calendar.DAY_OF_MONTH, 1)
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}.timeInMillis
}
private fun getStartOfYear(): Long {
return Calendar.getInstance().apply {
set(Calendar.DAY_OF_YEAR, 1)
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}.timeInMillis
}
private fun String.capitalize(): String {
return this.replaceFirstChar { it.uppercase() }
}
}
sealed class AlbumUiState {
object Loading : AlbumUiState()
data class Success(
val albumName: String,
val albumType: String,
val photos: List<AlbumPhoto>,
val personCount: Int,
val totalFaces: Int
) : AlbumUiState()
data class Error(val message: String) : AlbumUiState()
}
data class AlbumPhoto(
val image: ImageEntity,
val faceTags: List<PhotoFaceTagEntity>,
val persons: List<PersonEntity>
)

View File

@@ -0,0 +1,358 @@
package com.placeholder.sherpai2.ui.album
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.*
import androidx.compose.foundation.lazy.items
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.graphics.Brush
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.placeholder.sherpai2.ui.search.DateRange
import com.placeholder.sherpai2.ui.search.DisplayMode
import com.placeholder.sherpai2.ui.search.components.ImageGridItem
/**
* AlbumViewScreen - Beautiful album detail view
*
* Features:
* - Album stats
* - Search within album
* - Date filtering
* - Simple/Verbose toggle
* - Clean person display
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AlbumViewScreen(
onBack: () -> Unit,
onImageClick: (String) -> Unit,
viewModel: AlbumViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val searchQuery by viewModel.searchQuery.collectAsStateWithLifecycle()
val dateRange by viewModel.dateRange.collectAsStateWithLifecycle()
val displayMode by viewModel.displayMode.collectAsStateWithLifecycle()
Scaffold(
topBar = {
TopAppBar(
title = {
Column {
when (val state = uiState) {
is AlbumUiState.Success -> {
Text(
text = state.albumName,
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
Text(
text = "${state.photos.size} photos",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
else -> {
Text("Album")
}
}
}
},
navigationIcon = {
IconButton(onClick = onBack) {
Icon(Icons.Default.ArrowBack, "Back")
}
},
actions = {
IconButton(onClick = { viewModel.toggleDisplayMode() }) {
Icon(
imageVector = if (displayMode == DisplayMode.SIMPLE) {
Icons.Default.ViewList
} else {
Icons.Default.ViewModule
},
contentDescription = "Toggle view"
)
}
}
)
}
) { paddingValues ->
when (val state = uiState) {
is AlbumUiState.Loading -> {
Box(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}
is AlbumUiState.Error -> {
Box(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Icon(
Icons.Default.Error,
contentDescription = null,
modifier = Modifier.size(48.dp),
tint = MaterialTheme.colorScheme.error
)
Text(state.message)
Button(onClick = onBack) {
Text("Go Back")
}
}
}
}
is AlbumUiState.Success -> {
AlbumContent(
state = state,
searchQuery = searchQuery,
dateRange = dateRange,
displayMode = displayMode,
onSearchChange = { viewModel.setSearchQuery(it) },
onDateRangeChange = { viewModel.setDateRange(it) },
onImageClick = onImageClick,
modifier = Modifier.padding(paddingValues)
)
}
}
}
}
@Composable
private fun AlbumContent(
state: AlbumUiState.Success,
searchQuery: String,
dateRange: DateRange,
displayMode: DisplayMode,
onSearchChange: (String) -> Unit,
onDateRangeChange: (DateRange) -> Unit,
onImageClick: (String) -> Unit,
modifier: Modifier = Modifier
) {
Column(
modifier = modifier.fillMaxSize()
) {
// Stats card
Card(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.3f)
)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
horizontalArrangement = Arrangement.SpaceAround
) {
StatItem(Icons.Default.Photo, "Photos", state.photos.size.toString())
if (state.totalFaces > 0) {
StatItem(Icons.Default.Face, "Faces", state.totalFaces.toString())
}
if (state.personCount > 0) {
StatItem(Icons.Default.People, "People", state.personCount.toString())
}
}
}
// Search bar
OutlinedTextField(
value = searchQuery,
onValueChange = onSearchChange,
placeholder = { Text("Search in album...") },
leadingIcon = { Icon(Icons.Default.Search, null) },
trailingIcon = {
if (searchQuery.isNotEmpty()) {
IconButton(onClick = { onSearchChange("") }) {
Icon(Icons.Default.Clear, "Clear")
}
}
},
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
singleLine = true,
shape = RoundedCornerShape(16.dp)
)
Spacer(Modifier.height(8.dp))
// Date filters
LazyRow(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
items(DateRange.entries) { range ->
val isActive = dateRange == range
FilterChip(
selected = isActive,
onClick = { onDateRangeChange(range) },
label = { Text(range.displayName) }
)
}
}
Spacer(Modifier.height(8.dp))
// Photo grid
if (state.photos.isEmpty()) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(
text = "No photos in this album",
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
} else {
LazyVerticalGrid(
columns = GridCells.Adaptive(120.dp),
contentPadding = PaddingValues(12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp),
modifier = Modifier.fillMaxSize()
) {
items(
items = state.photos,
key = { it.image.imageId }
) { photo ->
PhotoCard(
photo = photo,
displayMode = displayMode,
onImageClick = onImageClick
)
}
}
}
}
}
@Composable
private fun StatItem(icon: androidx.compose.ui.graphics.vector.ImageVector, label: String, value: String) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
Icon(
icon,
contentDescription = null,
modifier = Modifier.size(24.dp),
tint = MaterialTheme.colorScheme.primary
)
Text(
text = value,
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
Text(
text = label,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
@Composable
private fun PhotoCard(
photo: AlbumPhoto,
displayMode: DisplayMode,
onImageClick: (String) -> Unit
) {
Card(
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(12.dp)
) {
Column {
ImageGridItem(
image = photo.image,
onClick = { onImageClick(photo.image.imageUri) }
)
if (photo.persons.isNotEmpty()) {
when (displayMode) {
DisplayMode.SIMPLE -> {
Surface(
color = MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.3f),
modifier = Modifier.fillMaxWidth()
) {
Text(
text = photo.persons.take(3).joinToString(", ") { it.name },
style = MaterialTheme.typography.bodySmall,
modifier = Modifier.padding(8.dp),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
DisplayMode.VERBOSE -> {
Surface(
color = MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.5f),
modifier = Modifier.fillMaxWidth()
) {
Column(
modifier = Modifier.padding(8.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
photo.persons.take(3).forEachIndexed { index, person ->
Row(
horizontalArrangement = Arrangement.spacedBy(6.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Icons.Default.Face,
null,
Modifier.size(14.dp),
MaterialTheme.colorScheme.primary
)
Text(
text = person.name,
style = MaterialTheme.typography.bodySmall,
modifier = Modifier.weight(1f),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
if (index < photo.faceTags.size) {
val confidence = (photo.faceTags[index].confidence * 100).toInt()
Text(
text = "$confidence%",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.primary
)
}
}
}
}
}
}
}
}
}
}
}

View File

@@ -1,17 +1,162 @@
package com.placeholder.sherpai2.ui.devscreens package com.placeholder.sherpai2.ui.devscreens
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.material3.Text 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.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
/**
* Beautiful placeholder screen for features under development
*
* Shows:
* - Feature name
* - Description
* - "Coming Soon" indicator
* - Consistent styling with rest of app
*/
@Composable @Composable
fun DummyScreen(label: String) { fun DummyScreen(
title: String,
subtitle: String = "This feature is under development"
) {
Box( Box(
modifier = Modifier.fillMaxSize(), modifier = Modifier
.fillMaxSize()
.background(
Brush.verticalGradient(
colors = listOf(
MaterialTheme.colorScheme.surface,
MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.3f)
)
)
),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
Text(label) Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(24.dp),
modifier = Modifier.padding(48.dp)
) {
// Icon badge
Surface(
modifier = Modifier.size(96.dp),
shape = RoundedCornerShape(24.dp),
color = MaterialTheme.colorScheme.primaryContainer,
shadowElevation = 8.dp
) {
Box(contentAlignment = Alignment.Center) {
Icon(
Icons.Default.Construction,
contentDescription = null,
modifier = Modifier.size(48.dp),
tint = MaterialTheme.colorScheme.primary
)
}
}
Spacer(modifier = Modifier.height(8.dp))
// Title
Text(
text = title,
style = MaterialTheme.typography.headlineMedium,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center
)
// Subtitle
Text(
text = subtitle,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center,
modifier = Modifier.padding(horizontal = 24.dp)
)
Spacer(modifier = Modifier.height(8.dp))
// Coming soon badge
Surface(
shape = RoundedCornerShape(16.dp),
color = MaterialTheme.colorScheme.tertiaryContainer,
shadowElevation = 2.dp
) {
Row(
modifier = Modifier.padding(horizontal = 20.dp, vertical = 12.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Icons.Default.Schedule,
contentDescription = null,
modifier = Modifier.size(20.dp),
tint = MaterialTheme.colorScheme.onTertiaryContainer
)
Text(
text = "Coming Soon",
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.SemiBold,
color = MaterialTheme.colorScheme.onTertiaryContainer
)
}
}
Spacer(modifier = Modifier.height(24.dp))
// Feature preview card
Card(
modifier = Modifier.fillMaxWidth(0.8f),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)
),
shape = RoundedCornerShape(16.dp)
) {
Column(
modifier = Modifier.padding(20.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
Text(
text = "What's planned:",
style = MaterialTheme.typography.titleSmall,
fontWeight = FontWeight.Bold
)
FeatureItem("Full implementation")
FeatureItem("Beautiful UI design")
FeatureItem("Smooth animations")
FeatureItem("Production-ready code")
}
}
}
} }
} }
@Composable
private fun FeatureItem(text: String) {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Icons.Default.CheckCircle,
contentDescription = null,
modifier = Modifier.size(16.dp),
tint = MaterialTheme.colorScheme.primary
)
Text(
text = text,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}

View File

@@ -0,0 +1,459 @@
package com.placeholder.sherpai2.ui.explore
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.LazyRow
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.filled.*
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.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
/**
* ExploreScreen - REDESIGNED
*
* Features:
* - Rectangular album cards (more compact)
* - Stories section (recent highlights)
* - Clickable navigation to AlbumViewScreen
* - Beautiful gradients and icons
*/
@Composable
fun ExploreScreen(
onAlbumClick: (albumType: String, albumId: String) -> Unit,
viewModel: ExploreViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsState()
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.surface)
) {
// Header with gradient
Box(
modifier = Modifier
.fillMaxWidth()
.background(
Brush.verticalGradient(
colors = listOf(
MaterialTheme.colorScheme.primaryContainer,
MaterialTheme.colorScheme.surface
)
)
)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
Text(
text = "Explore",
style = MaterialTheme.typography.headlineLarge,
fontWeight = FontWeight.Bold
)
Text(
text = "Your photo collection organized",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
when (val state = uiState) {
is ExploreViewModel.ExploreUiState.Loading -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}
is ExploreViewModel.ExploreUiState.Success -> {
ExploreContent(
smartAlbums = state.smartAlbums,
onAlbumClick = onAlbumClick
)
}
is ExploreViewModel.ExploreUiState.Error -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Icon(
Icons.Default.Error,
contentDescription = null,
modifier = Modifier.size(48.dp),
tint = MaterialTheme.colorScheme.error
)
Text(
text = state.message,
color = MaterialTheme.colorScheme.error
)
}
}
}
}
}
}
@Composable
private fun ExploreContent(
smartAlbums: List<SmartAlbum>,
onAlbumClick: (albumType: String, albumId: String) -> Unit
) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(vertical = 16.dp),
verticalArrangement = Arrangement.spacedBy(24.dp)
) {
// Stories Section (Recent Highlights)
item {
StoriesSection(
albums = smartAlbums.filter { it.imageCount > 0 }.take(10),
onAlbumClick = onAlbumClick
)
}
// Time-based Albums
val timeAlbums = smartAlbums.filterIsInstance<SmartAlbum.TimeRange>()
if (timeAlbums.isNotEmpty()) {
item {
AlbumSection(
title = "📅 Time Capsules",
albums = timeAlbums,
onAlbumClick = onAlbumClick
)
}
}
// Face-based Albums
val faceAlbums = smartAlbums.filterIsInstance<SmartAlbum.Tagged>()
.filter { it.tagValue in listOf("group_photo", "selfie", "couple") }
if (faceAlbums.isNotEmpty()) {
item {
AlbumSection(
title = "👥 People & Groups",
albums = faceAlbums,
onAlbumClick = onAlbumClick
)
}
}
// Relationship Albums
val relationshipAlbums = smartAlbums.filterIsInstance<SmartAlbum.Tagged>()
.filter { it.tagValue in listOf("family", "friend", "colleague") }
if (relationshipAlbums.isNotEmpty()) {
item {
AlbumSection(
title = "❤️ Relationships",
albums = relationshipAlbums,
onAlbumClick = onAlbumClick
)
}
}
// Time of Day Albums
val timeOfDayAlbums = smartAlbums.filterIsInstance<SmartAlbum.Tagged>()
.filter { it.tagValue in listOf("morning", "afternoon", "evening", "night") }
if (timeOfDayAlbums.isNotEmpty()) {
item {
AlbumSection(
title = "🌅 Times of Day",
albums = timeOfDayAlbums,
onAlbumClick = onAlbumClick
)
}
}
// Scene Albums
val sceneAlbums = smartAlbums.filterIsInstance<SmartAlbum.Tagged>()
.filter { it.tagValue in listOf("indoor", "outdoor") }
if (sceneAlbums.isNotEmpty()) {
item {
AlbumSection(
title = "🏞️ Scenes",
albums = sceneAlbums,
onAlbumClick = onAlbumClick
)
}
}
// Special Occasions
val specialAlbums = smartAlbums.filterIsInstance<SmartAlbum.Tagged>()
.filter { it.tagValue in listOf("birthday", "high_res") }
if (specialAlbums.isNotEmpty()) {
item {
AlbumSection(
title = "⭐ Special",
albums = specialAlbums,
onAlbumClick = onAlbumClick
)
}
}
// Person Albums
val personAlbums = smartAlbums.filterIsInstance<SmartAlbum.Person>()
if (personAlbums.isNotEmpty()) {
item {
AlbumSection(
title = "👤 People",
albums = personAlbums,
onAlbumClick = onAlbumClick
)
}
}
}
}
/**
* Stories section - Instagram-style circular highlights
*/
@Composable
private fun StoriesSection(
albums: List<SmartAlbum>,
onAlbumClick: (albumType: String, albumId: String) -> Unit
) {
Column(
modifier = Modifier.fillMaxWidth()
) {
Text(
text = "📖 Stories",
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
)
LazyRow(
contentPadding = PaddingValues(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp)
) {
items(albums) { album ->
StoryCircle(
album = album,
onClick = {
val (type, id) = getAlbumNavigation(album)
onAlbumClick(type, id)
}
)
}
}
}
}
/**
* Story circle - circular album preview
*/
@Composable
private fun StoryCircle(
album: SmartAlbum,
onClick: () -> Unit
) {
val (icon, gradient) = getAlbumIconAndGradient(album)
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.clickable(onClick = onClick)
) {
Box(
modifier = Modifier
.size(80.dp)
.clip(CircleShape)
.background(gradient),
contentAlignment = Alignment.Center
) {
Icon(
imageVector = icon,
contentDescription = null,
tint = Color.White,
modifier = Modifier.size(36.dp)
)
}
Text(
text = album.displayName,
style = MaterialTheme.typography.labelSmall,
maxLines = 2,
modifier = Modifier.width(80.dp),
fontWeight = FontWeight.Medium
)
Text(
text = "${album.imageCount}",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
/**
* Album section with horizontal scrolling rectangular cards
*/
@Composable
private fun AlbumSection(
title: String,
albums: List<SmartAlbum>,
onAlbumClick: (albumType: String, albumId: String) -> Unit
) {
Column(
modifier = Modifier.fillMaxWidth()
) {
Text(
text = title,
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
)
LazyRow(
contentPadding = PaddingValues(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
items(albums) { album ->
AlbumCard(
album = album,
onClick = {
val (type, id) = getAlbumNavigation(album)
onAlbumClick(type, id)
}
)
}
}
}
}
/**
* Rectangular album card - more compact than square
*/
@Composable
private fun AlbumCard(
album: SmartAlbum,
onClick: () -> Unit
) {
val (icon, gradient) = getAlbumIconAndGradient(album)
Card(
modifier = Modifier
.width(180.dp)
.height(120.dp)
.clickable(onClick = onClick),
shape = RoundedCornerShape(16.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(gradient)
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.SpaceBetween
) {
// Icon
Icon(
imageVector = icon,
contentDescription = null,
tint = Color.White,
modifier = Modifier.size(32.dp)
)
// Album info
Column {
Text(
text = album.displayName,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
color = Color.White,
maxLines = 1
)
Text(
text = "${album.imageCount} ${if (album.imageCount == 1) "photo" else "photos"}",
style = MaterialTheme.typography.bodySmall,
color = Color.White.copy(alpha = 0.9f)
)
}
}
}
}
}
/**
* Get navigation parameters for album
*/
private fun getAlbumNavigation(album: SmartAlbum): Pair<String, String> {
return when (album) {
is SmartAlbum.TimeRange.Today -> "time" to "today"
is SmartAlbum.TimeRange.ThisWeek -> "time" to "week"
is SmartAlbum.TimeRange.ThisMonth -> "time" to "month"
is SmartAlbum.TimeRange.LastYear -> "time" to "year"
is SmartAlbum.Tagged -> "tag" to album.tagValue
is SmartAlbum.Person -> "person" to album.personId
}
}
/**
* Get icon and gradient for album type
*/
private fun getAlbumIconAndGradient(album: SmartAlbum): Pair<ImageVector, Brush> {
return when (album) {
is SmartAlbum.TimeRange.Today -> Icons.Default.Today to gradientBlue()
is SmartAlbum.TimeRange.ThisWeek -> Icons.Default.DateRange to gradientTeal()
is SmartAlbum.TimeRange.ThisMonth -> Icons.Default.CalendarMonth to gradientGreen()
is SmartAlbum.TimeRange.LastYear -> Icons.Default.HistoryEdu to gradientPurple()
is SmartAlbum.Tagged -> when (album.tagValue) {
"group_photo" -> Icons.Default.Group to gradientOrange()
"selfie" -> Icons.Default.CameraAlt to gradientPink()
"couple" -> Icons.Default.Favorite to gradientRed()
"family" -> Icons.Default.FamilyRestroom to gradientIndigo()
"friend" -> Icons.Default.People to gradientCyan()
"colleague" -> Icons.Default.BusinessCenter to gradientGray()
"morning" -> Icons.Default.WbSunny to gradientYellow()
"afternoon" -> Icons.Default.LightMode to gradientOrange()
"evening" -> Icons.Default.WbTwilight to gradientOrange()
"night" -> Icons.Default.NightsStay to gradientDarkBlue()
"outdoor" -> Icons.Default.Landscape to gradientGreen()
"indoor" -> Icons.Default.Home to gradientBrown()
"birthday" -> Icons.Default.Cake to gradientPink()
"high_res" -> Icons.Default.HighQuality to gradientGold()
else -> Icons.Default.Label to gradientBlue()
}
is SmartAlbum.Person -> Icons.Default.Person to gradientPurple()
}
}
// Gradient helpers
private fun gradientBlue() = Brush.linearGradient(listOf(Color(0xFF1976D2), Color(0xFF1565C0)))
private fun gradientTeal() = Brush.linearGradient(listOf(Color(0xFF00897B), Color(0xFF00796B)))
private fun gradientGreen() = Brush.linearGradient(listOf(Color(0xFF388E3C), Color(0xFF2E7D32)))
private fun gradientPurple() = Brush.linearGradient(listOf(Color(0xFF7B1FA2), Color(0xFF6A1B9A)))
private fun gradientOrange() = Brush.linearGradient(listOf(Color(0xFFF57C00), Color(0xFFE64A19)))
private fun gradientPink() = Brush.linearGradient(listOf(Color(0xFFD81B60), Color(0xFFC2185B)))
private fun gradientRed() = Brush.linearGradient(listOf(Color(0xFFE53935), Color(0xFFD32F2F)))
private fun gradientIndigo() = Brush.linearGradient(listOf(Color(0xFF3949AB), Color(0xFF303F9F)))
private fun gradientCyan() = Brush.linearGradient(listOf(Color(0xFF00ACC1), Color(0xFF0097A7)))
private fun gradientGray() = Brush.linearGradient(listOf(Color(0xFF616161), Color(0xFF424242)))
private fun gradientYellow() = Brush.linearGradient(listOf(Color(0xFFFDD835), Color(0xFFFBC02D)))
private fun gradientDarkBlue() = Brush.linearGradient(listOf(Color(0xFF283593), Color(0xFF1A237E)))
private fun gradientBrown() = Brush.linearGradient(listOf(Color(0xFF5D4037), Color(0xFF4E342E)))
private fun gradientGold() = Brush.linearGradient(listOf(Color(0xFFFFB300), Color(0xFFFFA000)))

View File

@@ -0,0 +1,302 @@
package com.placeholder.sherpai2.ui.explore
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.placeholder.sherpai2.data.local.dao.ImageDao
import com.placeholder.sherpai2.data.local.dao.ImageTagDao
import com.placeholder.sherpai2.data.local.dao.PersonDao
import com.placeholder.sherpai2.data.local.dao.TagDao
import com.placeholder.sherpai2.data.local.entity.ImageEntity
import com.placeholder.sherpai2.data.repository.FaceRecognitionRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.util.Calendar
import javax.inject.Inject
@HiltViewModel
class ExploreViewModel @Inject constructor(
private val imageDao: ImageDao,
private val tagDao: TagDao,
private val imageTagDao: ImageTagDao,
private val personDao: PersonDao,
private val faceRecognitionRepository: FaceRecognitionRepository
) : ViewModel() {
private val _uiState = MutableStateFlow<ExploreUiState>(ExploreUiState.Loading)
val uiState: StateFlow<ExploreUiState> = _uiState.asStateFlow()
sealed class ExploreUiState {
object Loading : ExploreUiState()
data class Success(
val smartAlbums: List<SmartAlbum>
) : ExploreUiState()
data class Error(val message: String) : ExploreUiState()
}
init {
loadExploreData()
}
fun loadExploreData() {
viewModelScope.launch {
try {
_uiState.value = ExploreUiState.Loading
val smartAlbums = buildSmartAlbums()
_uiState.value = ExploreUiState.Success(
smartAlbums = smartAlbums
)
} catch (e: Exception) {
_uiState.value = ExploreUiState.Error(
e.message ?: "Failed to load explore data"
)
}
}
}
private suspend fun buildSmartAlbums(): List<SmartAlbum> {
val albums = mutableListOf<SmartAlbum>()
// Time-based albums
albums.add(SmartAlbum.TimeRange.Today)
albums.add(SmartAlbum.TimeRange.ThisWeek)
albums.add(SmartAlbum.TimeRange.ThisMonth)
albums.add(SmartAlbum.TimeRange.LastYear)
// Face-based albums (from system tags)
val groupPhotoTag = tagDao.getByValue("group_photo")
if (groupPhotoTag != null) {
val count = tagDao.getTagUsageCount(groupPhotoTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("group_photo", "Group Photos", count))
}
}
val selfieTag = tagDao.getByValue("selfie")
if (selfieTag != null) {
val count = tagDao.getTagUsageCount(selfieTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("selfie", "Selfies", count))
}
}
val coupleTag = tagDao.getByValue("couple")
if (coupleTag != null) {
val count = tagDao.getTagUsageCount(coupleTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("couple", "Couples", count))
}
}
// Relationship albums
val familyTag = tagDao.getByValue("family")
if (familyTag != null) {
val count = tagDao.getTagUsageCount(familyTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("family", "Family Moments", count))
}
}
val friendTag = tagDao.getByValue("friend")
if (friendTag != null) {
val count = tagDao.getTagUsageCount(friendTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("friend", "With Friends", count))
}
}
val colleagueTag = tagDao.getByValue("colleague")
if (colleagueTag != null) {
val count = tagDao.getTagUsageCount(colleagueTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("colleague", "Work Events", count))
}
}
// Time of day albums
val morningTag = tagDao.getByValue("morning")
if (morningTag != null) {
val count = tagDao.getTagUsageCount(morningTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("morning", "Morning Moments", count))
}
}
val eveningTag = tagDao.getByValue("evening")
if (eveningTag != null) {
val count = tagDao.getTagUsageCount(eveningTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("evening", "Golden Hour", count))
}
}
val nightTag = tagDao.getByValue("night")
if (nightTag != null) {
val count = tagDao.getTagUsageCount(nightTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("night", "Night Life", count))
}
}
// Scene albums
val outdoorTag = tagDao.getByValue("outdoor")
if (outdoorTag != null) {
val count = tagDao.getTagUsageCount(outdoorTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("outdoor", "Outdoor Adventures", count))
}
}
val indoorTag = tagDao.getByValue("indoor")
if (indoorTag != null) {
val count = tagDao.getTagUsageCount(indoorTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("indoor", "Indoor Moments", count))
}
}
// Special occasions
val birthdayTag = tagDao.getByValue("birthday")
if (birthdayTag != null) {
val count = tagDao.getTagUsageCount(birthdayTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("birthday", "Birthdays", count))
}
}
// Quality albums
val highResTag = tagDao.getByValue("high_res")
if (highResTag != null) {
val count = tagDao.getTagUsageCount(highResTag.tagId)
if (count > 0) {
albums.add(SmartAlbum.Tagged("high_res", "Best Quality", count))
}
}
// Person albums
val persons = personDao.getAllPersons()
persons.forEach { person ->
val stats = faceRecognitionRepository.getPersonFaceStats(person.id)
if (stats != null && stats.taggedPhotoCount > 0) {
albums.add(SmartAlbum.Person(
personId = person.id,
personName = person.name,
imageCount = stats.taggedPhotoCount
))
}
}
return albums
}
/**
* Get images for a specific smart album
*/
suspend fun getImagesForAlbum(album: SmartAlbum): List<ImageEntity> {
return when (album) {
is SmartAlbum.TimeRange.Today -> {
val startOfDay = getStartOfDay()
imageDao.getImagesInRange(startOfDay, System.currentTimeMillis())
}
is SmartAlbum.TimeRange.ThisWeek -> {
val startOfWeek = getStartOfWeek()
imageDao.getImagesInRange(startOfWeek, System.currentTimeMillis())
}
is SmartAlbum.TimeRange.ThisMonth -> {
val startOfMonth = getStartOfMonth()
imageDao.getImagesInRange(startOfMonth, System.currentTimeMillis())
}
is SmartAlbum.TimeRange.LastYear -> {
val oneYearAgo = System.currentTimeMillis() - (365L * 24 * 60 * 60 * 1000)
imageDao.getImagesInRange(oneYearAgo, System.currentTimeMillis())
}
is SmartAlbum.Tagged -> {
val tag = tagDao.getByValue(album.tagValue)
if (tag != null) {
val imageIds = imageTagDao.findImagesByTag(tag.tagId, 0.5f)
imageDao.getImagesByIds(imageIds)
} else {
emptyList()
}
}
is SmartAlbum.Person -> {
faceRecognitionRepository.getImagesForPerson(album.personId)
}
}
}
private fun getStartOfDay(): Long {
return Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}.timeInMillis
}
private fun getStartOfWeek(): Long {
return Calendar.getInstance().apply {
set(Calendar.DAY_OF_WEEK, firstDayOfWeek)
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}.timeInMillis
}
private fun getStartOfMonth(): Long {
return Calendar.getInstance().apply {
set(Calendar.DAY_OF_MONTH, 1)
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}.timeInMillis
}
}
/**
* Smart album types
*/
sealed class SmartAlbum {
abstract val displayName: String
abstract val imageCount: Int
sealed class TimeRange : SmartAlbum() {
data object Today : TimeRange() {
override val displayName = "Today"
override val imageCount = 0 // Calculated dynamically
}
data object ThisWeek : TimeRange() {
override val displayName = "This Week"
override val imageCount = 0
}
data object ThisMonth : TimeRange() {
override val displayName = "This Month"
override val imageCount = 0
}
data object LastYear : TimeRange() {
override val displayName = "Last Year"
override val imageCount = 0
}
}
data class Tagged(
val tagValue: String,
override val displayName: String,
override val imageCount: Int
) : SmartAlbum()
data class Person(
val personId: String,
val personName: String,
override val imageCount: Int
) : SmartAlbum() {
override val displayName = personName
}
}

View File

@@ -0,0 +1,614 @@
package com.placeholder.sherpai2.ui.modelinventory
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
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.filled.*
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.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import java.text.SimpleDateFormat
import java.util.*
/**
* PersonInventoryScreen - Manage trained face models
*
* Features:
* - List all trained persons
* - View stats
* - DELETE models
* - SCAN LIBRARY to find person in all photos (NEW!)
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PersonInventoryScreen(
modifier: Modifier = Modifier,
viewModel: PersonInventoryViewModel = hiltViewModel(),
onViewPersonPhotos: (String) -> Unit = {}
) {
val uiState by viewModel.uiState.collectAsState()
val scanningState by viewModel.scanningState.collectAsState()
var personToDelete by remember { mutableStateOf<PersonInventoryViewModel.PersonWithStats?>(null) }
var personToScan by remember { mutableStateOf<PersonInventoryViewModel.PersonWithStats?>(null) }
Scaffold(
topBar = {
TopAppBar(
title = { Text("Trained People") },
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
),
actions = {
IconButton(onClick = { viewModel.loadPersons() }) {
Icon(Icons.Default.Refresh, contentDescription = "Refresh")
}
}
)
}
) { paddingValues ->
Box(
modifier = modifier
.fillMaxSize()
.padding(paddingValues)
) {
when (val state = uiState) {
is PersonInventoryViewModel.InventoryUiState.Loading -> {
LoadingView()
}
is PersonInventoryViewModel.InventoryUiState.Success -> {
if (state.persons.isEmpty()) {
EmptyView()
} else {
PersonListView(
persons = state.persons,
onDeleteClick = { personToDelete = it },
onScanClick = { personToScan = it },
onViewPhotos = { onViewPersonPhotos(it.person.id) },
scanningState = scanningState
)
}
}
is PersonInventoryViewModel.InventoryUiState.Error -> {
ErrorView(
message = state.message,
onRetry = { viewModel.loadPersons() }
)
}
}
// Scanning overlay
if (scanningState is PersonInventoryViewModel.ScanningState.Scanning) {
ScanningOverlay(scanningState as PersonInventoryViewModel.ScanningState.Scanning)
}
}
}
// Delete confirmation dialog
personToDelete?.let { personWithStats ->
AlertDialog(
onDismissRequest = { personToDelete = null },
title = { Text("Delete ${personWithStats.person.name}?") },
text = {
Text(
"This will delete the face model and all ${personWithStats.stats.taggedPhotoCount} " +
"face tags. Your photos will NOT be deleted."
)
},
confirmButton = {
TextButton(
onClick = {
viewModel.deletePerson(
personWithStats.person.id,
personWithStats.stats.faceModelId
)
personToDelete = null
},
colors = ButtonDefaults.textButtonColors(
contentColor = MaterialTheme.colorScheme.error
)
) {
Text("Delete")
}
},
dismissButton = {
TextButton(onClick = { personToDelete = null }) {
Text("Cancel")
}
}
)
}
// Scan library confirmation dialog
personToScan?.let { personWithStats ->
AlertDialog(
onDismissRequest = { personToScan = null },
icon = { Icon(Icons.Default.Search, contentDescription = null) },
title = { Text("Scan Library for ${personWithStats.person.name}?") },
text = {
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
Text(
"This will scan your entire photo library and automatically tag " +
"all photos containing ${personWithStats.person.name}."
)
Text(
"Currently tagged: ${personWithStats.stats.taggedPhotoCount} photos",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
},
confirmButton = {
Button(
onClick = {
viewModel.scanLibraryForPerson(
personWithStats.person.id,
personWithStats.stats.faceModelId
)
personToScan = null
}
) {
Icon(Icons.Default.Search, contentDescription = null)
Spacer(modifier = Modifier.width(8.dp))
Text("Start Scan")
}
},
dismissButton = {
TextButton(onClick = { personToScan = null }) {
Text("Cancel")
}
}
)
}
}
@Composable
private fun LoadingView() {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
CircularProgressIndicator()
Text(
text = "Loading trained models...",
style = MaterialTheme.typography.bodyMedium
)
}
}
}
@Composable
private fun EmptyView() {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp),
modifier = Modifier.padding(32.dp)
) {
Icon(
Icons.Default.Face,
contentDescription = null,
modifier = Modifier.size(64.dp),
tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.5f)
)
Text(
text = "No trained people yet",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)
Text(
text = "Train a person using 10+ photos to start recognizing faces",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
@Composable
private fun ErrorView(
message: String,
onRetry: () -> Unit
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp),
modifier = Modifier.padding(32.dp)
) {
Icon(
Icons.Default.Warning,
contentDescription = null,
modifier = Modifier.size(64.dp),
tint = MaterialTheme.colorScheme.error
)
Text(
text = "Error",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)
Text(
text = message,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Button(onClick = onRetry) {
Icon(Icons.Default.Refresh, contentDescription = null)
Spacer(modifier = Modifier.width(8.dp))
Text("Retry")
}
}
}
}
@Composable
private fun PersonListView(
persons: List<PersonInventoryViewModel.PersonWithStats>,
onDeleteClick: (PersonInventoryViewModel.PersonWithStats) -> Unit,
onScanClick: (PersonInventoryViewModel.PersonWithStats) -> Unit,
onViewPhotos: (PersonInventoryViewModel.PersonWithStats) -> Unit,
scanningState: PersonInventoryViewModel.ScanningState
) {
LazyColumn(
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
// Summary card
item {
SummaryCard(totalPersons = persons.size)
Spacer(modifier = Modifier.height(8.dp))
}
// Person cards
items(persons) { personWithStats ->
PersonCard(
personWithStats = personWithStats,
onDeleteClick = { onDeleteClick(personWithStats) },
onScanClick = { onScanClick(personWithStats) },
onViewPhotos = { onViewPhotos(personWithStats) },
isScanning = scanningState is PersonInventoryViewModel.ScanningState.Scanning &&
scanningState.personId == personWithStats.person.id
)
}
}
}
@Composable
private fun SummaryCard(totalPersons: Int) {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Icons.Default.Face,
contentDescription = null,
modifier = Modifier.size(48.dp),
tint = MaterialTheme.colorScheme.primary
)
Column {
Text(
text = "$totalPersons trained ${if (totalPersons == 1) "person" else "people"}",
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
Text(
text = "Face recognition models ready",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.7f)
)
}
}
}
}
@Composable
private fun PersonCard(
personWithStats: PersonInventoryViewModel.PersonWithStats,
onDeleteClick: () -> Unit,
onScanClick: () -> Unit,
onViewPhotos: () -> Unit,
isScanning: Boolean
) {
val stats = personWithStats.stats
Card(
modifier = Modifier.fillMaxWidth(),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
// Header: Name and actions
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
Box(
modifier = Modifier
.size(48.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.primary),
contentAlignment = Alignment.Center
) {
Text(
text = personWithStats.person.name.take(1).uppercase(),
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onPrimary
)
}
Column {
Text(
text = personWithStats.person.name,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
text = "ID: ${personWithStats.person.id.take(8)}",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
IconButton(onClick = onDeleteClick) {
Icon(
Icons.Default.Delete,
contentDescription = "Delete",
tint = MaterialTheme.colorScheme.error
)
}
}
Spacer(modifier = Modifier.height(16.dp))
// Stats grid
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly
) {
StatItem(
icon = Icons.Default.PhotoCamera,
label = "Training",
value = "${stats.trainingImageCount}"
)
StatItem(
icon = Icons.Default.AccountBox,
label = "Tagged",
value = "${stats.taggedPhotoCount}"
)
StatItem(
icon = Icons.Default.CheckCircle,
label = "Confidence",
value = "${(stats.averageConfidence * 100).toInt()}%",
valueColor = if (stats.averageConfidence >= 0.8f) {
MaterialTheme.colorScheme.primary
} else if (stats.averageConfidence >= 0.6f) {
MaterialTheme.colorScheme.tertiary
} else {
MaterialTheme.colorScheme.error
}
)
}
Spacer(modifier = Modifier.height(16.dp))
// Last detected
stats.lastDetectedAt?.let { timestamp ->
Surface(
modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.surfaceVariant,
shape = RoundedCornerShape(8.dp)
) {
Row(
modifier = Modifier.padding(12.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Icons.Default.DateRange,
contentDescription = null,
modifier = Modifier.size(16.dp),
tint = MaterialTheme.colorScheme.onSurfaceVariant
)
Text(
text = "Last detected: ${formatDate(timestamp)}",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
Spacer(modifier = Modifier.height(12.dp))
// Action buttons row
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
// Scan Library button (PRIMARY ACTION)
Button(
onClick = onScanClick,
modifier = Modifier.weight(1f),
enabled = !isScanning,
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.primary
)
) {
if (isScanning) {
CircularProgressIndicator(
modifier = Modifier.size(16.dp),
color = MaterialTheme.colorScheme.onPrimary,
strokeWidth = 2.dp
)
} else {
Icon(
Icons.Default.Search,
contentDescription = null,
modifier = Modifier.size(18.dp)
)
}
Spacer(modifier = Modifier.width(8.dp))
Text(if (isScanning) "Scanning..." else "Scan Library")
}
// View photos button
if (stats.taggedPhotoCount > 0) {
OutlinedButton(
onClick = onViewPhotos,
modifier = Modifier.weight(1f)
) {
Icon(
Icons.Default.Photo,
contentDescription = null,
modifier = Modifier.size(18.dp)
)
Spacer(modifier = Modifier.width(8.dp))
Text("View (${stats.taggedPhotoCount})")
}
}
}
}
}
}
@Composable
private fun StatItem(
icon: ImageVector,
label: String,
value: String,
valueColor: Color = MaterialTheme.colorScheme.primary
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
Icon(
icon,
contentDescription = null,
modifier = Modifier.size(24.dp),
tint = MaterialTheme.colorScheme.onSurfaceVariant
)
Text(
text = value,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
color = valueColor
)
Text(
text = label,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
/**
* Scanning overlay showing progress
*/
@Composable
private fun ScanningOverlay(state: PersonInventoryViewModel.ScanningState.Scanning) {
Box(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.surface.copy(alpha = 0.95f)),
contentAlignment = Alignment.Center
) {
Card(
modifier = Modifier
.fillMaxWidth(0.85f)
.padding(24.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 8.dp)
) {
Column(
modifier = Modifier.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Icon(
Icons.Default.Search,
contentDescription = null,
modifier = Modifier.size(48.dp),
tint = MaterialTheme.colorScheme.primary
)
Text(
text = "Scanning Library",
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
Text(
text = "Finding ${state.personName} in your photos...",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
LinearProgressIndicator(
progress = { state.progress / state.total.toFloat() },
modifier = Modifier.fillMaxWidth(),
)
Text(
text = "${state.progress} / ${state.total} photos scanned",
style = MaterialTheme.typography.bodySmall
)
Text(
text = "${state.facesFound} faces detected",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.primary
)
}
}
}
}
private fun formatDate(timestamp: Long): String {
val formatter = SimpleDateFormat("MMM d, yyyy h:mm a", Locale.getDefault())
return formatter.format(Date(timestamp))
}

View File

@@ -0,0 +1,349 @@
package com.placeholder.sherpai2.ui.modelinventory
import android.app.Application
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.face.FaceDetection
import com.google.mlkit.vision.face.FaceDetectorOptions
import com.placeholder.sherpai2.data.local.entity.PersonEntity
import com.placeholder.sherpai2.data.repository.DetectedFace
import com.placeholder.sherpai2.data.repository.FaceRecognitionRepository
import com.placeholder.sherpai2.data.repository.PersonFaceStats
import com.placeholder.sherpai2.domain.repository.ImageRepository
import com.placeholder.sherpai2.ml.ThresholdStrategy
import com.placeholder.sherpai2.ml.ImageQuality
import com.placeholder.sherpai2.ml.DetectionContext
import com.placeholder.sherpai2.util.DebugFlags
import com.placeholder.sherpai2.util.DiagnosticLogger
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
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.withContext
import kotlinx.coroutines.tasks.await
import javax.inject.Inject
/**
* PersonInventoryViewModel - Enhanced with smart threshold strategy
*
* Toggle diagnostics in DebugFlags.kt:
* - ENABLE_FACE_RECOGNITION_LOGGING = true/false
* - USE_LIBERAL_THRESHOLDS = true/false
*/
@HiltViewModel
class PersonInventoryViewModel @Inject constructor(
application: Application,
private val faceRecognitionRepository: FaceRecognitionRepository,
private val imageRepository: ImageRepository
) : AndroidViewModel(application) {
private val _uiState = MutableStateFlow<InventoryUiState>(InventoryUiState.Loading)
val uiState: StateFlow<InventoryUiState> = _uiState.asStateFlow()
private val _scanningState = MutableStateFlow<ScanningState>(ScanningState.Idle)
val scanningState: StateFlow<ScanningState> = _scanningState.asStateFlow()
private val faceDetector by lazy {
val options = FaceDetectorOptions.Builder()
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE)
.setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_NONE)
.setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_NONE)
.setMinFaceSize(0.10f)
.build()
FaceDetection.getClient(options)
}
data class PersonWithStats(
val person: PersonEntity,
val stats: PersonFaceStats
)
sealed class InventoryUiState {
object Loading : InventoryUiState()
data class Success(val persons: List<PersonWithStats>) : InventoryUiState()
data class Error(val message: String) : InventoryUiState()
}
sealed class ScanningState {
object Idle : ScanningState()
data class Scanning(
val personId: String,
val personName: String,
val progress: Int,
val total: Int,
val facesFound: Int,
val facesDetected: Int = 0
) : ScanningState()
data class Complete(
val personName: String,
val facesFound: Int,
val imagesScanned: Int,
val totalFacesDetected: Int = 0
) : ScanningState()
}
init {
loadPersons()
}
fun loadPersons() {
viewModelScope.launch {
try {
_uiState.value = InventoryUiState.Loading
val persons = faceRecognitionRepository.getPersonsWithFaceModels()
val personsWithStats = persons.mapNotNull { person ->
val stats = faceRecognitionRepository.getPersonFaceStats(person.id)
if (stats != null) {
PersonWithStats(person, stats)
} else {
null
}
}.sortedByDescending { it.stats.taggedPhotoCount }
_uiState.value = InventoryUiState.Success(personsWithStats)
} catch (e: Exception) {
_uiState.value = InventoryUiState.Error(
e.message ?: "Failed to load persons"
)
}
}
}
fun deletePerson(personId: String, faceModelId: String) {
viewModelScope.launch {
try {
faceRecognitionRepository.deleteFaceModel(faceModelId)
loadPersons()
} catch (e: Exception) {
_uiState.value = InventoryUiState.Error(
"Failed to delete: ${e.message}"
)
}
}
}
/**
* Scan library with SMART threshold selection
*/
fun scanLibraryForPerson(personId: String, faceModelId: String) {
viewModelScope.launch {
try {
if (DebugFlags.ENABLE_FACE_RECOGNITION_LOGGING) {
DiagnosticLogger.i("=== STARTING LIBRARY SCAN (ENHANCED) ===")
DiagnosticLogger.i("PersonId: $personId")
DiagnosticLogger.i("FaceModelId: $faceModelId")
}
val currentState = _uiState.value
val person = if (currentState is InventoryUiState.Success) {
currentState.persons.find { it.person.id == personId }?.person
} else null
val personName = person?.name ?: "Unknown"
// Get face model to determine training count
val faceModel = faceRecognitionRepository.getFaceModelById(faceModelId)
val trainingCount = faceModel?.trainingImageCount ?: 15
DiagnosticLogger.i("Training count: $trainingCount")
val allImages = imageRepository.getAllImages().first()
val totalImages = allImages.size
DiagnosticLogger.i("Total images in library: $totalImages")
_scanningState.value = ScanningState.Scanning(
personId = personId,
personName = personName,
progress = 0,
total = totalImages,
facesFound = 0,
facesDetected = 0
)
var facesFound = 0
var totalFacesDetected = 0
allImages.forEachIndexed { index, imageWithEverything ->
val image = imageWithEverything.image
DiagnosticLogger.d("--- Image ${index + 1}/$totalImages ---")
DiagnosticLogger.d("ImageId: ${image.imageId}")
// Detect faces with ML Kit
val detectedFaces = detectFacesInImage(image.imageUri)
totalFacesDetected += detectedFaces.size
DiagnosticLogger.d("Faces detected: ${detectedFaces.size}")
if (detectedFaces.isNotEmpty()) {
// ENHANCED: Calculate image quality
val imageQuality = ThresholdStrategy.estimateImageQuality(
width = image.width,
height = image.height
)
// ENHANCED: Estimate detection context
val detectionContext = ThresholdStrategy.estimateDetectionContext(
faceCount = detectedFaces.size,
faceAreaRatio = if (detectedFaces.isNotEmpty()) {
calculateFaceAreaRatio(detectedFaces[0], image.width, image.height)
} else 0f
)
// ENHANCED: Get smart threshold
val scanThreshold = if (DebugFlags.USE_LIBERAL_THRESHOLDS) {
ThresholdStrategy.getLiberalThreshold(trainingCount)
} else {
ThresholdStrategy.getOptimalThreshold(
trainingCount = trainingCount,
imageQuality = imageQuality,
detectionContext = detectionContext
)
}
DiagnosticLogger.d("Quality: $imageQuality, Context: $detectionContext")
DiagnosticLogger.d("Using threshold: $scanThreshold")
// Scan image with smart threshold
val tags = faceRecognitionRepository.scanImage(
imageId = image.imageId,
detectedFaces = detectedFaces,
threshold = scanThreshold
)
DiagnosticLogger.d("Tags created: ${tags.size}")
tags.forEach { tag ->
DiagnosticLogger.d(" Tag: model=${tag.faceModelId.take(8)}, conf=${String.format("%.3f", tag.confidence)}")
}
val matchingTags = tags.filter { it.faceModelId == faceModelId }
DiagnosticLogger.d("Matching tags for target: ${matchingTags.size}")
facesFound += matchingTags.size
}
_scanningState.value = ScanningState.Scanning(
personId = personId,
personName = personName,
progress = index + 1,
total = totalImages,
facesFound = facesFound,
facesDetected = totalFacesDetected
)
}
DiagnosticLogger.i("=== SCAN COMPLETE ===")
DiagnosticLogger.i("Images scanned: $totalImages")
DiagnosticLogger.i("Faces detected: $totalFacesDetected")
DiagnosticLogger.i("Faces matched: $facesFound")
DiagnosticLogger.i("Hit rate: ${if (totalFacesDetected > 0) (facesFound * 100 / totalFacesDetected) else 0}%")
_scanningState.value = ScanningState.Complete(
personName = personName,
facesFound = facesFound,
imagesScanned = totalImages,
totalFacesDetected = totalFacesDetected
)
loadPersons()
delay(3000)
_scanningState.value = ScanningState.Idle
} catch (e: Exception) {
DiagnosticLogger.e("Scan failed", e)
_scanningState.value = ScanningState.Idle
_uiState.value = InventoryUiState.Error(
"Scan failed: ${e.message}"
)
}
}
}
private suspend fun detectFacesInImage(imageUri: String): List<DetectedFace> = withContext(Dispatchers.Default) {
try {
val uri = Uri.parse(imageUri)
val inputStream = getApplication<Application>().contentResolver.openInputStream(uri)
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()
if (bitmap == null) {
DiagnosticLogger.w("Failed to load bitmap from: $imageUri")
return@withContext emptyList()
}
DiagnosticLogger.d("Bitmap: ${bitmap.width}x${bitmap.height}")
val image = InputImage.fromBitmap(bitmap, 0)
val faces = faceDetector.process(image).await()
DiagnosticLogger.d("ML Kit found ${faces.size} faces")
faces.mapNotNull { face ->
val boundingBox = face.boundingBox
val croppedFace = try {
val left = boundingBox.left.coerceAtLeast(0)
val top = boundingBox.top.coerceAtLeast(0)
val width = boundingBox.width().coerceAtMost(bitmap.width - left)
val height = boundingBox.height().coerceAtMost(bitmap.height - top)
if (width > 0 && height > 0) {
Bitmap.createBitmap(bitmap, left, top, width, height)
} else {
null
}
} catch (e: Exception) {
DiagnosticLogger.e("Face crop failed", e)
null
}
if (croppedFace != null) {
DetectedFace(
croppedBitmap = croppedFace,
boundingBox = boundingBox
)
} else {
null
}
}
} catch (e: Exception) {
DiagnosticLogger.e("Face detection failed: $imageUri", e)
emptyList()
}
}
/**
* Calculate face area ratio (for context detection)
*/
private fun calculateFaceAreaRatio(
face: DetectedFace,
imageWidth: Int,
imageHeight: Int
): Float {
val faceArea = face.boundingBox.width() * face.boundingBox.height()
val imageArea = imageWidth * imageHeight
return faceArea.toFloat() / imageArea.toFloat()
}
suspend fun getPersonImages(personId: String) =
faceRecognitionRepository.getImagesForPerson(personId)
override fun onCleared() {
super.onCleared()
faceDetector.close()
}
}

View File

@@ -1,46 +1,157 @@
package com.placeholder.sherpai2.ui.navigation package com.placeholder.sherpai2.ui.navigation
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Label
import androidx.compose.material.icons.filled.* import androidx.compose.material.icons.filled.*
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
/** /**
* Drawer-only metadata. * AppDestinations - Navigation metadata for drawer UI
* *
* These objects: * Clean, organized structure:
* - Drive the drawer UI * - Routes for navigation
* - Provide labels and icons * - Icons for visual identity
* - Map cleanly to navigation routes * - Labels for display
* - Descriptions for clarity
* - Grouped by function
*/ */
sealed class AppDestinations( sealed class AppDestinations(
val route: String, val route: String,
val icon: ImageVector, val icon: ImageVector,
val label: String val label: String,
val description: String = ""
) { ) {
object Tour : AppDestinations(AppRoutes.TOUR, Icons.Default.PhotoLibrary, "Tour")
object Search : AppDestinations(AppRoutes.SEARCH, Icons.Default.Search, "Search")
object Models : AppDestinations(AppRoutes.MODELS, Icons.Default.Layers, "Models")
object Inventory : AppDestinations(AppRoutes.INVENTORY, Icons.Default.Inventory2, "Inv")
object Train : AppDestinations(AppRoutes.TRAIN, Icons.Default.TrackChanges, "Train")
object Tags : AppDestinations(AppRoutes.TAGS, Icons.Default.LocalOffer, "Tags")
object ImageDetails : AppDestinations(AppRoutes.IMAGE_DETAIL, Icons.Default.LocalOffer, "IMAGE_DETAIL") // ==================
// PHOTO BROWSING
// ==================
object Upload : AppDestinations(AppRoutes.UPLOAD, Icons.Default.CloudUpload, "Upload") data object Search : AppDestinations(
object Settings : AppDestinations(AppRoutes.SETTINGS, Icons.Default.Settings, "Settings") route = AppRoutes.SEARCH,
icon = Icons.Default.Search,
label = "Search",
description = "Find photos by tag or person"
)
data object Explore : AppDestinations(
route = AppRoutes.EXPLORE,
icon = Icons.Default.Explore,
label = "Explore",
description = "Browse smart albums"
)
// ImageDetail is not in drawer (internal navigation only)
// ==================
// FACE RECOGNITION
// ==================
data object Inventory : AppDestinations(
route = AppRoutes.INVENTORY,
icon = Icons.Default.Face,
label = "People",
description = "Trained face models"
)
data object Train : AppDestinations(
route = AppRoutes.TRAIN,
icon = Icons.Default.ModelTraining,
label = "Train",
description = "Train new person"
)
data object Models : AppDestinations(
route = AppRoutes.MODELS,
icon = Icons.Default.SmartToy,
label = "Models",
description = "AI model management"
)
// ==================
// ORGANIZATION
// ==================
data object Tags : AppDestinations(
route = AppRoutes.TAGS,
icon = Icons.AutoMirrored.Filled.Label,
label = "Tags",
description = "Manage photo tags"
)
data object Upload : AppDestinations(
route = AppRoutes.UPLOAD,
icon = Icons.Default.UploadFile,
label = "Upload",
description = "Add new photos"
)
// ==================
// SETTINGS
// ==================
data object Settings : AppDestinations(
route = AppRoutes.SETTINGS,
icon = Icons.Default.Settings,
label = "Settings",
description = "App preferences"
)
} }
val mainDrawerItems = listOf( /**
AppDestinations.Tour, * Organized destination groups for beautiful drawer sections
*/
// Photo browsing section
val photoDestinations = listOf(
AppDestinations.Search, AppDestinations.Search,
AppDestinations.Models, AppDestinations.Explore
AppDestinations.Inventory,
AppDestinations.Train,
AppDestinations.Tags,
AppDestinations.ImageDetails
) )
val utilityDrawerItems = listOf( // Face recognition section
AppDestinations.Upload, val faceRecognitionDestinations = listOf(
AppDestinations.Settings AppDestinations.Inventory,
AppDestinations.Train,
AppDestinations.Models
) )
// Organization section
val organizationDestinations = listOf(
AppDestinations.Tags,
AppDestinations.Upload
)
// Settings (separate, pinned to bottom)
val settingsDestination = AppDestinations.Settings
/**
* All drawer items (excludes Settings which is handled separately)
*/
val allMainDrawerDestinations = photoDestinations + faceRecognitionDestinations + organizationDestinations
/**
* Helper function to get destination by route
* Useful for highlighting current route in drawer
*/
fun getDestinationByRoute(route: String?): AppDestinations? {
return when (route) {
AppRoutes.SEARCH -> AppDestinations.Search
AppRoutes.EXPLORE -> AppDestinations.Explore
AppRoutes.INVENTORY -> AppDestinations.Inventory
AppRoutes.TRAIN -> AppDestinations.Train
AppRoutes.MODELS -> AppDestinations.Models
AppRoutes.TAGS -> AppDestinations.Tags
AppRoutes.UPLOAD -> AppDestinations.Upload
AppRoutes.SETTINGS -> AppDestinations.Settings
else -> null
}
}
/**
* Legacy support (for backwards compatibility)
* These match your old structure
*/
@Deprecated("Use organized groups instead", ReplaceWith("allMainDrawerDestinations"))
val mainDrawerItems = allMainDrawerDestinations
@Deprecated("Use settingsDestination instead", ReplaceWith("listOf(settingsDestination)"))
val utilityDrawerItems = listOf(settingsDestination)

View File

@@ -2,35 +2,47 @@ package com.placeholder.sherpai2.ui.navigation
import android.net.Uri import android.net.Uri
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.ViewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.NavType import androidx.navigation.NavType
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
import androidx.navigation.navArgument import androidx.navigation.navArgument
import com.placeholder.sherpai2.ui.devscreens.DummyScreen import com.placeholder.sherpai2.ui.devscreens.DummyScreen
import com.placeholder.sherpai2.ui.explore.ExploreScreen
import com.placeholder.sherpai2.ui.imagedetail.ImageDetailScreen import com.placeholder.sherpai2.ui.imagedetail.ImageDetailScreen
import com.placeholder.sherpai2.ui.modelinventory.PersonInventoryScreen
import com.placeholder.sherpai2.ui.search.SearchScreen import com.placeholder.sherpai2.ui.search.SearchScreen
import com.placeholder.sherpai2.ui.search.SearchViewModel import com.placeholder.sherpai2.ui.search.SearchViewModel
import java.net.URLDecoder import com.placeholder.sherpai2.ui.tags.TagManagementScreen
import java.net.URLEncoder
import com.placeholder.sherpai2.ui.tour.TourViewModel
import com.placeholder.sherpai2.ui.tour.TourScreen
import com.placeholder.sherpai2.ui.trainingprep.ImageSelectorScreen import com.placeholder.sherpai2.ui.trainingprep.ImageSelectorScreen
import com.placeholder.sherpai2.ui.trainingprep.TrainingScreen import com.placeholder.sherpai2.ui.trainingprep.ScanResultsScreen
import com.placeholder.sherpai2.ui.navigation.AppRoutes
import com.placeholder.sherpai2.ui.navigation.AppRoutes.ScanResultsScreen
import com.placeholder.sherpai2.ui.trainingprep.ScanningState import com.placeholder.sherpai2.ui.trainingprep.ScanningState
import com.placeholder.sherpai2.ui.trainingprep.TrainViewModel import com.placeholder.sherpai2.ui.trainingprep.TrainViewModel
import androidx.compose.runtime.LaunchedEffect import com.placeholder.sherpai2.ui.trainingprep.TrainingScreen
import androidx.compose.runtime.collectAsState import java.net.URLDecoder
import androidx.compose.runtime.getValue import java.net.URLEncoder
import com.placeholder.sherpai2.ui.trainingprep.ScanResultsScreen
/**
* AppNavHost - Main navigation graph
* UPDATED: Added Explore and Tags screens
*
* Complete flow:
* - Photo browsing (Search, Explore, Detail)
* - Face recognition (Inventory, Train)
* - Organization (Tags, Upload)
* - Settings
*
* Features:
* - URL encoding for safe navigation
* - Proper back stack management
* - State preservation
* - Beautiful placeholders
*/
@Composable @Composable
fun AppNavHost( fun AppNavHost(
navController: NavHostController, navController: NavHostController,
@@ -42,20 +54,44 @@ fun AppNavHost(
modifier = modifier modifier = modifier
) { ) {
/** SEARCH SCREEN **/ // ==========================================
// PHOTO BROWSING
// ==========================================
/**
* SEARCH SCREEN
* Main photo browser with face tag search
*/
composable(AppRoutes.SEARCH) { composable(AppRoutes.SEARCH) {
val searchViewModel: SearchViewModel = hiltViewModel() val searchViewModel: SearchViewModel = hiltViewModel()
SearchScreen( SearchScreen(
searchViewModel = searchViewModel, searchViewModel = searchViewModel,
onImageClick = { imageUri -> onImageClick = { imageUri ->
// Encode the URI to safely pass as argument
val encodedUri = URLEncoder.encode(imageUri, "UTF-8") val encodedUri = URLEncoder.encode(imageUri, "UTF-8")
navController.navigate("${AppRoutes.IMAGE_DETAIL}/$encodedUri") navController.navigate("${AppRoutes.IMAGE_DETAIL}/$encodedUri")
} }
) )
} }
/** IMAGE DETAIL SCREEN **/ /**
* EXPLORE SCREEN
* Browse smart albums (auto-generated from tags)
*/
composable(AppRoutes.EXPLORE) {
ExploreScreen(
onAlbumClick = { albumType, albumId ->
println("Album clicked: type=$albumType id=$albumId")
// Example future navigation
// navController.navigate("${AppRoutes.ALBUM}/$albumType/$albumId")
}
)
}
/**
* IMAGE DETAIL SCREEN
* Single photo view with metadata
*/
composable( composable(
route = "${AppRoutes.IMAGE_DETAIL}/{imageUri}", route = "${AppRoutes.IMAGE_DETAIL}/{imageUri}",
arguments = listOf( arguments = listOf(
@@ -64,8 +100,6 @@ fun AppNavHost(
} }
) )
) { backStackEntry -> ) { backStackEntry ->
// Decode URI to restore original value
val imageUri = backStackEntry.arguments?.getString("imageUri") val imageUri = backStackEntry.arguments?.getString("imageUri")
?.let { URLDecoder.decode(it, "UTF-8") } ?.let { URLDecoder.decode(it, "UTF-8") }
?: error("imageUri missing from navigation") ?: error("imageUri missing from navigation")
@@ -76,70 +110,142 @@ fun AppNavHost(
) )
} }
composable(AppRoutes.TOUR) { // ==========================================
val tourViewModel: TourViewModel = hiltViewModel() // FACE RECOGNITION SYSTEM
TourScreen( // ==========================================
tourViewModel = tourViewModel,
onImageClick = { imageUri -> /**
navController.navigate("${AppRoutes.IMAGE_DETAIL}/$imageUri") * PERSON INVENTORY SCREEN
* View all trained face models
*
* Features:
* - List all trained people
* - Show stats (training count, tagged photos, confidence)
* - Delete models
* - View photos containing each person
*/
composable(AppRoutes.INVENTORY) {
PersonInventoryScreen(
onViewPersonPhotos = { personId ->
// Navigate back to search
// TODO: In future, add person filter to search screen
navController.navigate(AppRoutes.SEARCH)
} }
) )
} }
/** TRAINING FLOW **/ /**
* TRAINING FLOW
* Train new face recognition model
*
* Flow:
* 1. TrainingScreen (select images button)
* 2. ImageSelectorScreen (pick 15-50 photos)
* 3. ScanResultsScreen (validation + name input)
* 4. Training completes → navigate to Inventory
*/
composable(AppRoutes.TRAIN) { entry -> composable(AppRoutes.TRAIN) { entry ->
val trainViewModel: TrainViewModel = hiltViewModel() val trainViewModel: TrainViewModel = hiltViewModel()
val uiState by trainViewModel.uiState.collectAsState() val uiState by trainViewModel.uiState.collectAsState()
// Observe the result from the ImageSelector // Get images selected from ImageSelector
val selectedUris = entry.savedStateHandle.get<List<Uri>>("selected_image_uris") val selectedUris = entry.savedStateHandle.get<List<Uri>>("selected_image_uris")
// If we have new URIs and we are currently Idle, start scanning // Start scanning when new images are selected
LaunchedEffect(selectedUris) { LaunchedEffect(selectedUris) {
if (selectedUris != null && uiState is ScanningState.Idle) { if (selectedUris != null && uiState is ScanningState.Idle) {
trainViewModel.scanAndTagFaces(selectedUris) trainViewModel.scanAndTagFaces(selectedUris)
// Clear the handle so it doesn't re-trigger on configuration change
entry.savedStateHandle.remove<List<Uri>>("selected_image_uris") entry.savedStateHandle.remove<List<Uri>>("selected_image_uris")
} }
} }
if (uiState is ScanningState.Idle) { when (uiState) {
// Initial state: Show start button or prompt is ScanningState.Idle -> {
TrainingScreen( // Show start screen with "Select Images" button
onSelectImages = { navController.navigate(AppRoutes.IMAGE_SELECTOR) } TrainingScreen(
) onSelectImages = {
} else { navController.navigate(AppRoutes.IMAGE_SELECTOR)
// Processing or Success state: Show the results screen
ScanResultsScreen(
state = uiState,
onFinish = {
navController.navigate(AppRoutes.SEARCH) {
popUpTo(AppRoutes.TRAIN) { inclusive = true }
} }
} )
) }
else -> {
// Show validation results and training UI
ScanResultsScreen(
state = uiState,
onFinish = {
// After training, go to inventory to see new person
navController.navigate(AppRoutes.INVENTORY) {
popUpTo(AppRoutes.TRAIN) { inclusive = true }
}
}
)
}
} }
} }
/**
* IMAGE SELECTOR SCREEN
* Pick images for training (internal screen)
*/
composable(AppRoutes.IMAGE_SELECTOR) { composable(AppRoutes.IMAGE_SELECTOR) {
ImageSelectorScreen( ImageSelectorScreen(
onImagesSelected = { uris -> onImagesSelected = { uris ->
// Pass selected URIs back to Train screen
navController.previousBackStackEntry navController.previousBackStackEntry
?.savedStateHandle ?.savedStateHandle
?.set("selected_image_uris", uris) ?.set("selected_image_uris", uris)
navController.popBackStack() navController.popBackStack()
} }
) )
} }
/** DUMMY SCREENS FOR OTHER DRAWER ITEMS **/ /**
//composable(AppRoutes.TOUR) { DummyScreen("Tour (stub)") } * MODELS SCREEN
composable(AppRoutes.MODELS) { DummyScreen("Models (stub)") } * AI model management (placeholder)
composable(AppRoutes.INVENTORY) { DummyScreen("Inventory (stub)") } */
//composable(AppRoutes.TRAIN) { DummyScreen("Train (stub)") } composable(AppRoutes.MODELS) {
composable(AppRoutes.TAGS) { DummyScreen("Tags (stub)") } DummyScreen(
composable(AppRoutes.UPLOAD) { DummyScreen("Upload (stub)") } title = "AI Models",
composable(AppRoutes.SETTINGS) { DummyScreen("Settings (stub)") } subtitle = "Manage face recognition models"
)
}
// ==========================================
// ORGANIZATION
// ==========================================
/**
* TAGS SCREEN
* Manage photo tags with auto-tagging features
*/
composable(AppRoutes.TAGS) {
TagManagementScreen()
}
/**
* UPLOAD SCREEN
* Import new photos (placeholder)
*/
composable(AppRoutes.UPLOAD) {
DummyScreen(
title = "Upload",
subtitle = "Add photos to your library"
)
}
// ==========================================
// SETTINGS
// ==========================================
/**
* SETTINGS SCREEN
* App preferences (placeholder)
*/
composable(AppRoutes.SETTINGS) {
DummyScreen(
title = "Settings",
subtitle = "App preferences and configuration"
)
}
} }
} }

View File

@@ -11,22 +11,26 @@ package com.placeholder.sherpai2.ui.navigation
* - Keeps NavHost decoupled from icons / labels * - Keeps NavHost decoupled from icons / labels
*/ */
object AppRoutes { object AppRoutes {
const val TOUR = "tour" // Photo browsing
const val SEARCH = "search" const val SEARCH = "search"
const val MODELS = "models" const val EXPLORE = "explore" // UPDATED: Changed from TOUR
const val INVENTORY = "inv"
const val TRAIN = "train"
const val TAGS = "tags"
const val UPLOAD = "upload"
const val SETTINGS = "settings"
const val IMAGE_DETAIL = "IMAGE_DETAIL" const val IMAGE_DETAIL = "IMAGE_DETAIL"
const val CROP_SCREEN = "CROP_SCREEN" // Face recognition
const val INVENTORY = "inv"
const val TRAIN = "train"
const val MODELS = "models"
// Organization
const val TAGS = "tags"
const val UPLOAD = "upload"
// Settings
const val SETTINGS = "settings"
// Internal training flow screens
const val IMAGE_SELECTOR = "Image Selection" const val IMAGE_SELECTOR = "Image Selection"
const val CROP_SCREEN = "CROP_SCREEN"
const val TRAINING_SCREEN = "TRAINING_SCREEN" const val TRAINING_SCREEN = "TRAINING_SCREEN"
const val ScanResultsScreen = "First Scan Results" const val ScanResultsScreen = "First Scan Results"
}
//const val IMAGE_DETAIL = "IMAGE_DETAIL"
}

View File

@@ -1,85 +1,243 @@
package com.placeholder.sherpai2.ui.presentation package com.placeholder.sherpai2.ui.presentation
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.material3.DividerDefaults
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Label import androidx.compose.material.icons.automirrored.filled.Label
import androidx.compose.material.icons.automirrored.filled.List
import androidx.compose.material.icons.filled.* import androidx.compose.material.icons.filled.*
import androidx.compose.material3.HorizontalDivider
import com.placeholder.sherpai2.ui.navigation.AppRoutes import com.placeholder.sherpai2.ui.navigation.AppRoutes
/**
* Beautiful app drawer with sections, gradient header, and polish
* UPDATED: Tour → Explore
*/
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun AppDrawerContent( fun AppDrawerContent(
currentRoute: String?, currentRoute: String?,
onDestinationClicked: (String) -> Unit onDestinationClicked: (String) -> Unit
) { ) {
// Drawer sheet with fixed width ModalDrawerSheet(
ModalDrawerSheet(modifier = Modifier.width(280.dp)) { modifier = Modifier.width(300.dp),
drawerContainerColor = MaterialTheme.colorScheme.surface
) {
Column(modifier = Modifier.fillMaxSize()) {
// Header / Logo // ===== BEAUTIFUL GRADIENT HEADER =====
Text( Box(
"SherpAI Control Panel", modifier = Modifier
style = MaterialTheme.typography.headlineSmall, .fillMaxWidth()
modifier = Modifier.padding(16.dp) .background(
) Brush.verticalGradient(
colors = listOf(
MaterialTheme.colorScheme.primaryContainer,
MaterialTheme.colorScheme.surface
)
)
)
.padding(24.dp)
) {
Column(
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
// App icon/logo area
Surface(
modifier = Modifier.size(56.dp),
shape = RoundedCornerShape(16.dp),
color = MaterialTheme.colorScheme.primary,
shadowElevation = 4.dp
) {
Box(contentAlignment = Alignment.Center) {
Icon(
Icons.Default.Face,
contentDescription = null,
modifier = Modifier.size(32.dp),
tint = MaterialTheme.colorScheme.onPrimary
)
}
}
HorizontalDivider( Text(
Modifier.fillMaxWidth(), "SherpAI",
thickness = DividerDefaults.Thickness, style = MaterialTheme.typography.headlineMedium,
color = DividerDefaults.color fontWeight = FontWeight.Bold,
) color = MaterialTheme.colorScheme.onSurface
)
// Main drawer items Text(
val mainItems = listOf( "Face Recognition System",
Triple(AppRoutes.SEARCH, "Search", Icons.Default.Search), style = MaterialTheme.typography.bodyMedium,
Triple(AppRoutes.TOUR, "Tour", Icons.Default.Place), color = MaterialTheme.colorScheme.onSurfaceVariant
Triple(AppRoutes.MODELS, "Models", Icons.Default.ModelTraining), )
Triple(AppRoutes.INVENTORY, "Inventory", Icons.AutoMirrored.Filled.List), }
Triple(AppRoutes.TRAIN, "Train", Icons.Default.Train),
Triple(AppRoutes.TAGS, "Tags", Icons.AutoMirrored.Filled.Label)
)
Column(modifier = Modifier.padding(vertical = 8.dp)) {
mainItems.forEach { (route, label, icon) ->
NavigationDrawerItem(
label = { Text(label) },
icon = { Icon(icon, contentDescription = label) },
selected = route == currentRoute,
onClick = { onDestinationClicked(route) },
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
)
} }
}
Divider( Spacer(modifier = Modifier.height(8.dp))
Modifier
.fillMaxWidth()
.padding(vertical = 8.dp),
thickness = DividerDefaults.Thickness
)
// Utility items // ===== NAVIGATION SECTIONS =====
val utilityItems = listOf( Column(
Triple(AppRoutes.UPLOAD, "Upload", Icons.Default.UploadFile), modifier = Modifier
Triple(AppRoutes.SETTINGS, "Settings", Icons.Default.Settings) .fillMaxWidth()
) .weight(1f)
.padding(horizontal = 12.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
Column(modifier = Modifier.padding(vertical = 8.dp)) { // Photos Section
utilityItems.forEach { (route, label, icon) -> DrawerSection(title = "Photos")
NavigationDrawerItem(
label = { Text(label) }, val photoItems = listOf(
icon = { Icon(icon, contentDescription = label) }, DrawerItem(AppRoutes.SEARCH, "Search", Icons.Default.Search, "Find photos by tag or person"),
selected = route == currentRoute, DrawerItem(AppRoutes.EXPLORE, "Explore", Icons.Default.Explore, "Browse smart albums")
onClick = { onDestinationClicked(route) },
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
) )
photoItems.forEach { item ->
DrawerNavigationItem(
item = item,
selected = item.route == currentRoute,
onClick = { onDestinationClicked(item.route) }
)
}
Spacer(modifier = Modifier.height(8.dp))
// Face Recognition Section
DrawerSection(title = "Face Recognition")
val faceItems = listOf(
DrawerItem(AppRoutes.INVENTORY, "People", Icons.Default.Face, "Trained face models"),
DrawerItem(AppRoutes.TRAIN, "Train", Icons.Default.ModelTraining, "Train new person"),
DrawerItem(AppRoutes.MODELS, "Models", Icons.Default.SmartToy, "AI model management")
)
faceItems.forEach { item ->
DrawerNavigationItem(
item = item,
selected = item.route == currentRoute,
onClick = { onDestinationClicked(item.route) }
)
}
Spacer(modifier = Modifier.height(8.dp))
// Organization Section
DrawerSection(title = "Organization")
val orgItems = listOf(
DrawerItem(AppRoutes.TAGS, "Tags", Icons.AutoMirrored.Filled.Label, "Manage photo tags"),
DrawerItem(AppRoutes.UPLOAD, "Upload", Icons.Default.UploadFile, "Add new photos")
)
orgItems.forEach { item ->
DrawerNavigationItem(
item = item,
selected = item.route == currentRoute,
onClick = { onDestinationClicked(item.route) }
)
}
Spacer(modifier = Modifier.weight(1f))
// Settings at bottom
HorizontalDivider(
modifier = Modifier.padding(vertical = 8.dp),
color = MaterialTheme.colorScheme.outlineVariant
)
DrawerNavigationItem(
item = DrawerItem(
AppRoutes.SETTINGS,
"Settings",
Icons.Default.Settings,
"App preferences"
),
selected = AppRoutes.SETTINGS == currentRoute,
onClick = { onDestinationClicked(AppRoutes.SETTINGS) }
)
Spacer(modifier = Modifier.height(8.dp))
} }
} }
} }
} }
/**
* Section header in drawer
*/
@Composable
private fun DrawerSection(title: String) {
Text(
text = title,
style = MaterialTheme.typography.labelMedium,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
)
}
/**
* Individual navigation item with icon, label, and subtitle
*/
@Composable
private fun DrawerNavigationItem(
item: DrawerItem,
selected: Boolean,
onClick: () -> Unit
) {
NavigationDrawerItem(
label = {
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
Text(
text = item.label,
style = MaterialTheme.typography.bodyLarge,
fontWeight = if (selected) FontWeight.SemiBold else FontWeight.Normal
)
item.subtitle?.let {
Text(
text = it,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f)
)
}
}
},
icon = {
Icon(
item.icon,
contentDescription = item.label,
modifier = Modifier.size(24.dp)
)
},
selected = selected,
onClick = onClick,
modifier = Modifier
.padding(NavigationDrawerItemDefaults.ItemPadding)
.clip(RoundedCornerShape(12.dp)),
colors = NavigationDrawerItemDefaults.colors(
selectedContainerColor = MaterialTheme.colorScheme.primaryContainer,
selectedIconColor = MaterialTheme.colorScheme.primary,
selectedTextColor = MaterialTheme.colorScheme.onPrimaryContainer,
unselectedContainerColor = Color.Transparent
)
)
}
/**
* Data class for drawer items
*/
private data class DrawerItem(
val route: String,
val label: String,
val icon: androidx.compose.ui.graphics.vector.ImageVector,
val subtitle: String? = null
)

View File

@@ -1,32 +1,37 @@
package com.placeholder.sherpai2.ui.presentation package com.placeholder.sherpai2.ui.presentation
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu import androidx.compose.material.icons.filled.*
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.navigation.NavController import androidx.compose.ui.text.font.FontWeight
import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.placeholder.sherpai2.ui.navigation.AppNavHost import com.placeholder.sherpai2.ui.navigation.AppNavHost
import com.placeholder.sherpai2.ui.navigation.AppRoutes import com.placeholder.sherpai2.ui.navigation.AppRoutes
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
/**
* Beautiful main screen with gradient header, dynamic actions, and polish
*/
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun MainScreen() { fun MainScreen() {
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed) val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
// Navigation controller for NavHost
val navController = rememberNavController() val navController = rememberNavController()
// Track current backstack entry to update top bar title dynamically
val navBackStackEntry by navController.currentBackStackEntryAsState() val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route ?: AppRoutes.SEARCH val currentRoute = navBackStackEntry?.destination?.route ?: AppRoutes.SEARCH
// Drawer content for navigation
ModalNavigationDrawer( ModalNavigationDrawer(
drawerState = drawerState, drawerState = drawerState,
drawerContent = { drawerContent = {
@@ -37,7 +42,6 @@ fun MainScreen() {
drawerState.close() drawerState.close()
if (route != currentRoute) { if (route != currentRoute) {
navController.navigate(route) { navController.navigate(route) {
// Avoid multiple copies of the same destination
launchSingleTop = true launchSingleTop = true
} }
} }
@@ -46,17 +50,120 @@ fun MainScreen() {
) )
}, },
) { ) {
// Main scaffold with top bar
Scaffold( Scaffold(
topBar = { topBar = {
TopAppBar( TopAppBar(
title = { Text(currentRoute.replaceFirstChar { it.uppercase() }) }, title = {
Column {
Text(
text = getScreenTitle(currentRoute),
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
getScreenSubtitle(currentRoute)?.let { subtitle ->
Text(
text = subtitle,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
},
navigationIcon = { navigationIcon = {
IconButton(onClick = { scope.launch { drawerState.open() } }) { IconButton(
Icon(Icons.Filled.Menu, contentDescription = "Open Drawer") onClick = { scope.launch { drawerState.open() } }
) {
Icon(
Icons.Default.Menu,
contentDescription = "Open Menu",
tint = MaterialTheme.colorScheme.primary
)
}
},
actions = {
// Dynamic actions based on current screen
when (currentRoute) {
AppRoutes.SEARCH -> {
IconButton(onClick = { /* TODO: Open filter dialog */ }) {
Icon(
Icons.Default.FilterList,
contentDescription = "Filter",
tint = MaterialTheme.colorScheme.primary
)
}
}
AppRoutes.INVENTORY -> {
IconButton(onClick = {
navController.navigate(AppRoutes.TRAIN)
}) {
Icon(
Icons.Default.PersonAdd,
contentDescription = "Add Person",
tint = MaterialTheme.colorScheme.primary
)
}
}
AppRoutes.TAGS -> {
IconButton(onClick = { /* TODO: Add tag */ }) {
Icon(
Icons.Default.Add,
contentDescription = "Add Tag",
tint = MaterialTheme.colorScheme.primary
)
}
}
}
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.surface,
titleContentColor = MaterialTheme.colorScheme.onSurface,
navigationIconContentColor = MaterialTheme.colorScheme.primary,
actionIconContentColor = MaterialTheme.colorScheme.primary
)
)
},
floatingActionButton = {
// Dynamic FAB based on screen
AnimatedVisibility(
visible = shouldShowFab(currentRoute),
enter = slideInVertically(initialOffsetY = { it }) + fadeIn(),
exit = slideOutVertically(targetOffsetY = { it }) + fadeOut()
) {
when (currentRoute) {
AppRoutes.SEARCH -> {
ExtendedFloatingActionButton(
onClick = { /* TODO: Advanced search */ },
icon = {
Icon(Icons.Default.Tune, "Advanced Search")
},
text = { Text("Filters") },
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer
)
}
AppRoutes.TAGS -> {
FloatingActionButton(
onClick = { /* TODO: Add new tag */ },
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer
) {
Icon(Icons.Default.Add, "Add Tag")
}
}
AppRoutes.UPLOAD -> {
ExtendedFloatingActionButton(
onClick = { /* TODO: Select photos */ },
icon = { Icon(Icons.Default.CloudUpload, "Upload") },
text = { Text("Select Photos") },
containerColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary
)
}
else -> {
// No FAB for other screens
} }
} }
) }
} }
) { paddingValues -> ) { paddingValues ->
AppNavHost( AppNavHost(
@@ -66,3 +173,47 @@ fun MainScreen() {
} }
} }
} }
/**
* Get human-readable screen title
*/
private fun getScreenTitle(route: String): String {
return when (route) {
AppRoutes.SEARCH -> "Search"
AppRoutes.EXPLORE -> "Explore" // Will be renamed to EXPLORE
AppRoutes.INVENTORY -> "People"
AppRoutes.TRAIN -> "Train New Person"
AppRoutes.MODELS -> "AI Models"
AppRoutes.TAGS -> "Tag Management"
AppRoutes.UPLOAD -> "Upload Photos"
AppRoutes.SETTINGS -> "Settings"
else -> "SherpAI"
}
}
/**
* Get subtitle for screens that need context
*/
private fun getScreenSubtitle(route: String): String? {
return when (route) {
AppRoutes.SEARCH -> "Find photos by tags, people, or date"
AppRoutes.EXPLORE -> "Browse your collection"
AppRoutes.INVENTORY -> "Trained face models"
AppRoutes.TRAIN -> "Add a new person to recognize"
AppRoutes.TAGS -> "Organize your photo collection"
AppRoutes.UPLOAD -> "Add photos to your library"
else -> null
}
}
/**
* Determine if FAB should be shown for current screen
*/
private fun shouldShowFab(route: String): Boolean {
return when (route) {
AppRoutes.SEARCH,
AppRoutes.TAGS,
AppRoutes.UPLOAD -> true
else -> false
}
}

View File

@@ -1,70 +1,478 @@
package com.placeholder.sherpai2.ui.search package com.placeholder.sherpai2.ui.search
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.* import androidx.compose.foundation.lazy.grid.*
import androidx.compose.foundation.lazy.items
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.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.placeholder.sherpai2.ui.search.components.ImageGridItem import com.placeholder.sherpai2.ui.search.components.ImageGridItem
import com.placeholder.sherpai2.ui.search.SearchViewModel
/** /**
* SearchScreen * SearchScreen - COMPLETE REDESIGN
* *
* Purpose: * Features:
* - Validate tag-based queries * - Near-match search ("low" → "low_res")
* - Preview matching images * - Quick tag filter chips
* * - Date range filtering
* This is NOT final UX. * - Clean person-only display
* It is a diagnostic surface. * - Simple/Verbose toggle
*/ */
@OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun SearchScreen( fun SearchScreen(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
searchViewModel: SearchViewModel, searchViewModel: SearchViewModel,
onImageClick: (String) -> Unit onImageClick: (String) -> Unit,
onAlbumClick: (String) -> Unit = {} // For opening album view
) { ) {
var query by remember { mutableStateOf("") } val searchQuery by searchViewModel.searchQuery.collectAsStateWithLifecycle()
val activeTagFilters by searchViewModel.activeTagFilters.collectAsStateWithLifecycle()
val dateRange by searchViewModel.dateRange.collectAsStateWithLifecycle()
val displayMode by searchViewModel.displayMode.collectAsStateWithLifecycle()
val systemTags by searchViewModel.systemTags.collectAsStateWithLifecycle()
/**
* Reactive result set.
* Updates whenever:
* - query changes
* - database changes
*/
val images by searchViewModel val images by searchViewModel
.searchImagesByTag(query) .searchImages()
.collectAsStateWithLifecycle(initialValue = emptyList()) .collectAsStateWithLifecycle(initialValue = emptyList())
Column( Scaffold { paddingValues ->
modifier = modifier Column(
.fillMaxSize() modifier = modifier
.padding(12.dp) .fillMaxSize()
) { .padding(paddingValues)
OutlinedTextField(
value = query,
onValueChange = { query = it },
label = { Text("Search by tag") },
modifier = Modifier.fillMaxWidth(),
singleLine = true
)
Spacer(modifier = Modifier.height(12.dp))
LazyVerticalGrid(
columns = GridCells.Adaptive(120.dp),
contentPadding = PaddingValues(4.dp),
verticalArrangement = Arrangement.spacedBy(4.dp),
horizontalArrangement = Arrangement.spacedBy(4.dp),
modifier = Modifier.fillMaxSize()
) { ) {
items(images) { imageWithEverything -> // Header with gradient
ImageGridItem(image = imageWithEverything.image) Box(
modifier = Modifier
.fillMaxWidth()
.background(
Brush.verticalGradient(
colors = listOf(
MaterialTheme.colorScheme.primaryContainer,
MaterialTheme.colorScheme.surface
)
)
)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
// Title
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth()
) {
Column {
Text(
text = "Search Photos",
style = MaterialTheme.typography.headlineMedium,
fontWeight = FontWeight.Bold
)
Text(
text = "Near-match • Filters • Smart tags",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
// Simple/Verbose toggle
IconButton(
onClick = { searchViewModel.toggleDisplayMode() }
) {
Icon(
imageVector = if (displayMode == DisplayMode.SIMPLE) {
Icons.Default.ViewList
} else {
Icons.Default.ViewModule
},
contentDescription = "Toggle view mode",
tint = MaterialTheme.colorScheme.primary
)
}
}
// Search bar
OutlinedTextField(
value = searchQuery,
onValueChange = { searchViewModel.setSearchQuery(it) },
placeholder = { Text("Search... (e.g., 'low', 'gro', 'nig')") },
leadingIcon = {
Icon(Icons.Default.Search, contentDescription = null)
},
trailingIcon = {
if (searchQuery.isNotEmpty()) {
IconButton(onClick = { searchViewModel.setSearchQuery("") }) {
Icon(Icons.Default.Clear, contentDescription = "Clear")
}
}
},
modifier = Modifier.fillMaxWidth(),
singleLine = true,
shape = RoundedCornerShape(16.dp)
)
}
}
// Quick Tag Filters
if (systemTags.isNotEmpty()) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Quick Filters",
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.primary
)
if (activeTagFilters.isNotEmpty()) {
TextButton(onClick = { searchViewModel.clearTagFilters() }) {
Text("Clear all")
}
}
}
LazyRow(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
items(systemTags) { tag ->
val isActive = tag.value in activeTagFilters
FilterChip(
selected = isActive,
onClick = { searchViewModel.toggleTagFilter(tag.value) },
label = {
Row(
horizontalArrangement = Arrangement.spacedBy(4.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = getTagEmoji(tag.value),
style = MaterialTheme.typography.bodyMedium
)
Text(
text = tag.value.replace("_", " "),
style = MaterialTheme.typography.bodySmall
)
}
},
leadingIcon = if (isActive) {
{ Icon(Icons.Default.Check, null, Modifier.size(16.dp)) }
} else null
)
}
}
}
}
// Date Range Filters
LazyRow(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
items(DateRange.entries) { range ->
val isActive = dateRange == range
FilterChip(
selected = isActive,
onClick = { searchViewModel.setDateRange(range) },
label = { Text(range.displayName) },
leadingIcon = if (isActive) {
{ Icon(Icons.Default.DateRange, null, Modifier.size(16.dp)) }
} else null
)
}
}
HorizontalDivider(
modifier = Modifier.padding(horizontal = 16.dp),
color = MaterialTheme.colorScheme.outlineVariant
)
// Results
if (images.isEmpty() && searchQuery.isBlank() && activeTagFilters.isEmpty()) {
EmptySearchState()
} else if (images.isEmpty()) {
NoResultsState(
query = searchQuery,
hasFilters = activeTagFilters.isNotEmpty() || dateRange != DateRange.ALL_TIME
)
} else {
Column {
// Results header
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "${images.size} ${if (images.size == 1) "photo" else "photos"}",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)
// View Album button (if search results can be grouped)
if (activeTagFilters.size == 1 || searchQuery.isNotBlank()) {
TextButton(
onClick = {
val albumTag = activeTagFilters.firstOrNull() ?: searchQuery
onAlbumClick(albumTag)
}
) {
Icon(
Icons.Default.Collections,
contentDescription = null,
modifier = Modifier.size(16.dp)
)
Spacer(Modifier.width(4.dp))
Text("View Album")
}
}
}
// Photo grid
LazyVerticalGrid(
columns = GridCells.Adaptive(120.dp),
contentPadding = PaddingValues(12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp),
modifier = Modifier.fillMaxSize()
) {
items(
items = images,
key = { it.image.imageId }
) { imageWithFaceTags ->
PhotoCard(
imageWithFaceTags = imageWithFaceTags,
displayMode = displayMode,
onImageClick = onImageClick
)
}
}
}
} }
} }
} }
} }
/**
* Photo card with clean person display
*/
@Composable
private fun PhotoCard(
imageWithFaceTags: ImageWithFaceTags,
displayMode: DisplayMode,
onImageClick: (String) -> Unit
) {
Card(
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(12.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
) {
Column {
// Image
ImageGridItem(
image = imageWithFaceTags.image,
onClick = { onImageClick(imageWithFaceTags.image.imageUri) }
)
// Person tags
if (imageWithFaceTags.persons.isNotEmpty()) {
when (displayMode) {
DisplayMode.SIMPLE -> {
// SIMPLE: Just names, no icons, no percentages
Surface(
color = MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.3f),
modifier = Modifier.fillMaxWidth()
) {
Text(
text = imageWithFaceTags.persons
.take(3)
.joinToString(", ") { it.name },
style = MaterialTheme.typography.bodySmall,
modifier = Modifier.padding(8.dp),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
DisplayMode.VERBOSE -> {
// VERBOSE: Icons + names + confidence
Surface(
color = MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.5f),
modifier = Modifier.fillMaxWidth()
) {
Column(
modifier = Modifier.padding(8.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
imageWithFaceTags.persons
.take(3)
.forEachIndexed { index, person ->
Row(
horizontalArrangement = Arrangement.spacedBy(6.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Icons.Default.Face,
contentDescription = null,
modifier = Modifier.size(14.dp),
tint = MaterialTheme.colorScheme.primary
)
Text(
text = person.name,
style = MaterialTheme.typography.bodySmall,
modifier = Modifier.weight(1f),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
if (index < imageWithFaceTags.faceTags.size) {
val confidence = (imageWithFaceTags.faceTags[index].confidence * 100).toInt()
Text(
text = "$confidence%",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.primary
)
}
}
}
if (imageWithFaceTags.persons.size > 3) {
Text(
text = "+${imageWithFaceTags.persons.size - 3} more",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.primary
)
}
}
}
}
}
}
}
}
}
@Composable
private fun EmptySearchState() {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp),
modifier = Modifier.padding(32.dp)
) {
Icon(
Icons.Default.Search,
contentDescription = null,
modifier = Modifier.size(80.dp),
tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.3f)
)
Text(
text = "Search or filter photos",
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
Text(
text = "Try searching or tapping quick filters",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
@Composable
private fun NoResultsState(query: String, hasFilters: Boolean) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp),
modifier = Modifier.padding(32.dp)
) {
Icon(
Icons.Default.SearchOff,
contentDescription = null,
modifier = Modifier.size(80.dp),
tint = MaterialTheme.colorScheme.error.copy(alpha = 0.5f)
)
Text(
text = "No results found",
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
if (query.isNotBlank()) {
Text(
text = "No matches for \"$query\"",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
if (hasFilters) {
Text(
text = "Try removing some filters",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
}
/**
* Get emoji for tag type
*/
private fun getTagEmoji(tagValue: String): String {
return when (tagValue) {
"night" -> "🌙"
"morning" -> "🌅"
"afternoon" -> "☀️"
"evening" -> "🌇"
"indoor" -> "🏠"
"outdoor" -> "🌲"
"group_photo" -> "👥"
"selfie" -> "🤳"
"couple" -> "💑"
"family" -> "👨‍👩‍👧"
"friend" -> "🤝"
"birthday" -> "🎂"
"high_res" -> ""
"low_res" -> "📦"
"landscape" -> "🖼️"
"portrait" -> "📱"
"square" -> ""
else -> "🏷️"
}
}

View File

@@ -1,24 +1,288 @@
package com.placeholder.sherpai2.ui.search package com.placeholder.sherpai2.ui.search
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.placeholder.sherpai2.data.local.dao.TagDao
import com.placeholder.sherpai2.data.local.entity.ImageEntity
import com.placeholder.sherpai2.data.local.entity.PersonEntity
import com.placeholder.sherpai2.data.local.entity.PhotoFaceTagEntity
import com.placeholder.sherpai2.data.local.entity.TagEntity
import com.placeholder.sherpai2.data.repository.FaceRecognitionRepository
import com.placeholder.sherpai2.domain.repository.ImageRepository import com.placeholder.sherpai2.domain.repository.ImageRepository
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import java.util.Calendar
import javax.inject.Inject import javax.inject.Inject
/** /**
* SearchViewModel * SearchViewModel - COMPLETE REDESIGN
* *
* Stateless except for query-driven flows. * Features:
* - Near-match search ("low" → "low_res", "gro" → "group_photo")
* - Date range filtering
* - Quick tag filters
* - Clean person-only display
* - Simple/Verbose toggle
*/ */
@HiltViewModel @HiltViewModel
class SearchViewModel @Inject constructor( class SearchViewModel @Inject constructor(
private val imageRepository: ImageRepository private val imageRepository: ImageRepository,
private val faceRecognitionRepository: FaceRecognitionRepository,
private val tagDao: TagDao
) : ViewModel() { ) : ViewModel() {
fun searchImagesByTag(tag: String) = // Search query with near-match support
if (tag.isBlank()) { private val _searchQuery = MutableStateFlow("")
imageRepository.getAllImages() val searchQuery: StateFlow<String> = _searchQuery.asStateFlow()
} else {
imageRepository.findImagesByTag(tag) // Active tag filters (quick chips)
private val _activeTagFilters = MutableStateFlow<Set<String>>(emptySet())
val activeTagFilters: StateFlow<Set<String>> = _activeTagFilters.asStateFlow()
// Date range filter
private val _dateRange = MutableStateFlow(DateRange.ALL_TIME)
val dateRange: StateFlow<DateRange> = _dateRange.asStateFlow()
// Display mode (simple = names only, verbose = icons + percentages)
private val _displayMode = MutableStateFlow(DisplayMode.SIMPLE)
val displayMode: StateFlow<DisplayMode> = _displayMode.asStateFlow()
// Available system tags for quick filters
private val _systemTags = MutableStateFlow<List<TagEntity>>(emptyList())
val systemTags: StateFlow<List<TagEntity>> = _systemTags.asStateFlow()
init {
loadSystemTags()
}
/**
* Main search flow - combines query, tag filters, and date range
*/
fun searchImages(): Flow<List<ImageWithFaceTags>> {
return combine(
_searchQuery,
_activeTagFilters,
_dateRange
) { query, tagFilters, dateRange ->
Triple(query, tagFilters, dateRange)
}.flatMapLatest { (query, tagFilters, dateRange) ->
channelFlow {
// Get matching tags FIRST (suspend call)
val matchingTags = if (query.isNotBlank()) {
findMatchingTags(query)
} else {
emptyList()
}
// Get base images
val imagesFlow = when {
matchingTags.isNotEmpty() -> {
// Search by all matching tags
combine(matchingTags.map { tag ->
imageRepository.findImagesByTag(tag.value)
}) { results ->
results.flatMap { it }.distinctBy { it.image.imageId }
}
}
tagFilters.isNotEmpty() -> {
// Filter by active tags
combine(tagFilters.map { tagValue ->
imageRepository.findImagesByTag(tagValue)
}) { results ->
results.flatMap { it }.distinctBy { it.image.imageId }
}
}
else -> imageRepository.getAllImages()
}
// Apply date filtering and add face data
imagesFlow.collect { imagesList ->
val filtered = imagesList
.filter { imageWithEverything ->
isInDateRange(imageWithEverything.image.capturedAt, dateRange)
}
.map { imageWithEverything ->
// Get face tags with person info
val tagsWithPersons = faceRecognitionRepository.getFaceTagsWithPersons(
imageWithEverything.image.imageId
)
ImageWithFaceTags(
image = imageWithEverything.image,
faceTags = tagsWithPersons.map { it.first },
persons = tagsWithPersons.map { it.second }
)
}
.sortedByDescending { it.image.capturedAt }
send(filtered)
}
}
} }
}
/**
* Near-match search: "low" matches "low_res", "gro" matches "group_photo"
*/
private suspend fun findMatchingTags(query: String): List<TagEntity> {
val normalizedQuery = query.trim().lowercase()
// Get all system tags
val allTags = tagDao.getByType("SYSTEM")
// Find tags that contain the query or match it closely
return allTags.filter { tag ->
val tagValue = tag.value.lowercase()
// Exact match
tagValue == normalizedQuery ||
// Contains match
tagValue.contains(normalizedQuery) ||
// Starts with match
tagValue.startsWith(normalizedQuery) ||
// Fuzzy match (remove underscores and compare)
tagValue.replace("_", "").contains(normalizedQuery.replace("_", ""))
}.sortedBy { tag ->
// Sort by relevance: exact > starts with > contains
when {
tag.value.lowercase() == normalizedQuery -> 0
tag.value.lowercase().startsWith(normalizedQuery) -> 1
else -> 2
}
}
}
/**
* Load available system tags for quick filters
*/
private fun loadSystemTags() {
viewModelScope.launch {
val tags = tagDao.getByType("SYSTEM")
// Get usage counts for all tags
val tagsWithUsage = tags.map { tag ->
tag to tagDao.getTagUsageCount(tag.tagId)
}
// Sort by most commonly used
val sortedTags = tagsWithUsage
.sortedByDescending { (_, usageCount) -> usageCount }
.take(12) // Show top 12 most used tags
.map { (tag, _) -> tag }
_systemTags.value = sortedTags
}
}
/**
* Update search query
*/
fun setSearchQuery(query: String) {
_searchQuery.value = query
}
/**
* Toggle a tag filter
*/
fun toggleTagFilter(tagValue: String) {
_activeTagFilters.value = if (tagValue in _activeTagFilters.value) {
_activeTagFilters.value - tagValue
} else {
_activeTagFilters.value + tagValue
}
}
/**
* Clear all tag filters
*/
fun clearTagFilters() {
_activeTagFilters.value = emptySet()
}
/**
* Set date range filter
*/
fun setDateRange(range: DateRange) {
_dateRange.value = range
}
/**
* Toggle display mode (simple/verbose)
*/
fun toggleDisplayMode() {
_displayMode.value = when (_displayMode.value) {
DisplayMode.SIMPLE -> DisplayMode.VERBOSE
DisplayMode.VERBOSE -> DisplayMode.SIMPLE
}
}
/**
* Check if timestamp is in date range
*/
private fun isInDateRange(timestamp: Long, range: DateRange): Boolean {
return when (range) {
DateRange.ALL_TIME -> true
DateRange.TODAY -> isToday(timestamp)
DateRange.THIS_WEEK -> isThisWeek(timestamp)
DateRange.THIS_MONTH -> isThisMonth(timestamp)
DateRange.THIS_YEAR -> isThisYear(timestamp)
}
}
private fun isToday(timestamp: Long): Boolean {
val today = Calendar.getInstance()
val date = Calendar.getInstance().apply { timeInMillis = timestamp }
return today.get(Calendar.YEAR) == date.get(Calendar.YEAR) &&
today.get(Calendar.DAY_OF_YEAR) == date.get(Calendar.DAY_OF_YEAR)
}
private fun isThisWeek(timestamp: Long): Boolean {
val today = Calendar.getInstance()
val date = Calendar.getInstance().apply { timeInMillis = timestamp }
return today.get(Calendar.YEAR) == date.get(Calendar.YEAR) &&
today.get(Calendar.WEEK_OF_YEAR) == date.get(Calendar.WEEK_OF_YEAR)
}
private fun isThisMonth(timestamp: Long): Boolean {
val today = Calendar.getInstance()
val date = Calendar.getInstance().apply { timeInMillis = timestamp }
return today.get(Calendar.YEAR) == date.get(Calendar.YEAR) &&
today.get(Calendar.MONTH) == date.get(Calendar.MONTH)
}
private fun isThisYear(timestamp: Long): Boolean {
val today = Calendar.getInstance()
val date = Calendar.getInstance().apply { timeInMillis = timestamp }
return today.get(Calendar.YEAR) == date.get(Calendar.YEAR)
}
} }
/**
* Data class containing image with face recognition data
*/
data class ImageWithFaceTags(
val image: ImageEntity,
val faceTags: List<PhotoFaceTagEntity>,
val persons: List<PersonEntity>
)
/**
* Date range filters
*/
enum class DateRange(val displayName: String) {
ALL_TIME("All Time"),
TODAY("Today"),
THIS_WEEK("This Week"),
THIS_MONTH("This Month"),
THIS_YEAR("This Year")
}
/**
* Display modes for photo tags
*/
enum class DisplayMode {
SIMPLE, // Just person names
VERBOSE // Names + icons + confidence percentages
}

View File

@@ -0,0 +1,624 @@
package com.placeholder.sherpai2.ui.tags
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
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.items
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.graphics.Brush
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.placeholder.sherpai2.data.local.entity.TagWithUsage
@Composable
fun TagManagementScreen(
viewModel: TagManagementViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsState()
val scanningState by viewModel.scanningState.collectAsState()
var showAddTagDialog by remember { mutableStateOf(false) }
var showScanMenu by remember { mutableStateOf(false) }
var searchQuery by remember { mutableStateOf("") }
Scaffold(
floatingActionButton = {
// Single extended FAB with dropdown menu
var showMenu by remember { mutableStateOf(false) }
Column(
horizontalAlignment = Alignment.End,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
// Dropdown menu for scan options
if (showMenu) {
Card(
modifier = Modifier.width(180.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
) {
Column {
ListItem(
headlineContent = { Text("Scan All", style = MaterialTheme.typography.bodyMedium) },
leadingContent = {
Icon(
Icons.Default.AutoFixHigh,
null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(20.dp)
)
},
modifier = Modifier.clickable {
viewModel.scanForAllTags()
showMenu = false
}
)
ListItem(
headlineContent = { Text("Base Tags", style = MaterialTheme.typography.bodyMedium) },
leadingContent = {
Icon(
Icons.Default.PhotoCamera,
null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(20.dp)
)
},
modifier = Modifier.clickable {
viewModel.scanForBaseTags()
showMenu = false
}
)
ListItem(
headlineContent = { Text("Relationships", style = MaterialTheme.typography.bodyMedium) },
leadingContent = {
Icon(
Icons.Default.People,
null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(20.dp)
)
},
modifier = Modifier.clickable {
viewModel.scanForRelationshipTags()
showMenu = false
}
)
ListItem(
headlineContent = { Text("Birthdays", style = MaterialTheme.typography.bodyMedium) },
leadingContent = {
Icon(
Icons.Default.Cake,
null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(20.dp)
)
},
modifier = Modifier.clickable {
viewModel.scanForBirthdayTags()
showMenu = false
}
)
}
}
}
// Main FAB
ExtendedFloatingActionButton(
onClick = { showMenu = !showMenu },
icon = {
Icon(
if (showMenu) Icons.Default.Close else Icons.Default.AutoFixHigh,
"Scan"
)
},
text = { Text(if (showMenu) "Close" else "Scan Tags") }
)
}
}
) { paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
) {
// Stats Bar
StatsBar(uiState)
// Search Bar
SearchBar(
searchQuery = searchQuery,
onSearchChange = {
searchQuery = it
viewModel.searchTags(it)
}
)
// Scanning Progress
AnimatedVisibility(
visible = scanningState !is TagManagementViewModel.TagScanningState.Idle,
enter = expandVertically() + fadeIn(),
exit = shrinkVertically() + fadeOut()
) {
ScanningProgress(scanningState, viewModel)
}
// Tag List
when (val state = uiState) {
is TagManagementViewModel.TagUiState.Loading -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}
is TagManagementViewModel.TagUiState.Success -> {
TagList(
tags = state.tags,
onDeleteTag = { viewModel.deleteTag(it) }
)
}
is TagManagementViewModel.TagUiState.Error -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(
text = state.message,
color = MaterialTheme.colorScheme.error
)
}
}
}
}
}
// Add Tag Dialog
if (showAddTagDialog) {
AddTagDialog(
onDismiss = { showAddTagDialog = false },
onConfirm = { tagName ->
viewModel.createUserTag(tagName)
showAddTagDialog = false
}
)
}
// Scan Menu
if (showScanMenu) {
ScanMenuDialog(
onDismiss = { showScanMenu = false },
onScanSelected = { scanType ->
when (scanType) {
TagManagementViewModel.ScanType.BASE_TAGS -> viewModel.scanForBaseTags()
TagManagementViewModel.ScanType.RELATIONSHIP_TAGS -> viewModel.scanForRelationshipTags()
TagManagementViewModel.ScanType.BIRTHDAY_TAGS -> viewModel.scanForBirthdayTags()
TagManagementViewModel.ScanType.SCENE_TAGS -> viewModel.scanForSceneTags()
TagManagementViewModel.ScanType.ALL -> viewModel.scanForAllTags()
}
showScanMenu = false
}
)
}
}
@Composable
private fun StatsBar(uiState: TagManagementViewModel.TagUiState) {
if (uiState is TagManagementViewModel.TagUiState.Success) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
horizontalArrangement = Arrangement.SpaceAround
) {
StatItem("Total", uiState.totalTags.toString(), Icons.Default.Label)
StatItem("System", uiState.systemTags.toString(), Icons.Default.AutoAwesome)
StatItem("User", uiState.userTags.toString(), Icons.Default.PersonOutline)
}
}
}
}
@Composable
private fun StatItem(label: String, value: String, icon: ImageVector) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Icon(
imageVector = icon,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(24.dp)
)
Spacer(modifier = Modifier.height(4.dp))
Text(
text = value,
style = MaterialTheme.typography.headlineSmall,
fontWeight = FontWeight.Bold
)
Text(
text = label,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
@Composable
private fun SearchBar(
searchQuery: String,
onSearchChange: (String) -> Unit
) {
OutlinedTextField(
value = searchQuery,
onValueChange = onSearchChange,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
placeholder = { Text("Search tags...") },
leadingIcon = { Icon(Icons.Default.Search, "Search") },
trailingIcon = {
if (searchQuery.isNotEmpty()) {
IconButton(onClick = { onSearchChange("") }) {
Icon(Icons.Default.Clear, "Clear")
}
}
},
singleLine = true
)
}
@Composable
private fun ScanningProgress(
scanningState: TagManagementViewModel.TagScanningState,
viewModel: TagManagementViewModel
) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer
)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
when (scanningState) {
is TagManagementViewModel.TagScanningState.Scanning -> {
Text(
text = "Scanning: ${scanningState.scanType.name.replace("_", " ")}",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.height(8.dp))
LinearProgressIndicator(
progress = { scanningState.progress.toFloat() / scanningState.total.toFloat() },
modifier = Modifier.fillMaxWidth(),
)
Spacer(modifier = Modifier.height(4.dp))
Text(
text = "${scanningState.progress} / ${scanningState.total} images",
style = MaterialTheme.typography.bodySmall
)
Text(
text = "Tags applied: ${scanningState.tagsApplied}",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.primary
)
}
is TagManagementViewModel.TagScanningState.Complete -> {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Column {
Text(
text = "✓ Scan Complete",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.primary
)
Text(
text = "${scanningState.tagsApplied} tags applied to ${scanningState.imagesProcessed} images",
style = MaterialTheme.typography.bodySmall
)
}
IconButton(onClick = { viewModel.resetScanningState() }) {
Icon(Icons.Default.Close, "Close")
}
}
}
is TagManagementViewModel.TagScanningState.Error -> {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Error: ${scanningState.message}",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.error
)
IconButton(onClick = { viewModel.resetScanningState() }) {
Icon(Icons.Default.Close, "Close")
}
}
}
else -> { /* Idle - don't show */ }
}
}
}
}
@Composable
private fun TagList(
tags: List<TagWithUsage>,
onDeleteTag: (String) -> Unit
) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
items(tags, key = { it.tagId }) { tag ->
TagListItem(tag, onDeleteTag)
}
}
}
@Composable
private fun TagListItem(
tag: TagWithUsage,
onDeleteTag: (String) -> Unit
) {
var showDeleteConfirm by remember { mutableStateOf(false) }
Card(
modifier = Modifier.fillMaxWidth(),
onClick = { /* TODO: Navigate to images with this tag */ }
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
// Tag type icon
Icon(
imageVector = if (tag.type == "SYSTEM") Icons.Default.AutoAwesome else Icons.Default.Label,
contentDescription = null,
tint = if (tag.type == "SYSTEM")
MaterialTheme.colorScheme.secondary
else
MaterialTheme.colorScheme.primary
)
Column {
Text(
text = tag.value,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Medium
)
Text(
text = if (tag.type == "SYSTEM") "System tag" else "User tag",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
// Usage count badge
Surface(
shape = RoundedCornerShape(12.dp),
color = MaterialTheme.colorScheme.primaryContainer
) {
Text(
text = tag.usageCount.toString(),
modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp),
style = MaterialTheme.typography.labelMedium,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onPrimaryContainer
)
}
// Delete button (only for user tags)
if (tag.type == "GENERIC") {
IconButton(onClick = { showDeleteConfirm = true }) {
Icon(
Icons.Default.Delete,
contentDescription = "Delete tag",
tint = MaterialTheme.colorScheme.error
)
}
}
}
}
}
if (showDeleteConfirm) {
AlertDialog(
onDismissRequest = { showDeleteConfirm = false },
title = { Text("Delete Tag?") },
text = { Text("Are you sure you want to delete '${tag.value}'? This will remove it from ${tag.usageCount} images.") },
confirmButton = {
TextButton(
onClick = {
onDeleteTag(tag.tagId)
showDeleteConfirm = false
}
) {
Text("Delete", color = MaterialTheme.colorScheme.error)
}
},
dismissButton = {
TextButton(onClick = { showDeleteConfirm = false }) {
Text("Cancel")
}
}
)
}
}
@Composable
private fun AddTagDialog(
onDismiss: () -> Unit,
onConfirm: (String) -> Unit
) {
var tagName by remember { mutableStateOf("") }
AlertDialog(
onDismissRequest = onDismiss,
title = { Text("Add New Tag") },
text = {
OutlinedTextField(
value = tagName,
onValueChange = { tagName = it },
label = { Text("Tag name") },
singleLine = true,
modifier = Modifier.fillMaxWidth()
)
},
confirmButton = {
TextButton(
onClick = { onConfirm(tagName) },
enabled = tagName.isNotBlank()
) {
Text("Add")
}
},
dismissButton = {
TextButton(onClick = onDismiss) {
Text("Cancel")
}
}
)
}
@Composable
private fun ScanMenuDialog(
onDismiss: () -> Unit,
onScanSelected: (TagManagementViewModel.ScanType) -> Unit
) {
AlertDialog(
onDismissRequest = onDismiss,
title = { Text("Scan for Tags") },
text = {
Column(
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
ScanOption(
title = "Base Tags",
description = "Face count, orientation, time, quality",
icon = Icons.Default.PhotoCamera,
onClick = { onScanSelected(TagManagementViewModel.ScanType.BASE_TAGS) }
)
ScanOption(
title = "Relationship Tags",
description = "Family, friends, colleagues",
icon = Icons.Default.People,
onClick = { onScanSelected(TagManagementViewModel.ScanType.RELATIONSHIP_TAGS) }
)
ScanOption(
title = "Birthday Tags",
description = "Photos near birthdays",
icon = Icons.Default.Cake,
onClick = { onScanSelected(TagManagementViewModel.ScanType.BIRTHDAY_TAGS) }
)
ScanOption(
title = "Scene Tags",
description = "Indoor/outdoor detection",
icon = Icons.Default.Landscape,
onClick = { onScanSelected(TagManagementViewModel.ScanType.SCENE_TAGS) }
)
Divider()
ScanOption(
title = "Scan All",
description = "Run all scans",
icon = Icons.Default.AutoFixHigh,
onClick = { onScanSelected(TagManagementViewModel.ScanType.ALL) }
)
}
},
confirmButton = {
TextButton(onClick = onDismiss) {
Text("Cancel")
}
}
)
}
@Composable
private fun ScanOption(
title: String,
description: String,
icon: ImageVector,
onClick: () -> Unit
) {
Card(
modifier = Modifier
.fillMaxWidth()
.clickable(onClick = onClick),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant
)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
imageVector = icon,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(32.dp)
)
Column {
Text(
text = title,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Medium
)
Text(
text = description,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
}

View File

@@ -0,0 +1,398 @@
package com.placeholder.sherpai2.ui.tags
import android.app.Application
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.face.FaceDetection
import com.google.mlkit.vision.face.FaceDetectorOptions
import com.placeholder.sherpai2.data.local.dao.ImageTagDao
import com.placeholder.sherpai2.data.local.dao.TagDao
import com.placeholder.sherpai2.data.local.entity.TagEntity
import com.placeholder.sherpai2.data.local.entity.TagWithUsage
import com.placeholder.sherpai2.data.repository.DetectedFace
import com.placeholder.sherpai2.data.service.AutoTaggingService
import com.placeholder.sherpai2.domain.repository.ImageRepository
import com.placeholder.sherpai2.util.DiagnosticLogger
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
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.withContext
import kotlinx.coroutines.tasks.await
import javax.inject.Inject
@HiltViewModel
class TagManagementViewModel @Inject constructor(
application: Application,
private val tagDao: TagDao,
private val imageTagDao: ImageTagDao,
private val imageRepository: ImageRepository,
private val autoTaggingService: AutoTaggingService
) : AndroidViewModel(application) {
private val _uiState = MutableStateFlow<TagUiState>(TagUiState.Loading)
val uiState: StateFlow<TagUiState> = _uiState.asStateFlow()
private val _scanningState = MutableStateFlow<TagScanningState>(TagScanningState.Idle)
val scanningState: StateFlow<TagScanningState> = _scanningState.asStateFlow()
private val faceDetector by lazy {
val options = FaceDetectorOptions.Builder()
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE)
.setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_NONE)
.setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_NONE)
.setMinFaceSize(0.10f)
.build()
FaceDetection.getClient(options)
}
sealed class TagUiState {
object Loading : TagUiState()
data class Success(
val tags: List<TagWithUsage>,
val totalTags: Int,
val systemTags: Int,
val userTags: Int
) : TagUiState()
data class Error(val message: String) : TagUiState()
}
sealed class TagScanningState {
object Idle : TagScanningState()
data class Scanning(
val scanType: ScanType,
val progress: Int,
val total: Int,
val tagsApplied: Int,
val currentImage: String = ""
) : TagScanningState()
data class Complete(
val scanType: ScanType,
val imagesProcessed: Int,
val tagsApplied: Int,
val newTagsCreated: Int = 0
) : TagScanningState()
data class Error(val message: String) : TagScanningState()
}
enum class ScanType {
BASE_TAGS, // Face count, orientation, resolution, time-of-day
RELATIONSHIP_TAGS, // Family, friend, colleague from person entities
BIRTHDAY_TAGS, // Birthday tags for DOB matches
SCENE_TAGS, // Indoor/outdoor estimation
ALL // Run all scans
}
init {
loadTags()
}
fun loadTags() {
viewModelScope.launch {
try {
_uiState.value = TagUiState.Loading
val tagsWithUsage = tagDao.getMostUsedTags(1000) // Get all tags
val systemTags = tagsWithUsage.count { it.type == "SYSTEM" }
val userTags = tagsWithUsage.count { it.type == "GENERIC" }
_uiState.value = TagUiState.Success(
tags = tagsWithUsage,
totalTags = tagsWithUsage.size,
systemTags = systemTags,
userTags = userTags
)
} catch (e: Exception) {
_uiState.value = TagUiState.Error(
e.message ?: "Failed to load tags"
)
}
}
}
fun createUserTag(tagName: String) {
viewModelScope.launch {
try {
val trimmedName = tagName.trim().lowercase()
if (trimmedName.isEmpty()) {
_uiState.value = TagUiState.Error("Tag name cannot be empty")
return@launch
}
// Check if tag already exists
val existing = tagDao.getByValue(trimmedName)
if (existing != null) {
_uiState.value = TagUiState.Error("Tag '$trimmedName' already exists")
return@launch
}
val newTag = TagEntity.createUserTag(trimmedName)
tagDao.insert(newTag)
loadTags()
} catch (e: Exception) {
_uiState.value = TagUiState.Error(
"Failed to create tag: ${e.message}"
)
}
}
}
fun deleteTag(tagId: String) {
viewModelScope.launch {
try {
tagDao.delete(tagId)
loadTags()
} catch (e: Exception) {
_uiState.value = TagUiState.Error(
"Failed to delete tag: ${e.message}"
)
}
}
}
fun searchTags(query: String) {
viewModelScope.launch {
try {
val results = if (query.isBlank()) {
tagDao.getMostUsedTags(1000)
} else {
tagDao.searchTagsWithUsage(query, 100)
}
val systemTags = results.count { it.type == "SYSTEM" }
val userTags = results.count { it.type == "GENERIC" }
_uiState.value = TagUiState.Success(
tags = results,
totalTags = results.size,
systemTags = systemTags,
userTags = userTags
)
} catch (e: Exception) {
_uiState.value = TagUiState.Error("Search failed: ${e.message}")
}
}
}
// ======================
// AUTO-TAGGING SCANS
// ======================
/**
* Scan library for base tags (face count, orientation, time, quality, scene)
*/
fun scanForBaseTags() {
performScan(ScanType.BASE_TAGS)
}
/**
* Scan for relationship tags (family, friend, colleague)
*/
fun scanForRelationshipTags() {
performScan(ScanType.RELATIONSHIP_TAGS)
}
/**
* Scan for birthday tags
*/
fun scanForBirthdayTags() {
performScan(ScanType.BIRTHDAY_TAGS)
}
/**
* Scan for scene tags (indoor/outdoor)
*/
fun scanForSceneTags() {
performScan(ScanType.SCENE_TAGS)
}
/**
* Scan for ALL tags
*/
fun scanForAllTags() {
performScan(ScanType.ALL)
}
private fun performScan(scanType: ScanType) {
viewModelScope.launch {
try {
DiagnosticLogger.i("=== STARTING TAG SCAN: $scanType ===")
_scanningState.value = TagScanningState.Scanning(
scanType = scanType,
progress = 0,
total = 0,
tagsApplied = 0
)
val allImages = imageRepository.getAllImages().first()
var tagsApplied = 0
var newTagsCreated = 0
DiagnosticLogger.i("Processing ${allImages.size} images")
allImages.forEachIndexed { index, imageWithEverything ->
val image = imageWithEverything.image
_scanningState.value = TagScanningState.Scanning(
scanType = scanType,
progress = index + 1,
total = allImages.size,
tagsApplied = tagsApplied,
currentImage = image.imageId.take(8)
)
when (scanType) {
ScanType.BASE_TAGS -> {
tagsApplied += scanImageForBaseTags(image.imageUri, image)
}
ScanType.SCENE_TAGS -> {
tagsApplied += scanImageForSceneTags(image.imageUri, image)
}
ScanType.RELATIONSHIP_TAGS -> {
// Handled at person level, not per-image
}
ScanType.BIRTHDAY_TAGS -> {
// Handled at person level, not per-image
}
ScanType.ALL -> {
tagsApplied += scanImageForBaseTags(image.imageUri, image)
tagsApplied += scanImageForSceneTags(image.imageUri, image)
}
}
}
// Handle person-level scans
if (scanType == ScanType.RELATIONSHIP_TAGS || scanType == ScanType.ALL) {
DiagnosticLogger.i("Scanning relationship tags...")
tagsApplied += autoTaggingService.autoTagAllRelationships()
}
if (scanType == ScanType.BIRTHDAY_TAGS || scanType == ScanType.ALL) {
DiagnosticLogger.i("Scanning birthday tags...")
tagsApplied += autoTaggingService.autoTagAllBirthdays(daysRange = 3)
}
DiagnosticLogger.i("=== SCAN COMPLETE ===")
DiagnosticLogger.i("Images processed: ${allImages.size}")
DiagnosticLogger.i("Tags applied: $tagsApplied")
_scanningState.value = TagScanningState.Complete(
scanType = scanType,
imagesProcessed = allImages.size,
tagsApplied = tagsApplied,
newTagsCreated = newTagsCreated
)
loadTags()
} catch (e: Exception) {
DiagnosticLogger.e("Scan failed", e)
_scanningState.value = TagScanningState.Error(
"Scan failed: ${e.message}"
)
}
}
}
private suspend fun scanImageForBaseTags(
imageUri: String,
image: com.placeholder.sherpai2.data.local.entity.ImageEntity
): Int = withContext(Dispatchers.Default) {
try {
val uri = Uri.parse(imageUri)
val inputStream = getApplication<Application>().contentResolver.openInputStream(uri)
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()
if (bitmap == null) return@withContext 0
// Detect faces
val detectedFaces = detectFaces(bitmap)
// Auto-tag with base tags
autoTaggingService.autoTagImage(image, bitmap, detectedFaces)
} catch (e: Exception) {
DiagnosticLogger.e("Base tag scan failed for $imageUri", e)
0
}
}
private suspend fun scanImageForSceneTags(
imageUri: String,
image: com.placeholder.sherpai2.data.local.entity.ImageEntity
): Int = withContext(Dispatchers.Default) {
try {
val uri = Uri.parse(imageUri)
val inputStream = getApplication<Application>().contentResolver.openInputStream(uri)
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()
if (bitmap == null) return@withContext 0
// Only auto-tag scene tags (indoor/outdoor already included in autoTagImage)
// This is a subset of base tags, so we don't need separate logic
0
} catch (e: Exception) {
DiagnosticLogger.e("Scene tag scan failed for $imageUri", e)
0
}
}
private suspend fun detectFaces(bitmap: android.graphics.Bitmap): List<DetectedFace> = withContext(Dispatchers.Default) {
try {
val image = InputImage.fromBitmap(bitmap, 0)
val faces = faceDetector.process(image).await()
faces.mapNotNull { face ->
val boundingBox = face.boundingBox
val croppedFace = try {
val left = boundingBox.left.coerceAtLeast(0)
val top = boundingBox.top.coerceAtLeast(0)
val width = boundingBox.width().coerceAtMost(bitmap.width - left)
val height = boundingBox.height().coerceAtMost(bitmap.height - top)
if (width > 0 && height > 0) {
android.graphics.Bitmap.createBitmap(bitmap, left, top, width, height)
} else {
null
}
} catch (e: Exception) {
null
}
if (croppedFace != null) {
DetectedFace(
croppedBitmap = croppedFace,
boundingBox = boundingBox
)
} else {
null
}
}
} catch (e: Exception) {
emptyList()
}
}
fun resetScanningState() {
_scanningState.value = TagScanningState.Idle
}
override fun onCleared() {
super.onCleared()
faceDetector.close()
}
}

View File

@@ -41,6 +41,7 @@ fun FacePickerScreen(
// from the Bitmap scale to the UI View scale. // from the Bitmap scale to the UI View scale.
Canvas(modifier = Modifier.fillMaxSize().clickable { /* Handle general tap */ }) { Canvas(modifier = Modifier.fillMaxSize().clickable { /* Handle general tap */ }) {
// Implementation of coordinate mapping goes here // Implementation of coordinate mapping goes here
// TODO implement coordinate mapping
} }
// Simplified: Just show the options as a list of crops if Canvas mapping is too complex for now // Simplified: Just show the options as a list of crops if Canvas mapping is too complex for now

View File

@@ -3,128 +3,338 @@ package com.placeholder.sherpai2.ui.trainingprep
import android.net.Uri import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddPhotoAlternate import androidx.compose.material.icons.filled.*
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.material3.Text
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import coil.compose.AsyncImage
import androidx.compose.foundation.lazy.grid.items
/**
* Enhanced ImageSelectorScreen
*
* Changes:
* - NO LIMIT on photo count (was 10)
* - Recommends 20-30 photos
* - Real-time progress feedback
* - Quality indicators
* - Training tips
*/
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun ImageSelectorScreen( fun ImageSelectorScreen(
onImagesSelected: (List<Uri>) -> Unit onImagesSelected: (List<Uri>) -> Unit
) { ) {
//1. Persist state across configuration changes var selectedImages by remember { mutableStateOf<List<Uri>>(emptyList()) }
var selectedUris by rememberSaveable { mutableStateOf<List<Uri>>(emptyList()) }
val context = LocalContext.current
val launcher = rememberLauncherForActivityResult( val photoPicker = rememberLauncherForActivityResult(
ActivityResultContracts.OpenMultipleDocuments() contract = ActivityResultContracts.GetMultipleContents()
) { uris -> ) { uris ->
// 2. Take first 10 and try to persist permissions if (uris.isNotEmpty()) {
val limitedUris = uris.take(10) selectedImages = uris
selectedUris = limitedUris }
} }
Scaffold( Scaffold(
topBar = { TopAppBar(title = { Text("Select Training Photos") }) } topBar = {
) { padding -> TopAppBar(
title = { Text("Select Training Photos") },
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
)
)
}
) { paddingValues ->
Column( Column(
modifier = Modifier modifier = Modifier
.padding(padding) .fillMaxSize()
.padding(16.dp) .padding(paddingValues)
.fillMaxSize(), .padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp) verticalArrangement = Arrangement.spacedBy(16.dp)
) { ) {
OutlinedCard(
onClick = { launcher.launch(arrayOf("image/*")) }, // Gradient header with tips
modifier = Modifier.fillMaxWidth() Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
),
shape = RoundedCornerShape(16.dp)
) { ) {
Column( Column(
modifier = Modifier.padding(24.dp), modifier = Modifier.padding(20.dp),
horizontalAlignment = Alignment.CenterHorizontally verticalArrangement = Arrangement.spacedBy(12.dp)
) { ) {
Icon(Icons.Default.AddPhotoAlternate, contentDescription = null) Row(
Spacer(Modifier.height(8.dp)) horizontalArrangement = Arrangement.spacedBy(12.dp),
Text("Select up to 10 images of the person") verticalAlignment = Alignment.CenterVertically
) {
Surface(
shape = RoundedCornerShape(12.dp),
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(48.dp)
) {
Box(contentAlignment = Alignment.Center) {
Icon(
Icons.Default.PhotoCamera,
contentDescription = null,
tint = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.size(28.dp)
)
}
}
Column {
Text(
"Training Tips",
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
Text(
"More photos = better recognition",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.7f)
)
}
}
Spacer(Modifier.height(4.dp))
TipItem("✓ Select 20-30 photos for best results", true)
TipItem("✓ Include different angles and lighting", true)
TipItem("✓ Mix expressions (smile, neutral, laugh)", true)
TipItem("✓ With/without glasses if applicable", true)
TipItem("✗ Avoid blurry or very dark photos", false)
}
}
// Progress indicator
AnimatedVisibility(selectedImages.isNotEmpty()) {
ProgressCard(selectedImages.size)
}
Spacer(Modifier.weight(1f))
// Select photos button
Button(
onClick = { photoPicker.launch("image/*") },
modifier = Modifier.fillMaxWidth(),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.primary
),
contentPadding = PaddingValues(vertical = 16.dp)
) {
Icon(Icons.Default.PhotoLibrary, contentDescription = null)
Spacer(Modifier.width(8.dp))
Text(
if (selectedImages.isEmpty()) {
"Select Training Photos"
} else {
"Selected: ${selectedImages.size} photos - Tap to change"
},
style = MaterialTheme.typography.titleMedium
)
}
// Continue button
AnimatedVisibility(selectedImages.size >= 15) {
Button(
onClick = { onImagesSelected(selectedImages) },
modifier = Modifier.fillMaxWidth(),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.secondary
),
contentPadding = PaddingValues(vertical = 16.dp)
) {
Icon(Icons.Default.Check, contentDescription = null)
Spacer(Modifier.width(8.dp))
Text( Text(
text = "${selectedUris.size} / 10 selected", "Continue with ${selectedImages.size} photos",
style = MaterialTheme.typography.labelLarge, style = MaterialTheme.typography.titleMedium
color = if (selectedUris.size == 10) MaterialTheme.colorScheme.error
else if (selectedUris.isNotEmpty()) MaterialTheme.colorScheme.primary
else MaterialTheme.colorScheme.outline
) )
} }
} }
// 3. Conditional rendering for empty state // Minimum warning
if (selectedUris.isEmpty()) { if (selectedImages.isNotEmpty() && selectedImages.size < 15) {
Box(Modifier Card(
.weight(1f) modifier = Modifier.fillMaxWidth(),
.fillMaxWidth(), contentAlignment = Alignment.Center) { colors = CardDefaults.cardColors(
Text("No images selected", style = MaterialTheme.typography.bodyMedium) containerColor = MaterialTheme.colorScheme.errorContainer
} )
} else {
LazyVerticalGrid(
columns = GridCells.Fixed(3),
modifier = Modifier.weight(1f),
contentPadding = PaddingValues(4.dp)
) { ) {
items(selectedUris, key = { it.toString() }) { uri -> Row(
Box(modifier = Modifier.padding(4.dp)) { modifier = Modifier.padding(16.dp),
AsyncImage( horizontalArrangement = Arrangement.spacedBy(12.dp),
model = uri, verticalAlignment = Alignment.CenterVertically
contentDescription = null, ) {
modifier = Modifier Icon(
.aspectRatio(1f) Icons.Default.Warning,
.clip(RoundedCornerShape(8.dp)), contentDescription = null,
contentScale = ContentScale.Crop tint = MaterialTheme.colorScheme.error
)
Column {
Text(
"Need at least 15 photos",
style = MaterialTheme.typography.titleSmall,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onErrorContainer
)
Text(
"You have ${selectedImages.size}. Select ${15 - selectedImages.size} more.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onErrorContainer.copy(alpha = 0.8f)
) )
// 4. Ability to remove specific images
Surface(
onClick = { selectedUris = selectedUris - uri },
modifier = Modifier
.align(Alignment.TopEnd)
.padding(4.dp),
shape = CircleShape,
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.8f)
) {
Icon(
Icons.Default.Close,
contentDescription = "Remove",
modifier = Modifier.size(16.dp)
)
}
} }
} }
} }
} }
Button(
modifier = Modifier.fillMaxWidth(),
enabled = selectedUris.isNotEmpty(),
onClick = { onImagesSelected(selectedUris) }
) {
Text("Start Face Detection")
}
} }
} }
} }
@Composable
private fun TipItem(text: String, isGood: Boolean) {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.Top
) {
Icon(
if (isGood) Icons.Default.CheckCircle else Icons.Default.Cancel,
contentDescription = null,
modifier = Modifier.size(18.dp),
tint = if (isGood) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.error
}
)
Text(
text = text,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onPrimaryContainer
)
}
}
@Composable
private fun ProgressCard(photoCount: Int) {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = when {
photoCount >= 25 -> MaterialTheme.colorScheme.primaryContainer
photoCount >= 20 -> MaterialTheme.colorScheme.tertiaryContainer
else -> MaterialTheme.colorScheme.surfaceVariant
}
),
shape = RoundedCornerShape(16.dp)
) {
Column(
modifier = Modifier.padding(20.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Column {
Text(
text = "$photoCount photos selected",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)
Text(
text = when {
photoCount >= 30 -> "Excellent! Maximum diversity"
photoCount >= 25 -> "Great! Very good coverage"
photoCount >= 20 -> "Good! Should work well"
photoCount >= 15 -> "Acceptable - more is better"
else -> "Need ${15 - photoCount} more"
},
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
Surface(
shape = RoundedCornerShape(12.dp),
color = when {
photoCount >= 25 -> MaterialTheme.colorScheme.primary
photoCount >= 20 -> MaterialTheme.colorScheme.tertiary
photoCount >= 15 -> MaterialTheme.colorScheme.secondary
else -> MaterialTheme.colorScheme.outline
},
modifier = Modifier.size(56.dp)
) {
Box(contentAlignment = Alignment.Center) {
Text(
text = when {
photoCount >= 25 -> ""
photoCount >= 20 -> ""
photoCount >= 15 -> ""
else -> "..."
},
style = MaterialTheme.typography.headlineMedium,
color = Color.White
)
}
}
}
// Progress bar
LinearProgressIndicator(
progress = { (photoCount / 30f).coerceAtMost(1f) },
modifier = Modifier
.fillMaxWidth()
.height(8.dp),
color = when {
photoCount >= 25 -> MaterialTheme.colorScheme.primary
photoCount >= 20 -> MaterialTheme.colorScheme.tertiary
else -> MaterialTheme.colorScheme.secondary
},
trackColor = MaterialTheme.colorScheme.surfaceVariant,
)
// Expected accuracy
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
"Expected accuracy:",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Text(
when {
photoCount >= 30 -> "90-95%"
photoCount >= 25 -> "85-90%"
photoCount >= 20 -> "80-85%"
photoCount >= 15 -> "75-80%"
else -> "< 75%"
},
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.Bold,
color = when {
photoCount >= 25 -> MaterialTheme.colorScheme.primary
photoCount >= 20 -> MaterialTheme.colorScheme.tertiary
else -> MaterialTheme.colorScheme.secondary
}
)
}
}
}
}

View File

@@ -8,12 +8,13 @@ import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.* import androidx.compose.material.icons.filled.*
import androidx.compose.material3.* import androidx.compose.material3.*
@@ -25,6 +26,8 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
@@ -38,6 +41,28 @@ fun ScanResultsScreen(
trainViewModel: TrainViewModel = hiltViewModel() trainViewModel: TrainViewModel = hiltViewModel()
) { ) {
var showFacePickerDialog by remember { mutableStateOf<FaceDetectionHelper.FaceDetectionResult?>(null) } var showFacePickerDialog by remember { mutableStateOf<FaceDetectionHelper.FaceDetectionResult?>(null) }
var showNameInputDialog by remember { mutableStateOf(false) }
// Observe training state
val trainingState by trainViewModel.trainingState.collectAsState()
// Handle training state changes
LaunchedEffect(trainingState) {
when (trainingState) {
is TrainingState.Success -> {
// Training completed successfully
val success = trainingState as TrainingState.Success
// You can show a success message or navigate away
// For now, we'll just reset and finish
trainViewModel.resetTrainingState()
onFinish()
}
is TrainingState.Error -> {
// Error will be shown in dialog, no action needed here
}
else -> { /* Idle or Processing */ }
}
}
Scaffold( Scaffold(
topBar = { topBar = {
@@ -69,7 +94,10 @@ fun ScanResultsScreen(
is ScanningState.Success -> { is ScanningState.Success -> {
ImprovedResultsView( ImprovedResultsView(
result = state.sanityCheckResult, result = state.sanityCheckResult,
onContinue = onFinish, onContinue = {
// Show name input dialog instead of immediately finishing
showNameInputDialog = true
},
onRetry = onFinish, onRetry = onFinish,
onReplaceImage = { oldUri, newUri -> onReplaceImage = { oldUri, newUri ->
trainViewModel.replaceImage(oldUri, newUri) trainViewModel.replaceImage(oldUri, newUri)
@@ -87,6 +115,11 @@ fun ScanResultsScreen(
) )
} }
} }
// Show training overlay if processing
if (trainingState is TrainingState.Processing) {
TrainingOverlay(trainingState = trainingState as TrainingState.Processing)
}
} }
} }
@@ -101,6 +134,185 @@ fun ScanResultsScreen(
} }
) )
} }
// Name Input Dialog
if (showNameInputDialog) {
NameInputDialog(
onDismiss = { showNameInputDialog = false },
onConfirm = { name ->
showNameInputDialog = false
trainViewModel.createFaceModel(name)
},
trainingState = trainingState
)
}
}
/**
* Dialog for entering person's name before training
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun NameInputDialog(
onDismiss: () -> Unit,
onConfirm: (String) -> Unit,
trainingState: TrainingState
) {
var personName by remember { mutableStateOf("") }
val isError = trainingState is TrainingState.Error
AlertDialog(
onDismissRequest = {
if (trainingState !is TrainingState.Processing) {
onDismiss()
}
},
title = {
Text(
text = if (isError) "Training Error" else "Who is this?",
style = MaterialTheme.typography.headlineSmall
)
},
text = {
Column(
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
if (isError) {
// Show error message
val error = trainingState as TrainingState.Error
Surface(
color = MaterialTheme.colorScheme.errorContainer,
shape = RoundedCornerShape(8.dp)
) {
Row(
modifier = Modifier.padding(12.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Icons.Default.Warning,
contentDescription = null,
tint = MaterialTheme.colorScheme.error
)
Text(
text = error.message,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onErrorContainer
)
}
}
} else {
Text(
text = "Enter the name of the person in these training images. This will help you find their photos later.",
style = MaterialTheme.typography.bodyMedium
)
}
OutlinedTextField(
value = personName,
onValueChange = { personName = it },
label = { Text("Person's Name") },
placeholder = { Text("e.g., John Doe") },
singleLine = true,
enabled = trainingState !is TrainingState.Processing,
keyboardOptions = KeyboardOptions(
capitalization = KeyboardCapitalization.Words,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(
onDone = {
if (personName.isNotBlank()) {
onConfirm(personName.trim())
}
}
),
modifier = Modifier.fillMaxWidth()
)
}
},
confirmButton = {
Button(
onClick = { onConfirm(personName.trim()) },
enabled = personName.isNotBlank() && trainingState !is TrainingState.Processing
) {
if (trainingState is TrainingState.Processing) {
CircularProgressIndicator(
modifier = Modifier.size(16.dp),
strokeWidth = 2.dp,
color = MaterialTheme.colorScheme.onPrimary
)
Spacer(modifier = Modifier.width(8.dp))
}
Text(if (isError) "Try Again" else "Start Training")
}
},
dismissButton = {
if (trainingState !is TrainingState.Processing) {
TextButton(onClick = onDismiss) {
Text("Cancel")
}
}
}
)
}
/**
* Overlay shown during training process
*/
@Composable
private fun TrainingOverlay(trainingState: TrainingState.Processing) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black.copy(alpha = 0.7f)),
contentAlignment = Alignment.Center
) {
Card(
modifier = Modifier
.padding(32.dp)
.fillMaxWidth(0.9f),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surface
)
) {
Column(
modifier = Modifier.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
CircularProgressIndicator(
modifier = Modifier.size(64.dp),
strokeWidth = 6.dp
)
Text(
text = "Creating Face Model",
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
Text(
text = trainingState.stage,
style = MaterialTheme.typography.bodyMedium,
textAlign = TextAlign.Center,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
if (trainingState.total > 0) {
LinearProgressIndicator(
progress = { (trainingState.progress.toFloat() / trainingState.total.toFloat()).coerceIn(0f, 1f) },
modifier = Modifier.fillMaxWidth()
)
Text(
text = "${trainingState.progress} / ${trainingState.total}",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
}
} }
@Composable @Composable
@@ -579,7 +791,7 @@ private fun ValidationIssuesCard(errors: List<TrainingSanityChecker.ValidationEr
) )
} }
Divider(color = MaterialTheme.colorScheme.error.copy(alpha = 0.3f)) HorizontalDivider(color = MaterialTheme.colorScheme.error.copy(alpha = 0.3f))
errors.forEach { error -> errors.forEach { error ->
when (error) { when (error) {

View File

@@ -5,6 +5,9 @@ import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.placeholder.sherpai2.data.local.entity.PersonEntity
import com.placeholder.sherpai2.data.repository.FaceRecognitionRepository
import com.placeholder.sherpai2.ml.FaceNetModel
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
@@ -21,9 +24,31 @@ sealed class ScanningState {
data class Error(val message: String) : ScanningState() data class Error(val message: String) : ScanningState()
} }
sealed class TrainingState {
object Idle : TrainingState()
data class Processing(val stage: String, val progress: Int, val total: Int) : TrainingState()
data class Success(
val personName: String,
val personId: String,
val relationship: String?
) : TrainingState()
data class Error(val message: String) : TrainingState()
}
/**
* Person info captured before photo selection
*/
data class PersonInfo(
val name: String,
val dateOfBirth: Long?,
val relationship: String
)
@HiltViewModel @HiltViewModel
class TrainViewModel @Inject constructor( class TrainViewModel @Inject constructor(
application: Application application: Application,
private val faceRecognitionRepository: FaceRecognitionRepository,
private val faceNetModel: FaceNetModel
) : AndroidViewModel(application) { ) : AndroidViewModel(application) {
private val sanityChecker = TrainingSanityChecker(application) private val sanityChecker = TrainingSanityChecker(application)
@@ -32,10 +57,13 @@ class TrainViewModel @Inject constructor(
private val _uiState = MutableStateFlow<ScanningState>(ScanningState.Idle) private val _uiState = MutableStateFlow<ScanningState>(ScanningState.Idle)
val uiState: StateFlow<ScanningState> = _uiState.asStateFlow() val uiState: StateFlow<ScanningState> = _uiState.asStateFlow()
// Keep track of current images for replacements private val _trainingState = MutableStateFlow<TrainingState>(TrainingState.Idle)
private var currentImageUris: List<Uri> = emptyList() val trainingState: StateFlow<TrainingState> = _trainingState.asStateFlow()
// Keep track of manual face selections (imageUri -> selectedFaceIndex) // Store person info for later use during training
private var personInfo: PersonInfo? = null
private var currentImageUris: List<Uri> = emptyList()
private val manualFaceSelections = mutableMapOf<Uri, ManualFaceSelection>() private val manualFaceSelections = mutableMapOf<Uri, ManualFaceSelection>()
data class ManualFaceSelection( data class ManualFaceSelection(
@@ -44,17 +72,82 @@ class TrainViewModel @Inject constructor(
) )
/** /**
* Scan and validate images for training * Store person info before photo selection
*/ */
fun setPersonInfo(name: String, dateOfBirth: Long?, relationship: String) {
personInfo = PersonInfo(name, dateOfBirth, relationship)
}
/**
* Create face model with captured person info
*/
fun createFaceModel(personName: String) {
val currentState = _uiState.value
if (currentState !is ScanningState.Success) {
_trainingState.value = TrainingState.Error("No validated images available")
return
}
val validImages = currentState.sanityCheckResult.validImagesWithFaces
if (validImages.size < 15) { // Updated minimum
_trainingState.value = TrainingState.Error(
"Need at least 15 valid images, have ${validImages.size}"
)
return
}
viewModelScope.launch {
try {
_trainingState.value = TrainingState.Processing(
stage = "Creating person and training model",
progress = 0,
total = validImages.size
)
// Create person with captured info
val person = PersonEntity.create(
name = personName,
dateOfBirth = personInfo?.dateOfBirth,
relationship = personInfo?.relationship
)
// Create person with face model
val personId = faceRecognitionRepository.createPersonWithFaceModel(
person = person, // Pass full PersonEntity now
validImages = validImages,
onProgress = { current, total ->
_trainingState.value = TrainingState.Processing(
stage = "Processing image $current/$total",
progress = current,
total = total
)
}
)
_trainingState.value = TrainingState.Success(
personName = personName,
personId = personId,
relationship = person.relationship
)
} catch (e: Exception) {
_trainingState.value = TrainingState.Error(
e.message ?: "Failed to create face model"
)
}
}
}
fun resetTrainingState() {
_trainingState.value = TrainingState.Idle
}
fun scanAndTagFaces(imageUris: List<Uri>) { fun scanAndTagFaces(imageUris: List<Uri>) {
currentImageUris = imageUris currentImageUris = imageUris
manualFaceSelections.clear() manualFaceSelections.clear()
performScan(imageUris) performScan(imageUris)
} }
/**
* Replace a single image and re-scan
*/
fun replaceImage(oldUri: Uri, newUri: Uri) { fun replaceImage(oldUri: Uri, newUri: Uri) {
viewModelScope.launch { viewModelScope.launch {
val updatedUris = currentImageUris.toMutableList() val updatedUris = currentImageUris.toMutableList()
@@ -63,23 +156,15 @@ class TrainViewModel @Inject constructor(
if (index != -1) { if (index != -1) {
updatedUris[index] = newUri updatedUris[index] = newUri
currentImageUris = updatedUris currentImageUris = updatedUris
// Remove manual selection for old URI if any
manualFaceSelections.remove(oldUri) manualFaceSelections.remove(oldUri)
// Re-scan all images
performScan(currentImageUris) performScan(currentImageUris)
} }
} }
} }
/**
* User manually selected a face from a multi-face image
*/
fun selectFaceFromImage(imageUri: Uri, faceIndex: Int, croppedFaceBitmap: Bitmap) { fun selectFaceFromImage(imageUri: Uri, faceIndex: Int, croppedFaceBitmap: Bitmap) {
manualFaceSelections[imageUri] = ManualFaceSelection(faceIndex, croppedFaceBitmap) manualFaceSelections[imageUri] = ManualFaceSelection(faceIndex, croppedFaceBitmap)
// Re-process the results with the manual selection
val currentState = _uiState.value val currentState = _uiState.value
if (currentState is ScanningState.Success) { if (currentState is ScanningState.Success) {
val updatedResult = applyManualSelections(currentState.sanityCheckResult) val updatedResult = applyManualSelections(currentState.sanityCheckResult)
@@ -87,25 +172,19 @@ class TrainViewModel @Inject constructor(
} }
} }
/**
* Perform the actual scanning
*/
private fun performScan(imageUris: List<Uri>) { private fun performScan(imageUris: List<Uri>) {
viewModelScope.launch { viewModelScope.launch {
try { try {
_uiState.value = ScanningState.Processing(0, imageUris.size) _uiState.value = ScanningState.Processing(0, imageUris.size)
// Perform sanity checks
val result = sanityChecker.performSanityChecks( val result = sanityChecker.performSanityChecks(
imageUris = imageUris, imageUris = imageUris,
minImagesRequired = 10, minImagesRequired = 15, // Updated minimum
allowMultipleFaces = true, // Allow multiple faces - user can pick allowMultipleFaces = true,
duplicateSimilarityThreshold = 0.95 duplicateSimilarityThreshold = 0.95
) )
// Apply any manual face selections
val finalResult = applyManualSelections(result) val finalResult = applyManualSelections(result)
_uiState.value = ScanningState.Success(finalResult) _uiState.value = ScanningState.Success(finalResult)
} catch (e: Exception) { } catch (e: Exception) {
@@ -116,26 +195,19 @@ class TrainViewModel @Inject constructor(
} }
} }
/**
* Apply manual face selections to the results
*/
private fun applyManualSelections( private fun applyManualSelections(
result: TrainingSanityChecker.SanityCheckResult result: TrainingSanityChecker.SanityCheckResult
): TrainingSanityChecker.SanityCheckResult { ): TrainingSanityChecker.SanityCheckResult {
// If no manual selections, return original
if (manualFaceSelections.isEmpty()) { if (manualFaceSelections.isEmpty()) {
return result return result
} }
// Update face detection results with manual selections
val updatedFaceResults = result.faceDetectionResults.map { faceResult -> val updatedFaceResults = result.faceDetectionResults.map { faceResult ->
val manualSelection = manualFaceSelections[faceResult.uri] val manualSelection = manualFaceSelections[faceResult.uri]
if (manualSelection != null) { if (manualSelection != null) {
// Replace the cropped face with the manually selected one
faceResult.copy( faceResult.copy(
croppedFaceBitmap = manualSelection.croppedFaceBitmap, croppedFaceBitmap = manualSelection.croppedFaceBitmap,
// Treat as single face since user selected one
faceCount = 1 faceCount = 1
) )
} else { } else {
@@ -143,12 +215,11 @@ class TrainViewModel @Inject constructor(
} }
} }
// Update valid images list
val updatedValidImages = updatedFaceResults val updatedValidImages = updatedFaceResults
.filter { it.hasFace } .filter { it.hasFace }
.filter { it.croppedFaceBitmap != null } .filter { it.croppedFaceBitmap != null }
.filter { it.errorMessage == null } .filter { it.errorMessage == null }
.filter { it.faceCount >= 1 } // Now accept if user picked a face .filter { it.faceCount >= 1 }
.map { result -> .map { result ->
TrainingSanityChecker.ValidTrainingImage( TrainingSanityChecker.ValidTrainingImage(
uri = result.uri, uri = result.uri,
@@ -157,31 +228,27 @@ class TrainViewModel @Inject constructor(
) )
} }
// Recalculate validation errors
val updatedErrors = result.validationErrors.toMutableList() val updatedErrors = result.validationErrors.toMutableList()
// Remove multiple face errors for images with manual selections
updatedErrors.removeAll { error -> updatedErrors.removeAll { error ->
error is TrainingSanityChecker.ValidationError.MultipleFacesDetected && error is TrainingSanityChecker.ValidationError.MultipleFacesDetected &&
manualFaceSelections.containsKey(error.uri) manualFaceSelections.containsKey(error.uri)
} }
// Check if we have enough valid images now if (updatedValidImages.size < 15) { // Updated minimum
if (updatedValidImages.size < 10) {
if (updatedErrors.none { it is TrainingSanityChecker.ValidationError.InsufficientImages }) { if (updatedErrors.none { it is TrainingSanityChecker.ValidationError.InsufficientImages }) {
updatedErrors.add( updatedErrors.add(
TrainingSanityChecker.ValidationError.InsufficientImages( TrainingSanityChecker.ValidationError.InsufficientImages(
required = 10, required = 15,
available = updatedValidImages.size available = updatedValidImages.size
) )
) )
} }
} else { } else {
// Remove insufficient images error if we now have enough
updatedErrors.removeAll { it is TrainingSanityChecker.ValidationError.InsufficientImages } updatedErrors.removeAll { it is TrainingSanityChecker.ValidationError.InsufficientImages }
} }
val isValid = updatedErrors.isEmpty() && updatedValidImages.size >= 10 val isValid = updatedErrors.isEmpty() && updatedValidImages.size >= 15
return result.copy( return result.copy(
isValid = isValid, isValid = isValid,
@@ -191,30 +258,27 @@ class TrainViewModel @Inject constructor(
) )
} }
/**
* Get formatted error messages
*/
fun getFormattedErrors(result: TrainingSanityChecker.SanityCheckResult): List<String> { fun getFormattedErrors(result: TrainingSanityChecker.SanityCheckResult): List<String> {
return sanityChecker.formatValidationErrors(result.validationErrors) return sanityChecker.formatValidationErrors(result.validationErrors)
} }
/**
* Reset to idle state
*/
fun reset() { fun reset() {
_uiState.value = ScanningState.Idle _uiState.value = ScanningState.Idle
_trainingState.value = TrainingState.Idle
currentImageUris = emptyList() currentImageUris = emptyList()
manualFaceSelections.clear() manualFaceSelections.clear()
personInfo = null
} }
override fun onCleared() { override fun onCleared() {
super.onCleared() super.onCleared()
sanityChecker.cleanup() sanityChecker.cleanup()
faceDetectionHelper.cleanup() faceDetectionHelper.cleanup()
faceNetModel.close()
} }
} }
// Extension function to copy FaceDetectionResult with modifications // Extension functions for copying results
private fun FaceDetectionHelper.FaceDetectionResult.copy( private fun FaceDetectionHelper.FaceDetectionResult.copy(
uri: Uri = this.uri, uri: Uri = this.uri,
hasFace: Boolean = this.hasFace, hasFace: Boolean = this.hasFace,
@@ -233,7 +297,6 @@ private fun FaceDetectionHelper.FaceDetectionResult.copy(
) )
} }
// Extension function to copy SanityCheckResult with modifications
private fun TrainingSanityChecker.SanityCheckResult.copy( private fun TrainingSanityChecker.SanityCheckResult.copy(
isValid: Boolean = this.isValid, isValid: Boolean = this.isValid,
faceDetectionResults: List<FaceDetectionHelper.FaceDetectionResult> = this.faceDetectionResults, faceDetectionResults: List<FaceDetectionHelper.FaceDetectionResult> = this.faceDetectionResults,

View File

@@ -1,31 +1,516 @@
package com.placeholder.sherpai2.ui.trainingprep package com.placeholder.sherpai2.ui.trainingprep
import androidx.compose.foundation.layout.padding import androidx.compose.animation.AnimatedVisibility
import androidx.compose.material3.Button import androidx.compose.foundation.background
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.foundation.layout.*
import androidx.compose.material3.Scaffold import androidx.compose.foundation.rememberScrollState
import androidx.compose.material3.Text import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.TopAppBar import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable 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.Modifier
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import java.text.SimpleDateFormat
import java.util.*
/**
* Beautiful TrainingScreen with person info capture
*
* Features:
* - Name input
* - Date of birth picker
* - Relationship selector
* - Onboarding cards
* - Beautiful gradient design
* - Clear call to action
*/
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun TrainingScreen( fun TrainingScreen(
onSelectImages: () -> Unit onSelectImages: () -> Unit,
modifier: Modifier = Modifier
) { ) {
var showInfoDialog by remember { mutableStateOf(false) }
Scaffold( Scaffold(
topBar = { topBar = {
TopAppBar( TopAppBar(
title = { Text("Training") } title = { Text("Train New Person") },
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
)
) )
} }
) { padding -> ) { paddingValues ->
Button( Column(
modifier = Modifier.padding(padding), modifier = modifier
onClick = onSelectImages .fillMaxSize()
.padding(paddingValues)
.verticalScroll(rememberScrollState())
.padding(20.dp),
verticalArrangement = Arrangement.spacedBy(20.dp)
) { ) {
Text("Select Images")
// Hero section with gradient
HeroCard()
// How it works section
HowItWorksSection()
// Requirements section
RequirementsCard()
Spacer(Modifier.weight(1f))
// Main CTA button
Button(
onClick = { showInfoDialog = true },
modifier = Modifier
.fillMaxWidth()
.height(60.dp),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.primary
),
shape = RoundedCornerShape(16.dp)
) {
Icon(
Icons.Default.PersonAdd,
contentDescription = null,
modifier = Modifier.size(24.dp)
)
Spacer(Modifier.width(12.dp))
Text(
"Start Training",
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
}
Spacer(Modifier.height(8.dp))
}
}
// Person info dialog
if (showInfoDialog) {
PersonInfoDialog(
onDismiss = { showInfoDialog = false },
onConfirm = { name, dob, relationship ->
showInfoDialog = false
// TODO: Store this info before photo selection
// For now, just proceed to photo selection
onSelectImages()
}
)
}
}
@Composable
private fun HeroCard() {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
),
shape = RoundedCornerShape(20.dp)
) {
Box(
modifier = Modifier
.fillMaxWidth()
.background(
Brush.verticalGradient(
colors = listOf(
MaterialTheme.colorScheme.primaryContainer,
MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.7f)
)
)
)
) {
Column(
modifier = Modifier.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Surface(
shape = RoundedCornerShape(20.dp),
color = MaterialTheme.colorScheme.primary,
shadowElevation = 8.dp,
modifier = Modifier.size(80.dp)
) {
Box(contentAlignment = Alignment.Center) {
Icon(
Icons.Default.Face,
contentDescription = null,
modifier = Modifier.size(48.dp),
tint = MaterialTheme.colorScheme.onPrimary
)
}
}
Text(
"Face Recognition Training",
style = MaterialTheme.typography.headlineMedium,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center
)
Text(
"Train the AI to recognize someone in your photos",
style = MaterialTheme.typography.bodyLarge,
textAlign = TextAlign.Center,
color = MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.8f)
)
}
} }
} }
} }
@Composable
private fun HowItWorksSection() {
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
Text(
"How It Works",
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
StepCard(
number = 1,
icon = Icons.Default.Info,
title = "Enter Person Details",
description = "Name, birthday, and relationship"
)
StepCard(
number = 2,
icon = Icons.Default.PhotoLibrary,
title = "Select Training Photos",
description = "Choose 20-30 photos of the person"
)
StepCard(
number = 3,
icon = Icons.Default.ModelTraining,
title = "AI Learns Their Face",
description = "Takes ~30 seconds to train"
)
StepCard(
number = 4,
icon = Icons.Default.Search,
title = "Auto-Tag Your Library",
description = "Find them in all your photos"
)
}
}
@Composable
private fun StepCard(
number: Int,
icon: androidx.compose.ui.graphics.vector.ImageVector,
title: String,
description: String
) {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant
),
shape = RoundedCornerShape(12.dp)
) {
Row(
modifier = Modifier.padding(16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
// Number badge
Surface(
shape = RoundedCornerShape(12.dp),
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(48.dp)
) {
Box(contentAlignment = Alignment.Center) {
Text(
text = number.toString(),
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onPrimary
)
}
}
Column(modifier = Modifier.weight(1f)) {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
icon,
contentDescription = null,
modifier = Modifier.size(20.dp),
tint = MaterialTheme.colorScheme.primary
)
Text(
title,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold
)
}
Spacer(Modifier.height(4.dp))
Text(
description,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
}
@Composable
private fun RequirementsCard() {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer
),
shape = RoundedCornerShape(16.dp)
) {
Column(
modifier = Modifier.padding(20.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Icons.Default.CheckCircle,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary
)
Text(
"What You'll Need",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)
}
RequirementItem("20-30 photos of the person", true)
RequirementItem("Different angles and lighting", true)
RequirementItem("Clear face visibility", true)
RequirementItem("Mix of expressions", true)
RequirementItem("2-3 minutes of your time", true)
}
}
}
@Composable
private fun RequirementItem(text: String, isMet: Boolean) {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
if (isMet) Icons.Default.Check else Icons.Default.Close,
contentDescription = null,
modifier = Modifier.size(18.dp),
tint = if (isMet) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.error
}
)
Text(
text = text,
style = MaterialTheme.typography.bodyMedium
)
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun PersonInfoDialog(
onDismiss: () -> Unit,
onConfirm: (name: String, dateOfBirth: Long?, relationship: String) -> Unit
) {
var name by remember { mutableStateOf("") }
var dateOfBirth by remember { mutableStateOf<Long?>(null) }
var selectedRelationship by remember { mutableStateOf("Other") }
var showDatePicker by remember { mutableStateOf(false) }
val relationships = listOf(
"Family" to "👨‍👩‍👧‍👦",
"Friend" to "🤝",
"Partner" to "❤️",
"Child" to "👶",
"Parent" to "👪",
"Sibling" to "👫",
"Colleague" to "💼",
"Other" to "👤"
)
AlertDialog(
onDismissRequest = onDismiss,
title = {
Column {
Text("Person Details")
Text(
"Help us organize your photos",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
},
text = {
Column(
modifier = Modifier.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
// Name field
OutlinedTextField(
value = name,
onValueChange = { name = it },
label = { Text("Name *") },
placeholder = { Text("e.g., John Doe") },
leadingIcon = {
Icon(Icons.Default.Person, contentDescription = null)
},
modifier = Modifier.fillMaxWidth(),
singleLine = true
)
// Date of birth
OutlinedButton(
onClick = { showDatePicker = true },
modifier = Modifier.fillMaxWidth()
) {
Icon(Icons.Default.Cake, contentDescription = null)
Spacer(Modifier.width(8.dp))
Text(
if (dateOfBirth != null) {
"Birthday: ${formatDate(dateOfBirth!!)}"
} else {
"Add Birthday (Optional)"
}
)
}
// Relationship selector
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text(
"Relationship",
style = MaterialTheme.typography.labelMedium
)
// Relationship chips
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
relationships.take(4).forEach { (rel, emoji) ->
FilterChip(
selected = selectedRelationship == rel,
onClick = { selectedRelationship = rel },
label = { Text("$emoji $rel") }
)
}
}
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
relationships.drop(4).forEach { (rel, emoji) ->
FilterChip(
selected = selectedRelationship == rel,
onClick = { selectedRelationship = rel },
label = { Text("$emoji $rel") }
)
}
}
}
// Privacy note
Card(
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant
)
) {
Row(
modifier = Modifier.padding(12.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Icon(
Icons.Default.Lock,
contentDescription = null,
modifier = Modifier.size(16.dp),
tint = MaterialTheme.colorScheme.primary
)
Text(
"All data stays on your device",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
},
confirmButton = {
Button(
onClick = {
if (name.isNotBlank()) {
onConfirm(name, dateOfBirth, selectedRelationship)
}
},
enabled = name.isNotBlank()
) {
Text("Continue")
}
},
dismissButton = {
TextButton(onClick = onDismiss) {
Text("Cancel")
}
}
)
// Date picker dialog
if (showDatePicker) {
DatePickerDialog(
onDismissRequest = { showDatePicker = false },
confirmButton = {
TextButton(
onClick = {
// Get selected date from date picker
// For now, set to current date as placeholder
dateOfBirth = System.currentTimeMillis()
showDatePicker = false
}
) {
Text("OK")
}
},
dismissButton = {
TextButton(onClick = { showDatePicker = false }) {
Text("Cancel")
}
}
) {
// Material3 DatePicker
DatePicker(
state = rememberDatePickerState(),
modifier = Modifier.padding(16.dp)
)
}
}
}
private fun formatDate(timestamp: Long): String {
val formatter = SimpleDateFormat("MMM dd, yyyy", Locale.getDefault())
return formatter.format(Date(timestamp))
}

View File

@@ -0,0 +1,68 @@
package com.placeholder.sherpai2.util
/**
* Debug feature flags
*
* Toggle these to enable/disable diagnostic features
* Set to false before release builds!
*/
object DebugFlags {
/**
* Enable verbose face recognition logging
*
* When true:
* - Logs every face detection
* - Logs similarity scores
* - Logs matching decisions
* - Shows why images are skipped
*
* Filter Logcat by: "FaceRecognition"
*/
const val ENABLE_FACE_RECOGNITION_LOGGING = true // ← Toggle here
/**
* Show confidence scores in UI
*/
const val SHOW_CONFIDENCE_IN_UI = true // ← Toggle here
/**
* Lower thresholds for better recall (more matches, some false positives)
*/
const val USE_LIBERAL_THRESHOLDS = true // ← Toggle here
}
/**
* Diagnostic logger - only logs when flag is enabled
*/
object DiagnosticLogger {
private const val TAG = "FaceRecognition"
fun d(message: String) {
if (DebugFlags.ENABLE_FACE_RECOGNITION_LOGGING) {
android.util.Log.d(TAG, message)
}
}
fun i(message: String) {
if (DebugFlags.ENABLE_FACE_RECOGNITION_LOGGING) {
android.util.Log.i(TAG, message)
}
}
fun w(message: String) {
if (DebugFlags.ENABLE_FACE_RECOGNITION_LOGGING) {
android.util.Log.w(TAG, message)
}
}
fun e(message: String, throwable: Throwable? = null) {
if (DebugFlags.ENABLE_FACE_RECOGNITION_LOGGING) {
if (throwable != null) {
android.util.Log.e(TAG, message, throwable)
} else {
android.util.Log.e(TAG, message)
}
}
}
}

View File

@@ -8,4 +8,5 @@ plugins {
//https://github.com/google/dagger/issues/4048#issuecomment-1864237679 //https://github.com/google/dagger/issues/4048#issuecomment-1864237679
alias(libs.plugins.ksp) apply false alias(libs.plugins.ksp) apply false
alias(libs.plugins.hilt.android) apply false alias(libs.plugins.hilt.android) apply false
} }

View File

@@ -20,4 +20,5 @@ kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the # Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies, # resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library # thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true android.nonTransitiveRClass=true
org.gradle.java.home=/snap/android-studio/current/jbr

View File

@@ -19,10 +19,15 @@ room = "2.8.4"
# Images # Images
coil = "2.7.0" coil = "2.7.0"
#Face Detect # Face Detect
mlkit-face-detection = "16.1.6" mlkit-face-detection = "16.1.6"
coroutines-play-services = "1.8.1" coroutines-play-services = "1.8.1"
# Models
tensorflow-lite = "2.14.0"
tensorflow-lite-support = "0.4.4"
gson = "2.10.1"
[libraries] [libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle" } androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle" }
@@ -56,10 +61,17 @@ coil-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coi
mlkit-face-detection = { group = "com.google.mlkit", name = "face-detection", version.ref = "mlkit-face-detection"} mlkit-face-detection = { group = "com.google.mlkit", name = "face-detection", version.ref = "mlkit-face-detection"}
kotlinx-coroutines-play-services = {group = "org.jetbrains.kotlinx",name = "kotlinx-coroutines-play-services",version.ref = "coroutines-play-services"} kotlinx-coroutines-play-services = {group = "org.jetbrains.kotlinx",name = "kotlinx-coroutines-play-services",version.ref = "coroutines-play-services"}
# TensorFlow Lite for FaceNet
tensorflow-lite = { group = "org.tensorflow", name = "tensorflow-lite", version.ref = "tensorflow-lite" }
tensorflow-lite-support = { group = "org.tensorflow", name = "tensorflow-lite-support", version.ref = "tensorflow-lite-support" }
tensorflow-lite-gpu = { group = "org.tensorflow", name = "tensorflow-lite-gpu", version.ref = "tensorflow-lite" }
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
[plugins] [plugins]
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hilt" } hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }