|
@@ -4,6 +4,7 @@ import android.util.Log
|
|
|
import androidx.compose.runtime.Composable
|
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
|
import androidx.compose.runtime.MutableState
|
|
|
+import androidx.compose.runtime.State
|
|
|
import androidx.compose.runtime.getValue
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
import androidx.compose.runtime.remember
|
|
@@ -13,6 +14,9 @@ import androidx.lifecycle.ViewModel
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
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 io.github.jan.supabase.gotrue.auth
|
|
|
import io.github.jan.supabase.postgrest.from
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
@@ -25,17 +29,47 @@ class CatalogViewModel(): ViewModel() {
|
|
|
private val _articles: MutableStateFlow<List<Article>> = MutableStateFlow( listOf() )
|
|
|
val articles: StateFlow<List<Article>> = _articles.asStateFlow()
|
|
|
|
|
|
- fun get() {
|
|
|
+ private val _role_name: MutableStateFlow<String> = MutableStateFlow("")
|
|
|
+ val role_name: StateFlow<String> = _role_name.asStateFlow()
|
|
|
+
|
|
|
+ fun GetArticles() {
|
|
|
viewModelScope.launch {
|
|
|
withContext(Dispatchers.IO) {
|
|
|
val updatedCategories = Constants.supabase.from("articles")
|
|
|
.select()
|
|
|
- Log.d("OOOOOOOOOOOOOOOOOO", updatedCategories.data.toString())
|
|
|
_articles.value = updatedCategories.decodeList<Article>()
|
|
|
- articles.value!!.forEach { it ->
|
|
|
- Log.d("Cdsdsdsdssds", it.title)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun GetUserRole() {
|
|
|
+ viewModelScope.launch {
|
|
|
+ try {
|
|
|
+ val userDate = Constants.supabase.from("users").select {
|
|
|
+ filter {
|
|
|
+ eq("id", Constants.supabase.auth.currentUserOrNull()!!.id)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val userRoleId = userDate.decodeSingle<User>().id_role
|
|
|
+ Log.d("DSDSDSDSD", 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","пипец")
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|