diff --git a/app/src/main/java/com/example/myapplication/Profile.kt b/app/src/main/java/com/example/myapplication/Profile.kt index a891946..39640c9 100644 --- a/app/src/main/java/com/example/myapplication/Profile.kt +++ b/app/src/main/java/com/example/myapplication/Profile.kt @@ -44,10 +44,12 @@ import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavController import androidx.paging.compose.collectAsLazyPagingItems import com.example.myapplication.Graph.AuthScreen +import com.example.myapplication.Graph.BottomBarScreen import com.example.myapplication.dataBase.model.RoleEnum import com.example.myapplication.ui.AppViewModelProvider import com.example.myapplication.ui.edit.DayDropDownViewModel import com.example.myapplication.ui.edit.DirectionDropDownViewModel +import com.example.myapplication.ui.edit.LessonEditViewModel import com.example.myapplication.ui.lesson.LessonListViewModel import com.example.myapplication.ui.user.CurrentUserViewModel import kotlinx.coroutines.launch @@ -512,7 +514,7 @@ fun Profile(navController: NavController, modifier: Modifier = Modifier, lessonL ) ) Text( - text = "Отменить запись", + text = "К записи", color = Color.White, textAlign = TextAlign.Center, style = TextStyle( @@ -527,12 +529,14 @@ fun Profile(navController: NavController, modifier: Modifier = Modifier, lessonL .clickable( enabled = getUser?.lessonId != null, onClick = { - getUser?.lessonId = null - coroutineScope.launch { - getUser?.let { - currentUserViewModel.updateUser(it) - } - } +// getUser?.lessonId = null +// coroutineScope.launch { +// getUser?.let { +// currentUserViewModel.updateUser(it) +// } +// } + val route = BottomBarScreen.LessonRecord.route.replace("{id}", lessonId.toString()) + navController.navigate(route) }) ) } @@ -544,3 +548,4 @@ fun Profile(navController: NavController, modifier: Modifier = Modifier, lessonL } + diff --git a/app/src/main/java/com/example/myapplication/ui/edit/LessonEditViewModel.kt b/app/src/main/java/com/example/myapplication/ui/edit/LessonEditViewModel.kt index 2a9fac7..84a116a 100644 --- a/app/src/main/java/com/example/myapplication/ui/edit/LessonEditViewModel.kt +++ b/app/src/main/java/com/example/myapplication/ui/edit/LessonEditViewModel.kt @@ -51,6 +51,33 @@ class LessonEditViewModel( } } + suspend fun updateRecords() { + if (validateInput()) { + if (lessonUid > 0) { + val updatedCountRecord = lessonUiState.lessonDetails.countRecord - 1 + lessonUiState = lessonUiState.copy( + lessonDetails = lessonUiState.lessonDetails.copy(countRecord = updatedCountRecord) + ) + lessonRepository.updateLesson(lessonUiState.lessonDetails.toLesson(lessonUid)) + } else { + lessonRepository.insertLesson(lessonUiState.lessonDetails.toLesson()) + } + + } + } + + suspend fun updateRecordsForCancel() { + if (validateInput()) { + if (lessonUid > 0) { + val updatedCountRecord = lessonUiState.lessonDetails.countRecord + 1 + lessonUiState = lessonUiState.copy( + lessonDetails = lessonUiState.lessonDetails.copy(countRecord = updatedCountRecord) + ) + lessonRepository.updateLesson(lessonUiState.lessonDetails.toLesson(lessonUid)) + } + } + } + private fun validateInput(uiState: LessonDetails = lessonUiState.lessonDetails): Boolean { return with(uiState) { teacher.isNotBlank() diff --git a/app/src/main/java/com/example/myapplication/ui/edit/LessonRecord.kt b/app/src/main/java/com/example/myapplication/ui/edit/LessonRecord.kt index e5b3d4e..ffcbf07 100644 --- a/app/src/main/java/com/example/myapplication/ui/edit/LessonRecord.kt +++ b/app/src/main/java/com/example/myapplication/ui/edit/LessonRecord.kt @@ -36,25 +36,28 @@ fun LessonRecord( directionViewModel.setCurrentDirection(viewModel.lessonUiState.lessonDetails.directionId) val getUser by remember { mutableStateOf(currentUserViewModel.user) } Log.d("getUserLesson", "gU: $getUser") - + val lessonCurId = viewModel.getUidLesson() LessonRecord( lessonUiState = viewModel.lessonUiState, directionUiState = directionViewModel.directionUiState, + enabled = getUser?.lessonId != lessonCurId, onClick = { coroutineScope.launch { - val lessonCurId = viewModel.getUidLesson() Log.d("LessonBefore", "LB: ${getUser?.lessonId}") - - getUser?.let { - it.lessonId = lessonCurId - Log.d("LessonAfter", "LA: ${getUser?.lessonId}") - currentUserViewModel.updateUser(it) - Log.d("UserAfter", "UA: $getUser") + if (lessonCurId != getUser?.lessonId) { + getUser?.let { + it.lessonId = lessonCurId + Log.d("LessonAfter", "LA: ${getUser?.lessonId}") + currentUserViewModel.updateUser(it) + Log.d("UserAfter", "UA: $getUser") + } + viewModel.updateRecords() } navController.popBackStack() } - } + }, + ) {} } @@ -63,6 +66,7 @@ fun LessonRecord( private fun LessonRecord( lessonUiState: LessonUiState, directionUiState: DirectionUiState, + enabled: Boolean, onClick: () -> Unit, onUpdate: (LessonDetails) -> Unit ) { @@ -113,6 +117,7 @@ private fun LessonRecord( ) Button( onClick = onClick, + enabled = enabled, shape = MaterialTheme.shapes.small, modifier = Modifier.fillMaxWidth() ) { diff --git a/app/src/main/java/com/example/myapplication/ui/lesson/LessonListViewModel.kt b/app/src/main/java/com/example/myapplication/ui/lesson/LessonListViewModel.kt index ffa3a5f..182fbc3 100644 --- a/app/src/main/java/com/example/myapplication/ui/lesson/LessonListViewModel.kt +++ b/app/src/main/java/com/example/myapplication/ui/lesson/LessonListViewModel.kt @@ -38,35 +38,3 @@ class LessonListViewModel( } } -//class LessonListViewModel(private val lessonRepository: LessonRepository): ViewModel() { -// var teacher = mutableStateOf("") -// val classNumber = mutableStateOf("") -// val time = mutableStateOf("") -// val directionId = mutableStateOf("") -// val dayOfWeekId = mutableStateOf("") -// val LessonList = lessonRepository.call().cachedIn(viewModelScope) -// var lesson: Lesson? = null -// -// fun insert() = viewModelScope.launch { -// val lesson = Lesson( -// teacher = teacher.value, -// classNumber = classNumber.value.toInt(), -// time = time.value, -// dayOfWeekId = dayOfWeekId.value.toInt(), -// directionId = directionId.value.toInt() -// ) -// lessonRepository.insertLesson(lesson) -// } -// -// fun delete(lesson: Lesson) = viewModelScope.launch { -// lessonRepository.deleteLesson(lesson) -// } -// -// fun getLessonByUid(uid: Int) = viewModelScope.launch { -// lessonRepository.getLessonByUid(uid) -// } -// -// fun update(lesson: Lesson) = viewModelScope.launch { -// lessonRepository.updateLesson(lesson) -// } -//} \ No newline at end of file diff --git a/server/data.json b/server/data.json index a5e8d7d..1f5cddc 100644 --- a/server/data.json +++ b/server/data.json @@ -61,18 +61,18 @@ "teacher": "Анна", "time": "9.00-10.00", "classNumber": 2, - "directionId": 1, "dayOfWeekId": 1, - "countRecord": 7 + "directionId": 1, + "countRecord": 6 }, { "id": 3, "teacher": "Мария", "time": "17.00-18.00", "classNumber": 3, - "directionId": 3, "dayOfWeekId": 2, - "countRecord": 3 + "directionId": 3, + "countRecord": 2 }, { "id": 4, @@ -90,7 +90,7 @@ "classNumber": 6, "dayOfWeekId": 5, "directionId": 1, - "countRecord": 7 + "countRecord": 5 } ], "users": [ @@ -132,7 +132,7 @@ "password": "user8", "fio": "user8", "role": "User", - "lessonId": null + "lessonId": 5 } ] } \ No newline at end of file