This commit is contained in:
Андрей Дувакин 2025-01-26 21:56:55 +05:00
parent 56593401d3
commit ed98b2eff7
3 changed files with 88 additions and 3 deletions

View File

@ -40,6 +40,8 @@ android {
}
dependencies {
implementation(libs.gson)
implementation(libs.coil.compose)
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)

View File

@ -8,13 +8,18 @@ import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
@ -31,26 +36,38 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
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.RectangleShape
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.example.russionroadsapp.ui.theme.RussionRoadsAppTheme
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
val listState = rememberLazyListState()
val coroutineScope = rememberCoroutineScope()
var messageToast by remember {
mutableStateOf("")
}
var newsList by remember {
mutableStateOf(listOf<Map<String, String>>())
}
var requestNews by remember {
mutableStateOf(true)
}
@ -80,6 +97,21 @@ class MainActivity : ComponentActivity() {
}
}
LaunchedEffect(newsString) {
if (newsString != "") {
val gson = Gson()
val dataType = object : TypeToken<List<Map<String, String>>>() {}.type
val data: List<Map<String, String>> = gson.fromJson(
newsString,
dataType
)
newsList = data
newsString = ""
println(data)
}
}
Column(
Modifier
.fillMaxSize()
@ -142,12 +174,24 @@ class MainActivity : ComponentActivity() {
modifier = Modifier
.clip(CircleShape)
.clickable {
coroutineScope.launch {
val targetIndex = (listState.firstVisibleItemIndex - 1).coerceAtLeast(0)
listState.animateScrollToItem(targetIndex)
}
}
.padding(20.dp)
.weight(1f)
)
Text(newsString)
LazyRow(
state = listState,
modifier = Modifier.weight(4f),
horizontalArrangement = Arrangement.spacedBy(20.dp)
) {
items(newsList.size) { index ->
NewsCard(newsList[index])
}
}
Icon(
Icons.AutoMirrored.Filled.ArrowForward,
@ -155,9 +199,14 @@ class MainActivity : ComponentActivity() {
modifier = Modifier
.clip(CircleShape)
.clickable {
coroutineScope.launch {
val targetIndex =
(listState.firstVisibleItemIndex + 1).coerceAtMost(newsList.size - 1)
listState.animateScrollToItem(targetIndex)
}
}
.padding(20.dp)
.weight(1f)
)
}
@ -167,3 +216,33 @@ class MainActivity : ComponentActivity() {
}
}
}
@Composable
fun NewsCard(news: Map<String, String>) {
Column(
Modifier
.size(230.dp, 500.dp)
.clip(RoundedCornerShape(10.dp))
.background(Color(91, 188, 34, 255)),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceAround,
) {
AsyncImage(
model = news["image"],
contentDescription = "Фото",
modifier = Modifier
.size(150.dp, 150.dp)
.clip(CircleShape),
contentScale = ContentScale.Crop
)
news["title"]?.let { Text(it) }
news["description"]?.let { Text(it) }
Row(
Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text("")
news["pubDate"]?.let { Text(it) }
}
}
}

View File

@ -1,5 +1,7 @@
[versions]
agp = "8.8.0"
coilCompose = "2.4.0"
gson = "2.10.1"
kotlin = "2.0.0"
coreKtx = "1.10.1"
junit = "4.13.2"
@ -11,6 +13,8 @@ composeBom = "2024.04.01"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
coil-compose = { module = "io.coil-kt:coil-compose", version.ref = "coilCompose" }
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }