Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/13268 persist activity type filters in parent vm #13485

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class ActivityLogListFragment : Fragment() {
uiHelpers.updateVisibility(activity_type_filter, visibility)
})

viewModel.showActivityTypeFilterDialog.observe(viewLifecycleOwner, Observer { remoteSiteId ->
showActivityTypeFilterDialog(remoteSiteId)
viewModel.showActivityTypeFilterDialog.observe(viewLifecycleOwner, Observer { event ->
showActivityTypeFilterDialog(event.siteId, event.initialSelection)
})

viewModel.showDateRangePicker.observe(viewLifecycleOwner, Observer { event ->
Expand Down Expand Up @@ -181,8 +181,9 @@ class ActivityLogListFragment : Fragment() {
picker.addOnPositiveButtonClickListener { viewModel.onDateRangeSelected(it) }
}

private fun showActivityTypeFilterDialog(remoteSiteId: RemoteId) {
ActivityLogTypeFilterFragment.newInstance(remoteSiteId).show(childFragmentManager, ACTIVITY_TYPE_FILTER_TAG)
private fun showActivityTypeFilterDialog(remoteSiteId: RemoteId, initialSelection: List<Int>) {
ActivityLogTypeFilterFragment.newInstance(remoteSiteId, initialSelection)
.show(childFragmentManager, ACTIVITY_TYPE_FILTER_TAG)
}

private fun refreshProgressBars(eventListStatus: ActivityLogViewModel.ActivityLogListStatus?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.wordpress.android.util.getColorResIdFromAttribute
import org.wordpress.android.viewmodel.activitylog.ActivityLogViewModel
import javax.inject.Inject

private const val ARG_INITIAL_SELECTION = "arg_initial_selection"
private const val ACTIONS_MENU_GROUP = 1

/**
Expand Down Expand Up @@ -101,6 +102,7 @@ class ActivityLogTypeFilterFragment : DialogFragment() {
})
viewModel.start(
remoteSiteId = RemoteId(requireNotNull(arguments).getLong(WordPress.REMOTE_SITE_ID)),
initialSelection = requireNotNull(arguments).getIntArray(ARG_INITIAL_SELECTION)?.toList() ?: listOf(),
parentViewModel = parentViewModel
)
}
Expand Down Expand Up @@ -151,8 +153,9 @@ class ActivityLogTypeFilterFragment : DialogFragment() {

companion object {
@JvmStatic
fun newInstance(remoteSiteId: RemoteId): ActivityLogTypeFilterFragment {
fun newInstance(remoteSiteId: RemoteId, initialSelection: List<Int>): ActivityLogTypeFilterFragment {
val args = Bundle()
args.putIntArray(ARG_INITIAL_SELECTION, initialSelection.toIntArray())
args.putLong(WordPress.REMOTE_SITE_ID, remoteSiteId.value)
return ActivityLogTypeFilterFragment().apply { arguments = args }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,24 @@ class ActivityLogTypeFilterViewModel @Inject constructor(
private var isStarted = false
private lateinit var remoteSiteId: RemoteId
private lateinit var parentViewModel: ActivityLogViewModel
private lateinit var initialSelection: List<Int>

private val _uiState = MutableLiveData<UiState>()
val uiState: LiveData<UiState> = _uiState

private val _dismissDialog = MutableLiveData<Event<Unit>>()
val dismissDialog: LiveData<Event<Unit>> = _dismissDialog

fun start(remoteSiteId: RemoteId, parentViewModel: ActivityLogViewModel) {
fun start(
remoteSiteId: RemoteId,
parentViewModel: ActivityLogViewModel,
initialSelection: List<Int>
) {
if (isStarted) return
isStarted = true
this.remoteSiteId = remoteSiteId
this.parentViewModel = parentViewModel
this.initialSelection = initialSelection

fetchAvailableActivityTypes()
}
Expand Down Expand Up @@ -71,7 +77,8 @@ class ActivityLogTypeFilterViewModel @Inject constructor(
ListItemUiState.ActivityType(
id = it.id,
title = UiStringText(it.toString()),
onClick = { onItemClicked(it.id) }
onClick = { onItemClicked(it.id) },
checked = initialSelection.contains(it.id)
)
}
Content(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class ActivityLogViewModel @Inject constructor(
val showRewindDialog: LiveData<ActivityLogListItem>
get() = _showRewindDialog

private val _showActivityTypeFilterDialog = SingleLiveEvent<RemoteId>()
val showActivityTypeFilterDialog: LiveData<RemoteId>
private val _showActivityTypeFilterDialog = SingleLiveEvent<ShowActivityTypePicker>()
val showActivityTypeFilterDialog: LiveData<ShowActivityTypePicker>
get() = _showActivityTypeFilterDialog

private val _showDateRangePicker = SingleLiveEvent<ShowDateRangePicker>()
Expand Down Expand Up @@ -101,7 +101,10 @@ class ActivityLogViewModel @Inject constructor(

private var lastRewindActivityId: String? = null
private var lastRewindStatus: Status? = null

private var currentDateRangeFilter: DateRange? = null
private var currentActivityTypeFilter: List<Int> = listOf()

private val rewindProgressObserver = Observer<RewindProgress> {
if (it?.activityLogItem?.activityID != lastRewindActivityId || it?.status != lastRewindStatus) {
lastRewindActivityId = it?.activityLogItem?.activityID
Expand Down Expand Up @@ -175,11 +178,11 @@ class ActivityLogViewModel @Inject constructor(

fun onActivityTypeFilterClicked() {
// TODO malinjir pass initially selected activity types
_showActivityTypeFilterDialog.value = RemoteId(site.siteId)
_showActivityTypeFilterDialog.value = ShowActivityTypePicker(RemoteId(site.siteId), currentActivityTypeFilter)
}

fun onActivityTypesSelected(activityTypeIds: List<Int>) {
// TODO malinjir store the ids
currentActivityTypeFilter = activityTypeIds
// TODO malinjir: refetch/load data
}

Expand Down Expand Up @@ -327,4 +330,5 @@ class ActivityLogViewModel @Inject constructor(
}

data class ShowDateRangePicker(val initialSelection: DateRange?)
data class ShowActivityTypePicker(val siteId: RemoteId, val initialSelection: List<Int>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() {
).isEmpty()
}

@Test
fun `items are checked, when the user opens the screen with active activity type filter`() = test {
val uiStates = init().uiStates
val initialSelection = listOf(1, 4)

startVM(initialSelection = initialSelection)

assertThat((uiStates.last() as Content).items.filterIsInstance(ActivityType::class.java)
.filter { it.checked }.map { it.id }
).containsExactlyElementsOf(initialSelection)
}

private suspend fun init(successResponse: Boolean = true, activityTypeCount: Int = 5): Observers {
val uiStates = mutableListOf<UiState>()
val dismissDialogEvents = mutableListOf<Unit>()
Expand All @@ -190,8 +202,8 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() {
return Observers(uiStates, dismissDialogEvents)
}

private fun startVM() {
viewModel.start(RemoteId(0L), parentViewModel)
private fun startVM(initialSelection: List<Int> = listOf()) {
viewModel.start(RemoteId(0L), parentViewModel, initialSelection)
}

private fun generateActivityTypes(count: Int): List<DummyActivityType> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.MutableLiveData
import com.nhaarman.mockitokotlin2.KArgumentCaptor
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.argumentCaptor
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
Expand Down Expand Up @@ -71,7 +72,8 @@ class ActivityLogViewModelTest {
Date(),
true,
null,
null)
null
)

val event = ActivityLogListItem.Event(
"activityId",
Expand All @@ -96,7 +98,7 @@ class ActivityLogViewModelTest {
null,
Date(),
null
)
)

@Before
fun setUp() = runBlocking<Unit> {
Expand All @@ -121,7 +123,7 @@ class ActivityLogViewModelTest {
whenever(store.getRewindStatusForSite(site)).thenReturn(rewindStatusModel)
whenever(rewindStatusService.rewindProgress).thenReturn(rewindProgress)
whenever(rewindStatusService.rewindAvailable).thenReturn(rewindAvailable)
whenever(store.fetchActivities(any())).thenReturn(mock())
whenever(store.fetchActivities(anyOrNull())).thenReturn(mock())
}

@Test
Expand Down Expand Up @@ -152,7 +154,7 @@ class ActivityLogViewModelTest {
@Test
fun onDataFetchedPostsDataAndChangesStatusIfCanLoadMore() = runBlocking {
val canLoadMore = true
whenever(store.fetchActivities(any())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))

viewModel.start(site)

Expand All @@ -167,11 +169,12 @@ class ActivityLogViewModelTest {
@Test
fun onDataFetchedLoadsMoreDataIfCanLoadMore() = runBlocking {
val canLoadMore = true
whenever(store.fetchActivities(any())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))

viewModel.start(site)

reset(store)
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))

viewModel.onScrolledToBottom()

Expand All @@ -181,7 +184,7 @@ class ActivityLogViewModelTest {
@Test
fun onDataFetchedPostsDataAndChangesStatusIfCannotLoadMore() = runBlocking {
val canLoadMore = false
whenever(store.fetchActivities(any())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))

viewModel.start(site)

Expand All @@ -197,7 +200,7 @@ class ActivityLogViewModelTest {
fun onDataFetchedShowsFooterIfCannotLoadMoreAndIsFreeSite() = runBlocking {
val canLoadMore = false
whenever(site.hasFreePlan).thenReturn(true)
whenever(store.fetchActivities(any())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))

viewModel.start(site)

Expand Down Expand Up @@ -232,22 +235,22 @@ class ActivityLogViewModelTest {
@Test
fun onDataFetchedDoesNotLoadMoreDataIfCannotLoadMore() = runBlocking<Unit> {
val canLoadMore = false
whenever(store.fetchActivities(any())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(1, canLoadMore, FETCH_ACTIVITIES))

viewModel.start(site)

reset(store)

viewModel.onScrolledToBottom()

verify(store, never()).fetchActivities(any())
verify(store, never()).fetchActivities(anyOrNull())
}

@Test
fun onDataFetchedGoesToTopWhenSomeRowsAffected() = runBlocking {
assertTrue(moveToTopEvents.isEmpty())

whenever(store.fetchActivities(any())).thenReturn(OnActivityLogFetched(10, true, FETCH_ACTIVITIES))
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(10, true, FETCH_ACTIVITIES))

viewModel.start(site)

Expand All @@ -258,7 +261,7 @@ class ActivityLogViewModelTest {
fun onDataFetchedDoesNotLoadMoreDataIfNoRowsAffected() = runBlocking<Unit> {
val canLoadMore = true

whenever(store.fetchActivities(any())).thenReturn(OnActivityLogFetched(0, canLoadMore, FETCH_ACTIVITIES))
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(0, canLoadMore, FETCH_ACTIVITIES))

viewModel.start(site)

Expand All @@ -268,7 +271,7 @@ class ActivityLogViewModelTest {
@Test
fun headerIsDisplayedForFirstItemOrWhenDifferentThenPrevious() = runBlocking {
val canLoadMore = true
whenever(store.fetchActivities(any())).thenReturn(OnActivityLogFetched(3, canLoadMore, FETCH_ACTIVITIES))
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(3, canLoadMore, FETCH_ACTIVITIES))

viewModel.start(site)

Expand Down Expand Up @@ -318,10 +321,11 @@ class ActivityLogViewModelTest {

@Test
fun loadsNextPageOnScrollToBottom() = runBlocking {
whenever(store.fetchActivities(any())).thenReturn(OnActivityLogFetched(10, true, FETCH_ACTIVITIES))
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(10, true, FETCH_ACTIVITIES))

viewModel.start(site)
reset(store)
whenever(store.fetchActivities(anyOrNull())).thenReturn(OnActivityLogFetched(10, true, FETCH_ACTIVITIES))

viewModel.onScrolledToBottom()

Expand Down Expand Up @@ -357,7 +361,17 @@ class ActivityLogViewModelTest {
fun onActivityTypeFilterClickRemoteSiteIdIsPassed() {
viewModel.onActivityTypeFilterClicked()

assertEquals(RemoteId(site.siteId), viewModel.showActivityTypeFilterDialog.value)
assertEquals(RemoteId(site.siteId), viewModel.showActivityTypeFilterDialog.value!!.siteId)
}

@Test
fun onActivityTypeFilterClickPreviouslySelectedTypesPassed() {
val selectedItems = listOf(1, 4)
viewModel.onActivityTypesSelected(selectedItems)

viewModel.onActivityTypeFilterClicked()

assertEquals(selectedItems, viewModel.showActivityTypeFilterDialog.value!!.initialSelection)
}

private suspend fun assertFetchEvents(canLoadMore: Boolean = false) {
Expand All @@ -374,8 +388,10 @@ class ActivityLogViewModelTest {
birthday.set(1985, 8, 27)

val list = mutableListOf<ActivityLogModel>()
val activity = ActivityLogModel("", "", null, "", "", "",
"", true, "", birthday.time)
val activity = ActivityLogModel(
"", "", null, "", "", "",
"", true, "", birthday.time
)
list.add(activity)
list.add(activity.copy())

Expand Down