1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 06:37:30 +00:00

Re-add ProfileFragmentViewModel params and fix tests

This commit is contained in:
Ammar Githam 2021-07-07 15:09:33 +09:00
parent 52826ad28c
commit 751259d430
4 changed files with 66 additions and 34 deletions

View File

@ -85,6 +85,14 @@ class ProfileFragment : Fragment(), OnRefreshListener, ConfirmDialogFragmentCall
private lateinit var appStateViewModel: AppStateViewModel private lateinit var appStateViewModel: AppStateViewModel
private lateinit var viewModel: ProfileFragmentViewModel private lateinit var viewModel: ProfileFragmentViewModel
private val userRepository by lazy { UserRepository.getInstance() }
private val friendshipRepository by lazy { FriendshipRepository.getInstance() }
private val storiesRepository by lazy { StoriesRepository.getInstance() }
private val mediaRepository by lazy { MediaRepository.getInstance() }
private val graphQLRepository by lazy { GraphQLRepository.getInstance() }
private val favoriteRepository by lazy { FavoriteRepository.getInstance(requireContext()) }
private val directMessagesRepository by lazy { DirectMessagesRepository.getInstance() }
private val confirmDialogFragmentRequestCode = 100 private val confirmDialogFragmentRequestCode = 100
private val ppOptsDialogRequestCode = 101 private val ppOptsDialogRequestCode = 101
private val bioDialogRequestCode = 102 private val bioDialogRequestCode = 102
@ -309,7 +317,15 @@ class ProfileFragment : Fragment(), OnRefreshListener, ConfirmDialogFragmentCall
viewModel = ViewModelProvider( viewModel = ViewModelProvider(
this, this,
ProfileFragmentViewModelFactory( ProfileFragmentViewModelFactory(
FavoriteRepository.getInstance(requireContext()), csrfToken,
deviceUuid,
userRepository,
friendshipRepository,
storiesRepository,
mediaRepository,
graphQLRepository,
favoriteRepository,
directMessagesRepository,
if (isLoggedIn) DirectMessagesManager else null, if (isLoggedIn) DirectMessagesManager else null,
this, this,
arguments arguments

View File

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

View File

@ -8,9 +8,7 @@ import awais.instagrabber.models.enums.FavoriteType
import awais.instagrabber.repositories.* import awais.instagrabber.repositories.*
import awais.instagrabber.repositories.responses.* import awais.instagrabber.repositories.responses.*
import awais.instagrabber.repositories.responses.directmessages.* import awais.instagrabber.repositories.responses.directmessages.*
import awais.instagrabber.repositories.responses.stories.ArchiveResponse import awais.instagrabber.repositories.responses.stories.*
import awais.instagrabber.repositories.responses.stories.ReelsTrayResponse
import awais.instagrabber.repositories.responses.stories.StoryStickerResponse
open class UserServiceAdapter : UserService { open class UserServiceAdapter : UserService {
override suspend fun getUserInfo(uid: Long): WrappedUser { override suspend fun getUserInfo(uid: Long): WrappedUser {
@ -47,7 +45,7 @@ open class FriendshipServiceAdapter : FriendshipService {
} }
open class StoriesServiceAdapter : StoriesService { open class StoriesServiceAdapter : StoriesService {
override suspend fun fetch(mediaId: Long): String { override suspend fun fetch(mediaId: Long): StoryMediaResponse {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
@ -63,11 +61,19 @@ open class StoriesServiceAdapter : StoriesService {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override suspend fun getUserStory(url: String): String { override suspend fun getReelsMedia(id: String): ReelsMediaResponse {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override suspend fun respondToSticker(storyId: String, stickerId: String, action: String, form: Map<String, String>): StoryStickerResponse { override suspend fun getStories(type: String, id: String): ReelsResponse {
TODO("Not yet implemented")
}
override suspend fun getUserStories(id: Long): ReelsResponse {
TODO("Not yet implemented")
}
override suspend fun respondToSticker(storyId: Long, stickerId: Long, action: String, form: Map<String, String>): StoryStickerResponse {
TODO("Not yet implemented") TODO("Not yet implemented")
} }

View File

@ -15,7 +15,6 @@ import awais.instagrabber.repositories.requests.StoryViewerOptions
import awais.instagrabber.repositories.responses.FriendshipStatus import awais.instagrabber.repositories.responses.FriendshipStatus
import awais.instagrabber.repositories.responses.User import awais.instagrabber.repositories.responses.User
import awais.instagrabber.repositories.responses.stories.Story import awais.instagrabber.repositories.responses.stories.Story
import awais.instagrabber.repositories.responses.stories.StoryMedia
import awais.instagrabber.webservices.* import awais.instagrabber.webservices.*
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.json.JSONException import org.json.JSONException
@ -320,13 +319,13 @@ internal class ProfileFragmentViewModelTest {
"username" to testPublicUser.username "username" to testPublicUser.username
) )
) )
val testUserStories = listOf(StoryMedia()) val testUserStories = Story()
val testUserHighlights = listOf(Story()) val testUserHighlights = listOf(Story())
val userRepository = object : UserRepository(UserServiceAdapter()) { val userRepository = object : UserRepository(UserServiceAdapter()) {
override suspend fun getUsernameInfo(username: String): User = testPublicUser override suspend fun getUsernameInfo(username: String): User = testPublicUser
} }
val storiesRepository = object : StoriesRepository(StoriesServiceAdapter()) { val storiesRepository = object : StoriesRepository(StoriesServiceAdapter()) {
override suspend fun getStories(options: StoryViewerOptions): List<StoryMedia> = testUserStories override suspend fun getStories(options: StoryViewerOptions): Story = testUserStories
override suspend fun fetchHighlights(profileId: Long): List<Story> = testUserHighlights override suspend fun fetchHighlights(profileId: Long): List<Story> = testUserHighlights
} }
val viewModel = ProfileFragmentViewModel( val viewModel = ProfileFragmentViewModel(