Browse Source

FODISFJLDKJKLFDSJKLSDFJKLSFDJIKL:SDLFJIKFSD:JIKLO

CoolVery 1 month ago
parent
commit
b9f3af5185

+ 5 - 3
app/build.gradle.kts

@@ -2,6 +2,8 @@ plugins {
     alias(libs.plugins.android.application)
     alias(libs.plugins.kotlin.android)
     kotlin("plugin.serialization") version "2.0.20"
+    id("kotlin-kapt")
+    id("com.google.dagger.hilt.android")
 }
 
 android {
@@ -78,8 +80,8 @@ dependencies {
     implementation(libs.storage.kt)
     implementation(libs.ktor.client.android)
 
-
-
-
+    implementation("com.google.dagger:hilt-android:2.52")
+    kapt("com.google.dagger:hilt-compiler:2.52")
+    implementation("androidx.hilt:hilt-navigation-compose:1.2.0")
 
 }

+ 5 - 0
app/src/main/AndroidManifest.xml

@@ -4,6 +4,11 @@
     <uses-permission android:name="android.permission.INTERNET" />
 
     <application
+
+        android:name=".MainApplication"
+
+
+
         android:allowBackup="true"
         android:dataExtractionRules="@xml/data_extraction_rules"
         android:fullBackupContent="@xml/backup_rules"

+ 2 - 13
app/src/main/java/com/example/collectionofshortstories/MainActivity.kt

@@ -24,9 +24,11 @@ import androidx.lifecycle.lifecycleScope
 import com.example.collectionofshortstories.domain.untils.Constants
 import com.example.collectionofshortstories.navigation.Navigathion
 import com.example.collectionofshortstories.ui.theme.CollectionOfShortStoriesTheme
+import dagger.hilt.android.AndroidEntryPoint
 import io.github.jan.supabase.gotrue.auth
 import kotlinx.coroutines.launch
 
+@AndroidEntryPoint
 class MainActivity : ComponentActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -41,19 +43,6 @@ class MainActivity : ComponentActivity() {
                 }
             }
         }
-
-        ProcessLifecycleOwner.get().lifecycle.addObserver(object : LifecycleEventObserver {
-            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
-                if (event == Lifecycle.Event.ON_DESTROY) {
-                    // Запуск сопрограммы для вызова signOut()
-                    lifecycleScope.launch {
-                        Log.d("exit","yes")
-                        Constants.supabase.auth.signOut()
-
-                    }
-                }
-            }
-        })
     }
 }
 

+ 8 - 0
app/src/main/java/com/example/collectionofshortstories/MainApplication.kt

@@ -0,0 +1,8 @@
+package com.example.collectionofshortstories
+
+import android.app.Application
+import dagger.hilt.android.HiltAndroidApp
+
+@HiltAndroidApp
+class MainApplication: Application() {
+}

+ 15 - 14
app/src/main/java/com/example/collectionofshortstories/view/authorization/AuthorizationAndLogin.kt

@@ -1,5 +1,6 @@
 package com.example.collectionofshortstories.view.authorization
 
+import android.util.Log
 import android.widget.Button
 import androidx.compose.foundation.BorderStroke
 import androidx.compose.foundation.background
@@ -30,19 +31,29 @@ import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import androidx.navigation.NavHostController
 import androidx.compose.material3.TextField
+import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.runtime.MutableState
+import androidx.compose.runtime.collectAsState
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
+import androidx.hilt.navigation.compose.hiltViewModel
 import com.example.collectionofshortstories.domain.untils.Constants
 import com.example.collectionofshortstories.navigation.Routes
 import io.github.jan.supabase.gotrue.auth
 
 @Composable
-fun AuthorizationAndLogin(navHostController: NavHostController) {
-    val viewModel = AuthorizationViewModel()
+fun AuthorizationAndLogin(navHostController: NavHostController, viewModel: AuthorizationViewModel = hiltViewModel()) {
     val email = remember { mutableStateOf("") }
     val password = remember { mutableStateOf("") }
-    val showError = remember { mutableStateOf(false) }
+
+    LaunchedEffect(key1 = viewModel.navigationTo) {
+        viewModel.navigationTo.collect { destination ->
+            destination?.let {
+                navHostController.navigate(destination)
+            }
+        }
+    }
+
     Column(
         horizontalAlignment = Alignment.CenterHorizontally,
         modifier = Modifier
@@ -83,16 +94,6 @@ fun AuthorizationAndLogin(navHostController: NavHostController) {
             Button(
                 onClick = {
                     viewModel.onSignInEmailPassword(email.value, password.value)
-                    if (Constants.supabase.auth.currentUserOrNull() != null) {
-                        navHostController.navigate(Routes.CATALOG) {
-                            popUpTo(Routes.AUTH) {
-                                inclusive = true
-                            }
-                        }
-                    }
-                    else {
-                        showError.value = true
-                    }
                 },
                 colors = ButtonDefaults.buttonColors(
                     containerColor = Color(0xFFFCE0C6)
@@ -108,7 +109,7 @@ fun AuthorizationAndLogin(navHostController: NavHostController) {
                     color = Color.Black
                 )
             }
-            if (showError.value) {
+            if (viewModel.wrongSignIn.value) {
                 Box(
                     modifier = Modifier
                         .border(BorderStroke(2.dp, Color.Black))

+ 17 - 5
app/src/main/java/com/example/collectionofshortstories/view/authorization/AuthorizationAndLoginViewModel.kt

@@ -1,18 +1,32 @@
 package com.example.collectionofshortstories.view.authorization
 
 import android.util.Log
+import androidx.compose.runtime.MutableState
+import androidx.compose.runtime.mutableStateOf
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
 import androidx.navigation.NavHostController
 import com.example.collectionofshortstories.domain.untils.Constants
+import com.example.collectionofshortstories.model.Role
+import com.example.collectionofshortstories.model.User
 import com.example.collectionofshortstories.navigation.Routes
 import com.example.collectionofshortstories.view.catalog.Catalog
+import dagger.hilt.android.lifecycle.HiltViewModel
 import io.github.jan.supabase.gotrue.auth
 import io.github.jan.supabase.gotrue.providers.builtin.Email
+import io.github.jan.supabase.postgrest.from
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asStateFlow
 import kotlinx.coroutines.launch
+import javax.inject.Inject
 
-class AuthorizationViewModel(): ViewModel() {
+@HiltViewModel
+class AuthorizationViewModel @Inject constructor() : ViewModel() {
+    var wrongSignIn = mutableStateOf(false)
 
+    private var _navigationTo = MutableStateFlow<String?>(null)
+    val navigationTo = _navigationTo.asStateFlow()
 
     fun onSignInEmailPassword(emailUser: String, passwordUser: String) {
         viewModelScope.launch {
@@ -20,13 +34,11 @@ class AuthorizationViewModel(): ViewModel() {
                 val user = Constants.supabase.auth.signInWith(Email) {
                     email = emailUser
                     password = passwordUser
-
                 }
-                Log.d("user", Constants.supabase.auth.currentUserOrNull()!!.id.toString())
+                _navigationTo.value = Routes.CATALOG;
 
             } catch (e: Exception) {
-                println("Error")
-                println(e.message.toString())
+                wrongSignIn.value = true
             }
         }
     }

+ 7 - 14
app/src/main/java/com/example/collectionofshortstories/view/catalog/Catalog.kt

@@ -25,6 +25,7 @@ import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
+import androidx.hilt.navigation.compose.hiltViewModel
 import androidx.lifecycle.viewmodel.compose.viewModel
 import androidx.navigation.NavHostController
 import com.example.collectionofshortstories.domain.untils.Constants
@@ -34,26 +35,18 @@ import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
 
 @Composable
-fun Catalog(navHostController: NavHostController) {
-    val viewModel = CatalogViewModel()
-
-
+fun Catalog(navHostController: NavHostController, viewModel: CatalogViewModel = hiltViewModel()) {
     val articles by viewModel.articles.collectAsState(initial = emptyList())
-    val user_role by viewModel.role_name.collectAsState(initial = "")
 
-    LaunchedEffect(Unit) {
-        viewModel.GetArticles()
-        viewModel.GetUserRole()
-    }
 
-    // Кэширование значения для отображения кнопки
-    val showButton = remember(user_role) { user_role == "Писатель" }
+    viewModel.GetUserRole()
+    viewModel.GetArticles()
 
     Box() {
         LazyColumn(
             modifier = Modifier
-                .fillMaxSize() // Занимает весь экран
-                .padding(16.dp) // Добавлен отступ для LazyColumn
+                .fillMaxSize()
+                .padding(16.dp)
         ) {
             items(
                 articles,
@@ -73,7 +66,7 @@ fun Catalog(navHostController: NavHostController) {
                 }
             }
         }
-        if (showButton) {
+        if (viewModel.showButton.value) {
             Button(
                 onClick = { /* Действия при нажатии на кнопку */ },
                 modifier = Modifier

+ 16 - 28
app/src/main/java/com/example/collectionofshortstories/view/catalog/CatalogViewModel.kt

@@ -16,6 +16,7 @@ import com.example.collectionofshortstories.domain.untils.Constants
 import com.example.collectionofshortstories.model.Article
 import com.example.collectionofshortstories.model.Role
 import com.example.collectionofshortstories.model.User
+import dagger.hilt.android.lifecycle.HiltViewModel
 import io.github.jan.supabase.gotrue.auth
 import io.github.jan.supabase.postgrest.from
 import kotlinx.coroutines.Dispatchers
@@ -24,13 +25,14 @@ import kotlinx.coroutines.flow.StateFlow
 import kotlinx.coroutines.flow.asStateFlow
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
+import javax.inject.Inject
 
-class CatalogViewModel(): ViewModel() {
+@HiltViewModel
+class CatalogViewModel @Inject constructor(): ViewModel() {
     private val _articles: MutableStateFlow<List<Article>> = MutableStateFlow( listOf() )
     val articles: StateFlow<List<Article>> = _articles.asStateFlow()
 
-    private val _role_name: MutableStateFlow<String> = MutableStateFlow("")
-    val role_name: StateFlow<String> = _role_name.asStateFlow()
+    val showButton = mutableStateOf(false)
 
     fun GetArticles() {
         viewModelScope.launch {
@@ -44,34 +46,20 @@ class CatalogViewModel(): ViewModel() {
 
     fun GetUserRole() {
         viewModelScope.launch {
-            try {
-                val userDate = Constants.supabase.from("users").select {
+            val userDate = Constants.supabase.from("users").select {
+                filter {
+                    eq("id", Constants.supabase.auth.currentUserOrNull()!!.id)
+                }
+            }.decodeSingleOrNull<User>()
+            val roleDate = Constants.supabase.from("roles")
+                .select {
                     filter {
-                        eq("id", Constants.supabase.auth.currentUserOrNull()!!.id)
+                        eq("id", userDate!!.id_role)
                     }
-                }.decodeSingleOrNull<User>()
-                println(userDate.toString())
-                println(userDate!!.id_role.toString())
-                val userRoleId = userDate.id_role
-                println(userRoleId.toString())
-                val roleDate = Constants.supabase.from("roles")
-                    .select {
-                        filter {
-                            eq("id", userRoleId)
-                        }
-                    }.decodeSingleOrNull<Role>()
-                if (roleDate == null) {
-                    Log.d("DSDSDSDSD","BAS")
-                }
-                else {
-                    Log.d("DSDSDSDSD","TES")
-                    _role_name.value = roleDate.title
-                }
-            } catch (e: Exception){
-                Log.d("DSDSDSDSD",e.message.toString())
+                }.decodeSingleOrNull<Role>()
+            if (roleDate!!.title == "Писатель") {
+                showButton.value = true
             }
-
-
         }
     }
 }

+ 4 - 0
app/src/main/java/com/example/collectionofshortstories/view/start/LoadProgramViewModel.kt

@@ -1,11 +1,14 @@
 package com.example.collectionofshortstories.view.start
 
 import android.content.res.Configuration
+import android.util.Log
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.ui.platform.LocalConfiguration
 import androidx.navigation.NavHostController
+import com.example.collectionofshortstories.domain.untils.Constants
 import com.example.collectionofshortstories.navigation.Routes
+import io.github.jan.supabase.gotrue.auth
 import kotlinx.coroutines.delay
 
 @Composable
@@ -13,6 +16,7 @@ fun SwitchingTheFirstPage(navController: NavHostController) {
     val configuration = LocalConfiguration.current
     if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
         LaunchedEffect(key1 = true) {
+            Constants.supabase.auth.signOut()
             delay(1500L)
             navController.navigate(Routes.AUTH) {
                 popUpTo(Routes.START)

+ 1 - 0
build.gradle.kts

@@ -2,4 +2,5 @@
 plugins {
     alias(libs.plugins.android.application) apply false
     alias(libs.plugins.kotlin.android) apply false
+    id("com.google.dagger.hilt.android") version "2.52" apply false
 }