diff --git a/Server/data.json b/Server/data.json index 2d55f88..6a64792 100644 --- a/Server/data.json +++ b/Server/data.json @@ -18,7 +18,7 @@ "id": 3, "login": "User2", "password": "123", - "email": "user2@gmail.ru", + "email": "user2@mail.ru", "role": "USER" } ], diff --git a/app/src/main/java/com/example/myapplication/api/ServerService.kt b/app/src/main/java/com/example/myapplication/api/ServerService.kt index 2a6b604..c1c8052 100644 --- a/app/src/main/java/com/example/myapplication/api/ServerService.kt +++ b/app/src/main/java/com/example/myapplication/api/ServerService.kt @@ -117,7 +117,7 @@ interface ServerService{ @Path("id") id: Int, ): UserRemote companion object { - private const val BASE_URL = "http://89.239.174.67:8079/" + private const val BASE_URL = "http://100.74.118.221:8079/" @Volatile private var INSTANCE: ServerService? = null diff --git a/app/src/main/java/com/example/myapplication/composeui/AuthorCell.kt b/app/src/main/java/com/example/myapplication/composeui/AuthorCell.kt index cc31684..f6ed460 100644 --- a/app/src/main/java/com/example/myapplication/composeui/AuthorCell.kt +++ b/app/src/main/java/com/example/myapplication/composeui/AuthorCell.kt @@ -59,8 +59,14 @@ fun AuthorCell(navController: NavController?, author: Author, viewModel: AuthorL modifier = Modifier .padding(all = 5.dp), onClick = { - val route = Screen.AuthorEdit.route.replace("{id}", author.id.toString()) - navController!!.navigate(route) + if(LiveStore.getRole() == "ADMIN") { + val route = Screen.AuthorEdit.route.replace("{id}", author.id.toString()) + navController!!.navigate(route) + } + else{ + val toast = Toast.makeText(MainActivity.appContext, "У вас нет соответствующих прав", Toast.LENGTH_SHORT) + toast.show() + } }, ) { Icon( @@ -73,8 +79,14 @@ fun AuthorCell(navController: NavController?, author: Author, viewModel: AuthorL modifier = Modifier .padding(all = 5.dp), onClick = { - scope.launch { + if(LiveStore.getRole() == "ADMIN") { + scope.launch { viewModel.deleteAuthor(author) + } + } + else{ + val toast = Toast.makeText(MainActivity.appContext, "У вас нет соответствующих прав", Toast.LENGTH_SHORT) + toast.show() } }, colors = ButtonDefaults.buttonColors(containerColor = Color.Red), diff --git a/app/src/main/java/com/example/myapplication/composeui/BookCell.kt b/app/src/main/java/com/example/myapplication/composeui/BookCell.kt index 474fa01..2f8b11e 100644 --- a/app/src/main/java/com/example/myapplication/composeui/BookCell.kt +++ b/app/src/main/java/com/example/myapplication/composeui/BookCell.kt @@ -1,5 +1,6 @@ package com.example.myapplication.composeui +import android.widget.Toast import androidx.compose.foundation.Image import androidx.compose.foundation.border import androidx.compose.foundation.clickable @@ -29,9 +30,11 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavController +import com.example.myapplication.MainActivity import com.example.myapplication.composeui.ViewModel.BookListViewModel import com.example.myapplication.composeui.navigation.Screen import com.example.myapplication.db.model.Book +import com.example.myapplication.store.LiveStore import kotlinx.coroutines.launch @Composable @@ -79,8 +82,14 @@ fun BookCell(navController: NavController?, book: Book, viewModel: BookListViewM modifier = Modifier .padding(all = 5.dp), onClick = { - val route = Screen.BookEdit.route.replace("{id}", book.id.toString()) - navController!!.navigate(route) + if((LiveStore.getUserId() == book.userId) || (LiveStore.getRole() == "ADMIN")) { + val route = Screen.BookEdit.route.replace("{id}", book.id.toString()) + navController!!.navigate(route) + } + else{ + val toast = Toast.makeText(MainActivity.appContext, "У вас нет соответствующих прав", Toast.LENGTH_SHORT) + toast.show() + } }, ) { Icon( @@ -93,8 +102,14 @@ fun BookCell(navController: NavController?, book: Book, viewModel: BookListViewM modifier = Modifier .padding(all = 5.dp), onClick = { - scope.launch { - viewModel.deleteBook(book) + if((LiveStore.getUserId() == book.userId) || (LiveStore.getRole() == "ADMIN")) { + scope.launch { + viewModel.deleteBook(book) + } + } + else{ + val toast = Toast.makeText(MainActivity.appContext, "У вас нет соответствующих прав", Toast.LENGTH_SHORT) + toast.show() } }, colors = ButtonDefaults.buttonColors(containerColor = Color.Red), diff --git a/app/src/main/java/com/example/myapplication/composeui/Profile.kt b/app/src/main/java/com/example/myapplication/composeui/Profile.kt index 8939f3c..ee40329 100644 --- a/app/src/main/java/com/example/myapplication/composeui/Profile.kt +++ b/app/src/main/java/com/example/myapplication/composeui/Profile.kt @@ -8,12 +8,16 @@ import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.material3.Button +import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign @@ -24,10 +28,15 @@ import androidx.navigation.compose.rememberNavController import com.example.myapplication.composeui.navigation.Screen import com.example.myapplication.R import com.example.myapplication.store.LiveStore +import com.example.myapplication.store.PreferencesStore import com.example.myapplication.ui.theme.MyApplicationTheme +import kotlinx.coroutines.launch @Composable fun Profile(navController: NavController) { + val scope = rememberCoroutineScope() + val context = LocalContext.current + val store = PreferencesStore(context) Column(Modifier.padding(all = 40.dp)) { Image( painter = painterResource(id = R.drawable.user), @@ -53,6 +62,21 @@ fun Profile(navController: NavController) { }) { Text(stringResource(id = R.string.my_books)) } + Spacer(Modifier.padding(bottom = 10.dp)) + Button( + onClick = { + scope.launch { + store.setUid("") + } + navController?.navigate(Screen.Main.route) + }, + modifier = Modifier + .fillMaxWidth() + .padding(all = 10.dp), + colors = ButtonDefaults.buttonColors(containerColor = Color.LightGray, contentColor = Color.Black)) + { + Text(stringResource(id = R.string.exit)) + } } } diff --git a/app/src/main/java/com/example/myapplication/composeui/ProfileEdit.kt b/app/src/main/java/com/example/myapplication/composeui/ProfileEdit.kt index 9b20926..8dd7366 100644 --- a/app/src/main/java/com/example/myapplication/composeui/ProfileEdit.kt +++ b/app/src/main/java/com/example/myapplication/composeui/ProfileEdit.kt @@ -12,10 +12,12 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavController +import com.example.myapplication.R import com.example.myapplication.composeui.ViewModel.UserDetails import com.example.myapplication.composeui.ViewModel.UserEditViewModel import com.example.myapplication.composeui.ViewModel.UserUiState @@ -52,7 +54,7 @@ private fun ProfileEdit( value = userUiState.userDetails.email, onValueChange = { onUpdate(userUiState.userDetails.copy(email = it)) }, singleLine = true, - label = { Text("Почта", fontSize = 20.sp) }, + label = { Text(stringResource(id = R.string.email), fontSize = 20.sp) }, modifier = Modifier .fillMaxWidth() .padding(10.dp, 5.dp) @@ -60,7 +62,7 @@ private fun ProfileEdit( OutlinedTextField( value = userUiState.userDetails.login, onValueChange = { onUpdate(userUiState.userDetails.copy(login = it)) }, - label = { Text("Логин", fontSize = 20.sp) }, + label = { Text(stringResource(id = R.string.login), fontSize = 20.sp) }, modifier = Modifier .fillMaxWidth() .padding(10.dp, 5.dp) @@ -69,7 +71,7 @@ private fun ProfileEdit( value = userUiState.userDetails.password, onValueChange = { onUpdate(userUiState.userDetails.copy(password = it)) }, singleLine = true, - label = { Text("Пароль", fontSize = 20.sp) }, + label = { Text(stringResource(id = R.string.password), fontSize = 20.sp) }, modifier = Modifier .fillMaxWidth() .padding(10.dp, 5.dp) @@ -89,9 +91,8 @@ private fun ProfileEdit( .padding(10.dp, 0.dp), colors = ButtonDefaults.buttonColors(containerColor = Color.LightGray, contentColor = Color.Black)) { - Text("Назад") + Text(stringResource(id = R.string.back)) } - } } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 483a438..bb06db2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,6 +16,8 @@ Мои книги Книги автора Войти + Выйти + Удалить аккаунт Создать аккаунт Почта Логин @@ -30,4 +32,6 @@ Загрузить Добавить Читать + Изменить + Назад \ No newline at end of file diff --git a/app/src/main/res/xml/network_security_config.xml b/app/src/main/res/xml/network_security_config.xml index 53d4a5a..f8e2a03 100644 --- a/app/src/main/res/xml/network_security_config.xml +++ b/app/src/main/res/xml/network_security_config.xml @@ -3,6 +3,6 @@ 100.87.48.148 192.168.43.198 - 89.239.174.67 + 100.74.118.221 \ No newline at end of file