create dataService sibgleton and load it on app launch

This commit is contained in:
Satindar Dhillon 2023-01-03 12:15:27 -08:00
parent 6a6ad34f4d
commit 3aff827665
7 changed files with 63 additions and 5 deletions

View file

@ -1,7 +1,9 @@
package app.omnivore.omnivore
import android.content.Context
import androidx.room.Room
import app.omnivore.omnivore.networking.Networker
import app.omnivore.omnivore.persistence.AppDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@ -26,4 +28,8 @@ object AppModule {
@Singleton
@Provides
fun provideAnalytics(@ApplicationContext app: Context) = EventTracker(app)
@Singleton
@Provides
fun provideDataService(@ApplicationContext app: Context) = DataService(app)
}

View file

@ -0,0 +1,15 @@
package app.omnivore.omnivore
import android.content.Context
import androidx.room.Room
import app.omnivore.omnivore.persistence.AppDatabase
import javax.inject.Inject
class DataService @Inject constructor(
context: Context
) {
val db = Room.databaseBuilder(
context,
AppDatabase::class.java, "omnivore-database"
).build()
}

View file

@ -33,7 +33,7 @@ suspend fun Networker.linkedItem(slug: String): LinkedItemQueryResponse {
id = it.labelFields.id,
name = it.labelFields.name,
color = it.labelFields.color,
createdAt = it.labelFields.createdAt,
createdAt = it.labelFields.createdAt as String?,
labelDescription = it.labelFields.description
)
}

View file

@ -0,0 +1,16 @@
package app.omnivore.omnivore.persistence
import androidx.room.Database
import androidx.room.RoomDatabase
import app.omnivore.omnivore.persistence.entities.Viewer
import app.omnivore.omnivore.persistence.entities.ViewerDao
@Database(
entities = [
Viewer::class
],
version = 1
)
abstract class AppDatabase : RoomDatabase() {
abstract fun viewerDao(): ViewerDao
}

View file

@ -1,2 +0,0 @@
package app.omnivore.omnivore.persistence

View file

@ -1,7 +1,6 @@
package app.omnivore.omnivore.persistence.entities
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.*
@Entity
data class Viewer(
@ -11,3 +10,17 @@ data class Viewer(
val profileImageURL: String?,
)
@Dao
interface ViewerDao {
@Query("SELECT * FROM viewer")
fun getAll(): List<Viewer>
@Query("SELECT * FROM viewer WHERE userID IN (:viewerIds)")
fun loadAllByIds(viewerIds: IntArray): List<Viewer>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(vararg viewers: Viewer)
@Delete
fun delete(viewer: Viewer)
}

View file

@ -1,22 +1,27 @@
package app.omnivore.omnivore.ui.home
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.omnivore.omnivore.DataService
import app.omnivore.omnivore.persistence.entities.LinkedItem
import app.omnivore.omnivore.networking.*
import app.omnivore.omnivore.persistence.AppDatabase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
@HiltViewModel
class HomeViewModel @Inject constructor(
private val networker: Networker,
private val dataService: DataService
): ViewModel() {
private var cursor: String? = null
private var items: List<LinkedItem> = listOf()
@ -53,6 +58,11 @@ class HomeViewModel @Inject constructor(
}
viewModelScope.launch {
withContext(Dispatchers.IO) {
val viewers = dataService.db.viewerDao().getAll()
Log.d("appDatabase", "found ${viewers.count()} viewers in db")
}
val thisSearchIdx = searchIdx
searchIdx += 1