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 } }, - ) } }