1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-24 15:47:30 +00:00

some profile viewmodel refactoring

This commit is contained in:
Austin Huang 2021-07-06 12:35:19 -04:00
parent 7522b5f083
commit cd1b1d5da2
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
2 changed files with 17 additions and 36 deletions

View File

@ -304,15 +304,7 @@ class ProfileFragment : Fragment(), OnRefreshListener, ConfirmDialogFragmentCall
viewModel = ViewModelProvider( viewModel = ViewModelProvider(
this, this,
ProfileFragmentViewModelFactory( ProfileFragmentViewModelFactory(
csrfToken,
deviceUuid,
UserRepository.getInstance(),
FriendshipRepository.getInstance(),
StoriesRepository.getInstance(),
MediaRepository.getInstance(),
GraphQLRepository.getInstance(),
FavoriteRepository.getInstance(requireContext()), FavoriteRepository.getInstance(requireContext()),
DirectMessagesRepository.getInstance(),
if (isLoggedIn) DirectMessagesManager else null, if (isLoggedIn) DirectMessagesManager else null,
this, this,
arguments arguments

View File

@ -16,9 +16,12 @@ 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.*
@ -31,18 +34,20 @@ import java.time.LocalDateTime
class ProfileFragmentViewModel( class ProfileFragmentViewModel(
private val state: SavedStateHandle, private val state: SavedStateHandle,
private val csrfToken: String?, private val favoriteRepository: FavoriteRepository?,
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)
@ -238,7 +243,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
@ -286,7 +291,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(
@ -592,15 +597,7 @@ class ProfileFragmentViewModel(
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
class ProfileFragmentViewModelFactory( class ProfileFragmentViewModelFactory(
private val csrfToken: String?, private val favoriteRepository: FavoriteRepository?,
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,
@ -612,15 +609,7 @@ 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