1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 05:16:58 +00:00

actually support highlights and user stories

properly, meaning not trying to init another ProfileFragmentViewModel
This commit is contained in:
Austin Huang 2021-07-06 16:56:45 -04:00
parent 7e9e3b0fbf
commit fef2552bdf
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
6 changed files with 32 additions and 56 deletions

View File

@ -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<Any?>? = 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?

View File

@ -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)

View File

@ -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) {

View File

@ -187,7 +187,6 @@ class ProfileFragmentViewModel(
}
}
}
val stories: LiveData<List<Story>?> = userStories.map { if (it.data == null) listOf() else listOf(it.data) }
private val highlightsFetchControlledRunner = ControlledRunner<List<Story>?>()
val userHighlights: LiveData<Resource<List<Story>?>> = currentUserProfileActionLiveData.switchMap { currentUserAndProfilePair ->
@ -219,7 +218,6 @@ class ProfileFragmentViewModel(
}
}
}
val highlights: LiveData<List<Story>?> = userHighlights.map { it.data }
private suspend fun fetchUser(
currentUser: User?,

View File

@ -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<StoryMedia>> list;
public MutableLiveData<List<StoryMedia>> getList() {
if (list == null) {
list = new MutableLiveData<>();
}
return list;
}
}

View File

@ -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<List<Story>?>()
/* set functions */
fun setStory(story: Story) {
@ -181,6 +184,10 @@ class StoryFragmentViewModel : ViewModel() {
/* get functions */
fun getHighlights(): LiveData<List<Story>?> {
return highlights
}
fun getCurrentStory(): LiveData<Story?> {
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<Resource<Any?>> {
val data = MutableLiveData<Resource<Any?>>()
data.postValue(loading(null))