2023-07-27 22:49:55 -06:00
|
|
|
package com.isolaatti.home.presentation
|
|
|
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
|
|
import com.isolaatti.auth.domain.AuthRepository
|
2023-08-06 22:11:28 -06:00
|
|
|
import com.isolaatti.posting.posts.domain.PostsRepository
|
|
|
|
|
import com.isolaatti.posting.posts.presentation.PostListingViewModelBase
|
|
|
|
|
import com.isolaatti.posting.posts.presentation.UpdateEvent
|
2023-07-27 22:49:55 -06:00
|
|
|
import com.isolaatti.profile.data.remote.UserProfileDto
|
|
|
|
|
import com.isolaatti.profile.domain.use_case.GetProfile
|
|
|
|
|
import com.isolaatti.utils.Resource
|
|
|
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.flow.flowOn
|
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
@HiltViewModel
|
2023-08-06 22:11:28 -06:00
|
|
|
class FeedViewModel @Inject constructor(
|
|
|
|
|
private val getProfileUseCase: GetProfile,
|
|
|
|
|
private val authRepository: AuthRepository,
|
|
|
|
|
private val postsRepository: PostsRepository
|
|
|
|
|
) : PostListingViewModelBase() {
|
|
|
|
|
|
|
|
|
|
override fun getFeed(refresh: Boolean) {
|
|
|
|
|
viewModelScope.launch {
|
|
|
|
|
if (refresh) {
|
|
|
|
|
posts.value = null
|
|
|
|
|
}
|
|
|
|
|
postsRepository.getFeed(getLastId()).onEach { feedDtoResource ->
|
|
|
|
|
when (feedDtoResource) {
|
|
|
|
|
is Resource.Success -> {
|
|
|
|
|
loadingPosts.postValue(false)
|
|
|
|
|
posts.postValue(Pair(posts.value?.first?.concatFeed(feedDtoResource.data) ?: feedDtoResource.data, UpdateEvent(UpdateEvent.UpdateType.PAGE_ADDED, null)))
|
|
|
|
|
noMoreContent.postValue(feedDtoResource.data?.moreContent == false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is Resource.Loading -> {
|
2023-08-15 00:02:29 -06:00
|
|
|
if(!refresh)
|
|
|
|
|
loadingPosts.postValue(true)
|
2023-08-06 22:11:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is Resource.Error -> {
|
|
|
|
|
errorLoading.postValue(feedDtoResource.errorType)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-15 00:02:29 -06:00
|
|
|
isLoadingFromScrolling = false
|
2023-08-06 22:11:28 -06:00
|
|
|
}.flowOn(Dispatchers.IO).launchIn(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-27 22:49:55 -06:00
|
|
|
|
|
|
|
|
// User profile
|
|
|
|
|
private val _userProfile: MutableLiveData<UserProfileDto> = MutableLiveData()
|
|
|
|
|
val userProfile: LiveData<UserProfileDto> get() = _userProfile
|
|
|
|
|
|
|
|
|
|
fun getProfile() {
|
|
|
|
|
viewModelScope.launch {
|
2023-08-06 22:11:28 -06:00
|
|
|
authRepository.getUserId().onEach { userId ->
|
2023-07-27 22:49:55 -06:00
|
|
|
userId?.let {
|
2023-08-06 22:11:28 -06:00
|
|
|
getProfileUseCase(userId).onEach { profile ->
|
|
|
|
|
if (profile is Resource.Success) {
|
2023-07-27 22:49:55 -06:00
|
|
|
profile.data?.let { _userProfile.postValue(it) }
|
|
|
|
|
}
|
|
|
|
|
}.flowOn(Dispatchers.IO).launchIn(this)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}.flowOn(Dispatchers.IO).launchIn(this)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|