From fef2552bdfa618711977413965ea4146e2e28e4b Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 6 Jul 2021 16:56:45 -0400 Subject: [PATCH] actually support highlights and user stories properly, meaning not trying to init another ProfileFragmentViewModel --- .../fragments/StoryViewerFragment.kt | 44 +++++-------------- .../fragments/main/ProfileFragment.kt | 2 +- .../requests/StoryViewerOptions.java | 4 +- .../viewmodels/ProfileFragmentViewModel.kt | 2 - .../viewmodels/StoriesViewModel.java | 19 -------- .../viewmodels/StoryFragmentViewModel.kt | 17 +++++++ 6 files changed, 32 insertions(+), 56 deletions(-) delete mode 100644 app/src/main/java/awais/instagrabber/viewmodels/StoriesViewModel.java diff --git a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.kt b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.kt index 6d0d4fdd..40362e0c 100644 --- a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.kt +++ b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.kt @@ -47,7 +47,9 @@ import awais.instagrabber.utils.TextUtils.epochSecondToString import awais.instagrabber.utils.ResponseBodyUtils import awais.instagrabber.utils.Utils import awais.instagrabber.utils.extensions.TAG -import awais.instagrabber.viewmodels.* +import awais.instagrabber.viewmodels.ArchivesViewModel +import awais.instagrabber.viewmodels.FeedStoriesViewModel +import awais.instagrabber.viewmodels.StoryFragmentViewModel import awais.instagrabber.webservices.MediaRepository import awais.instagrabber.webservices.StoriesRepository import com.facebook.drawee.backends.pipeline.Fresco @@ -92,7 +94,6 @@ class StoryViewerFragment : Fragment() { private var backStackSavedStateResultLiveData: MutableLiveData? = null private lateinit var fragmentActivity: AppCompatActivity private lateinit var storiesViewModel: StoryFragmentViewModel - private lateinit var appStateViewModel: AppStateViewModel private lateinit var binding: FragmentStoryViewerBinding @Suppress("UNCHECKED_CAST") @@ -121,7 +122,6 @@ class StoryViewerFragment : Fragment() { super.onCreate(savedInstanceState) fragmentActivity = requireActivity() as AppCompatActivity storiesViewModel = ViewModelProvider(this).get(StoryFragmentViewModel::class.java) - appStateViewModel = ViewModelProvider(fragmentActivity).get(AppStateViewModel::class.java) setHasOptionsMenu(true) } @@ -191,26 +191,18 @@ class StoryViewerFragment : Fragment() { } private fun init() { - val args = arguments - if (args == null) return + val args = arguments ?: return val fragmentArgs = StoryViewerFragmentArgs.fromBundle(args) options = fragmentArgs.options currentFeedStoryIndex = options!!.currentFeedStoryIndex val type = options!!.type if (currentFeedStoryIndex >= 0) { listViewModel = when (type) { - StoryViewerOptions.Type.HIGHLIGHT, StoryViewerOptions.Type.USER -> { - val pArgs = Bundle() - pArgs.putString("username", options!!.name) - ViewModelProvider( - this, ProfileFragmentViewModelFactory(null, null, this, pArgs) - ).get(ProfileFragmentViewModel::class.java) - } StoryViewerOptions.Type.STORY_ARCHIVE -> ViewModelProvider(fragmentActivity).get(ArchivesViewModel::class.java) StoryViewerOptions.Type.FEED_STORY_POSITION -> ViewModelProvider(fragmentActivity).get(FeedStoriesViewModel::class.java) - else -> ViewModelProvider(fragmentActivity).get(FeedStoriesViewModel::class.java) + else -> null } } setupButtons() @@ -280,22 +272,8 @@ class StoryViewerFragment : Fragment() { val type = options!!.type when (type) { StoryViewerOptions.Type.HIGHLIGHT -> { - val profileFragmentViewModel = listViewModel as ProfileFragmentViewModel? - appStateViewModel.currentUserLiveData.observe( - viewLifecycleOwner, profileFragmentViewModel!!::setCurrentUser - ) - profileFragmentViewModel.currentUserProfileActionLiveData.observe(viewLifecycleOwner) {} - profileFragmentViewModel.userHighlights.observe(viewLifecycleOwner) {} - liveModels = profileFragmentViewModel.highlights - } - StoryViewerOptions.Type.USER -> { - val profileFragmentViewModel = listViewModel as ProfileFragmentViewModel? - appStateViewModel.currentUserLiveData.observe( - viewLifecycleOwner, profileFragmentViewModel!!::setCurrentUser - ) - profileFragmentViewModel.currentUserProfileActionLiveData.observe(viewLifecycleOwner) {} - profileFragmentViewModel.userStories.observe(viewLifecycleOwner) {} - liveModels = profileFragmentViewModel.stories + storiesViewModel.fetchHighlights(options!!.id) + liveModels = storiesViewModel.getHighlights() } StoryViewerOptions.Type.FEED_STORY_POSITION -> { val feedStoriesViewModel = listViewModel as FeedStoriesViewModel? @@ -305,6 +283,9 @@ class StoryViewerFragment : Fragment() { val archivesViewModel = listViewModel as ArchivesViewModel? liveModels = archivesViewModel!!.list } + StoryViewerOptions.Type.USER -> { + resetView() + } } } if (liveModels != null) liveModels.observe(viewLifecycleOwner, { models -> @@ -387,13 +368,12 @@ class StoryViewerFragment : Fragment() { var fetchOptions: StoryViewerOptions? = null when (type) { StoryViewerOptions.Type.HIGHLIGHT -> { - val profileFragmentViewModel = listViewModel as ProfileFragmentViewModel? - val models = profileFragmentViewModel!!.highlights.value + val models = storiesViewModel.getHighlights().value if (models == null || models.isEmpty() || currentFeedStoryIndex >= models.size || currentFeedStoryIndex < 0) { Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show() return } - fetchOptions = StoryViewerOptions.forHighlight(models[currentFeedStoryIndex].id) + fetchOptions = StoryViewerOptions.forHighlight(0L, models[currentFeedStoryIndex].id) } StoryViewerOptions.Type.FEED_STORY_POSITION -> { val feedStoriesViewModel = listViewModel as FeedStoriesViewModel? diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt index ab731719..c8dd85e2 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt @@ -843,7 +843,7 @@ class ProfileFragment : Fragment(), OnRefreshListener, ConfirmDialogFragmentCall private fun setupHighlights() { val context = context ?: return highlightsAdapter = HighlightsAdapter { model, position -> - val options = StoryViewerOptions.forHighlight(model.user?.username) + val options = StoryViewerOptions.forHighlight(model.user!!.pk, "") options.currentFeedStoryIndex = position val action = ProfileFragmentDirections.actionProfileFragmentToStoryViewerFragment(options) NavHostFragment.findNavController(this).navigate(action) diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/StoryViewerOptions.java b/app/src/main/java/awais/instagrabber/repositories/requests/StoryViewerOptions.java index 8202ec7d..7a82a298 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/StoryViewerOptions.java +++ b/app/src/main/java/awais/instagrabber/repositories/requests/StoryViewerOptions.java @@ -45,8 +45,8 @@ public class StoryViewerOptions implements Serializable { return new StoryViewerOptions(id, name, Type.USER); } - public static StoryViewerOptions forHighlight(final String highlight) { - return new StoryViewerOptions(highlight, Type.HIGHLIGHT); + public static StoryViewerOptions forHighlight(final long id, final String highlight) { + return new StoryViewerOptions(id, highlight, Type.HIGHLIGHT); } public static StoryViewerOptions forStory(final long mediaId, final String username) { diff --git a/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt b/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt index 013cfd48..3acd3e66 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt +++ b/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt @@ -187,7 +187,6 @@ class ProfileFragmentViewModel( } } } - val stories: LiveData?> = userStories.map { if (it.data == null) listOf() else listOf(it.data) } private val highlightsFetchControlledRunner = ControlledRunner?>() val userHighlights: LiveData?>> = currentUserProfileActionLiveData.switchMap { currentUserAndProfilePair -> @@ -219,7 +218,6 @@ class ProfileFragmentViewModel( } } } - val highlights: LiveData?> = userHighlights.map { it.data } private suspend fun fetchUser( currentUser: User?, diff --git a/app/src/main/java/awais/instagrabber/viewmodels/StoriesViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/StoriesViewModel.java deleted file mode 100644 index 0f512403..00000000 --- a/app/src/main/java/awais/instagrabber/viewmodels/StoriesViewModel.java +++ /dev/null @@ -1,19 +0,0 @@ -package awais.instagrabber.viewmodels; - -import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; - -import java.util.List; - -import awais.instagrabber.repositories.responses.stories.StoryMedia; - -public class StoriesViewModel extends ViewModel { - private MutableLiveData> list; - - public MutableLiveData> getList() { - if (list == null) { - list = new MutableLiveData<>(); - } - return list; - } -} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/viewmodels/StoryFragmentViewModel.kt b/app/src/main/java/awais/instagrabber/viewmodels/StoryFragmentViewModel.kt index a1040ca2..d463c251 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/StoryFragmentViewModel.kt +++ b/app/src/main/java/awais/instagrabber/viewmodels/StoryFragmentViewModel.kt @@ -59,6 +59,9 @@ class StoryFragmentViewModel : ViewModel() { private val storiesRepository: StoriesRepository by lazy { StoriesRepository.getInstance() } private val mediaRepository: MediaRepository by lazy { MediaRepository.getInstance() } + // for highlights ONLY + private val highlights = MutableLiveData?>() + /* set functions */ fun setStory(story: Story) { @@ -181,6 +184,10 @@ class StoryFragmentViewModel : ViewModel() { /* get functions */ + fun getHighlights(): LiveData?> { + return highlights + } + fun getCurrentStory(): LiveData { return currentStory } @@ -441,6 +448,16 @@ class StoryFragmentViewModel : ViewModel() { return data } + fun fetchHighlights(id: Long) { + viewModelScope.launch(Dispatchers.IO) { + try { + val result = storiesRepository.fetchHighlights(id) + highlights.postValue(result) + } catch (e: Exception) { + } + } + } + fun fetchSingleMedia(mediaId: Long): LiveData> { val data = MutableLiveData>() data.postValue(loading(null))