Skip to content

Commit

Permalink
remove "show more" button from image selector form (#1692)
Browse files Browse the repository at this point in the history
  • Loading branch information
westnordost committed Mar 3, 2020
1 parent e9a22fa commit 5c2a903
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import kotlinx.android.synthetic.main.quest_generic_list.*
* I is the type of each item in the image list (a simple model object). In MVC, this would
* be the view model.
*
* T is the type of the answer object (also a simple model object) created by the quest
* T is the type of the answer object (also a simple model object) created by the quest
* form and consumed by the quest type. In MVC, this would be the model.
*/
abstract class AImageListQuestAnswerFragment<I,T> : AbstractQuestFormAnswerFragment<T>() {
Expand All @@ -34,8 +34,6 @@ abstract class AImageListQuestAnswerFragment<I,T> : AbstractQuestFormAnswerFragm
protected open val itemsPerRow = 4
/** return -1 for any number. Default: 1 */
protected open val maxSelectableItems = 1
/** return -1 for showing all items at once. Default: -1 */
protected open val maxNumberOfInitiallyShownItems = -1
/** return true to move last picked items to the front. On by default. Only respected if the
* items do not all fit into one line */
protected open val moveFavoritesToFront = true
Expand Down Expand Up @@ -70,20 +68,12 @@ abstract class AImageListQuestAnswerFragment<I,T> : AbstractQuestFormAnswerFragm
}
})

showMoreButton.setOnClickListener {
imageSelector.items = moveFavouritesToFront(items)
showMoreButton.visibility = View.GONE
}

var initiallyShow = maxNumberOfInitiallyShownItems
showMoreButton.visibility = View.GONE

imageSelector.items = moveFavouritesToFront(items)
if (savedInstanceState != null) {
if (savedInstanceState.getBoolean(EXPANDED)) initiallyShow = -1
showItems(initiallyShow)

val selectedIndices = savedInstanceState.getIntegerArrayList(SELECTED_INDICES)!!
imageSelector.select(selectedIndices)
} else {
showItems(initiallyShow)
}
list.adapter = imageSelector
}
Expand All @@ -102,20 +92,10 @@ abstract class AImageListQuestAnswerFragment<I,T> : AbstractQuestFormAnswerFragm
super.onSaveInstanceState(outState)
// note: the view might be not available anymore at this point!
outState.putIntegerArrayList(SELECTED_INDICES, ArrayList(imageSelector.selectedIndices))
outState.putBoolean(EXPANDED, showMoreButton?.visibility == View.GONE)
}

override fun isFormComplete() = imageSelector.selectedIndices.isNotEmpty()

private fun showItems(initiallyShow: Int) {
val allItems = items
val showAll = initiallyShow == -1 || initiallyShow >= allItems.size

showMoreButton.visibility = if(showAll) View.GONE else View.VISIBLE
val sortedItems = moveFavouritesToFront(allItems)
imageSelector.items = if(showAll) sortedItems else sortedItems.subList(0, initiallyShow)
}

private fun moveFavouritesToFront(originalList: List<Item<I>>): List<Item<I>> {
val result: LinkedList<Item<I>> = LinkedList(originalList)

Expand All @@ -127,6 +107,5 @@ abstract class AImageListQuestAnswerFragment<I,T> : AbstractQuestFormAnswerFragm

companion object {
private const val SELECTED_INDICES = "selected_indices"
private const val EXPANDED = "expanded"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class AddFireHydrantTypeForm : AImageListQuestAnswerFragment<String, String>() {
)

override val itemsPerRow = 2
override val maxNumberOfInitiallyShownItems = 2

override fun onClickOk(selectedItems: List<String>) {
applyAnswer(selectedItems.single())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class AddOrchardProduceForm : AImageListQuestAnswerFragment<String, List<String>
override val items get() = countryInfo.orchardProduces.mapNotNull { producesMap[it] }

override val itemsPerRow = 3
override val maxNumberOfInitiallyShownItems = -1
override val maxSelectableItems = -1

override fun onClickOk(selectedItems: List<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class AddReligionForm : AImageListQuestAnswerFragment<String,String>() {
Item("caodaism", R.drawable.ic_religion_caodaist, R.string.quest_religion_caodaist) // Vietnam
).sortedBy(countryInfo.popularReligions)

override val maxNumberOfInitiallyShownItems = 4

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
imageSelector.cellLayoutId = R.layout.cell_icon_select_with_label_below
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class AddRoofShapeForm : AImageListQuestAnswerFragment<String, String>() {
Item("cone", R.drawable.ic_roof_cone)
)

override val maxNumberOfInitiallyShownItems = 8

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
imageSelector.cellLayoutId = R.layout.cell_labeled_icon_select
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class AddSportForm : AImageListQuestAnswerFragment<String, List<String>>() {
).sortedBy(countryInfo.popularSports)

override val maxSelectableItems = -1
override val maxNumberOfInitiallyShownItems = 8

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down

0 comments on commit 5c2a903

Please sign in to comment.