Browse Source

actually support highlights and user stories

properly, meaning not trying to init another ProfileFragmentViewModel
renovate/org.jetbrains.kotlinx-kotlinx-coroutines-test-1.x
Austin Huang 4 years ago
parent
commit
fef2552bdf
No known key found for this signature in database GPG Key ID: 84C23AA04587A91F
  1. 44
      app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.kt
  2. 2
      app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt
  3. 4
      app/src/main/java/awais/instagrabber/repositories/requests/StoryViewerOptions.java
  4. 2
      app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt
  5. 19
      app/src/main/java/awais/instagrabber/viewmodels/StoriesViewModel.java
  6. 17
      app/src/main/java/awais/instagrabber/viewmodels/StoryFragmentViewModel.kt

44
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.ResponseBodyUtils
import awais.instagrabber.utils.Utils import awais.instagrabber.utils.Utils
import awais.instagrabber.utils.extensions.TAG 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.MediaRepository
import awais.instagrabber.webservices.StoriesRepository import awais.instagrabber.webservices.StoriesRepository
import com.facebook.drawee.backends.pipeline.Fresco import com.facebook.drawee.backends.pipeline.Fresco
@ -92,7 +94,6 @@ class StoryViewerFragment : Fragment() {
private var backStackSavedStateResultLiveData: MutableLiveData<Any?>? = null private var backStackSavedStateResultLiveData: MutableLiveData<Any?>? = null
private lateinit var fragmentActivity: AppCompatActivity private lateinit var fragmentActivity: AppCompatActivity
private lateinit var storiesViewModel: StoryFragmentViewModel private lateinit var storiesViewModel: StoryFragmentViewModel
private lateinit var appStateViewModel: AppStateViewModel
private lateinit var binding: FragmentStoryViewerBinding private lateinit var binding: FragmentStoryViewerBinding
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@ -121,7 +122,6 @@ class StoryViewerFragment : Fragment() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
fragmentActivity = requireActivity() as AppCompatActivity fragmentActivity = requireActivity() as AppCompatActivity
storiesViewModel = ViewModelProvider(this).get(StoryFragmentViewModel::class.java) storiesViewModel = ViewModelProvider(this).get(StoryFragmentViewModel::class.java)
appStateViewModel = ViewModelProvider(fragmentActivity).get(AppStateViewModel::class.java)
setHasOptionsMenu(true) setHasOptionsMenu(true)
} }
@ -191,26 +191,18 @@ class StoryViewerFragment : Fragment() {
} }
private fun init() { private fun init() {
val args = arguments
if (args == null) return
val args = arguments ?: return
val fragmentArgs = StoryViewerFragmentArgs.fromBundle(args) val fragmentArgs = StoryViewerFragmentArgs.fromBundle(args)
options = fragmentArgs.options options = fragmentArgs.options
currentFeedStoryIndex = options!!.currentFeedStoryIndex currentFeedStoryIndex = options!!.currentFeedStoryIndex
val type = options!!.type val type = options!!.type
if (currentFeedStoryIndex >= 0) { if (currentFeedStoryIndex >= 0) {
listViewModel = when (type) { 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 -> StoryViewerOptions.Type.STORY_ARCHIVE ->
ViewModelProvider(fragmentActivity).get(ArchivesViewModel::class.java) ViewModelProvider(fragmentActivity).get(ArchivesViewModel::class.java)
StoryViewerOptions.Type.FEED_STORY_POSITION -> StoryViewerOptions.Type.FEED_STORY_POSITION ->
ViewModelProvider(fragmentActivity).get(FeedStoriesViewModel::class.java) ViewModelProvider(fragmentActivity).get(FeedStoriesViewModel::class.java)
else -> ViewModelProvider(fragmentActivity).get(FeedStoriesViewModel::class.java)
else -> null
} }
} }
setupButtons() setupButtons()
@ -280,22 +272,8 @@ class StoryViewerFragment : Fragment() {
val type = options!!.type val type = options!!.type
when (type) { when (type) {
StoryViewerOptions.Type.HIGHLIGHT -> { 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 -> { StoryViewerOptions.Type.FEED_STORY_POSITION -> {
val feedStoriesViewModel = listViewModel as FeedStoriesViewModel? val feedStoriesViewModel = listViewModel as FeedStoriesViewModel?
@ -305,6 +283,9 @@ class StoryViewerFragment : Fragment() {
val archivesViewModel = listViewModel as ArchivesViewModel? val archivesViewModel = listViewModel as ArchivesViewModel?
liveModels = archivesViewModel!!.list liveModels = archivesViewModel!!.list
} }
StoryViewerOptions.Type.USER -> {
resetView()
}
} }
} }
if (liveModels != null) liveModels.observe(viewLifecycleOwner, { models -> if (liveModels != null) liveModels.observe(viewLifecycleOwner, { models ->
@ -387,13 +368,12 @@ class StoryViewerFragment : Fragment() {
var fetchOptions: StoryViewerOptions? = null var fetchOptions: StoryViewerOptions? = null
when (type) { when (type) {
StoryViewerOptions.Type.HIGHLIGHT -> { 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) { if (models == null || models.isEmpty() || currentFeedStoryIndex >= models.size || currentFeedStoryIndex < 0) {
Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show()
return return
} }
fetchOptions = StoryViewerOptions.forHighlight(models[currentFeedStoryIndex].id)
fetchOptions = StoryViewerOptions.forHighlight(0L, models[currentFeedStoryIndex].id)
} }
StoryViewerOptions.Type.FEED_STORY_POSITION -> { StoryViewerOptions.Type.FEED_STORY_POSITION -> {
val feedStoriesViewModel = listViewModel as FeedStoriesViewModel? val feedStoriesViewModel = listViewModel as FeedStoriesViewModel?

2
app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.kt

@ -843,7 +843,7 @@ class ProfileFragment : Fragment(), OnRefreshListener, ConfirmDialogFragmentCall
private fun setupHighlights() { private fun setupHighlights() {
val context = context ?: return val context = context ?: return
highlightsAdapter = HighlightsAdapter { model, position -> highlightsAdapter = HighlightsAdapter { model, position ->
val options = StoryViewerOptions.forHighlight(model.user?.username)
val options = StoryViewerOptions.forHighlight(model.user!!.pk, "")
options.currentFeedStoryIndex = position options.currentFeedStoryIndex = position
val action = ProfileFragmentDirections.actionProfileFragmentToStoryViewerFragment(options) val action = ProfileFragmentDirections.actionProfileFragmentToStoryViewerFragment(options)
NavHostFragment.findNavController(this).navigate(action) NavHostFragment.findNavController(this).navigate(action)

4
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); 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) { public static StoryViewerOptions forStory(final long mediaId, final String username) {

2
app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt

@ -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>?>() private val highlightsFetchControlledRunner = ControlledRunner<List<Story>?>()
val userHighlights: LiveData<Resource<List<Story>?>> = currentUserProfileActionLiveData.switchMap { currentUserAndProfilePair -> 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( private suspend fun fetchUser(
currentUser: User?, currentUser: User?,

19
app/src/main/java/awais/instagrabber/viewmodels/StoriesViewModel.java

@ -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;
}
}

17
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 storiesRepository: StoriesRepository by lazy { StoriesRepository.getInstance() }
private val mediaRepository: MediaRepository by lazy { MediaRepository.getInstance() } private val mediaRepository: MediaRepository by lazy { MediaRepository.getInstance() }
// for highlights ONLY
private val highlights = MutableLiveData<List<Story>?>()
/* set functions */ /* set functions */
fun setStory(story: Story) { fun setStory(story: Story) {
@ -181,6 +184,10 @@ class StoryFragmentViewModel : ViewModel() {
/* get functions */ /* get functions */
fun getHighlights(): LiveData<List<Story>?> {
return highlights
}
fun getCurrentStory(): LiveData<Story?> { fun getCurrentStory(): LiveData<Story?> {
return currentStory return currentStory
} }
@ -441,6 +448,16 @@ class StoryFragmentViewModel : ViewModel() {
return data 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?>> { fun fetchSingleMedia(mediaId: Long): LiveData<Resource<Any?>> {
val data = MutableLiveData<Resource<Any?>>() val data = MutableLiveData<Resource<Any?>>()
data.postValue(loading(null)) data.postValue(loading(null))

Loading…
Cancel
Save