Skip to content

Commit

Permalink
Merge pull request #4215 from matkoniecz/seamless_buildings
Browse files Browse the repository at this point in the history
buildings type selection in one list
  • Loading branch information
westnordost authored Sep 1, 2022
2 parents e517f2e + 9dbf3d3 commit a03f1f1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ abstract class AGroupedImageListQuestForm<I, T> : AbstractOsmQuestForm<T>() {
binding.list.layoutManager = imageSelector.gridLayoutManager
binding.list.isNestedScrollingEnabled = false

binding.showMoreButton.setOnClickListener {
imageSelector.items = allItems
binding.showMoreButton.visibility = View.GONE
}

binding.selectHintLabel.setText(R.string.quest_select_hint_most_specific)

imageSelector.listeners.add { checkIsFormComplete() }
Expand All @@ -82,7 +77,8 @@ abstract class AGroupedImageListQuestForm<I, T> : AbstractOsmQuestForm<T>() {
})
checkIsFormComplete()

imageSelector.items = getInitialItems()
imageSelector.items = getInitialItems() + allItems

binding.list.adapter = imageSelector
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ abstract class AImageListQuestForm<I, T> : AbstractOsmQuestForm<T>() {
}
})

binding.showMoreButton.visibility = View.GONE

imageSelector.items = moveFavouritesToFront(items)
if (savedInstanceState != null) {
val selectedIndices = savedInstanceState.getIntegerArrayList(SELECTED_INDICES)!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,25 @@ class GroupedImageSelectAdapter<T>(val gridLayoutManager: GridLayoutManager) :
var selectedItem: GroupableDisplayItem<T>? = null
private set

private val selectedIndex get() = selectedItem?.let { _items.indexOf(it) } ?: -1
// group is also remembered as item may be appearing
// once outside any groups and again in the group
private var selectedItemGroup: GroupableDisplayItem<T>? = null

private val selectedIndex: Int get() =
selectedItem?.let { indexOfItemGivenGroupMembership(it, selectedItemGroup) } ?: -1

private fun indexOfItemGivenGroupMembership(item: GroupableDisplayItem<T>, group: GroupableDisplayItem<T>?): Int {
var currentGroup: GroupableDisplayItem<T> ? = null
for (i in 0 until _items.size) {
if (_items[i].isGroup) {
currentGroup = _items[i]
}
if (item == _items[i] && group == currentGroup) {
return i
}
}
return -1
}

val listeners = mutableListOf<(GroupableDisplayItem<T>?) -> Unit>()

Expand All @@ -51,36 +69,36 @@ class GroupedImageSelectAdapter<T>(val gridLayoutManager: GridLayoutManager) :

override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.bind(_items[position])
holder.isSelected = selectedItem?.let { _items.indexOf(it) == position } == true
holder.isGroupExpanded = getGroup(selectedIndex) == position
val selectedIndex = selectedIndex
holder.isSelected = selectedIndex == position
holder.isGroupExpanded = getGroupIndex(selectedIndex) == position
}

private fun toggle(index: Int) {
val prevSelectedItem = selectedItem
if (selectedItem == null || prevSelectedItem !== _items[index]) {
selectedItem = _items[index]
} else {
selectedItem = null
}

val selectedItem = selectedItem
if (prevSelectedItem != null) {
val prevSelectedIndex = _items.indexOf(prevSelectedItem)
notifyItemChanged(prevSelectedIndex)

val previousGroupIndex = getGroup(prevSelectedIndex)
if (previousGroupIndex != -1) {
if (selectedItem == null || previousGroupIndex != getGroup(_items.indexOf(selectedItem))) {
val previousItem = selectedItem
val previousGroup = selectedItemGroup

val foundGroupIndex = getGroupIndex(index)
selectedItemGroup = if (foundGroupIndex == -1) null else _items[foundGroupIndex]
selectedItem = _items[index]

if (previousItem != null) {
val previousItemIndex = indexOfItemGivenGroupMembership(previousItem, previousGroup)
val previousGroupIndex = getGroupIndex(previousItemIndex)
notifyItemChanged(previousItemIndex)
if (previousGroup != null) {
if (selectedItem == null || previousGroup != selectedItemGroup) {
retractGroup(previousGroupIndex)
}
}
}
val selectedItem = selectedItem
if (selectedItem != null) {
val selectedIndex = _items.indexOf(selectedItem)
val selectedIndex = selectedIndex
notifyItemChanged(selectedIndex)

if (selectedItem.isGroup) {
if (prevSelectedItem == null || getGroup(_items.indexOf(prevSelectedItem)) != selectedIndex) {
if (previousItem == null || previousGroup != selectedItemGroup) {
expandGroup(selectedIndex)
}
}
Expand All @@ -90,7 +108,7 @@ class GroupedImageSelectAdapter<T>(val gridLayoutManager: GridLayoutManager) :
}
}

private fun getGroup(index: Int): Int {
private fun getGroupIndex(index: Int): Int {
for (i in index downTo 0) {
if (_items[i].isGroup) return i
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:padding="4dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:background="@drawable/image_select_cell_rounded">

<ImageView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground">

<View
android:id="@+id/topDivider"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/divider"/>

<RelativeLayout
android:id="@+id/group_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/topDivider"
android:layout_marginTop="4dp"
android:layout_marginBottom="1dp"
android:padding="4dp"
android:background="@drawable/image_select_cell_rounded">

Expand Down Expand Up @@ -57,10 +65,4 @@
android:layout_alignParentEnd="true"/>

</RelativeLayout>

<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_below="@id/group_container"
android:background="@color/divider"/>
</RelativeLayout>
7 changes: 0 additions & 7 deletions app/src/main/res/layout/quest_generic_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,4 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/showMoreButton"
android:text="@string/quest_roofShape_show_more"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"/>

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/raw/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ v46.0: |
<li>Tactile paving quests now enabled in Portugal (#4325)</li>
<li>Internet access: Do not ask for backcountry campsites</li>
<li>Steps incline: Fix display issue on Android 5 (#4317)</li>
<li>Buildings: Remove "show more" button (#3387, #4215), by @matkoniecz</li>
</ul>
<h3>Other enhancements and fixes</h3>
<ul>
Expand Down

0 comments on commit a03f1f1

Please sign in to comment.