From 7e60a880693dd8fbe580923be05f2455f0c4bbdd Mon Sep 17 00:00:00 2001 From: Danya_Mochalov Date: Tue, 21 Nov 2023 22:13:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=B2=D0=B1=D0=B0=D1=80=20=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BF=D0=BE=D0=BD=D0=B8=D0=BC?= =?UTF-8?q?=D0=B0=D0=B5=D1=82=20=D0=BA=D0=B0=D0=BA=D0=B0=D1=8F=20=D1=83=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=80=D0=BE=D0=BB=D1=8C=20+=20=D0=BA=D1=8D?= =?UTF-8?q?=D1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 1 + .../data/sharedpref/PreferencesManager.kt | 19 +++++++++++++++++++ .../authorization/AuthorizationScreen.kt | 6 ++++++ .../shawarma/viewmodels/UserViewModel.kt | 17 +++++++++++++---- .../example/shawarma/widgets/BottomNavBar.kt | 9 +++++++-- 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/example/shawarma/data/sharedpref/PreferencesManager.kt diff --git a/app/build.gradle b/app/build.gradle index 097b818..031a540 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/sharedpref/PreferencesManager.kt b/app/src/main/java/com/example/shawarma/data/sharedpref/PreferencesManager.kt new file mode 100644 index 0000000..96732ca --- /dev/null +++ b/app/src/main/java/com/example/shawarma/data/sharedpref/PreferencesManager.kt @@ -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 + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt b/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt index 7fee75b..44e4c06 100644 --- a/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt @@ -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() + 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 diff --git a/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt index aa2c3fb..f681967 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt @@ -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) + } + } } - } } } diff --git a/app/src/main/java/com/example/shawarma/widgets/BottomNavBar.kt b/app/src/main/java/com/example/shawarma/widgets/BottomNavBar.kt index 607aa89..f5132b7 100644 --- a/app/src/main/java/com/example/shawarma/widgets/BottomNavBar.kt +++ b/app/src/main/java/com/example/shawarma/widgets/BottomNavBar.kt @@ -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 } }, - ) } }