From 26bfaa6c3d31d664c17a22f59d51b33d66b25fbb Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:19:32 +0100 Subject: [PATCH 01/16] Rename ItemUiState to ListItemUiState --- .../activitylog/list/filter/ActivityLogTypeFilterAdapter.kt | 6 +++--- .../list/filter/ActivityLogTypeFilterViewHolder.kt | 4 ++-- .../list/filter/ActivityLogTypeFilterViewModel.kt | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterAdapter.kt b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterAdapter.kt index a477bb39af42..f67d51fdf588 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterAdapter.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterAdapter.kt @@ -2,11 +2,11 @@ package org.wordpress.android.ui.activitylog.list.filter import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView.Adapter -import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.ItemUiState +import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.ListItemUiState import org.wordpress.android.ui.utils.UiHelpers class ActivityLogTypeFilterAdapter(private val uiHelpers: UiHelpers) : Adapter() { - private val items = mutableListOf() + private val items = mutableListOf() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ActivityLogTypeFilterViewHolder { return ActivityLogTypeFilterViewHolder(parent, uiHelpers) @@ -18,7 +18,7 @@ class ActivityLogTypeFilterAdapter(private val uiHelpers: UiHelpers) : Adapter) { + fun update(newItems: List) { items.clear() items.addAll(newItems) notifyDataSetChanged() diff --git a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewHolder.kt b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewHolder.kt index 16645593e5d7..caed23f338d9 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewHolder.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/activitylog/list/filter/ActivityLogTypeFilterViewHolder.kt @@ -4,7 +4,7 @@ import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import org.wordpress.android.R -import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.ItemUiState +import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.ListItemUiState import org.wordpress.android.ui.utils.UiHelpers class ActivityLogTypeFilterViewHolder( @@ -13,6 +13,6 @@ class ActivityLogTypeFilterViewHolder( ) : RecyclerView.ViewHolder( LayoutInflater.from(parent.context).inflate(R.layout.activity_log_type_filter_item, parent, false) ) { - fun onBind(uiState: ItemUiState) { + fun onBind(uiState: ListItemUiState) { } } 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 8dc1464843b7..8deb5c4534e5 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 @@ -16,5 +16,5 @@ class ActivityLogTypeFilterViewModel @Inject constructor( isStarted = true } - object ItemUiState + object ListItemUiState } From 65063a31121c6fe5f896490b0567219015c22939 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:22:56 +0100 Subject: [PATCH 02/16] Update ListItemUiState --- .../list/filter/ActivityLogTypeFilterViewModel.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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 8deb5c4534e5..f8dbd8a9ef8e 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 @@ -2,6 +2,7 @@ package org.wordpress.android.ui.activitylog.list.filter import kotlinx.coroutines.CoroutineDispatcher import org.wordpress.android.modules.UI_THREAD +import org.wordpress.android.ui.utils.UiString import org.wordpress.android.viewmodel.ScopedViewModel import javax.inject.Inject import javax.inject.Named @@ -16,5 +17,14 @@ class ActivityLogTypeFilterViewModel @Inject constructor( isStarted = true } - object ListItemUiState + sealed class ListItemUiState { + data class SectionHeader( + val title: UiString + ) : ListItemUiState() + + data class ActivityType( + val title: UiString, + val checked: Boolean = false + ) : ListItemUiState() + } } From 9484be34b26ece6838f26532565f951be5f75e61 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:23:34 +0100 Subject: [PATCH 03/16] Init UiState in ActivityLogTypeFilterViewModel --- .../filter/ActivityLogTypeFilterViewModel.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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 f8dbd8a9ef8e..10369e150cba 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 @@ -17,6 +17,27 @@ class ActivityLogTypeFilterViewModel @Inject constructor( isStarted = true } + sealed class UiState { + open val loadingVisibility = false + open val items: List? = null + open val primaryAction: Action? = null + open val secondaryAction: Action? = null + + object FullscreenLoading : UiState() { + override val loadingVisibility: Boolean = true + } + + data class Content( + override val items: List, + override val primaryAction: Action, + override val secondaryAction: Action + ) : UiState() + + data class Action(val label: UiString) { + var action: (() -> Unit)? = null + } + } + sealed class ListItemUiState { data class SectionHeader( val title: UiString From 8432af28c29780207d9f6797793d9021c5130143 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:24:50 +0100 Subject: [PATCH 04/16] Add loading state to Activity type filter screen --- .../list/filter/ActivityLogTypeFilterViewModel.kt | 8 ++++++++ 1 file changed, 8 insertions(+) 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 10369e150cba..a4d9c021cfaa 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 @@ -1,7 +1,10 @@ package org.wordpress.android.ui.activitylog.list.filter +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import kotlinx.coroutines.CoroutineDispatcher import org.wordpress.android.modules.UI_THREAD +import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.FullscreenLoading import org.wordpress.android.ui.utils.UiString import org.wordpress.android.viewmodel.ScopedViewModel import javax.inject.Inject @@ -12,9 +15,14 @@ class ActivityLogTypeFilterViewModel @Inject constructor( ) : ScopedViewModel(mainDispatcher) { private var isStarted = false + private val _uiState = MutableLiveData() + val uiState: LiveData = _uiState + fun start() { if (isStarted) return isStarted = true + + _uiState.value = FullscreenLoading } sealed class UiState { From 8ad5905fb2d48aac0cbd6546b62494f38ede4a02 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:29:18 +0100 Subject: [PATCH 05/16] Invoke fetch available activity types when screen initialized --- .../filter/ActivityLogTypeFilterViewModel.kt | 39 +++++++++++++++++++ WordPress/src/main/res/values/strings.xml | 2 + 2 files changed, 41 insertions(+) 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 a4d9c021cfaa..03d52ecb9a5c 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 @@ -3,14 +3,24 @@ package org.wordpress.android.ui.activitylog.list.filter import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import okhttp3.internal.immutableListOf +import org.wordpress.android.R +import org.wordpress.android.modules.BG_THREAD import org.wordpress.android.modules.UI_THREAD +import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.Action +import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.Content import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.FullscreenLoading import org.wordpress.android.ui.utils.UiString +import org.wordpress.android.ui.utils.UiString.UiStringRes +import org.wordpress.android.ui.utils.UiString.UiStringText import org.wordpress.android.viewmodel.ScopedViewModel import javax.inject.Inject import javax.inject.Named class ActivityLogTypeFilterViewModel @Inject constructor( + @Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher, @Named(UI_THREAD) private val mainDispatcher: CoroutineDispatcher ) : ScopedViewModel(mainDispatcher) { private var isStarted = false @@ -23,6 +33,33 @@ class ActivityLogTypeFilterViewModel @Inject constructor( isStarted = true _uiState.value = FullscreenLoading + fetchAvailableActivityTypes() + } + + private fun fetchAvailableActivityTypes() { + launch { + // TODO malinjir initiate the fetch + onActivityTypesFetched(immutableListOf(DummyActivityType, DummyActivityType, DummyActivityType)) + } + } + + private suspend fun onActivityTypesFetched(activityTypes: List) { + _uiState.value = buildContentUiState(activityTypes) + } + + private suspend fun buildContentUiState(activityTypes: List): Content { + return withContext(bgDispatcher) { + // TODO malinjir replace "it.toString()" with activity type name + val activityTypeListItems: List = activityTypes + .map { + ListItemUiState.ActivityType(title = UiStringText(it.toString())) + } + Content( + activityTypeListItems, + primaryAction = Action(label = UiStringRes(R.string.activity_log_activity_type_filter_apply)), + secondaryAction = Action(label = UiStringRes(R.string.activity_log_activity_type_filter_clear)) + ) + } } sealed class UiState { @@ -56,4 +93,6 @@ class ActivityLogTypeFilterViewModel @Inject constructor( val checked: Boolean = false ) : ListItemUiState() } + + object DummyActivityType } diff --git a/WordPress/src/main/res/values/strings.xml b/WordPress/src/main/res/values/strings.xml index d7a506c47af7..d3dd077a6d5c 100644 --- a/WordPress/src/main/res/values/strings.xml +++ b/WordPress/src/main/res/values/strings.xml @@ -1026,6 +1026,8 @@ Rewind Site Are you sure you want to rewind your site back to %1$s at %2$s? This will remove all content and options created or changed since then. Since you\'re on a free plan, you\'ll see limited events in your activity. + Apply + Clear Scan From a236c462bfcc2635378a198e9472564c7bba528d Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:30:55 +0100 Subject: [PATCH 06/16] Add lambdas for primary and secondary actions --- .../list/filter/ActivityLogTypeFilterViewModel.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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 03d52ecb9a5c..94b4f64797f3 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 @@ -56,12 +56,22 @@ class ActivityLogTypeFilterViewModel @Inject constructor( } Content( activityTypeListItems, - primaryAction = Action(label = UiStringRes(R.string.activity_log_activity_type_filter_apply)), + primaryAction = Action(label = UiStringRes(R.string.activity_log_activity_type_filter_apply)) + .apply { action = ::onApplyClicked }, secondaryAction = Action(label = UiStringRes(R.string.activity_log_activity_type_filter_clear)) + .apply { action = ::onClearClicked } ) } } + private fun onApplyClicked() { + // TODO malinjir save and dismiss + } + + private fun onClearClicked() { + // TODO malinjir uncheck all items + } + sealed class UiState { open val loadingVisibility = false open val items: List? = null From af97ddeb328ebd2c4425a595fc07177ed37493cb Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:40:18 +0100 Subject: [PATCH 07/16] Add logic for onClearClicked --- .../list/filter/ActivityLogTypeFilterViewModel.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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 94b4f64797f3..b94b6bdfae27 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 @@ -69,7 +69,19 @@ class ActivityLogTypeFilterViewModel @Inject constructor( } private fun onClearClicked() { - // TODO malinjir uncheck all items + (_uiState.value as? Content)?.let { it -> + _uiState.value = it.copy(items = uncheckAllActivityTypeItems(it)) + } + } + + private fun uncheckAllActivityTypeItems(it: Content): List { + return it.items.map { item -> + if (item is ListItemUiState.ActivityType) { + item.copy(checked = false) + } else { + item + } + } } sealed class UiState { From ac3c2191a68d01b44283bcb0010e08d5e02f74bd Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:41:08 +0100 Subject: [PATCH 08/16] Add section header --- .../list/filter/ActivityLogTypeFilterViewModel.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 b94b6bdfae27..ade9ff24c404 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 @@ -9,6 +9,7 @@ import okhttp3.internal.immutableListOf import org.wordpress.android.R 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 import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.Action import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.Content import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.FullscreenLoading @@ -49,13 +50,15 @@ class ActivityLogTypeFilterViewModel @Inject constructor( private suspend fun buildContentUiState(activityTypes: List): Content { return withContext(bgDispatcher) { + // TODO malinjir replace the hardcoded header title + val headerListItem = SectionHeader(UiStringText("Test")) // TODO malinjir replace "it.toString()" with activity type name val activityTypeListItems: List = activityTypes .map { ListItemUiState.ActivityType(title = UiStringText(it.toString())) } Content( - activityTypeListItems, + listOf(headerListItem) + activityTypeListItems, primaryAction = Action(label = UiStringRes(R.string.activity_log_activity_type_filter_apply)) .apply { action = ::onApplyClicked }, secondaryAction = Action(label = UiStringRes(R.string.activity_log_activity_type_filter_clear)) From 7d120231ed34f9f18f436ef2569132b63ff7066d Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:44:40 +0100 Subject: [PATCH 09/16] Add basic tests for ActivityLogTypeFilterViewModel --- .../ActivityLogTypeFilterViewModelTest.kt | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) 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 9cc956f02032..fd5d44e5f9a2 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 @@ -1,10 +1,15 @@ package org.wordpress.android.ui.activitylog.list.filter import kotlinx.coroutines.InternalCoroutinesApi +import org.assertj.core.api.Assertions.assertThat import org.junit.Before import org.junit.Test import org.wordpress.android.BaseUnitTest import org.wordpress.android.TEST_DISPATCHER +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 +import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.Content @InternalCoroutinesApi class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { @@ -12,11 +17,34 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { @Before fun setUp() { - viewModel = ActivityLogTypeFilterViewModel(TEST_DISPATCHER) + viewModel = ActivityLogTypeFilterViewModel(TEST_DISPATCHER, TEST_DISPATCHER) } @Test - fun `skeleton test`() { - // Skeleton test. + fun `fullscreen loading shown, when screen initialized`() = test { + val uiStates = initObservers().uiStates + + viewModel.start() + + assertThat(uiStates[0]).isInstanceOf(UiState.FullscreenLoading::class.java) + } + + @Test + fun `section header gets added as first item in the list`() { + val uiStates = initObservers().uiStates + + viewModel.start() + + assertThat((viewModel.uiState.value as Content).items[0]).isInstanceOf(ListItemUiState.SectionHeader::class.java) } + + private fun initObservers(): Observers { + val uiStates = mutableListOf() + viewModel.uiState.observeForever { + uiStates.add(it) + } + return Observers((uiStates)) + } + + private data class Observers(val uiStates: List) } From 842fa18f83be757ab3e19a3e6349067387038621 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 10:58:55 +0100 Subject: [PATCH 10/16] Fix lint --- .../list/filter/ActivityLogTypeFilterViewModelTest.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 fd5d44e5f9a2..51c95087fae5 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 @@ -35,7 +35,8 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { viewModel.start() - assertThat((viewModel.uiState.value as Content).items[0]).isInstanceOf(ListItemUiState.SectionHeader::class.java) + assertThat((viewModel.uiState.value as Content).items[0]) + .isInstanceOf(ListItemUiState.SectionHeader::class.java) } private fun initObservers(): Observers { From 02f4933e7b7abdc90d8fc9ac26c42f939c19081e Mon Sep 17 00:00:00 2001 From: malinajirka Date: Fri, 20 Nov 2020 11:55:45 +0100 Subject: [PATCH 11/16] Update UiState structure --- .../list/filter/ActivityLogTypeFilterViewModel.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 ade9ff24c404..bd62eda19eb0 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 @@ -88,20 +88,20 @@ class ActivityLogTypeFilterViewModel @Inject constructor( } sealed class UiState { + open val contentVisibility = false open val loadingVisibility = false - open val items: List? = null - open val primaryAction: Action? = null - open val secondaryAction: Action? = null object FullscreenLoading : UiState() { override val loadingVisibility: Boolean = true } data class Content( - override val items: List, - override val primaryAction: Action, - override val secondaryAction: Action - ) : UiState() + val items: List, + val primaryAction: Action, + val secondaryAction: Action + ) : UiState() { + override val contentVisibility = true + } data class Action(val label: UiString) { var action: (() -> Unit)? = null From bdd76bc8fe7da0926b45b6610741d6df6aec9256 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Mon, 23 Nov 2020 12:30:12 +0100 Subject: [PATCH 12/16] Remove usage of okHttp's internal method --- .../activitylog/list/filter/ActivityLogTypeFilterViewModel.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 bd62eda19eb0..79594f4df8ac 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 @@ -5,7 +5,6 @@ import androidx.lifecycle.MutableLiveData import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import okhttp3.internal.immutableListOf import org.wordpress.android.R import org.wordpress.android.modules.BG_THREAD import org.wordpress.android.modules.UI_THREAD @@ -40,7 +39,7 @@ class ActivityLogTypeFilterViewModel @Inject constructor( private fun fetchAvailableActivityTypes() { launch { // TODO malinjir initiate the fetch - onActivityTypesFetched(immutableListOf(DummyActivityType, DummyActivityType, DummyActivityType)) + onActivityTypesFetched(listOf(DummyActivityType, DummyActivityType, DummyActivityType)) } } From 17735265601558f8800973ad76cf2298c187de42 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Mon, 23 Nov 2020 12:32:22 +0100 Subject: [PATCH 13/16] Refactor method in ActivityLogTypeFilterViewModel --- .../filter/ActivityLogTypeFilterViewModel.kt | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) 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 79594f4df8ac..fe0b35a7236b 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 @@ -53,9 +53,7 @@ class ActivityLogTypeFilterViewModel @Inject constructor( val headerListItem = SectionHeader(UiStringText("Test")) // TODO malinjir replace "it.toString()" with activity type name val activityTypeListItems: List = activityTypes - .map { - ListItemUiState.ActivityType(title = UiStringText(it.toString())) - } + .map { ListItemUiState.ActivityType(title = UiStringText(it.toString())) } Content( listOf(headerListItem) + activityTypeListItems, primaryAction = Action(label = UiStringRes(R.string.activity_log_activity_type_filter_apply)) @@ -72,19 +70,18 @@ class ActivityLogTypeFilterViewModel @Inject constructor( private fun onClearClicked() { (_uiState.value as? Content)?.let { it -> - _uiState.value = it.copy(items = uncheckAllActivityTypeItems(it)) + _uiState.value = it.copy(items = getAllActivityTypeItemsUnchecked(it.items)) } } - private fun uncheckAllActivityTypeItems(it: Content): List { - return it.items.map { item -> - if (item is ListItemUiState.ActivityType) { - item.copy(checked = false) - } else { - item + private fun getAllActivityTypeItemsUnchecked(listItemUiStates: List): List = + listItemUiStates.map { item -> + if (item is ListItemUiState.ActivityType) { + item.copy(checked = false) + } else { + item + } } - } - } sealed class UiState { open val contentVisibility = false From 73eb6e713a71f4483f9ec6d238e3d5b761763130 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Mon, 23 Nov 2020 12:35:23 +0100 Subject: [PATCH 14/16] Move Action outside of UiState --- .../list/filter/ActivityLogTypeFilterViewModel.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 fe0b35a7236b..73cf15decc55 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 @@ -9,7 +9,6 @@ import org.wordpress.android.R 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 -import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.Action import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.Content import org.wordpress.android.ui.activitylog.list.filter.ActivityLogTypeFilterViewModel.UiState.FullscreenLoading import org.wordpress.android.ui.utils.UiString @@ -99,9 +98,6 @@ class ActivityLogTypeFilterViewModel @Inject constructor( override val contentVisibility = true } - data class Action(val label: UiString) { - var action: (() -> Unit)? = null - } } sealed class ListItemUiState { @@ -115,5 +111,9 @@ class ActivityLogTypeFilterViewModel @Inject constructor( ) : ListItemUiState() } + data class Action(val label: UiString) { + var action: (() -> Unit)? = null + } + object DummyActivityType } From 416893abd3da953154503e839c77a482efbec525 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Mon, 23 Nov 2020 12:36:08 +0100 Subject: [PATCH 15/16] Refactor test in ActivityLogTypeFilterViewModelTest --- .../list/filter/ActivityLogTypeFilterViewModelTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 51c95087fae5..50888b363df8 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 @@ -30,8 +30,8 @@ class ActivityLogTypeFilterViewModelTest : BaseUnitTest() { } @Test - fun `section header gets added as first item in the list`() { - val uiStates = initObservers().uiStates + fun `section header gets added as first item in the list, when content shown`() { + initObservers() viewModel.start() From 13bbab592fd934303264db59898bcb907c204d52 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Mon, 23 Nov 2020 12:43:24 +0100 Subject: [PATCH 16/16] Fix lint --- .../ui/activitylog/list/filter/ActivityLogTypeFilterViewModel.kt | 1 - 1 file changed, 1 deletion(-) 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 73cf15decc55..7bbca768371d 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 @@ -97,7 +97,6 @@ class ActivityLogTypeFilterViewModel @Inject constructor( ) : UiState() { override val contentVisibility = true } - } sealed class ListItemUiState {