InfoApp Jetpack Compose | #2

MainActivity.kt

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import com.example.infoapp.ui.theme.InfoAppTheme
import com.example.infoapp.ui_components.DrawerMenu
import com.example.infoapp.ui_components.MainTopBar
import com.example.infoapp.utils.DrawerEvents
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val scaffoldState = rememberScaffoldState()
val coroutineScope = rememberCoroutineScope()
val topBarTitle = remember {
mutableStateOf("Грибы")
}
InfoAppTheme {
Scaffold(
scaffoldState = scaffoldState,
topBar = {
MainTopBar(
title = topBarTitle.value,
scaffoldState
)
},
drawerContent = {
DrawerMenu(){ event ->
when(event){
is DrawerEvents.OnItemClick -> {
topBarTitle.value = event.title
}
}
coroutineScope.launch {
scaffoldState.drawerState.close()
}
}
}
) {

}
}
}
}
}

DrawerMenu.kt

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.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.example.infoapp.ui.theme.MainRed
import com.example.infoapp.R
import com.example.infoapp.ui.theme.BgTransparent
import com.example.infoapp.utils.DrawerEvents


@Composable
fun DrawerMenu(onEvent: (DrawerEvents) -> Unit) {
Box(modifier = Modifier.fillMaxSize()) {
Image(
painter = painterResource(
id = R.drawable.drawer_list_bg
),
contentDescription = "Main Bg image",
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop
)
Column(modifier = Modifier.fillMaxSize()) {
Header()
Body(){ event ->
onEvent(event)
}
}
}
}

@Composable
fun Header() {
Card(
modifier = Modifier
.fillMaxWidth()
.height(170.dp)
.padding(5.dp),
shape = RoundedCornerShape(10.dp),
border = BorderStroke(1.dp, MainRed)
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.BottomCenter
) {
Image(
painter = painterResource(id = R.drawable.header_bg),
contentDescription = "Header image",
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop
)
Text(
text = "-Справочник ботаника-",
modifier = Modifier
.fillMaxWidth()
.background(MainRed)
.padding(10.dp),
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold,
color = Color.White
)
}
}
}

@Composable
fun Body(onEvent: (DrawerEvents) -> Unit) {
val list = stringArrayResource(id = R.array.drawer_list)
LazyColumn(modifier = Modifier.fillMaxSize()) {
itemsIndexed(list) { index, title ->
Card(
modifier = Modifier
.fillMaxWidth()
.padding(3.dp),
backgroundColor = BgTransparent
) {
Text(
text = title,
modifier = Modifier
.fillMaxWidth()
.clickable {
onEvent(DrawerEvents.OnItemClick(title, index))
}
.padding(10.dp)
.wrapContentWidth(),
fontWeight = FontWeight.Bold
)
}
}
}
}

DrawerEvents.kt

sealed class DrawerEvents{
data class OnItemClick(val title: String, val index: Int): DrawerEvents()
}

ListItem.kt

data class ListItem(
val title: String,
val imageName: String
)

main_array.kt

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="drawer_list">
<item>Грибы</item>
<item>Лечебные травы</item>
<item>Деревья</item>
<item>Фрукты</item>
<item>Овощи</item>
</string-array>

<string-array name="mushrooms">
<item>Лисичка|lesichka.png</item>
<item>Мухамор|muhamor.png</item>
</string-array>

<string-array name="medicinal_herbs">
<item>Ромашка|romashka.png</item>
<item>Крапива|krapiva.png</item>
</string-array>

<string-array name="trees">
<item>Тополь|topol.png</item>
<item>Дуб|dub.png</item>
</string-array>

<string-array name="fruits">
<item>Апельсин|apelsin.png</item>
<item>Яблоко|yabloco.png</item>
</string-array>

<string-array name="vegetables">
<item>Капуста|kapusta.png</item>
<item>Свекла|svekla.png</item>
</string-array>
</resources>

Картинки:

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

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