Skip to content

Commit 57d901e

Browse files
committed
update
1 parent 28ed102 commit 57d901e

File tree

7 files changed

+92
-7
lines changed

7 files changed

+92
-7
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
android:roundIcon="@mipmap/ic_launcher_round"
1111
android:supportsRtl="true"
1212
android:usesCleartextTraffic="true"
13-
android:theme="@style/Theme.JetpackDemo">
13+
android:theme="@style/Theme.JetpackDemo"
14+
android:name=".App">
15+
1416
<activity
1517
android:name=".page.MainActivity"
1618
android:exported="true"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.wzq.jd.compose.app
2+
3+
import android.annotation.SuppressLint
4+
import android.app.Application
5+
import android.content.Context
6+
import androidx.room.Room
7+
import com.wzq.jd.compose.app.data.local.AppDatabase
8+
9+
/**
10+
* create by wzq on 2023/12/20
11+
*
12+
*/
13+
class App: Application() {
14+
15+
companion object {
16+
@SuppressLint("StaticFieldLeak")
17+
private var _context: Context? = null
18+
19+
val context: Context get() = _context!!
20+
val db by lazy {
21+
Room.databaseBuilder(context, AppDatabase::class.java, "database-j")
22+
.build()
23+
}
24+
}
25+
26+
override fun onCreate() {
27+
super.onCreate()
28+
_context = this
29+
}
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.wzq.jd.compose.app.data.local
2+
3+
import androidx.room.Database
4+
import androidx.room.RoomDatabase
5+
import com.wzq.jd.compose.app.data.model.ArticleItem
6+
7+
/**
8+
* create by wzq on 2023/12/20
9+
*
10+
*/
11+
@Database(entities = [ArticleItem::class], version = 1)
12+
abstract class AppDatabase : RoomDatabase() {
13+
abstract fun articleDao(): ArticleDao
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.wzq.jd.compose.app.data.local
2+
3+
import androidx.room.Dao
4+
import androidx.room.Insert
5+
import androidx.room.OnConflictStrategy
6+
import androidx.room.Query
7+
import com.wzq.jd.compose.app.data.model.ArticleItem
8+
9+
/**
10+
* create by wzq on 2023/12/20
11+
*
12+
*/
13+
@Dao
14+
interface ArticleDao {
15+
16+
@Insert(onConflict = OnConflictStrategy.REPLACE)
17+
suspend fun insert(articles: List<ArticleItem>)
18+
19+
@Query("select * from articles where chapterId = :cid")
20+
suspend fun getArticlesByCid(cid: Int): List<ArticleItem>
21+
22+
@Query("select * from articles")
23+
suspend fun getArticlesAll(): List<ArticleItem>
24+
}

app/src/main/java/com/wzq/jd/compose/app/data/model/ArticleItem.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.wzq.jd.compose.app.data.model
22

33
import android.os.Parcelable
4+
import androidx.room.Entity
5+
import androidx.room.PrimaryKey
46
import kotlinx.parcelize.Parcelize
57
import kotlinx.serialization.Serializable
68

9+
@Entity (tableName = "articles")
710
@Parcelize
811
@Serializable
912
data class ArticleItem(
10-
val adminAdd: Boolean,
1113
val apkLink: String,
1214
val audit: Int,
1315
val author: String,
@@ -21,7 +23,7 @@ data class ArticleItem(
2123
val envelopePic: String,
2224
val fresh: Boolean,
2325
val host: String,
24-
val id: Int,
26+
@PrimaryKey val id: Int,
2527
val isAdminAdd: Boolean,
2628
val link: String,
2729
val niceDate: String,

app/src/main/java/com/wzq/jd/compose/app/page/categories/CategoriesViewModel.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import androidx.compose.runtime.mutableStateMapOf
44
import androidx.lifecycle.SavedStateHandle
55
import androidx.lifecycle.ViewModel
66
import androidx.lifecycle.viewModelScope
7+
import com.wzq.jd.compose.app.App
78
import com.wzq.jd.compose.app.data.DataRepos
89
import com.wzq.jd.compose.app.data.model.ArticleItem
910
import com.wzq.jd.compose.app.data.model.Categories
11+
import kotlinx.coroutines.delay
1012
import kotlinx.coroutines.launch
1113

1214
/**
@@ -24,10 +26,21 @@ class CategoriesViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
2426
if (pagerData.containsKey(index)) {
2527
return@launch
2628
}
27-
val cid = categories?.children?.get(index)?.id
28-
DataRepos.remoteRepo.getArticleList(cid = cid).onSuccess { result ->
29-
pagerData[index] = result.data.listData
29+
val cid = categories?.children?.get(index)?.id ?: 0
30+
println(cid)
31+
pagerData[index] = App.db.articleDao().getArticlesByCid(cid).also {
32+
println(it)
3033
}
34+
// val all = (App.db.articleDao().getArticlesAll())
35+
// println(all.size)
36+
launch {
37+
delay(5000)
38+
DataRepos.remoteRepo.getArticleList(cid = cid).onSuccess { result ->
39+
println(result.data.listData)
40+
pagerData[index] = result.data.listData
41+
}
42+
}
43+
println(11111)
3144
}
3245
}
3346

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ espresso-core = "3.5.1"
2323
[libraries]
2424
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigation-compose" }
2525
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
26-
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
26+
androidx-room-runtime = { module = "androidx.room:room-ktx", version.ref = "room" }
2727
core-ktx = { module = "androidx.core:core-ktx", version.ref = "core-ktx" }
2828

2929
coil = { module = "io.coil-kt:coil-compose", version.ref = "coil" }

0 commit comments

Comments
 (0)