Skip to content

Commit

Permalink
Revert "A"
Browse files Browse the repository at this point in the history
This reverts commit f24e7c4.
  • Loading branch information
jelychow committed Feb 3, 2025
1 parent f24e7c4 commit 05a2451
Show file tree
Hide file tree
Showing 25 changed files with 27 additions and 498 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/me/grey/picquery/PicQueryApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package me.grey.picquery
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import me.grey.picquery.common.AppModules
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
Expand All @@ -15,6 +18,7 @@ class PicQueryApplication : Application() {
@SuppressLint("StaticFieldLeak")
lateinit var context: Context
private const val TAG = "PicQueryApp"
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
}

override fun onCreate() {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/me/grey/picquery/common/AppModules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ private val domainModules = module {
AlbumManager(
albumRepository = get(),
photoRepository = get(),
embeddingRepository = get(),
imageSearcher = get(),
ioDispatcher = get()
)
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/me/grey/picquery/common/Routes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ enum class Routes {
Home,
Search,
Display,
IndexMgr,
Setting
}
6 changes: 2 additions & 4 deletions app/src/main/java/me/grey/picquery/data/dao/EmbeddingDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ interface EmbeddingDao {
)
fun getByAlbumIdList(albumIds: List<Long>, limit: Int, offset: Int): List<Embedding>

@Query(
"DELETE FROM $tableName WHERE album_id=(:albumId)"
)
fun removeByAlbumId(albumId: Long): Unit
// @Insert
// fun insertAll(embeddings: List<Embedding>)

@Upsert
fun upsertAll(embeddings: List<Embedding>)
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,4 @@ class AlbumRepository(
fun addAllSearchableAlbum(album: List<Album>) {
return database.albumDao().upsertAll(album)
}

fun removeSearchableAlbum(singleAlbum: Album) {
database.albumDao().delete(singleAlbum)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,4 @@ class EmbeddingRepository(
fun updateAll(list: List<Embedding>) {
return dataSource.upsertAll(list)
}

fun removeByAlbum(album: Album){
return dataSource.removeByAlbumId(album.id)
}
}
Empty file.
40 changes: 17 additions & 23 deletions app/src/main/java/me/grey/picquery/domain/AlbumManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ import androidx.compose.runtime.mutableStateOf
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.grey.picquery.PicQueryApplication
import me.grey.picquery.PicQueryApplication.Companion.context
import me.grey.picquery.R
import me.grey.picquery.common.showToast
import me.grey.picquery.data.data_source.AlbumRepository
import me.grey.picquery.data.data_source.EmbeddingRepository
import me.grey.picquery.data.data_source.PhotoRepository
import me.grey.picquery.data.model.Album
import me.grey.picquery.ui.albums.IndexingAlbumState

class AlbumManager(
private val albumRepository: AlbumRepository,
private val photoRepository: PhotoRepository,
private val embeddingRepository: EmbeddingRepository,
private val imageSearcher: ImageSearcher,
private val ioDispatcher: CoroutineDispatcher
) {
Expand All @@ -38,16 +37,14 @@ class AlbumManager(
get() = indexingAlbumState.value.isBusy

private val albumList = mutableStateListOf<Album>()
val searchableAlbumList = MutableStateFlow<List<Album>>(emptyList())
val unsearchableAlbumList = MutableStateFlow<List<Album>>(emptyList())
val searchableAlbumList = mutableStateListOf<Album>()
val unsearchableAlbumList = mutableStateListOf<Album>()
val albumsToEncode = mutableStateListOf<Album>()

private fun searchableAlbumFlow() = albumRepository.getSearchableAlbumFlow()

private var initialized = false

fun getAlbumList() = albumList

suspend fun initAllAlbumList() {
if (initialized) return
withContext(ioDispatcher) {
Expand All @@ -60,18 +57,18 @@ class AlbumManager(
}
}

suspend fun initDataFlow() {
private suspend fun initDataFlow() {
searchableAlbumFlow().collect {
// 从数据库中检索已经索引的相册
// 有些相册可能已经索引但已被删除,因此要从全部相册中筛选,而不能直接返回数据库的结果
val res = it.toMutableList().sortedByDescending { album: Album -> album.count }
searchableAlbumList.emit(res)
Log.d(TAG, "Searchable albums: ${it.size}")
searchableAlbumList.clear()
searchableAlbumList.addAll(it)
searchableAlbumList.sortByDescending { album: Album -> album.count }
// 从全部相册减去已经索引的ID,就是未索引的相册
val unsearchable = albumList.filter { all -> !it.contains(all) }

unsearchableAlbumList.emit(unsearchable.toMutableList().sortedByDescending { album: Album -> album.count })
Log.d(TAG, "Unsearchable albums: ${unsearchable.size}")
unsearchableAlbumList.clear()
unsearchableAlbumList.addAll(unsearchable)
unsearchableAlbumList.sortByDescending { album: Album -> album.count }
}
}

Expand All @@ -84,9 +81,9 @@ class AlbumManager(
}

fun toggleSelectAllAlbums() {
if (albumsToEncode.size != unsearchableAlbumList.value?.size) {
if (albumsToEncode.size != unsearchableAlbumList.size) {
albumsToEncode.clear()
albumsToEncode.addAll(unsearchableAlbumList.value)
albumsToEncode.addAll(unsearchableAlbumList)
} else {
albumsToEncode.clear()
}
Expand All @@ -108,10 +105,11 @@ private fun getPhotosFlow(albums: List<Album>) = albums.asFlow()
albums.sumOf { album -> photoRepository.getImageCountInAlbum(album.id) }
}

suspend fun encodeAlbums(albums: List<Album>) {
fun encodeAlbums(albums: List<Album>) {
PicQueryApplication.applicationScope.launch {
if (albums.isEmpty()) {
showToast(context.getString(R.string.no_album_selected))
return
return@launch
}

indexingAlbumState.value =
Expand Down Expand Up @@ -159,14 +157,10 @@ private fun getPhotosFlow(albums: List<Album>) = albums.asFlow()
status = IndexingAlbumState.Status.Error
)
}
}
}

fun clearIndexingState() {
indexingAlbumState.value = IndexingAlbumState()
}

fun removeSingleAlbumIndex(album: Album) {
embeddingRepository.removeByAlbum(album)
albumRepository.removeSearchableAlbum(album)
}
}
Loading

0 comments on commit 05a2451

Please sign in to comment.