InfoApp Jetpack Compose | #3

MainActivity.kt

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import com.example.infoapp.ui.theme.InfoAppTheme
import com.example.infoapp.ui_components.DrawerMenu
import com.example.infoapp.ui_components.MainListItem
import com.example.infoapp.ui_components.MainTopBar
import com.example.infoapp.utils.DrawerEvents
import com.example.infoapp.utils.IdArrayList
import com.example.infoapp.utils.ListItem
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 mainList = remember {
mutableStateOf(getListItemsByIndex(0, this))
}
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
mainList.value = getListItemsByIndex(
event.index, this@MainActivity)
}
}
coroutineScope.launch {
scaffoldState.drawerState.close()
}
}
}
) {
LazyColumn(modifier = Modifier.fillMaxSize()){
items(mainList.value){ item ->
MainListItem(item = item)
}
}
}
}
}
}
}

private fun getListItemsByIndex(index: Int, context: Context): List<ListItem>{
val list = ArrayList<ListItem>()
val arrayList = context.resources.getStringArray(IdArrayList.listId[index])
arrayList.forEach { item ->
val itemArray = item.split("|")
list.add(
ListItem(
itemArray[0],
itemArray[1]
)
)
}
return list
}

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.layout.*
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.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.example.infoapp.R
import com.example.infoapp.ui.theme.MainRed
import com.example.infoapp.utils.ListItem

@Composable
fun MainListItem(item: ListItem) {
Card(
modifier = Modifier
.fillMaxWidth()
.height(250.dp)
.padding(5.dp),
shape = RoundedCornerShape(10.dp),
border = BorderStroke(1.dp, MainRed)
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.BottomCenter
) {
AssetImage(
imageName = item.imageName,
contentDescription = item.title)
Text(
text = item.title,
modifier = Modifier
.fillMaxWidth()
.background(MainRed)
.padding(10.dp),
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold,
color = Color.White
)
}
}
}

@Composable
fun AssetImage(imageName: String, contentDescription: String){
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.fillMaxSize()
)
}

IdArrayList.kt

object IdArrayList {
val listId = listOf(
R.array.mushrooms,
R.array.medicinal_herbs,
R.array.trees,
R.array.fruits,
R.array.vegetables
)
}

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

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