|
@@ -6,12 +6,14 @@ import com.example.test.domain.utlls.Constant
|
|
|
|
|
|
import android.util.Log
|
|
|
import androidx.compose.foundation.Image
|
|
|
+import androidx.compose.foundation.border
|
|
|
import androidx.compose.foundation.layout.Box
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
import androidx.compose.foundation.layout.height
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
|
|
import androidx.compose.foundation.lazy.items
|
|
|
+import androidx.compose.material3.Button
|
|
|
import androidx.compose.material3.CircularProgressIndicator
|
|
|
import androidx.compose.material3.Text
|
|
|
import androidx.compose.runtime.Composable
|
|
@@ -22,38 +24,45 @@ import androidx.compose.runtime.remember
|
|
|
import androidx.compose.runtime.setValue
|
|
|
import androidx.compose.ui.Alignment
|
|
|
import androidx.compose.ui.Modifier
|
|
|
+import androidx.compose.ui.graphics.Color
|
|
|
+import androidx.compose.ui.graphics.RectangleShape
|
|
|
import androidx.compose.ui.layout.ContentScale
|
|
|
import androidx.compose.ui.platform.LocalContext
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
+import androidx.compose.ui.unit.sp
|
|
|
+import androidx.navigation.NavHostController
|
|
|
import coil.compose.AsyncImagePainter
|
|
|
import coil.compose.rememberAsyncImagePainter
|
|
|
import coil.request.ImageRequest
|
|
|
import coil.size.Size
|
|
|
+import com.example.test.model.Admin
|
|
|
import com.example.test.model.genre
|
|
|
+import io.github.jan.supabase.gotrue.auth
|
|
|
+import androidx.navigation.NavController
|
|
|
|
|
|
import io.github.jan.supabase.postgrest.from
|
|
|
-import io.github.jan.supabase.postgrest.query.PostgrestQueryBuilder
|
|
|
-import io.github.jan.supabase.postgrest.result.PostgrestResult
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
-import kotlinx.coroutines.selects.select
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
|
@Composable
|
|
|
-fun FilmsList() {
|
|
|
- var Films1 by remember { mutableStateOf<List<films>>(listOf()) }
|
|
|
- var Rating1 by remember { mutableStateOf<List<rating>>(listOf()) }
|
|
|
- var Genre1 by remember { mutableStateOf<List<genre>>(listOf()) }
|
|
|
+fun FilmsList(navHost: NavHostController) {
|
|
|
+ var films by remember { mutableStateOf<List<films>>(listOf()) }
|
|
|
+ var ratings by remember { mutableStateOf<List<rating>>(listOf()) }
|
|
|
+ var genres by remember { mutableStateOf<List<genre>>(listOf()) }
|
|
|
+ var users by remember { mutableStateOf<List<Admin>>(listOf()) }
|
|
|
|
|
|
LaunchedEffect(Unit) {
|
|
|
withContext(Dispatchers.IO) {
|
|
|
try {
|
|
|
- Films1 = Constant.supabase.from("films")
|
|
|
+ films = Constant.supabase.from("films")
|
|
|
.select().decodeList<films>()
|
|
|
- Rating1 = Constant.supabase.from("rating")
|
|
|
+ ratings = Constant.supabase.from("rating")
|
|
|
.select().decodeList<rating>()
|
|
|
- Genre1 = Constant.supabase.from("genre")
|
|
|
+ genres = Constant.supabase.from("genre")
|
|
|
.select().decodeList<genre>()
|
|
|
- Films1.forEach { film ->
|
|
|
+ users = Constant.supabase.from("Admin")
|
|
|
+ .select().decodeList<Admin>()
|
|
|
+ films.forEach { film ->
|
|
|
Log.d("C", film.films_name)
|
|
|
}
|
|
|
} catch (e: Exception) {
|
|
@@ -61,66 +70,114 @@ fun FilmsList() {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- LazyColumn {
|
|
|
|
|
|
+ LazyColumn {
|
|
|
items(
|
|
|
- Films1,
|
|
|
- key = { films -> films.id },
|
|
|
- ) { films ->
|
|
|
- val imageState = rememberAsyncImagePainter(
|
|
|
- model = ImageRequest.Builder(LocalContext.current).data(films.foto)
|
|
|
- .size(Size.ORIGINAL).build()
|
|
|
- ).state
|
|
|
- if (imageState is AsyncImagePainter.State.Error) {
|
|
|
- Box(
|
|
|
- modifier = Modifier
|
|
|
- .fillMaxWidth()
|
|
|
- .height(200.dp),
|
|
|
- contentAlignment = Alignment.Center
|
|
|
- ) {
|
|
|
- CircularProgressIndicator()
|
|
|
- }
|
|
|
- }
|
|
|
+ films,
|
|
|
+ key = { film -> film.id },
|
|
|
+ ) { film ->
|
|
|
+ FilmItem(
|
|
|
+ film = film,
|
|
|
+ ratings = ratings,
|
|
|
+ genres = genres,
|
|
|
+ users = users,
|
|
|
+ navHost = navHost,
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- if (imageState is AsyncImagePainter.State.Success) {
|
|
|
- Image(
|
|
|
- modifier = Modifier
|
|
|
- .fillMaxWidth()
|
|
|
- .height(200.dp),
|
|
|
- painter = imageState.painter,
|
|
|
- contentDescription = "",
|
|
|
- contentScale = ContentScale.Crop
|
|
|
- )
|
|
|
- }
|
|
|
+@Composable
|
|
|
+fun FilmItem(
|
|
|
+ film: films,
|
|
|
+ ratings: List<rating>,
|
|
|
+ genres: List<genre>,
|
|
|
+ users: List<Admin>,
|
|
|
+ navHost: NavHostController,
|
|
|
+) {
|
|
|
+ val imageState = rememberAsyncImagePainter(
|
|
|
+ model = ImageRequest.Builder(LocalContext.current).data(film.foto)
|
|
|
+ .size(Size.ORIGINAL).build()
|
|
|
+ ).state
|
|
|
+
|
|
|
+ if (imageState is AsyncImagePainter.State.Error) {
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(200.dp),
|
|
|
+ contentAlignment = Alignment.Center
|
|
|
+ ) {
|
|
|
+ CircularProgressIndicator()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (imageState is AsyncImagePainter.State.Success) {
|
|
|
+ Image(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(200.dp),
|
|
|
+ painter = imageState.painter,
|
|
|
+ contentDescription = "",
|
|
|
+ contentScale = ContentScale.Crop
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ Text(
|
|
|
+ film.films_name,
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(8.dp)
|
|
|
+ .border(width = 1.dp, color = Color.Gray, shape = RectangleShape)
|
|
|
+ .padding(horizontal = 4.dp, vertical = 2.dp),
|
|
|
+ )
|
|
|
+
|
|
|
+ val ratingId = film.rating
|
|
|
+ val rating = ratings.find { it.id == ratingId }
|
|
|
+ if (rating != null) {
|
|
|
+ Text(
|
|
|
+ "Рейтинг: ${rating.rating}",
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(8.dp)
|
|
|
+ .border(width = 1.dp, color = Color.Gray, shape = RectangleShape)
|
|
|
+ .padding(horizontal = 4.dp, vertical = 2.dp),
|
|
|
+ )
|
|
|
+ val genreId = film.genre
|
|
|
+ val genre = genres.find { it.id == genreId }
|
|
|
+ if (genre != null) {
|
|
|
Text(
|
|
|
- films.films_name,
|
|
|
- modifier = Modifier.padding(8.dp),
|
|
|
+ "Жанр: ${genre.genre}",
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(8.dp)
|
|
|
+ .border(width = 1.dp, color = Color.Gray, shape = RectangleShape)
|
|
|
+ .padding(horizontal = 4.dp, vertical = 2.dp),
|
|
|
)
|
|
|
- val ratingId = films.rating
|
|
|
- val rating = Rating1.find { it.id == ratingId }
|
|
|
- if (rating != null) {
|
|
|
- Text(
|
|
|
- "Рейтинг: ${rating.rating}",
|
|
|
- modifier = Modifier.padding(8.dp),
|
|
|
- )
|
|
|
- val genreId = films.genre
|
|
|
- val genre = Genre1.find { it.id == genreId }
|
|
|
- if (genre != null) {
|
|
|
- Text(
|
|
|
- "Жанр: ${genre.genre}",
|
|
|
- modifier = Modifier.padding(8.dp),
|
|
|
- )
|
|
|
- } else {
|
|
|
- Text(
|
|
|
- "Жанр не найден",
|
|
|
- modifier = Modifier.padding(8.dp),
|
|
|
- )
|
|
|
- }
|
|
|
- } else {
|
|
|
- Text(
|
|
|
- "Рейтинг не найден",
|
|
|
- modifier = Modifier.padding(8.dp),
|
|
|
- )
|
|
|
+ } else {
|
|
|
+ Text(
|
|
|
+ "Жанр не найден",
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(8.dp)
|
|
|
+ .border(width = 1.dp, color = Color.Gray, shape = RectangleShape)
|
|
|
+ .padding(horizontal = 4.dp, vertical = 2.dp),
|
|
|
+ )
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Text(
|
|
|
+ "Рейтинг не найден",
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(8.dp)
|
|
|
+ .border(width = 1.dp, color = Color.Gray, shape = RectangleShape)
|
|
|
+ .padding(horizontal = 4.dp, vertical = 2.dp),
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ val userId = Constant.supabase.auth.currentUserOrNull()?.id
|
|
|
+ val userIdString = userId.toString()
|
|
|
+ if (userId != null) {
|
|
|
+ val user = users.find { it.id_user == userIdString }
|
|
|
+ if (user != null) {
|
|
|
+ Button(onClick = {
|
|
|
+ navHost.navigate("Insert")
|
|
|
+ }) {
|
|
|
+ Text("Перейти на страницу изменений", fontSize = 25.sp, color = Color.White)
|
|
|
}
|
|
|
}
|
|
|
}
|