InfoApp Jetpack Compose | #5

Зависимости в build.gradle:

implementation 'com.google.dagger:hilt-android:2.46.1'
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
kapt 'com.google.dagger:hilt-compiler:2.46.1'

implementation 'androidx.room:room-ktx:2.5.2'
kapt 'androidx.room:room-compiler:2.5.2'

def nav_version = "2.5.3"
implementation("androidx.navigation:navigation-compose:$nav_version")

App.kt

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class App : Application() {
}

AndroidManifest.kt

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.InfoApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.InfoApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

MainDb.kt

import androidx.room.Database
import androidx.room.RoomDatabase
import com.example.infoapp.utils.ListItem

@Database(
entities = [ListItem::class],
version = 1
)
abstract class MainDb : RoomDatabase() {
abstract val dao: Dao
}

Dao.kt

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.example.infoapp.utils.ListItem
import kotlinx.coroutines.flow.Flow

@Dao
interface Dao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertItem(item: ListItem)
@Delete
suspend fun deleteItem(item: ListItem)
@Query("SELECT * FROM main WHERE category LIKE :cat")
suspend fun getAllItemsByCategory(cat: String): List<ListItem>
}

MainModule.kt

import android.app.Application
import androidx.room.Room
import com.example.infoapp.db.MainDb
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object MainModule {

@Provides
@Singleton
fun provideMainDb(app: Application): MainDb{
return Room.databaseBuilder(
app,
MainDb::class.java,
"info.db"
).createFromAsset("db/info.db").build()
}
}

MainScreen.kt

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Scaffold
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.infoapp.MainViewModel
import com.example.infoapp.utils.DrawerEvents
import com.example.infoapp.utils.ListItem
import kotlinx.coroutines.launch

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun MainScreen(
mainViewModel: MainViewModel = hiltViewModel(),
onClick: (ListItem) -> Unit
) {
val scaffoldState = rememberScaffoldState()
val coroutineScope = rememberCoroutineScope()
val mainList = mainViewModel.mainList
val topBarTitle = remember {
mutableStateOf("Грибы")
}
mainViewModel.getAllItemsByCategory(topBarTitle.value)
Scaffold(
scaffoldState = scaffoldState,
topBar = {
MainTopBar(
title = topBarTitle.value,
scaffoldState
)
},
drawerContent = {
DrawerMenu(){ event ->
when(event){
is DrawerEvents.OnItemClick -> {
topBarTitle.value = event.title
mainViewModel.getAllItemsByCategory(event.title)
}
}
coroutineScope.launch {
scaffoldState.drawerState.close()
}
}
}
) {
LazyColumn(modifier = Modifier.fillMaxSize()){
items(mainList.value){ item ->
MainListItem(item = item){ listItem ->
onClick(listItem)
}
}
}
}
}

ListItem.kt

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity (tableName = "main")
data class ListItem(
@PrimaryKey (autoGenerate = true)
val id: Int? = null,
val title: String,
val imageName: String,
val htmlName: String,
val category: String,
val isFav: Boolean
)

MainViewModel.kt

import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.infoapp.db.MainDb
import com.example.infoapp.ui.theme.MainRed
import com.example.infoapp.utils.ListItem
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
val mainDb: MainDb
): ViewModel() {
val mainList = mutableStateOf(emptyList<ListItem>())

fun getAllItemsByCategory(cat: String) = viewModelScope.launch {
mainList.value = mainDb.dao.getAllItemsByCategory(cat)
}

fun insertItem(item: ListItem) = viewModelScope.launch {
mainDb.dao.insertItem(item)
}

fun deleteItem(item: ListItem) = viewModelScope.launch {
mainDb.dao.deleteItem(item)
}
}

Добавить комментарий

Ваш адрес email не будет опубликован.