1
0
Fork 0
mirror of https://github.com/KokaKiwi/BarInsta synced 2026-03-14 08:21:35 +00:00

Update ProfileFragmentViewModel

This commit is contained in:
Ammar Githam 2021-06-23 20:52:45 +09:00
parent b1628492f5
commit 1ebf7a2e4b
5 changed files with 214 additions and 54 deletions

View file

@ -6,11 +6,12 @@ import androidx.savedstate.SavedStateRegistryOwner
import awais.instagrabber.db.repositories.AccountRepository
import awais.instagrabber.db.repositories.FavoriteRepository
import awais.instagrabber.managers.DirectMessagesManager
import awais.instagrabber.models.enums.BroadcastItemType
import awais.instagrabber.models.Resource
import awais.instagrabber.repositories.responses.User
import awais.instagrabber.repositories.responses.directmessages.RankedRecipient
import awais.instagrabber.webservices.*
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
class ProfileFragmentViewModel(
state: SavedStateHandle,
@ -21,12 +22,61 @@ class ProfileFragmentViewModel(
graphQLRepository: GraphQLRepository,
accountRepository: AccountRepository,
favoriteRepository: FavoriteRepository,
ioDispatcher: CoroutineDispatcher,
) : ViewModel() {
private val _profile = MutableLiveData<Resource<User?>>(Resource.loading(null))
private val _isLoggedIn = MutableLiveData(false)
private val _currentUser = MutableLiveData<Resource<User?>>(Resource.loading(null))
private var messageManager: DirectMessagesManager? = null
val profile: LiveData<Resource<User?>> = _profile
val currentUser: LiveData<Resource<User?>> = _currentUser
val isLoggedIn: LiveData<Boolean> = currentUser.map { it.data != null }
private val currentUserAndStateUsernameLiveData: LiveData<Pair<Resource<User?>, Resource<String?>>> =
object : MediatorLiveData<Pair<Resource<User?>, Resource<String?>>>() {
var user: Resource<User?> = Resource.loading(null)
var stateUsername: Resource<String?> = Resource.loading(null)
init {
addSource(currentUser) { currentUser ->
this.user = currentUser
value = currentUser to stateUsername
}
addSource(state.getLiveData<String?>("username")) { username ->
this.stateUsername = Resource.success(username)
value = user to this.stateUsername
}
// trigger currentUserAndStateUsernameLiveData switch map with a state username success resource
if (!state.contains("username")) {
this.stateUsername = Resource.success(null)
value = user to this.stateUsername
}
}
}
val profile: LiveData<Resource<User?>> = currentUserAndStateUsernameLiveData.switchMap {
val (userResource, stateUsernameResource) = it
liveData<Resource<User?>>(context = viewModelScope.coroutineContext + ioDispatcher) {
if (userResource.status == Resource.Status.LOADING || stateUsernameResource.status == Resource.Status.LOADING) {
emit(Resource.loading(null))
return@liveData
}
val user = userResource.data
val stateUsername = stateUsernameResource.data
if (stateUsername.isNullOrBlank()) {
emit(Resource.success(user))
return@liveData
}
try {
val fetchedUser = if (user != null) {
userRepository.getUsernameInfo(stateUsername) // logged in
} else {
graphQLRepository.fetchUser(stateUsername) // anonymous
}
emit(Resource.success(fetchedUser))
} catch (e: Exception) {
emit(Resource.error(e.message, null))
}
}
}
/**
* Username of profile without '`@`'
@ -37,30 +87,12 @@ class ProfileFragmentViewModel(
Resource.Status.SUCCESS -> it.data?.username ?: ""
}
}
val isLoggedIn: LiveData<Boolean> = _isLoggedIn
var currentUser: Resource<User?>? = null
set(value) {
_isLoggedIn.postValue(value?.data != null)
// if no profile, and value is valid, set it as profile
val profileValue = profile.value
if (
profileValue?.status != Resource.Status.LOADING
&& profileValue?.data == null
&& value?.status == Resource.Status.SUCCESS
&& value.data != null
) {
_profile.postValue(Resource.success(value.data))
}
field = value
}
init {
// Log.d(TAG, "${state.keys()} $userRepository $friendshipRepository $storiesRepository $mediaRepository")
val usernameFromState = state.get<String?>("username")
if (usernameFromState.isNullOrBlank()) {
_profile.postValue(Resource.success(null))
}
}
fun setCurrentUser(currentUser: Resource<User?>) {
_currentUser.postValue(currentUser)
}
fun shareDm(result: RankedRecipient) {
@ -104,6 +136,7 @@ class ProfileFragmentViewModelFactory(
graphQLRepository,
accountRepository,
favoriteRepository,
Dispatchers.IO,
) as T
}
}

View file

@ -12,7 +12,7 @@ import org.json.JSONObject
import java.util.*
class GraphQLRepository(private val service: GraphQLService) {
open class GraphQLRepository(private val service: GraphQLService) {
// TODO convert string response to a response class
private suspend fun fetch(
@ -176,7 +176,7 @@ class GraphQLRepository(private val service: GraphQLService) {
}
// TODO convert string response to a response class
suspend fun fetchUser(
open suspend fun fetchUser(
username: String,
): User {
val response = service.getUser(username)