diff --git a/WordPress/src/main/java/org/wordpress/android/WordPress.java b/WordPress/src/main/java/org/wordpress/android/WordPress.java index da8a544f12ff..88faeaeac44d 100644 --- a/WordPress/src/main/java/org/wordpress/android/WordPress.java +++ b/WordPress/src/main/java/org/wordpress/android/WordPress.java @@ -139,6 +139,7 @@ public class WordPress extends MultiDexApplication implements HasAndroidInjector, LifecycleObserver { public static final String SITE = "SITE"; public static final String LOCAL_SITE_ID = "LOCAL_SITE_ID"; + public static final String REMOTE_SITE_ID = "REMOTE_SITE_ID"; public static String versionName; public static WordPressDB wpDB; public static boolean sAppIsInTheBackground = true; diff --git a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/ActivityLogListFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/ActivityLogListFragment.kt index c662bf7d4a9c..c216196610a9 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/ActivityLogListFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/ActivityLogListFragment.kt @@ -17,6 +17,7 @@ import kotlinx.android.synthetic.main.activity_log_list_fragment.* import kotlinx.android.synthetic.main.activity_log_list_loading_item.* import org.wordpress.android.R import org.wordpress.android.WordPress +import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId import org.wordpress.android.fluxc.model.SiteModel import org.wordpress.android.ui.ActivityLauncher import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterFragment @@ -123,8 +124,8 @@ class ActivityLogListFragment : Fragment() { uiHelpers.updateVisibility(activity_type_filter, visibility) }) - viewModel.showActivityTypeFilterDialog.observe(viewLifecycleOwner, Observer { _ -> - showActivityTypeFilterDialog() + viewModel.showActivityTypeFilterDialog.observe(viewLifecycleOwner, Observer { remoteSiteId -> + showActivityTypeFilterDialog(remoteSiteId) }) viewModel.showDateRangePicker.observe(viewLifecycleOwner, Observer { event -> @@ -180,8 +181,8 @@ class ActivityLogListFragment : Fragment() { picker.addOnPositiveButtonClickListener { viewModel.onDateRangeSelected(it) } } - private fun showActivityTypeFilterDialog() { - ActivityLogTypeFilterFragment().show(parentFragmentManager, ACTIVITY_TYPE_FILTER_TAG) + private fun showActivityTypeFilterDialog(remoteSiteId: RemoteId) { + ActivityLogTypeFilterFragment.newInstance(remoteSiteId).show(parentFragmentManager, ACTIVITY_TYPE_FILTER_TAG) } private fun refreshProgressBars(eventListStatus: ActivityLogViewModel.ActivityLogListStatus?) { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterFragment.kt index 554e75deae9d..30f01c72a452 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterFragment.kt @@ -14,6 +14,7 @@ import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.activity_log_type_filter_fragment.* import org.wordpress.android.R import org.wordpress.android.WordPress +import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId import org.wordpress.android.ui.utils.UiHelpers import org.wordpress.android.util.ColorUtils import org.wordpress.android.util.getColorResIdFromAttribute @@ -47,6 +48,7 @@ class ActivityLogTypeFilterFragment : DialogFragment() { super.onViewCreated(view, savedInstanceState) initToolbar(view) initRecyclerView() + viewModel.start(remoteSiteId = RemoteId(requireNotNull(arguments).getLong(WordPress.REMOTE_SITE_ID))) } private fun initToolbar(view: View) { @@ -66,4 +68,13 @@ class ActivityLogTypeFilterFragment : DialogFragment() { private fun initAdapter() { recycler_view.adapter = ActivityLogTypeFilterAdapter(uiHelpers) } + + companion object { + @JvmStatic + fun newInstance(remoteSiteId: RemoteId): ActivityLogTypeFilterFragment { + val args = Bundle() + args.putLong(WordPress.REMOTE_SITE_ID, remoteSiteId.value) + return ActivityLogTypeFilterFragment().apply { arguments = args } + } + } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewModel.kt index c0fd694c1e31..96093993dce1 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewModel.kt @@ -6,6 +6,7 @@ import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.wordpress.android.R +import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId import org.wordpress.android.modules.BG_THREAD import org.wordpress.android.modules.UI_THREAD import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.ListItemUiState.SectionHeader @@ -18,7 +19,6 @@ import org.wordpress.android.ui.utils.UiString.UiStringText import org.wordpress.android.viewmodel.ScopedViewModel import javax.inject.Inject import javax.inject.Named -import kotlin.properties.Delegates class ActivityLogTypeFilterViewModel @Inject constructor( private val dummyActivityTypesProvider: DummyActivityTypesProvider, @@ -26,15 +26,15 @@ class ActivityLogTypeFilterViewModel @Inject constructor( @Named(UI_THREAD) private val mainDispatcher: CoroutineDispatcher ) : ScopedViewModel(mainDispatcher) { private var isStarted = false - private var siteId by Delegates.notNull() + private lateinit var remoteSiteId: RemoteId private val _uiState = MutableLiveData() val uiState: LiveData = _uiState - fun start(siteId: Long) { + fun start(remoteSiteId: RemoteId) { if (isStarted) return isStarted = true - this.siteId = siteId + this.remoteSiteId = remoteSiteId fetchAvailableActivityTypes() } @@ -42,7 +42,7 @@ class ActivityLogTypeFilterViewModel @Inject constructor( private fun fetchAvailableActivityTypes() { launch { _uiState.value = FullscreenLoading - val response = dummyActivityTypesProvider.fetchAvailableActivityTypes(siteId) + val response = dummyActivityTypesProvider.fetchAvailableActivityTypes(remoteSiteId.value) if (response.isError) { _uiState.value = buildErrorUiState() } else { @@ -52,7 +52,7 @@ class ActivityLogTypeFilterViewModel @Inject constructor( } private fun buildErrorUiState() = - UiState.Error(Action(UiStringRes(R.string.retry)).apply { action = ::onRetryClicked }) + UiState.Error(Action(UiStringRes(R.string.retry)).apply { action = ::onRetryClicked }) private suspend fun buildContentUiState(activityTypes: List): Content { return withContext(bgDispatcher) { diff --git a/WordPress/src/main/java/org/wordpress/android/viewmodel/activitylog/ActivityLogViewModel.kt b/WordPress/src/main/java/org/wordpress/android/viewmodel/activitylog/ActivityLogViewModel.kt index 5f0147af5697..5f54880fdf60 100644 --- a/WordPress/src/main/java/org/wordpress/android/viewmodel/activitylog/ActivityLogViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/viewmodel/activitylog/ActivityLogViewModel.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.Observer import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.launch import org.wordpress.android.R +import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId import org.wordpress.android.fluxc.model.SiteModel import org.wordpress.android.fluxc.model.activity.ActivityLogModel import org.wordpress.android.fluxc.model.activity.RewindStatusModel.Rewind.Status @@ -66,8 +67,8 @@ class ActivityLogViewModel @Inject constructor( val showRewindDialog: LiveData get() = _showRewindDialog - private val _showActivityTypeFilterDialog = SingleLiveEvent() - val showActivityTypeFilterDialog: LiveData + private val _showActivityTypeFilterDialog = SingleLiveEvent() + val showActivityTypeFilterDialog: LiveData get() = _showActivityTypeFilterDialog private val _showDateRangePicker = SingleLiveEvent() @@ -173,7 +174,7 @@ class ActivityLogViewModel @Inject constructor( } fun onActivityTypeFilterClicked() { - _showActivityTypeFilterDialog.value = Unit + _showActivityTypeFilterDialog.value = RemoteId(site.siteId) } fun onRewindConfirmed(rewindId: String) { diff --git a/WordPress/src/test/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewModelTest.kt index 3fb79c1de532..ddb91cafc4be 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewModelTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewModelTest.kt @@ -11,6 +11,7 @@ import org.junit.Test import org.mockito.Mock import org.wordpress.android.BaseUnitTest import org.wordpress.android.TEST_DISPATCHER +import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId import org.wordpress.android.test import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.ListItemUiState import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState @@ -31,7 +32,7 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { fun `fullscreen loading shown, when screen initialized`() = test { val uiStates = init().uiStates - viewModel.start(0L) + startVM() assertThat(uiStates[0]).isInstanceOf(UiState.FullscreenLoading::class.java) } @@ -40,7 +41,7 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { fun `available activity types fetched, when screen initialized, when content shown`() = test { init() - viewModel.start(0L) + startVM() verify(dummyActivityTypesProvider).fetchAvailableActivityTypes(anyOrNull()) } @@ -49,7 +50,7 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { fun `section header gets added as first item in the list, when content shown`() = test { init() - viewModel.start(0L) + startVM() assertThat((viewModel.uiState.value as UiState.Content).items[0]) .isInstanceOf(ListItemUiState.SectionHeader::class.java) @@ -59,7 +60,7 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { fun `content shown, when fetch available activity types completes successfully`() = test { init(successResponse = true) - viewModel.start(0L) + startVM() assertThat(viewModel.uiState.value).isInstanceOf(UiState.Content::class.java) } @@ -68,7 +69,7 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { fun `fullscreen error shown, when fetch available activity types completes with error`() = test { init(successResponse = false) - viewModel.start(0L) + startVM() assertThat(viewModel.uiState.value).isInstanceOf(UiState.Error::class.java) } @@ -76,7 +77,7 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { @Test fun `available activity types fetched, when error retry action invoked`() = test { init(successResponse = false) - viewModel.start(0L) + startVM() (viewModel.uiState.value as UiState.Error).retryAction.action!!.invoke() @@ -86,7 +87,7 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { @Test fun `content shown, when retry succeeds`() = test { init(successResponse = false) - viewModel.start(0L) + startVM() init(successResponse = true) (viewModel.uiState.value as UiState.Error).retryAction.action!!.invoke() @@ -99,7 +100,7 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { val activityTypeCount = 17 // random number init(activityTypeCount = activityTypeCount) - viewModel.start(0L) + startVM() assertThat((viewModel.uiState.value as UiState.Content).items.size).isEqualTo(1 + activityTypeCount) } @@ -121,6 +122,10 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { return Observers((uiStates)) } + private fun startVM() { + viewModel.start(RemoteId(0L)) + } + private fun generateActivityTypes(count: Int): List { return (1..count).asSequence().map { DummyActivityType(it.toString()) }.toList() } diff --git a/WordPress/src/test/java/org/wordpress/android/viewmodel/activitylog/ActivityLogViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/viewmodel/activitylog/ActivityLogViewModelTest.kt index cb5e869c11c6..3045512efa45 100644 --- a/WordPress/src/test/java/org/wordpress/android/viewmodel/activitylog/ActivityLogViewModelTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/viewmodel/activitylog/ActivityLogViewModelTest.kt @@ -13,6 +13,7 @@ import com.nhaarman.mockitokotlin2.whenever import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Before @@ -22,6 +23,7 @@ import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.junit.MockitoJUnitRunner import org.wordpress.android.fluxc.action.ActivityLogAction.FETCH_ACTIVITIES +import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId import org.wordpress.android.fluxc.model.SiteModel import org.wordpress.android.fluxc.model.activity.ActivityLogModel import org.wordpress.android.fluxc.model.activity.RewindStatusModel @@ -348,7 +350,14 @@ class ActivityLogViewModelTest { fun onActivityTypeFilterClickShowsActivityTypeFilter() { viewModel.onActivityTypeFilterClicked() - assertEquals(Unit, viewModel.showActivityTypeFilterDialog.value) + assertNotNull(viewModel.showActivityTypeFilterDialog.value) + } + + @Test + fun onActivityTypeFilterClickRemoteSiteIdIsPassed() { + viewModel.onActivityTypeFilterClicked() + + assertEquals(RemoteId(site.siteId), viewModel.showActivityTypeFilterDialog.value) } private suspend fun assertFetchEvents(canLoadMore: Boolean = false) {