надо перенести отмену на страницу с записью

This commit is contained in:
Екатерина Рогашова 2023-12-26 16:34:52 +04:00
parent d3ea61ad03
commit ea3b827b55
5 changed files with 59 additions and 54 deletions

View File

@ -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
}

View File

@ -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()

View File

@ -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()
) {

View File

@ -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)
// }
//}

View File

@ -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
}
]
}