Skip to content

Commit

Permalink
Implement subscribe action snackbar
Browse files Browse the repository at this point in the history
  • Loading branch information
RenanLukas committed May 10, 2024
1 parent 7fdb8b3 commit 5b45a34
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,26 @@ class ReaderTagsFeedFragment : ViewPagerFragment(R.layout.reader_tag_feed_fragme
}
}

private fun observeSnackbarEvents() {
viewModel.snackbarEvents.observeEvent(viewLifecycleOwner) { snackbarMessageHolder ->
activity?.findViewById<View>(R.id.coordinator)?.let { coordinator ->
with(snackbarMessageHolder) {
val snackbar = WPSnackbar.make(
coordinator,
uiHelpers.getTextOfUiString(requireContext(), message),
Snackbar.LENGTH_LONG
)
if (buttonTitle != null) {
snackbar.setAction(uiHelpers.getTextOfUiString(requireContext(), buttonTitle)) {
buttonAction.invoke()
}
}
snackbar.show()
}
}
}
}

private fun observeOpenMoreMenuEvents() {
viewModel.openMoreMenuEvents.observe(viewLifecycleOwner) {
val readerCardUiState = it.readerCardUiState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.wordpress.android.ui.reader.viewmodels.tagsfeed
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -13,6 +14,7 @@ import org.wordpress.android.datasets.wrappers.ReaderPostTableWrapper
import org.wordpress.android.models.ReaderPost
import org.wordpress.android.models.ReaderTag
import org.wordpress.android.modules.BG_THREAD
import org.wordpress.android.ui.pages.SnackbarMessageHolder
import org.wordpress.android.ui.reader.ReaderTypes
import org.wordpress.android.ui.reader.discover.FEATURED_IMAGE_HEIGHT_WIDTH_RATION
import org.wordpress.android.ui.reader.discover.PHOTON_WIDTH_QUALITY_RATION
Expand Down Expand Up @@ -56,9 +58,16 @@ class ReaderTagsFeedViewModel @Inject constructor(
private val _navigationEvents = MediatorLiveData<Event<ReaderNavigationEvents>>()
val navigationEvents: LiveData<Event<ReaderNavigationEvents>> = _navigationEvents

// Unlike the snackbarEvents observable which only expects messages from ReaderPostCardActionsHandler,
// this observable is controlled by this ViewModel.
private val _errorMessageEvents = MediatorLiveData<Event<Int>>()
val errorMessageEvents: LiveData<Event<Int>> = _errorMessageEvents

// This observable just expects messages from ReaderPostCardActionsHandler. Nothing is directly triggered
// from this ViewModel.
private val _snackbarEvents = MediatorLiveData<Event<SnackbarMessageHolder>>()
val snackbarEvents: LiveData<Event<SnackbarMessageHolder>> = _snackbarEvents

private val _openMoreMenuEvents = SingleLiveEvent<MoreMenuUiState>()
val openMoreMenuEvents: LiveData<MoreMenuUiState> = _openMoreMenuEvents

Expand All @@ -84,7 +93,9 @@ class ReaderTagsFeedViewModel @Inject constructor(

if (!hasInitialized) {
hasInitialized = true
readerPostCardActionsHandler.initScope(viewModelScope)
initNavigationEvents()
initSnackbarEvents()
}

// Initially add all tags to the list with the posts loading UI
Expand All @@ -110,6 +121,12 @@ class ReaderTagsFeedViewModel @Inject constructor(
}
}

private fun initSnackbarEvents() {
_snackbarEvents.addSource(readerPostCardActionsHandler.snackbarEvents) { event ->
_snackbarEvents.value = event
}
}

/**
* Fetch posts for a single tag. This method will emit a new state to [uiStateFlow] for different [UiState]s:
* [UiState.Initial], [UiState.Loaded], [UiState.Loading], [UiState.Empty], but only for the tag being fetched.
Expand Down

0 comments on commit 5b45a34

Please sign in to comment.