Skip to content

Commit

Permalink
fix: 修复自定义课程、事项更新失效
Browse files Browse the repository at this point in the history
  • Loading branch information
Mystery00 committed Dec 9, 2023
1 parent 81e752f commit 5bd701f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ object EventBus {
flow.emit(eventType)
}

fun tryPost(eventType: EventType) {
flow.tryEmit(eventType)
}

suspend fun subscribe(lifecycle: Lifecycle, block: (EventType) -> Unit) {
lifecycle.repeatOnLifecycle(Lifecycle.State.CREATED) {
flow.collect(block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class CustomCourseActivity : BaseSelectComposeActivity() {
openBottomSheet.value = false
return
}
viewModel.updateChange()
finish()
}

Expand Down Expand Up @@ -175,6 +174,9 @@ class CustomCourseActivity : BaseSelectComposeActivity() {
)
}
}
stickyHeader {
Divider()
}
itemsIndexed(
pager,
key = { index -> pager[index]?.courseId ?: index }) { item ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
Expand All @@ -48,9 +49,11 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
Expand All @@ -64,6 +67,7 @@ import com.maxkeppeler.sheets.calendar.models.CalendarStyle
import com.maxkeppeler.sheets.color.ColorDialog
import com.maxkeppeler.sheets.color.models.ColorConfig
import com.maxkeppeler.sheets.color.models.ColorSelection
import com.maxkeppeler.sheets.color.models.SingleColor
import com.maxkeppeler.sheets.date_time.DateTimeDialog
import com.maxkeppeler.sheets.date_time.models.DateTimeSelection
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -116,7 +120,6 @@ class CustomThingActivity : BaseSelectComposeActivity() {
openBottomSheet.value = false
return
}
viewModel.updateChange()
finish()
}

Expand Down Expand Up @@ -166,6 +169,9 @@ class CustomThingActivity : BaseSelectComposeActivity() {
}
}
}
stickyHeader {
Divider()
}
itemsIndexed(
pager,
key = { index -> pager[index]?.thingId ?: index }) { item ->
Expand Down Expand Up @@ -209,10 +215,11 @@ class CustomThingActivity : BaseSelectComposeActivity() {
@Composable
private fun BuildDateSelector(
dialogState: XhuDialogState,
data: MutableState<LocalDateTime>,
initData: LocalDateTime,
onDateChange: (LocalDateTime) -> Unit,
) {
val date = data.value.toLocalDate()
val time = data.value.toLocalTime()
val date = initData.toLocalDate()
val time = initData.toLocalTime()

if (dialogState.showing) {
CalendarDialog(
Expand All @@ -227,7 +234,7 @@ class CustomThingActivity : BaseSelectComposeActivity() {
selection = CalendarSelection.Date(
selectedDate = date,
) {
data.value = LocalDateTime.of(it, time)
onDateChange(LocalDateTime.of(it, time))
},
config = CalendarConfig(
yearSelection = true,
Expand All @@ -241,10 +248,11 @@ class CustomThingActivity : BaseSelectComposeActivity() {
@Composable
private fun BuildTimeSelector(
dialogState: XhuDialogState,
data: MutableState<LocalDateTime>,
initData: LocalDateTime,
onDateChange: (LocalDateTime) -> Unit,
) {
val date = data.value.toLocalDate()
val time = data.value.toLocalTime()
val date = initData.toLocalDate()
val time = initData.toLocalTime()

if (dialogState.showing) {
DateTimeDialog(
Expand All @@ -259,7 +267,7 @@ class CustomThingActivity : BaseSelectComposeActivity() {
selection = DateTimeSelection.Time(
selectedTime = time,
) { newTime ->
data.value = LocalDateTime.of(date, newTime)
onDateChange(LocalDateTime.of(date, newTime))
},
)
}
Expand All @@ -269,7 +277,8 @@ class CustomThingActivity : BaseSelectComposeActivity() {
@Composable
private fun BuildColorSelector(
dialogState: XhuDialogState,
currentColor: MutableState<Color>,
initData: Color,
onDateChange: (Color) -> Unit,
) {
if (!dialogState.showing) {
return
Expand All @@ -284,8 +293,9 @@ class CustomThingActivity : BaseSelectComposeActivity() {
dialogState.hide()
}),
selection = ColorSelection(
selectedColor = SingleColor(initData.toArgb()),
onSelectColor = {
currentColor.value = Color(it)
onDateChange(Color(it))
}
),
config = ColorConfig(
Expand All @@ -311,22 +321,44 @@ class CustomThingActivity : BaseSelectComposeActivity() {
val saveLoadingState by viewModel.saveLoadingState.collectAsState()

val customThing = customThingState.value
var thingTitle = customThing.title
var location = customThing.location
var allDay = customThing.allDay
var saveAsCountdown = customThing.saveAsCountDown
val startTime = remember { mutableStateOf(customThing.startTime.asLocalDateTime()) }
val endTime = remember { mutableStateOf(customThing.endTime.asLocalDateTime()) }
var remark = customThing.remark
val color = remember { mutableStateOf(customThing.color.parseColorHexString()) }

var thingTitle by remember { mutableStateOf(customThing.title) }
var location by remember { mutableStateOf(customThing.location) }
var allDay by remember { mutableStateOf(customThing.allDay) }
var saveAsCountdown by remember { mutableStateOf(customThing.saveAsCountDown) }
var startTime by remember { mutableStateOf(customThing.startTime.asLocalDateTime()) }
var endTime by remember { mutableStateOf(customThing.endTime.asLocalDateTime()) }
var remark by remember { mutableStateOf(customThing.remark) }
var color by remember { mutableStateOf(customThing.color.parseColorHexString()) }

LaunchedEffect(customThing) {
thingTitle = customThing.title
location = customThing.location
allDay = customThing.allDay
saveAsCountdown = customThing.saveAsCountDown
startTime = customThing.startTime.asLocalDateTime()
endTime = customThing.endTime.asLocalDateTime()
remark = customThing.remark
color = customThing.color.parseColorHexString()
}

val focusManager = LocalFocusManager.current

BuildDateSelector(startDateDialog, startTime)
BuildTimeSelector(startTimeDialog, startTime)
BuildDateSelector(endDateDialog, endTime)
BuildTimeSelector(endTimeDialog, endTime)
BuildColorSelector(showColorDialog, color)
BuildDateSelector(startDateDialog, startTime) {
startTime = it
}
BuildTimeSelector(startTimeDialog, startTime) {
startTime = it
}
BuildDateSelector(endDateDialog, endTime) {
endTime = it
}
BuildTimeSelector(endTimeDialog, endTime) {
endTime = it
}
BuildColorSelector(showColorDialog, color) {
color = it
}

fun dismissSheet() {
focusManager.clearFocus()
Expand Down Expand Up @@ -388,10 +420,10 @@ class CustomThingActivity : BaseSelectComposeActivity() {
thingTitle,
location,
allDay,
startTime.value,
endTime.value,
startTime,
endTime,
remark,
color.value,
color,
mapOf(
CustomThing.Key.SAVE_AS_COUNT_DOWN to saveAsCountdown.toString()
)
Expand Down Expand Up @@ -503,7 +535,7 @@ class CustomThingActivity : BaseSelectComposeActivity() {
indication = null,
interactionSource = MutableInteractionSource(),
),
text = startTime.value.format(dateFormatter),
text = startTime.format(dateFormatter),
)
if (!allDay) {
Text(
Expand All @@ -515,7 +547,7 @@ class CustomThingActivity : BaseSelectComposeActivity() {
indication = null,
interactionSource = MutableInteractionSource(),
),
text = startTime.value.format(Formatter.TIME_NO_SECONDS),
text = startTime.format(Formatter.TIME_NO_SECONDS),
)
}
}
Expand All @@ -536,7 +568,7 @@ class CustomThingActivity : BaseSelectComposeActivity() {
indication = null,
interactionSource = MutableInteractionSource(),
),
text = endTime.value.format(dateFormatter),
text = endTime.format(dateFormatter),
)
if (!allDay) {
Text(
Expand All @@ -548,7 +580,7 @@ class CustomThingActivity : BaseSelectComposeActivity() {
indication = null,
interactionSource = MutableInteractionSource(),
),
text = endTime.value.format(Formatter.TIME_NO_SECONDS),
text = endTime.format(Formatter.TIME_NO_SECONDS),
)
}
}
Expand Down Expand Up @@ -597,7 +629,7 @@ class CustomThingActivity : BaseSelectComposeActivity() {
modifier = Modifier
.padding(horizontal = 12.dp)
.size(24.dp),
color = color.value
color = color
) {}
Text(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class CustomCourseViewModel : PagingComposeViewModel<PageRequest, CustomCourseRe
private val _termSelect = MutableStateFlow<List<TermSelect>>(emptyList())
val termSelect: StateFlow<List<TermSelect>> = _termSelect

private var changed = false

//蹭课列表分页数据
private val allCoursePageRequestFlow = MutableStateFlow<AllCoursePageRequest?>(null)
private val _allCoursePageState = allCoursePageRequestFlow
Expand Down Expand Up @@ -161,8 +159,7 @@ class CustomCourseViewModel : PagingComposeViewModel<PageRequest, CustomCourseRe
}
_saveLoadingState.value = LoadingState(actionSuccess = true)
toastMessage("${request.courseName}》保存成功")
changed = true
loadCustomCourseList()
updateChange()
}
}

Expand All @@ -187,8 +184,7 @@ class CustomCourseViewModel : PagingComposeViewModel<PageRequest, CustomCourseRe
CustomCourseRepo.deleteCustomCourse(selectedUser, courseId)
_saveLoadingState.value = LoadingState(actionSuccess = true)
toastMessage("删除成功")
changed = true
loadCustomCourseList()
updateChange()
}
}

Expand All @@ -210,10 +206,11 @@ class CustomCourseViewModel : PagingComposeViewModel<PageRequest, CustomCourseRe
}
}

fun updateChange() {
if (changed) {
EventBus.tryPost(EventType.CHANGE_SHOW_CUSTOM_COURSE)
private fun updateChange() {
viewModelScope.launch {
EventBus.post(EventType.CHANGE_SHOW_CUSTOM_COURSE)
}
loadCustomCourseList()
}

internal data class AllCoursePageRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import vip.mystery0.xhu.timetable.base.PageRequest
import vip.mystery0.xhu.timetable.base.PagingComposeViewModel
import vip.mystery0.xhu.timetable.base.UserSelect
import vip.mystery0.xhu.timetable.config.networkErrorHandler
import vip.mystery0.xhu.timetable.config.store.EventBus
import vip.mystery0.xhu.timetable.config.store.User
import vip.mystery0.xhu.timetable.model.event.EventType
import vip.mystery0.xhu.timetable.model.request.CustomThingRequest
import vip.mystery0.xhu.timetable.model.response.CustomThingResponse
import vip.mystery0.xhu.timetable.repository.CustomThingRepo
import java.time.Instant
import java.time.temporal.ChronoUnit

class CustomThingViewModel : PagingComposeViewModel<User, CustomThingResponse>(
class CustomThingViewModel : PagingComposeViewModel<PageRequest, CustomThingResponse>(
{
CustomThingRepo.getCustomThingListStream(it)
CustomThingRepo.getCustomThingListStream(it.user)
}
) {
companion object {
Expand All @@ -32,8 +32,6 @@ class CustomThingViewModel : PagingComposeViewModel<User, CustomThingResponse>(
private val _saveLoadingState = MutableStateFlow(LoadingState(init = true))
val saveLoadingState: StateFlow<LoadingState> = _saveLoadingState

private var changed = false

init {
viewModelScope.launch {
_userSelect.value = initUserSelect()
Expand All @@ -56,7 +54,7 @@ class CustomThingViewModel : PagingComposeViewModel<User, CustomThingResponse>(
failed("选择用户为空,请重新选择")
return@launch
}
loadData(selectedUser)
loadData(PageRequest(selectedUser, 1, 1))
}
}

Expand Down Expand Up @@ -97,8 +95,7 @@ class CustomThingViewModel : PagingComposeViewModel<User, CustomThingResponse>(
}
_saveLoadingState.value = LoadingState()
toastMessage("${request.title}》保存成功")
changed = true
loadCustomThingList()
updateChange()
}
}

Expand All @@ -123,8 +120,7 @@ class CustomThingViewModel : PagingComposeViewModel<User, CustomThingResponse>(
CustomThingRepo.deleteCustomThing(selectedUser, thingId)
_saveLoadingState.value = LoadingState()
toastMessage("删除成功")
changed = true
loadCustomThingList()
updateChange()
}
}

Expand All @@ -134,10 +130,11 @@ class CustomThingViewModel : PagingComposeViewModel<User, CustomThingResponse>(
}
}

fun updateChange() {
if (changed) {
EventBus.tryPost(EventType.CHANGE_SHOW_CUSTOM_THING)
private fun updateChange() {
viewModelScope.launch {
EventBus.post(EventType.CHANGE_SHOW_CUSTOM_THING)
}
loadCustomThingList()
}

data class LoadingState(
Expand Down

0 comments on commit 5bd701f

Please sign in to comment.