|
@@ -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
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
}
|