File tree Expand file tree Collapse file tree 5 files changed +22
-15
lines changed
app/src/main/java/com/wzq/jd/compose/app Expand file tree Collapse file tree 5 files changed +22
-15
lines changed Original file line number Diff line number Diff line change @@ -3,29 +3,25 @@ package com.wzq.jd.compose.app
33import android.annotation.SuppressLint
44import android.app.Application
55import android.content.Context
6- import androidx.room.Room
76import com.wzq.jd.compose.app.data.local.AppDatabase
87
98/* *
109 * create by wzq on 2023/12/20
1110 *
1211 */
13- class App : Application () {
12+ class App : Application () {
1413
1514 companion object {
1615 @SuppressLint(" StaticFieldLeak" )
1716 private var _context : Context ? = null
1817
1918 val context: Context get() = _context !!
20- val db by lazy {
21- Room .databaseBuilder(context, AppDatabase ::class .java, " database-j" )
22- .fallbackToDestructiveMigration()
23- .build()
24- }
2519 }
2620
2721 override fun onCreate () {
2822 super .onCreate()
2923 _context = this
24+
25+ AppDatabase .instance
3026 }
3127}
Original file line number Diff line number Diff line change 11package com.wzq.jd.compose.app.data.local
22
33import androidx.room.Database
4+ import androidx.room.Room
45import androidx.room.RoomDatabase
6+ import com.wzq.jd.compose.app.App
57import com.wzq.jd.compose.app.data.model.ArticleItem
68import com.wzq.jd.compose.app.data.model.Categories
79
@@ -13,4 +15,12 @@ import com.wzq.jd.compose.app.data.model.Categories
1315abstract class AppDatabase : RoomDatabase () {
1416 abstract fun articleDao (): ArticleDao
1517 abstract fun categoriesDao (): CategoriesDao
18+
19+ companion object {
20+ val instance by lazy {
21+ Room .databaseBuilder(App .context, AppDatabase ::class .java, " database-j" )
22+ .fallbackToDestructiveMigration()
23+ .build()
24+ }
25+ }
1626}
Original file line number Diff line number Diff line change @@ -4,8 +4,8 @@ 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
87import com.wzq.jd.compose.app.data.DataRepos
8+ import com.wzq.jd.compose.app.data.local.AppDatabase
99import com.wzq.jd.compose.app.data.model.ArticleItem
1010import com.wzq.jd.compose.app.data.model.Categories
1111import kotlinx.coroutines.launch
@@ -20,21 +20,22 @@ class CategoriesViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
2020
2121 val pagerData = mutableStateMapOf<Int , List <ArticleItem >>()
2222
23- fun getItemList (index : Int , batch : Int = 0) {
23+ private val articleDao = AppDatabase .instance.articleDao()
24+
25+ fun getItemList (index : Int ) {
2426 viewModelScope.launch {
2527 if (pagerData.containsKey(index)) {
2628 return @launch
2729 }
2830 val cid = categories?.children?.get(index)?.id ? : return @launch
2931
30- val dao = App .db.articleDao()
31- val data = dao.getArticlesByCid(cid)
32+ val data = articleDao.getArticlesByCid(cid)
3233 if (data.isNotEmpty()) {
3334 pagerData[index] = data
3435 } else {
3536 DataRepos .remoteRepo.getArticleList(cid = cid).onSuccess { result ->
3637 pagerData[index] = result.data.listData.also {
37- dao .insert(it)
38+ articleDao .insert(it)
3839 }
3940 }
4041 }
Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ fun CategoryScreen(
6262
6363 LaunchedEffect (key1 = pagerState, block = {
6464 snapshotFlow { pagerState.currentPage }.collect {
65- viewModel.getItemList(pagerState.currentPage, 3 )
65+ viewModel.getItemList(pagerState.currentPage)
6666 }
6767 })
6868 ;
Original file line number Diff line number Diff line change @@ -4,8 +4,8 @@ import androidx.compose.runtime.mutableStateListOf
44import androidx.compose.runtime.mutableStateOf
55import androidx.lifecycle.ViewModel
66import androidx.lifecycle.viewModelScope
7- import com.wzq.jd.compose.app.App
87import com.wzq.jd.compose.app.data.DataRepos
8+ import com.wzq.jd.compose.app.data.local.AppDatabase
99import com.wzq.jd.compose.app.data.model.ArticleItem
1010import com.wzq.jd.compose.app.data.model.Categories
1111import com.wzq.jd.compose.app.page.PageState
@@ -23,7 +23,7 @@ class HomeViewModel : ViewModel() {
2323
2424 val categories = mutableStateListOf<Categories >()
2525
26- private val categoriesDao = App .db .categoriesDao()
26+ private val categoriesDao = AppDatabase .instance .categoriesDao()
2727
2828 init {
2929 getArticleList()
You can’t perform that action at this time.
0 commit comments