File tree Expand file tree Collapse file tree 7 files changed +92
-7
lines changed
java/com/wzq/jd/compose/app Expand file tree Collapse file tree 7 files changed +92
-7
lines changed Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package com.wzq.jd.compose.app.data.model
22
33import android.os.Parcelable
4+ import androidx.room.Entity
5+ import androidx.room.PrimaryKey
46import kotlinx.parcelize.Parcelize
57import kotlinx.serialization.Serializable
68
9+ @Entity (tableName = " articles" )
710@Parcelize
811@Serializable
912data 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 ,
Original file line number Diff line number Diff line change @@ -4,9 +4,11 @@ import androidx.compose.runtime.mutableStateMapOf
44import androidx.lifecycle.SavedStateHandle
55import androidx.lifecycle.ViewModel
66import androidx.lifecycle.viewModelScope
7+ import com.wzq.jd.compose.app.App
78import com.wzq.jd.compose.app.data.DataRepos
89import com.wzq.jd.compose.app.data.model.ArticleItem
910import com.wzq.jd.compose.app.data.model.Categories
11+ import kotlinx.coroutines.delay
1012import 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
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ espresso-core = "3.5.1"
2323[libraries ]
2424androidx-navigation-compose = { module = " androidx.navigation:navigation-compose" , version.ref = " navigation-compose" }
2525androidx-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" }
2727core-ktx = { module = " androidx.core:core-ktx" , version.ref = " core-ktx" }
2828
2929coil = { module = " io.coil-kt:coil-compose" , version.ref = " coil" }
You can’t perform that action at this time.
0 commit comments