Skip to content

Commit ecd27f1

Browse files
committed
Don't show local-only pages in Page Parent selection list
Even though the PageParent screen is not called when the device is offline, it's better to put an additional safe-guard in case this page is opened when there are pages that are local-only (created while device was offline and not published remotely yet), as they cannot be used as parent to other pages. So the changes here filter out local-only pages (the ones that have negative remote IDs).
1 parent 239020e commit ecd27f1

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageParentViewModel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ class PageParentViewModel
107107
)
108108

109109
val choices = pageStore.getPagesFromDb(site)
110-
.filter { it.remoteId != pageId && it.status == PUBLISHED }
110+
.filter {
111+
// negative local page ID used as a temp remote post ID for local-only pages and can't be assigned
112+
// as parent to other pages properly, better to filter them out
113+
it.remoteId > 0 && it.remoteId != pageId && it.status == PUBLISHED
114+
}
111115
val parentChoices = choices.filter { isNotChild(it, choices) }
112116
if (parentChoices.isNotEmpty()) {
113117
parents.add(Divider(resourceProvider.getString(R.string.pages)))

WordPress/src/test/java/org/wordpress/android/viewmodel/pages/PageParentViewModelTest.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ class PageParentViewModelTest {
7575
}
7676

7777
@Test
78-
fun `page list shows all pages for newly created page that was never saved (no id)`() {
79-
val allPageIds = fakePublishedPageList()
78+
fun `page list shows all pages except local-only pages for newly created page that was never saved (no id)`() {
79+
val validIds = fakePublishedPageList()
8080
.map { it.remoteId }
81+
.filterNot { it < 0 }
8182

8283
viewModel.start(site, 0L)
8384

@@ -86,16 +87,16 @@ class PageParentViewModelTest {
8687
?: emptyList()
8788

8889
assertThat(shownPageIds).first().isEqualTo(0L)
89-
assertThat(shownPageIds).containsAll(allPageIds)
90+
assertThat(shownPageIds).containsAll(validIds)
9091
}
9192

9293
@Test
93-
fun `page list shows all pages except for itself and children`() {
94+
fun `page list shows all pages except for itself, children and local-only pages`() {
9495
val pageId = 3L
9596
val childrenIds = listOf(4L, 5L)
9697
val validIds = fakePublishedPageList()
9798
.map { it.remoteId }
98-
.filterNot { it == pageId || it in childrenIds }
99+
.filterNot { it < 0 || it == pageId || it in childrenIds }
99100

100101
viewModel.start(site, pageId)
101102

@@ -165,8 +166,9 @@ class PageParentViewModelTest {
165166
val page3 = fakePublishedPageModel(3L, "Page 3")
166167
val page3A = fakePublishedPageModel(4L, "Page 3A", parent = page3)
167168
val page3B = fakePublishedPageModel(5L, "Page 3B", parent = page3A)
169+
val localPage = fakePublishedPageModel(-1L, "Local Page")
168170

169-
return listOf(page1, page2, page3, page3A, page3B)
171+
return listOf(page1, page2, page3, page3A, page3B, localPage)
170172
}
171173

172174
private fun fakePublishedPageModel(id: Long, title: String, parent: PageModel? = null): PageModel {

0 commit comments

Comments
 (0)