mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-12 17:57:29 +00:00
some profile viewmodel refactoring
This commit is contained in:
parent
7522b5f083
commit
cd1b1d5da2
@ -304,15 +304,7 @@ class ProfileFragment : Fragment(), OnRefreshListener, ConfirmDialogFragmentCall
|
||||
viewModel = ViewModelProvider(
|
||||
this,
|
||||
ProfileFragmentViewModelFactory(
|
||||
csrfToken,
|
||||
deviceUuid,
|
||||
UserRepository.getInstance(),
|
||||
FriendshipRepository.getInstance(),
|
||||
StoriesRepository.getInstance(),
|
||||
MediaRepository.getInstance(),
|
||||
GraphQLRepository.getInstance(),
|
||||
FavoriteRepository.getInstance(requireContext()),
|
||||
DirectMessagesRepository.getInstance(),
|
||||
if (isLoggedIn) DirectMessagesManager else null,
|
||||
this,
|
||||
arguments
|
||||
|
@ -16,9 +16,12 @@ 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.*
|
||||
@ -31,18 +34,20 @@ import java.time.LocalDateTime
|
||||
|
||||
class ProfileFragmentViewModel(
|
||||
private val state: SavedStateHandle,
|
||||
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 favoriteRepository: FavoriteRepository?,
|
||||
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)
|
||||
@ -238,7 +243,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
|
||||
@ -286,7 +291,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(
|
||||
@ -592,15 +597,7 @@ class ProfileFragmentViewModel(
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class ProfileFragmentViewModelFactory(
|
||||
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 favoriteRepository: FavoriteRepository?,
|
||||
private val messageManager: DirectMessagesManager?,
|
||||
owner: SavedStateRegistryOwner,
|
||||
defaultArgs: Bundle? = null,
|
||||
@ -612,15 +609,7 @@ class ProfileFragmentViewModelFactory(
|
||||
): T {
|
||||
return ProfileFragmentViewModel(
|
||||
handle,
|
||||
csrfToken,
|
||||
deviceUuid,
|
||||
userRepository,
|
||||
friendshipRepository,
|
||||
storiesRepository,
|
||||
mediaRepository,
|
||||
graphQLRepository,
|
||||
favoriteRepository,
|
||||
directMessagesRepository,
|
||||
messageManager,
|
||||
Dispatchers.IO,
|
||||
) as T
|
||||
|
Loading…
Reference in New Issue
Block a user