|
|
@ -16,12 +16,9 @@ import awais.instagrabber.repositories.responses.User |
|
|
|
import awais.instagrabber.repositories.responses.UserProfileContextLink |
|
|
|
import awais.instagrabber.repositories.responses.directmessages.RankedRecipient |
|
|
|
import awais.instagrabber.repositories.responses.stories.Story |
|
|
|
import awais.instagrabber.utils.Constants |
|
|
|
import awais.instagrabber.utils.ControlledRunner |
|
|
|
import awais.instagrabber.utils.Event |
|
|
|
import awais.instagrabber.utils.getCsrfTokenFromCookie |
|
|
|
import awais.instagrabber.utils.SingleRunner |
|
|
|
import awais.instagrabber.utils.Utils |
|
|
|
import awais.instagrabber.utils.extensions.TAG |
|
|
|
import awais.instagrabber.utils.extensions.isReallyPrivate |
|
|
|
import awais.instagrabber.viewmodels.ProfileFragmentViewModel.ProfileAction.* |
|
|
@ -34,20 +31,18 @@ import java.time.LocalDateTime |
|
|
|
|
|
|
|
class ProfileFragmentViewModel( |
|
|
|
private val state: SavedStateHandle, |
|
|
|
private val favoriteRepository: FavoriteRepository?, |
|
|
|
private val csrfToken: String?, |
|
|
|
private val deviceUuid: String?, |
|
|
|
private val userRepository: UserRepository, |
|
|
|
private val friendshipRepository: FriendshipRepository, |
|
|
|
private val storiesRepository: StoriesRepository, |
|
|
|
private val mediaRepository: MediaRepository, |
|
|
|
private val graphQLRepository: GraphQLRepository, |
|
|
|
private val favoriteRepository: FavoriteRepository, |
|
|
|
private val directMessagesRepository: DirectMessagesRepository, |
|
|
|
private val messageManager: DirectMessagesManager?, |
|
|
|
ioDispatcher: CoroutineDispatcher, |
|
|
|
) : ViewModel() { |
|
|
|
private val cookie: String = Utils.settingsHelper.getString(Constants.COOKIE) |
|
|
|
private val csrfToken: String? = getCsrfTokenFromCookie(cookie) |
|
|
|
private val deviceUuid: String = Utils.settingsHelper.getString(Constants.DEVICE_UUID) |
|
|
|
private val userRepository: UserRepository by lazy { UserRepository.getInstance() } |
|
|
|
private val friendshipRepository: FriendshipRepository by lazy { FriendshipRepository.getInstance() } |
|
|
|
private val storiesRepository: StoriesRepository by lazy { StoriesRepository.getInstance() } |
|
|
|
private val mediaRepository: MediaRepository by lazy { MediaRepository.getInstance() } |
|
|
|
private val graphQLRepository: GraphQLRepository by lazy { GraphQLRepository.getInstance() } |
|
|
|
private val directMessagesRepository: DirectMessagesRepository by lazy { DirectMessagesRepository.getInstance() } |
|
|
|
|
|
|
|
private val _currentUser = MutableLiveData<Resource<User?>>(Resource.loading(null)) |
|
|
|
private val _isFavorite = MutableLiveData(false) |
|
|
|
private val profileAction = MutableLiveData(INIT) |
|
|
@ -247,7 +242,7 @@ class ProfileFragmentViewModel( |
|
|
|
|
|
|
|
private suspend fun checkAndUpdateFavorite(fetchedUser: User) { |
|
|
|
try { |
|
|
|
val favorite = favoriteRepository!!.getFavorite(fetchedUser.username, FavoriteType.USER) |
|
|
|
val favorite = favoriteRepository.getFavorite(fetchedUser.username, FavoriteType.USER) |
|
|
|
if (favorite == null) { |
|
|
|
_isFavorite.postValue(false) |
|
|
|
return |
|
|
@ -295,7 +290,7 @@ class ProfileFragmentViewModel( |
|
|
|
viewModelScope.launch(Dispatchers.IO) { |
|
|
|
toggleFavoriteControlledRunner.afterPrevious { |
|
|
|
try { |
|
|
|
val favorite = favoriteRepository!!.getFavorite(username, FavoriteType.USER) |
|
|
|
val favorite = favoriteRepository.getFavorite(username, FavoriteType.USER) |
|
|
|
if (favorite == null) { |
|
|
|
// insert |
|
|
|
favoriteRepository.insertOrUpdateFavorite( |
|
|
@ -330,7 +325,7 @@ class ProfileFragmentViewModel( |
|
|
|
val currentUserId = currentUser.value?.data?.pk ?: return@afterPrevious |
|
|
|
val targetUserId = profile.value?.data?.pk ?: return@afterPrevious |
|
|
|
val csrfToken = csrfToken ?: return@afterPrevious |
|
|
|
val deviceUuid = deviceUuid |
|
|
|
val deviceUuid = deviceUuid ?: return@afterPrevious |
|
|
|
if (following) { |
|
|
|
if (!confirmed) { |
|
|
|
_eventLiveData.postValue(Event(ShowConfirmUnfollowDialog)) |
|
|
@ -369,7 +364,7 @@ class ProfileFragmentViewModel( |
|
|
|
val currentUserId = currentUser.value?.data?.pk ?: return@afterPrevious |
|
|
|
val targetUserId = profile.value?.data?.pk ?: return@afterPrevious |
|
|
|
val csrfToken = csrfToken ?: return@afterPrevious |
|
|
|
val deviceUuid = deviceUuid |
|
|
|
val deviceUuid = deviceUuid ?: return@afterPrevious |
|
|
|
val username = profile.value?.data?.username ?: return@afterPrevious |
|
|
|
val thread = directMessagesRepository.createThread( |
|
|
|
csrfToken, |
|
|
@ -403,7 +398,7 @@ class ProfileFragmentViewModel( |
|
|
|
val profile = profile.value?.data ?: return@afterPrevious |
|
|
|
friendshipRepository.toggleRestrict( |
|
|
|
csrfToken ?: return@afterPrevious, |
|
|
|
deviceUuid, |
|
|
|
deviceUuid ?: return@afterPrevious, |
|
|
|
profile.pk, |
|
|
|
!(profile.friendshipStatus?.isRestricted ?: false), |
|
|
|
) |
|
|
@ -425,7 +420,7 @@ class ProfileFragmentViewModel( |
|
|
|
friendshipRepository.changeBlock( |
|
|
|
csrfToken ?: return@afterPrevious, |
|
|
|
currentUser.value?.data?.pk ?: return@afterPrevious, |
|
|
|
deviceUuid, |
|
|
|
deviceUuid ?: return@afterPrevious, |
|
|
|
profile.friendshipStatus?.blocking ?: return@afterPrevious, |
|
|
|
profile.pk |
|
|
|
) |
|
|
@ -447,7 +442,7 @@ class ProfileFragmentViewModel( |
|
|
|
friendshipRepository.changeMute( |
|
|
|
csrfToken ?: return@afterPrevious, |
|
|
|
currentUser.value?.data?.pk ?: return@afterPrevious, |
|
|
|
deviceUuid, |
|
|
|
deviceUuid ?: return@afterPrevious, |
|
|
|
profile.friendshipStatus?.isMutingReel ?: return@afterPrevious, |
|
|
|
profile.pk, |
|
|
|
true |
|
|
@ -470,7 +465,7 @@ class ProfileFragmentViewModel( |
|
|
|
friendshipRepository.changeMute( |
|
|
|
csrfToken ?: return@afterPrevious, |
|
|
|
currentUser.value?.data?.pk ?: return@afterPrevious, |
|
|
|
deviceUuid, |
|
|
|
deviceUuid ?: return@afterPrevious, |
|
|
|
profile.friendshipStatus?.muting ?: return@afterPrevious, |
|
|
|
profile.pk, |
|
|
|
false |
|
|
@ -492,7 +487,7 @@ class ProfileFragmentViewModel( |
|
|
|
friendshipRepository.removeFollower( |
|
|
|
csrfToken ?: return@afterPrevious, |
|
|
|
currentUser.value?.data?.pk ?: return@afterPrevious, |
|
|
|
deviceUuid, |
|
|
|
deviceUuid ?: return@afterPrevious, |
|
|
|
profile.value?.data?.pk ?: return@afterPrevious |
|
|
|
) |
|
|
|
profileAction.postValue(REFRESH_FRIENDSHIP) |
|
|
@ -601,7 +596,15 @@ class ProfileFragmentViewModel( |
|
|
|
|
|
|
|
@Suppress("UNCHECKED_CAST") |
|
|
|
class ProfileFragmentViewModelFactory( |
|
|
|
private val favoriteRepository: FavoriteRepository?, |
|
|
|
private val csrfToken: String?, |
|
|
|
private val deviceUuid: String?, |
|
|
|
private val userRepository: UserRepository, |
|
|
|
private val friendshipRepository: FriendshipRepository, |
|
|
|
private val storiesRepository: StoriesRepository, |
|
|
|
private val mediaRepository: MediaRepository, |
|
|
|
private val graphQLRepository: GraphQLRepository, |
|
|
|
private val favoriteRepository: FavoriteRepository, |
|
|
|
private val directMessagesRepository: DirectMessagesRepository, |
|
|
|
private val messageManager: DirectMessagesManager?, |
|
|
|
owner: SavedStateRegistryOwner, |
|
|
|
defaultArgs: Bundle? = null, |
|
|
@ -613,7 +616,15 @@ class ProfileFragmentViewModelFactory( |
|
|
|
): T { |
|
|
|
return ProfileFragmentViewModel( |
|
|
|
handle, |
|
|
|
csrfToken, |
|
|
|
deviceUuid, |
|
|
|
userRepository, |
|
|
|
friendshipRepository, |
|
|
|
storiesRepository, |
|
|
|
mediaRepository, |
|
|
|
graphQLRepository, |
|
|
|
favoriteRepository, |
|
|
|
directMessagesRepository, |
|
|
|
messageManager, |
|
|
|
Dispatchers.IO, |
|
|
|
) as T |
|
|
|