Skip to content

Commit

Permalink
可以根据不同的file type检索文件
Browse files Browse the repository at this point in the history
升级一下依赖库
ksp替换kapt
  • Loading branch information
huaweikai committed Jan 25, 2024
1 parent 3a4ef94 commit c6b2e96
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 34 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

使用Android的SAF框架,进行一些文件权限申请,扫描一些图片文件到自己的沙盒环境,并不会对源文件进行操作。
将所有信息存储在一张表内,方便之后查询。
使用了Compose 技术完成,以及Room和Paging技术做到展示所有图片。
使用了Compose 技术完成,以及Room和Paging技术做到展示所有图片。

1/25号新增了根据file type检索图片

忘记说了,单击方向键是翻页,双击是回到顶部和去底部

Use Android's SAF framework to apply for some file permissions, scan some image files to your own sandbox environment, and will not operate on the source files.
Store all information in a table for easy query later.
Expand Down
20 changes: 9 additions & 11 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.kotlinAndroid)
id("kotlin-kapt")
alias(libs.plugins.kotlinKsp)
}

android {
namespace = "hua.dy.image"
compileSdk = 33
compileSdk = 34

defaultConfig {
applicationId = "hua.dy.image"
minSdk = 24
targetSdk = 33
versionCode = 3
versionName = "0.2"
targetSdk = 34
versionCode = 4
versionName = "0.3"

kapt {
arguments {
arg("room.schemaLocation", "$projectDir/schemas")
}
ksp {
arg("room.schemaLocation", "$projectDir/schemas")
}

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -49,7 +47,7 @@ android {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.4.3"
kotlinCompilerExtensionVersion = "1.5.7"
}
packaging {
resources {
Expand Down Expand Up @@ -81,7 +79,7 @@ dependencies {
implementation(libs.coil.gif)

// room
kapt(libs.room.compiler)
ksp(libs.room.compiler)
implementation(libs.room.ktx)
implementation(libs.room.runtime)
implementation(libs.room.paging)
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 3,
"versionName": "0.2",
"versionCode": 4,
"versionName": "0.3",
"outputFile": "app-release.apk"
}
],
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/hua/dy/image/db/DyImageDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ interface DyImageDao {
@Query("SELECT * FROM dy_image ORDER BY scan_time COLLATE NOCASE DESC")
fun getImageListByScanTime(): PagingSource<Int, ImageBean>

@Query("SELECT * FROM dy_image where file_type =:type ORDER BY file_time COLLATE NOCASE DESC")
fun getImageListByFileTime(type: Int): PagingSource<Int, ImageBean>

@Query("SELECT * FROM dy_image where file_type =:type ORDER BY file_length COLLATE NOCASE DESC")
fun getImageListByFileLength(type: Int): PagingSource<Int, ImageBean>

@Query("SELECT * FROM dy_image where file_type =:type ORDER BY scan_time COLLATE NOCASE DESC")
fun getImageListByScanTime(type: Int): PagingSource<Int, ImageBean>

@Delete
suspend fun deleteImage(imageBean: ImageBean)

Expand Down
28 changes: 26 additions & 2 deletions app/src/main/java/hua/dy/image/ui/Home.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hua.dy.image.ui

import android.os.Build.VERSION.SDK_INT
import android.widget.Toast
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateIntAsState
import androidx.compose.foundation.background
Expand All @@ -18,6 +19,7 @@ import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
Expand All @@ -31,6 +33,7 @@ import androidx.compose.material3.IconToggleButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -44,6 +47,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
Expand Down Expand Up @@ -75,6 +79,8 @@ fun Home() {
mutableStateOf(Pair(false, -1))
}

val context = LocalContext.current

val viewModel = viewModel(DyImageViewModel::class.java)

val imageData = viewModel.allImages.collectAsLazyPagingItems()
Expand Down Expand Up @@ -106,6 +112,13 @@ fun Home() {

val lazyScrollState = rememberLazyGridState()

val typeStringState = viewModel.typeState.collectAsState()

LaunchedEffect(key1 = typeStringState.value) {
Toast.makeText(context, "现在检索的是 ${typeStringState.value} 类型", Toast.LENGTH_SHORT)
.show()
}

val scope = rememberCoroutineScope()

val chatMainState = viewModel.chatImageStateFlow.collectAsState()
Expand Down Expand Up @@ -149,8 +162,19 @@ fun Home() {
modifier = Modifier
.padding(bottom = 16.dp)
) {
// 不加这个重建页面后,这个Column不显示了,暂时不知道为啥
Text(text = " ")
TextButton(
modifier = Modifier
.background(
MaterialTheme.colorScheme.background,
RoundedCornerShape(16.dp)
),
onClick = {
viewModel.changeType()
imageData.refresh()
}
) {
Text(text = typeStringState.value, color = MaterialTheme.colorScheme.surfaceTint)
}
AnimatedVisibility(visible = lazyScrollState.canScrollBackward) {
Icon(
imageVector = Icons.Outlined.KeyboardArrowUp,
Expand Down
52 changes: 47 additions & 5 deletions app/src/main/java/hua/dy/image/viewmodel/DyImageViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import androidx.paging.PagingSource
import androidx.paging.cachedIn
import androidx.paging.filter
import hua.dy.image.app.DyAppBean
import hua.dy.image.bean.GIF
import hua.dy.image.bean.ImageBean
import hua.dy.image.bean.JPG
import hua.dy.image.bean.Other
import hua.dy.image.bean.PNG
import hua.dy.image.db.dyImageDao
import hua.dy.image.utils.scanDyImages
import hua.dy.image.utils.sortValue
Expand All @@ -24,6 +28,8 @@ import splitties.init.appCtx

class DyImageViewModel: ViewModel() {



val allImages: Flow<PagingData<ImageBean>> =
Pager(
config = PagingConfig(
Expand All @@ -50,12 +56,48 @@ class DyImageViewModel: ViewModel() {
}
}

private var type = -1

fun changeType() {
type = when (type) {
-1 -> PNG
PNG, JPG -> type + 1
GIF -> Other
else -> -1
}
viewModelScope.launch {
_typeState.emit(getTypeString())
}
}

private fun getTypeString(): String {
return when (type) {
PNG -> "PNG"
JPG -> "JPG"
GIF -> "GIF"
Other -> "Other"
else -> "All"
}
}

private val _typeState = MutableStateFlow(getTypeString())
val typeState = _typeState.asStateFlow()

private fun getImagePagingSource(): PagingSource<Int, ImageBean> {
return when (sortValue) {
1 -> dyImageDao::getImageListByScanTime
2 -> dyImageDao::getImageListByFileLength
else -> dyImageDao::getImageListByFileTime
}.invoke()
return if (type == -1) {
when (sortValue) {
1 -> dyImageDao.getImageListByScanTime()
2 -> dyImageDao.getImageListByFileLength()
else -> dyImageDao.getImageListByFileTime()
}
} else {
when (sortValue) {
1 -> dyImageDao.getImageListByScanTime(type)
2 -> dyImageDao.getImageListByFileLength(type)
else -> dyImageDao.getImageListByFileTime(type)
}
}

}

private val _chatImagesStateFlow = MutableStateFlow(false)
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.kotlinAndroid) apply false
alias(libs.plugins.kotlinKsp) apply false
}
true // Needed to make the Suppress annotation work for the plugins block
26 changes: 14 additions & 12 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
[versions]
agp = "8.2.0-alpha12"
kotlin = "1.8.10"
core-ktx = "1.10.1"
agp = "8.2.2"
kotlin = "1.9.21"
core-ktx = "1.12.0"
junit = "4.13.2"
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"
lifecycle-runtime-ktx = "2.6.1"
activity-compose = "1.7.2"
compose-bom = "2023.03.00"
lifecycle-runtime-ktx = "2.7.0"
activity-compose = "1.8.2"
compose-bom = "2024.01.00"
document-util = "1.0.1"
splitties-appctx = "3.0.0"
room-ktx = "2.5.2"
room-ktx = "2.6.1"
coil = "2.4.0"
nav-version = "2.6.0"
paging-version = "3.2.0-rc01"
shared-preference = "1.1.1"
fast-md5 = "2.7.1"
nav-version = "2.7.6"
paging-version = "3.2.1"
shared-preference = "1.2.1"
#fast-md5 = "2.7.1"
accompanist = "0.31.5-beta"
ksp = "1.9.21-1.0.15"

[libraries]
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
Expand Down Expand Up @@ -45,12 +46,13 @@ coil = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" }
coil-gif = { group = "io.coil-kt", name = "coil-gif", version.ref = "coil" }
shared-preference = { group = "androidx.preference", name = "preference-ktx", version.ref = "shared-preference" }
navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "nav-version" }
fast-md5 = { group = "com.joyent.util", name = "fast-md5", version.ref = "fast-md5"}
#fast-md5 = { group = "com.joyent.util", name = "fast-md5", version.ref = "fast-md5"}
system-ui-controll = { group = "com.google.accompanist", name = "accompanist-systemuicontroller", version.ref = "accompanist" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlinKsp = { id = "com.google.devtools.ksp", version.ref = "ksp" }

[bundles]

2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Jul 13 20:37:24 HKT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit c6b2e96

Please sign in to comment.