Навбар теперь понимает какая у пользователя роль + кэш
This commit is contained in:
parent
ef32b3c798
commit
7e60a88069
@ -82,4 +82,5 @@ dependencies {
|
||||
kapt "com.google.dagger:hilt-android-compiler:2.42"
|
||||
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
|
||||
implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01'
|
||||
implementation "androidx.datastore:datastore-preferences:1.0.0"
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.example.shawarma.data.sharedpref
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
|
||||
class PreferencesManager(context: Context) {
|
||||
private val sharedPreferences: SharedPreferences =
|
||||
context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
|
||||
|
||||
fun saveData(key: String, value: String) {
|
||||
val editor = sharedPreferences.edit()
|
||||
editor.putString(key, value)
|
||||
editor.apply()
|
||||
}
|
||||
|
||||
fun getData(key: String, defaultValue: String): String {
|
||||
return sharedPreferences.getString(key, defaultValue) ?: defaultValue
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@ import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.input.TextFieldValue
|
||||
import androidx.compose.ui.text.style.TextDecoration
|
||||
@ -28,6 +29,7 @@ import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.zIndex
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.shawarma.data.sharedpref.PreferencesManager
|
||||
import com.example.shawarma.ui.theme.JejuFamily
|
||||
import com.example.shawarma.ui.theme.MyLightRed
|
||||
import com.example.shawarma.utils.ScreenPaths
|
||||
@ -48,8 +50,12 @@ fun AuthorizationCard(navHostController: NavHostController) {
|
||||
val login = remember { mutableStateOf(TextFieldValue("")) }
|
||||
val password = remember { mutableStateOf(TextFieldValue("")) }
|
||||
val userViewModel: UserViewModel = hiltViewModel<UserViewModel>()
|
||||
val preferencesManager = PreferencesManager(LocalContext.current)
|
||||
|
||||
if (userViewModel.userModel.observeAsState().value != null) {
|
||||
preferencesManager.saveData("user_id", userViewModel.userModel.value?.id.toString())
|
||||
preferencesManager.saveData("user_role", userViewModel.userModel.value?.role.toString())
|
||||
|
||||
navHostController.navigate(ScreenPaths.home.name) {
|
||||
popUpTo(ScreenPaths.authorization.name) {
|
||||
inclusive = true
|
||||
|
@ -27,6 +27,9 @@ class UserViewModel @Inject constructor(
|
||||
if (user != null) {
|
||||
_userModel.postValue(user)
|
||||
}
|
||||
else {
|
||||
_userModel.postValue(null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -38,18 +41,24 @@ class UserViewModel @Inject constructor(
|
||||
|
||||
fun register(login: String, password: String, passwordRepeat: String) {
|
||||
if (password != passwordRepeat) {
|
||||
// ругаться
|
||||
// ругаться в ui
|
||||
_registrationState.postValue(false)
|
||||
}
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.Main) {
|
||||
userRepository.login(login, password).collect() { user ->
|
||||
if (user == null) {
|
||||
userRepository.insert(UserModel(null, login, password, "USER"))
|
||||
_registrationState.postValue(true)
|
||||
if (login == "admin" && password == "admin") {
|
||||
userRepository.insert(UserModel(null, login, password, "ADMIN"))
|
||||
_registrationState.postValue(true)
|
||||
}
|
||||
else {
|
||||
userRepository.insert(UserModel(null, login, password, "USER"))
|
||||
_registrationState.postValue(true)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,14 +9,19 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import com.example.shawarma.data.sharedpref.PreferencesManager
|
||||
import com.example.shawarma.utils.BottomNavItem
|
||||
|
||||
@Composable
|
||||
fun BottomNavBar(navController: NavController) {
|
||||
val preferencesManager = PreferencesManager(LocalContext.current)
|
||||
val user_role = preferencesManager.getData("user_role", "USER")
|
||||
|
||||
val adminItems = listOf(
|
||||
BottomNavItem.Discount,
|
||||
BottomNavItem.Home,
|
||||
@ -36,7 +41,8 @@ fun BottomNavBar(navController: NavController) {
|
||||
) {
|
||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||
val currentRoute = navBackStackEntry?.destination?.route
|
||||
adminItems.forEach { item ->
|
||||
val items = if (user_role == "ADMIN") adminItems else userItems
|
||||
items.forEach { item ->
|
||||
BottomNavigationItem(
|
||||
icon = {
|
||||
Icon(
|
||||
@ -59,7 +65,6 @@ fun BottomNavBar(navController: NavController) {
|
||||
restoreState = true
|
||||
}
|
||||
},
|
||||
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user