新增动态为空时显示热门动态功能

This commit is contained in:
2024-09-06 15:48:15 +08:00
parent 025f66ca83
commit fc324240b6

View File

@@ -28,8 +28,10 @@ object MomentViewModel : ViewModel() {
private val _momentsFlow = MutableStateFlow<PagingData<MomentEntity>>(PagingData.empty())
val momentsFlow = _momentsFlow.asStateFlow()
val accountService: AccountService = AccountServiceImpl()
var existsException = mutableStateOf(false)
init {
viewModelScope.launch {
// 获取当前用户信息
val profile = accountService.getMyAccountProfile()
Pager(
config = PagingConfig(pageSize = 5, enablePlaceholders = false),
@@ -47,12 +49,19 @@ object MomentViewModel : ViewModel() {
fun refreshPager() {
viewModelScope.launch {
val profile = accountService.getMyAccountProfile()
// 检查是否有动态
val existMoments = momentService.getMoments(timelineId = profile.id, pageNumber = 1)
if (existMoments.list.isEmpty()) {
existsException.value = true
}
Pager(
config = PagingConfig(pageSize = 5, enablePlaceholders = false),
pagingSourceFactory = {
MomentPagingSource(
MomentRemoteDataSource(momentService),
timelineId = profile.id
// 如果没有动态,则显示热门动态
timelineId = if (existMoments.list.isEmpty()) null else profile.id,
trend = if (existMoments.list.isEmpty()) true else null
)
}
).flow.cachedIn(viewModelScope).collectLatest {