InfoApp Jetpack Compose | #6

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

implementation "androidx.constraintlayout:constraintlayout-compose:1.0.1"

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")
fun getAllItemsByCategory(cat: String): Flow<List<ListItem>>
@Query("SELECT * FROM main WHERE isFav = 1")
fun getFavorites(): Flow<List<ListItem>>
}

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.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
val mainDb: MainDb
): ViewModel() {
val mainList = mutableStateOf(emptyList<ListItem>())
private var job: Job? = null

fun getAllItemsByCategory(cat: String){
job?.cancel()
job = viewModelScope.launch {
mainDb.dao.getAllItemsByCategory(cat).collect{ list ->
mainList.value = list
}
}
}

fun getFavorites(){
job?.cancel()
job = viewModelScope.launch {
mainDb.dao.getFavorites().collect{ list ->
mainList.value = list
}
}
}

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

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

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.LaunchedEffect
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("Грибы")
}
LaunchedEffect(Unit){
mainViewModel.getAllItemsByCategory(topBarTitle.value)
}
Scaffold(
scaffoldState = scaffoldState,
topBar = {
MainTopBar(
title = topBarTitle.value,
scaffoldState
){
topBarTitle.value = "Избранные"
mainViewModel.getFavorites()
}
},
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)
}
}
}
}
}

MainListItem.kt

import android.graphics.BitmapFactory
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.infoapp.MainViewModel
import com.example.infoapp.ui.theme.BgTransparent2
import com.example.infoapp.ui.theme.Gray
import com.example.infoapp.ui.theme.MainRed
import com.example.infoapp.utils.ListItem

@Composable
fun MainListItem(
mainViewModel: MainViewModel = hiltViewModel(),
item: ListItem,
onClick: (ListItem) -> Unit
) {
Card(
modifier = Modifier
.fillMaxWidth()
.height(250.dp)
.padding(5.dp)
.clickable {
onClick(item)
},
shape = RoundedCornerShape(10.dp),
border = BorderStroke(1.dp, MainRed)
) {
ConstraintLayout(
modifier = Modifier.fillMaxSize()
) {
val (image, text, favoriteButton) = createRefs()

AssetImage(
imageName = item.imageName,
contentDescription = item.title,
modifier = Modifier
.fillMaxSize()
.constrainAs(image) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
bottom.linkTo(parent.bottom)
}
)
Text(
text = item.title,
modifier = Modifier
.fillMaxWidth()
.background(MainRed)
.padding(10.dp)
.constrainAs(text) {
bottom.linkTo(parent.bottom)
start.linkTo(parent.start)
end.linkTo(parent.end)
},
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold,
color = Color.White
)
IconButton(
onClick = {
mainViewModel.insertItem(
item.copy(isFav = !item.isFav)
)
},
modifier = Modifier.constrainAs(favoriteButton) {
top.linkTo(parent.top)
end.linkTo(parent.end)
}
) {
Icon(
imageVector = Icons.Default.Favorite,
contentDescription = "Favorite",
tint = if(item.isFav) MainRed else Gray,
modifier = Modifier
.clip(CircleShape)
.background(BgTransparent2)
.padding(5.dp)
)
}
}
}
}

@Composable
fun AssetImage(imageName: String, contentDescription: String, modifier: Modifier) {
val context = LocalContext.current
val assetManger = context.assets
val inputStream = assetManger.open(imageName)
val bitMap = BitmapFactory.decodeStream(inputStream)
Image(
bitmap = bitMap.asImageBitmap(),
contentDescription = contentDescription,
contentScale = ContentScale.Crop,
modifier = modifier
)
}

MainTopBar.kt

import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Menu
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.graphics.Color
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.infoapp.MainViewModel
import kotlinx.coroutines.launch

@Composable
fun MainTopBar(
title: String,
scaffoldState: ScaffoldState,
onFavClick: () -> Unit
) {
val coroutine = rememberCoroutineScope()
TopAppBar(
title = {
Text(text = title)
},
backgroundColor = Color.White,
navigationIcon = {
IconButton(
onClick = {
coroutine.launch {
scaffoldState.drawerState.open()
}
}
) {
Icon(
imageVector = Icons.Default.Menu,
contentDescription = "Menu"
)
}
},
actions = {
IconButton(
onClick = {
onFavClick()
}
) {
Icon(
imageVector = Icons.Default.Favorite,
contentDescription = "Favorite"
)
}
}
)
}

Color.kt

import androidx.compose.ui.graphics.Color

val Purple200 = Color(0xFFBB86FC)
val Purple500 = Color(0xFF6200EE)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
val MainRed = Color(0xE6BD0909)
val Gray = Color(0xE63A3A3A)
val BgTransparent = Color(0xB3FFFFFF)
val BgTransparent2 = Color(0x9CFFFFFF)

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

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