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

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