diff --git a/app/src/main/java/com/example/testktor/MainActivity.kt b/app/src/main/java/com/example/testktor/MainActivity.kt index c6a5cb0..54ee8a0 100644 --- a/app/src/main/java/com/example/testktor/MainActivity.kt +++ b/app/src/main/java/com/example/testktor/MainActivity.kt @@ -1,57 +1,3 @@ -//package com.example.testktor -// -//import android.os.Bundle -//import android.util.Log -//import androidx.activity.ComponentActivity -//import androidx.activity.compose.setContent -//import androidx.activity.enableEdgeToEdge -//import androidx.compose.foundation.layout.fillMaxSize -//import androidx.compose.foundation.layout.padding -//import androidx.compose.material3.Scaffold -//import androidx.compose.material3.Text -//import androidx.compose.material3.CircularProgressIndicator -//import androidx.compose.runtime.* -//import androidx.compose.ui.Modifier -//import androidx.compose.ui.tooling.preview.Preview -//import com.example.testktor.ui.theme.TestKtorTheme -//import io.ktor.client.* -//import io.ktor.client.request.* -//import io.ktor.client.engine.android.* -//import io.ktor.client.plugins.contentnegotiation.* -//import io.ktor.client.statement.HttpResponse -//import io.ktor.client.statement.bodyAsText -//import io.ktor.http.ContentType -//import io.ktor.http.contentType -//import kotlinx.coroutines.launch -//import kotlinx.serialization.Serializable -//import io.ktor.serialization.kotlinx.json.* -//import kotlinx.serialization.decodeFromString -//import kotlinx.serialization.json.Json -// -//@Serializable -//data class User(val id: Int, val name: String) -// -//class MainActivity : ComponentActivity() { -// private val client = HttpClient(Android) { -// install(ContentNegotiation) { -// json(Json { prettyPrint = true }) -// } -// } -// -// override fun onCreate(savedInstanceState: Bundle?) { -// super.onCreate(savedInstanceState) -// enableEdgeToEdge() -// setContent { -// TestKtorTheme { -// Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> -// UserScreen(modifier = Modifier.padding(innerPadding), client = client) -// } -// } -// } -// } -//} -// - package com.example.testktor import android.os.Bundle @@ -72,10 +18,11 @@ import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController import com.example.matuletest.ui.auth.FirstScreen import com.example.matuletest.ui.auth.SliderScreen -import com.example.testktor.auth.AuthScreen -import com.example.testktor.auth.ForgotPasswordScreen -import com.example.testktor.auth.RegScreen -import com.example.testktor.auth.ResetPasswordScreen +import com.example.testktor.screen.auth.AuthScreen +import com.example.testktor.screen.auth.ForgotPasswordScreen +import com.example.testktor.screen.auth.MainSneakersScreen +import com.example.testktor.screen.auth.RegScreen +import com.example.testktor.screen.auth.ResetPasswordScreen //import com.example.testktor.auth.AuthScreen //import com.example.testktor.auth.RegistrationScreen import com.example.testktor.ui.theme.TestKtorTheme @@ -93,26 +40,6 @@ import io.ktor.serialization.kotlinx.json.* import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json -//class MainActivity : ComponentActivity() { -// private val client = HttpClient(Android) { -// install(ContentNegotiation) { -// json(Json { prettyPrint = true }) -// } -// } -// -// override fun onCreate(savedInstanceState: Bundle?) { -// super.onCreate(savedInstanceState) -// enableEdgeToEdge() -// setContent { -// TestKtorTheme { -// Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> -// UserScreen(modifier = Modifier.padding(innerPadding), client = client) -// } -// } -// } -// } -//} - class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -144,6 +71,9 @@ class MainActivity : ComponentActivity() { composable("forgotPassword_screen") { ForgotPasswordScreen(navController) } + composable("mainSneakers_screen") { + MainSneakersScreen(navController) + } } } } diff --git a/app/src/main/java/com/example/testktor/getUser.kt b/app/src/main/java/com/example/testktor/getUser.kt index 1b04ba9..d3e34d4 100644 --- a/app/src/main/java/com/example/testktor/getUser.kt +++ b/app/src/main/java/com/example/testktor/getUser.kt @@ -16,80 +16,24 @@ import com.example.testktor.ui.theme.TestKtorTheme import io.ktor.client.HttpClient import io.ktor.client.engine.android.Android import io.ktor.client.request.get +import io.ktor.client.request.headers import io.ktor.client.request.post import io.ktor.client.request.setBody import io.ktor.client.statement.HttpResponse import io.ktor.client.statement.bodyAsText import io.ktor.client.utils.EmptyContent.contentType +import io.ktor.client.utils.EmptyContent.headers import io.ktor.http.ContentType +import io.ktor.http.HttpHeaders import io.ktor.http.contentType +import kotlinx.serialization.Contextual import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json +import java.math.BigDecimal import java.sql.Time -//@Composable -//fun UserScreen(modifier: Modifier = Modifier, client: HttpClient) { -// var users by remember { mutableStateOf?>(null) } -// var isLoading by remember { mutableStateOf(true) } -// var errorMessage by remember { mutableStateOf(null) } -// -// val scope = rememberCoroutineScope() -// -// LaunchedEffect(Unit) { -// try { -// val fetchedUsers = getUsers(client) -// users = fetchedUsers -// } catch (e: Exception) { -// errorMessage = "Error fetching users: ${e.localizedMessage}" -// Log.e("UserScreen", "Error: ${e.localizedMessage}", e) -// } -// isLoading = false -// } -// -// if (isLoading) { -// CircularProgressIndicator() -// } -// -// errorMessage?.let { -// Text(text = it) -// } -// -// users?.let { -// it.forEach { user -> -// Text(text = "User: ${user.name}, ID: ${user.id}") -// } -// } ?: run { -// Text(text = "No users found or failed to load.") -// } -//} -// -//suspend fun getUsers(client: HttpClient): List { -// val response: HttpResponse = client.get("http://212.67.10.54:8080/users") { -// contentType(ContentType.Application.Json) -// } -// val responseBody = response.bodyAsText() -// Log.d("getUsers", "Response: $responseBody") // Логируем ответ сервера -// -// val json = Json { ignoreUnknownKeys = true } -// -// return try { -// json.decodeFromString>(responseBody) // Десериализуем в List -// } catch (e: Exception) { -// Log.e("getUsers", "Error deserializing response", e) -// throw e // Повторно выбрасываем ошибку, чтобы она была обработана в UserScreen -// } -//} -// -//@Preview(showBackground = true) -//@Composable -//fun UserScreenPreview() { -// TestKtorTheme { -// UserScreen(client = HttpClient(Android)) -// } -//} - @Serializable data class User( val id: Int, @@ -98,59 +42,31 @@ data class User( val password: String, val resetToken: String) -//@Composable -//fun UserScreen(modifier: Modifier = Modifier, client: HttpClient) { -// var user by remember { mutableStateOf(null) } -// var isLoading by remember { mutableStateOf(true) } -// var errorMessage by remember { mutableStateOf(null) } -// -// val scope = rememberCoroutineScope() -// -// LaunchedEffect(Unit) { -// try { -// val fetchedUser = getUser(client) -// user = fetchedUser -// } catch (e: Exception) { -// errorMessage = "Error fetching user: ${e.localizedMessage}" -// Log.e("UserScreen", "Error: ${e.localizedMessage}", e) -// } -// isLoading = false -// } -// -// if (isLoading) { -// CircularProgressIndicator() -// } -// -// errorMessage?.let { -// Text(text = it) -// } -// -// user?.let { -// Text(text = "User: ${it.email}, ID: ${it.id}") -// } ?: run { -// Text(text = "No user found or failed to load.") -// } -//} +//@Serializable +//data class Sneaker( +// val id: Int, +// val name: String, +// val description: String, +// @Contextual val cost: BigDecimal, +// val discount: Int, +// val photo: String, +// val gender: Char, +// val bootSize: Int +//) -//suspend fun getUser(client: HttpClient): User { -// val response: HttpResponse = client.get("http://10.0.2.2:8080/user") { -// contentType(ContentType.Application.Json) -// } -// val responseBody = response.bodyAsText() -// Log.d("getUser", "Response: $responseBody") -// -// val json = Json { ignoreUnknownKeys = true } -// -// val buba = json.decodeFromString(responseBody) -// Log.d("json", buba.email) -// -// return try { -// json.decodeFromString(responseBody) -// } catch (e: Exception) { -// Log.e("getUser", "Error deserializing response", e) -// throw e -// } -//} +@Serializable +data class Sneaker( + val id: Int, + val name: String, + val description: String, + val cost: Double, + val discount: Int, + val photo: String, + val gender: Char, + val bootSize: Int +) + +var Token: String = ""; suspend fun authUser(client: HttpClient, email: String, password: String): Boolean { try { @@ -165,7 +81,12 @@ suspend fun authUser(client: HttpClient, email: String, password: String): Boole val json = Json { ignoreUnknownKeys = true } val responseMap = json.decodeFromString>(responseBody) - return responseMap["message"] == "Вход успешен" + if (responseMap.containsKey("Token") && responseMap["Token"]!!.isNotEmpty()){ + Token = responseMap["Token"]!! + return true + } + + return false } catch (e: Exception) { Log.e("authUser", "Error during login request", e) return false @@ -232,10 +153,23 @@ suspend fun resetPassword(client: HttpClient, code: String, newPassword: String) } } -//@Preview(showBackground = true) -//@Composable -//fun UserScreenPreview() { -// TestKtorTheme { -// UserScreen(client = HttpClient(Android)) -// } -//} \ No newline at end of file +suspend fun getSneakers(client: HttpClient): List{ + try { + val response: HttpResponse = client.get("http://10.0.2.2:8080/mainWindow") { + headers { + append(HttpHeaders.Authorization, "Bearer $Token") + } + } + + val responseBody = response.bodyAsText() + Log.d("getSneakers", "Response: $responseBody") + + val json = Json { ignoreUnknownKeys = true } + val responseMap = json.decodeFromString>>(responseBody) + + return responseMap["sneakers"] ?: emptyList() + } catch (e: Exception) { + Log.e("getSneakers", "error fetching sneakers", e) + return emptyList() + } +} diff --git a/app/src/main/java/com/example/testktor/auth/AuthScreen.kt b/app/src/main/java/com/example/testktor/screen/auth/AuthScreen.kt similarity index 92% rename from app/src/main/java/com/example/testktor/auth/AuthScreen.kt rename to app/src/main/java/com/example/testktor/screen/auth/AuthScreen.kt index e6897a0..c317886 100644 --- a/app/src/main/java/com/example/testktor/auth/AuthScreen.kt +++ b/app/src/main/java/com/example/testktor/screen/auth/AuthScreen.kt @@ -1,4 +1,4 @@ -package com.example.testktor.auth +package com.example.testktor.screen.auth import androidx.compose.foundation.hoverable import androidx.compose.foundation.layout.Arrangement @@ -75,14 +75,15 @@ fun AuthContent( // Отображение текста "Success" в левом верхнем углу if (authSuccess != null){ if (authSuccess == "Success") { - Text( - text = "Success", - modifier = Modifier - .padding(start = 16.dp, top = 16.dp) - .align(Alignment.Start), - color = Color.Green, - style = TextStyle(fontSize = 18.sp, fontWeight = FontWeight.Bold) - ) +// Text( +// text = "Success", +// modifier = Modifier +// .padding(start = 16.dp, top = 16.dp) +// .align(Alignment.Start), +// color = Color.Green, +// style = TextStyle(fontSize = 18.sp, fontWeight = FontWeight.Bold) +// ) + navController.navigate("mainSneakers_screen") } else{ Text( text = "Error", diff --git a/app/src/main/java/com/example/testktor/auth/BeforeAuthScreen.kt b/app/src/main/java/com/example/testktor/screen/auth/BeforeAuthScreen.kt similarity index 100% rename from app/src/main/java/com/example/testktor/auth/BeforeAuthScreen.kt rename to app/src/main/java/com/example/testktor/screen/auth/BeforeAuthScreen.kt diff --git a/app/src/main/java/com/example/testktor/auth/ForgotPasswordScreen.kt b/app/src/main/java/com/example/testktor/screen/auth/ForgotPasswordScreen.kt similarity index 99% rename from app/src/main/java/com/example/testktor/auth/ForgotPasswordScreen.kt rename to app/src/main/java/com/example/testktor/screen/auth/ForgotPasswordScreen.kt index 8b47f81..b13d9f5 100644 --- a/app/src/main/java/com/example/testktor/auth/ForgotPasswordScreen.kt +++ b/app/src/main/java/com/example/testktor/screen/auth/ForgotPasswordScreen.kt @@ -1,4 +1,4 @@ -package com.example.testktor.auth +package com.example.testktor.screen.auth import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column diff --git a/app/src/main/java/com/example/testktor/screen/auth/MainSneakersScreen.kt b/app/src/main/java/com/example/testktor/screen/auth/MainSneakersScreen.kt new file mode 100644 index 0000000..6e2b14c --- /dev/null +++ b/app/src/main/java/com/example/testktor/screen/auth/MainSneakersScreen.kt @@ -0,0 +1,63 @@ +package com.example.testktor.screen.auth + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +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.unit.dp +import androidx.compose.ui.unit.sp +import androidx.navigation.NavController +import com.example.testktor.Sneaker +import com.example.testktor.authUser +import com.example.testktor.getSneakers +import io.ktor.client.HttpClient +import io.ktor.client.engine.android.Android +import kotlinx.coroutines.launch + +@Composable +fun MainSneakersScreen(navController: NavController) { + var sneakers by remember { mutableStateOf>(emptyList()) } + val coroutineScope = rememberCoroutineScope() + + val client = HttpClient(Android) + + LaunchedEffect(Unit) { + coroutineScope.launch { + sneakers = getSneakers(client) + } + } + + SneakersContent(sneakers) +} + +@Composable +fun SneakersContent( + sneakers: List +) { + Column( + modifier = Modifier + .fillMaxSize() + .padding(16.dp), + horizontalAlignment = Alignment.CenterHorizontally + ) { + Text(text = "Список кроссовок", fontSize = 24.sp, modifier = Modifier.padding(bottom = 16.dp)) + + LazyColumn(modifier = Modifier.fillMaxWidth()) { + items(sneakers) { sneaker -> + Text(text = sneaker.name, fontSize = 18.sp, modifier = Modifier.padding(8.dp)) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/testktor/auth/RegistrationScreen.kt b/app/src/main/java/com/example/testktor/screen/auth/RegistrationScreen.kt similarity index 99% rename from app/src/main/java/com/example/testktor/auth/RegistrationScreen.kt rename to app/src/main/java/com/example/testktor/screen/auth/RegistrationScreen.kt index 82ea99d..084c49b 100644 --- a/app/src/main/java/com/example/testktor/auth/RegistrationScreen.kt +++ b/app/src/main/java/com/example/testktor/screen/auth/RegistrationScreen.kt @@ -1,4 +1,4 @@ -package com.example.testktor.auth +package com.example.testktor.screen.auth import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column diff --git a/app/src/main/java/com/example/testktor/auth/ResetPasswordScreen.kt b/app/src/main/java/com/example/testktor/screen/auth/ResetPasswordScreen.kt similarity index 99% rename from app/src/main/java/com/example/testktor/auth/ResetPasswordScreen.kt rename to app/src/main/java/com/example/testktor/screen/auth/ResetPasswordScreen.kt index ec46e4f..690c06e 100644 --- a/app/src/main/java/com/example/testktor/auth/ResetPasswordScreen.kt +++ b/app/src/main/java/com/example/testktor/screen/auth/ResetPasswordScreen.kt @@ -1,4 +1,4 @@ -package com.example.testktor.auth +package com.example.testktor.screen.auth import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column