Skip to content

Commit

Permalink
Merge pull request #13450 from wordpress-mobile/issue/13268-remote-si…
Browse files Browse the repository at this point in the history
…te-id

Issue/13268 remote site
  • Loading branch information
malinajirka authored Nov 26, 2020
2 parents 0944c1a + f48a056 commit 7478dd5
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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 }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,31 +19,30 @@ 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,
@Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher,
@Named(UI_THREAD) private val mainDispatcher: CoroutineDispatcher
) : ScopedViewModel(mainDispatcher) {
private var isStarted = false
private var siteId by Delegates.notNull<Long>()
private lateinit var remoteSiteId: RemoteId

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

fun start(siteId: Long) {
fun start(remoteSiteId: RemoteId) {
if (isStarted) return
isStarted = true
this.siteId = siteId
this.remoteSiteId = remoteSiteId

fetchAvailableActivityTypes()
}

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 {
Expand All @@ -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<DummyActivityType>): Content {
return withContext(bgDispatcher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,8 +67,8 @@ class ActivityLogViewModel @Inject constructor(
val showRewindDialog: LiveData<ActivityLogListItem>
get() = _showRewindDialog

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

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

fun onActivityTypeFilterClicked() {
_showActivityTypeFilterDialog.value = Unit
_showActivityTypeFilterDialog.value = RemoteId(site.siteId)
}

fun onRewindConfirmed(rewindId: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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())
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -68,15 +69,15 @@ 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)
}

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

Expand All @@ -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()
Expand All @@ -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)
}
Expand All @@ -121,6 +122,10 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() {
return Observers((uiStates))
}

private fun startVM() {
viewModel.start(RemoteId(0L))
}

private fun generateActivityTypes(count: Int): List<DummyActivityType> {
return (1..count).asSequence().map { DummyActivityType(it.toString()) }.toList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7478dd5

Please sign in to comment.