Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

house name and house number answer at once #3582

Merged
merged 12 commits into from
Dec 12, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ class AddHousenumber : OsmElementQuestType<HousenumberAnswer> {
WrongBuildingType -> {
changes.modify("building", "yes")
}
is HouseNameAndHouseNumber -> {
changes.add("addr:housenumber", answer.number)
changes.add("addr:housename", answer.name)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class AddHousenumberForm : AbstractQuestFormAnswerFragment<HousenumberAnswer>()

override val otherAnswers = listOf(
AnswerItem(R.string.quest_address_answer_no_housenumber) { onNoHouseNumber() },
AnswerItem(R.string.quest_address_answer_house_name_and_housenumber) { switchToHouseNameAndHouseNumber() },
AnswerItem(R.string.quest_address_answer_house_name) { switchToHouseName() },
AnswerItem(R.string.quest_housenumber_multiple_numbers) { showMultipleNumbersHint() }
)
Expand All @@ -40,22 +41,26 @@ class AddHousenumberForm : AbstractQuestFormAnswerFragment<HousenumberAnswer>()
private var addButton: View? = null
private var subtractButton: View? = null

private var isHousename: Boolean = false
enum class InterfaceMode {
HOUSENUMBER, HOUSENAME, HOUSENUMBER_AND_HOUSENAME
}
private var interfaceMode: InterfaceMode = InterfaceMode.HOUSENUMBER

private var houseNumberInputTextColors: ColorStateList? = null

private val isShowingHouseNumberHint: Boolean get() = houseNumberInputTextColors != null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = super.onCreateView(inflater, container, savedInstanceState)
isHousename = savedInstanceState?.getBoolean(IS_HOUSENAME) ?: false
setLayout(if(isHousename) R.layout.quest_housename else R.layout.quest_housenumber)
val prevMode = savedInstanceState?.getString(INTERFACE_MODE)?.let { InterfaceMode.valueOf(it) }
interfaceMode = prevMode ?: InterfaceMode.HOUSENUMBER
setLayoutBasedOnInterfaceMode()
return view
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(IS_HOUSENAME, isHousename)
outState.putString(INTERFACE_MODE, interfaceMode.name)
}

override fun onClickOk() {
Expand All @@ -67,13 +72,23 @@ class AddHousenumberForm : AbstractQuestFormAnswerFragment<HousenumberAnswer>()
}
}

override fun isFormComplete() = (!isShowingHouseNumberHint || isHousename) && createAnswer() != null
override fun isFormComplete() = when(interfaceMode) {
InterfaceMode.HOUSENUMBER -> !isShowingHouseNumberHint
InterfaceMode.HOUSENAME -> createAnswer() != null
InterfaceMode.HOUSENUMBER_AND_HOUSENAME -> houseNameInput?.nonEmptyInput != null && houseNumberInput?.nonEmptyInput != null
}

/* ------------------------------------- Other answers -------------------------------------- */

private fun switchToHouseName() {
isHousename = true
setLayout(R.layout.quest_housename)
interfaceMode = InterfaceMode.HOUSENAME
setLayoutBasedOnInterfaceMode()
houseNameInput?.requestFocus()
}

private fun switchToHouseNameAndHouseNumber() {
interfaceMode = InterfaceMode.HOUSENUMBER_AND_HOUSENAME
setLayoutBasedOnInterfaceMode()
houseNameInput?.requestFocus()
}

Expand Down Expand Up @@ -110,6 +125,16 @@ class AddHousenumberForm : AbstractQuestFormAnswerFragment<HousenumberAnswer>()

/* -------------------------- Set (different) housenumber layout --------------------------- */

private fun setLayoutBasedOnInterfaceMode() {
setLayout(
when (interfaceMode) {
InterfaceMode.HOUSENUMBER -> R.layout.quest_housenumber
InterfaceMode.HOUSENAME -> R.layout.quest_housename
InterfaceMode.HOUSENUMBER_AND_HOUSENAME -> R.layout.quest_housename_and_housenumber
}
)
}

private fun setLayout(layoutResourceId: Int) {
val view = setContentView(layoutResourceId)

Expand Down Expand Up @@ -240,27 +265,32 @@ class AddHousenumberForm : AbstractQuestFormAnswerFragment<HousenumberAnswer>()
}
}

private fun createAnswer(): HousenumberAnswer? =
if (houseNameInput != null) {
houseNameInput?.nonEmptyInput?.let { HouseName(it) }
}
else if (conscriptionNumberInput != null && streetNumberInput != null) {
conscriptionNumberInput?.nonEmptyInput?.let { conscriptionNumber ->
val streetNumber = streetNumberInput?.nonEmptyInput // streetNumber is optional
ConscriptionNumber(conscriptionNumber, streetNumber)
}
}
else if (blockNumberInput != null && houseNumberInput != null) {
blockNumberInput?.nonEmptyInput?.let { blockNumber ->
houseNumberInput?.nonEmptyInput?.let { houseNumber ->
HouseAndBlockNumber(houseNumber, blockNumber)
}
}
}
else if (houseNumberInput != null) {
houseNumberInput?.nonEmptyInput?.let { HouseNumber(it) }
override fun isRejectingClose(): Boolean {
val houseName = houseNameInput?.nonEmptyInput
val houseNumber = houseNumberInput?.nonEmptyInput
val conscriptionNumber = conscriptionNumberInput?.nonEmptyInput
val streetNumber = streetNumberInput?.nonEmptyInput
val blockNumber = blockNumberInput?.nonEmptyInput

return listOf(houseName, houseNumber, conscriptionNumber, streetNumber, blockNumber).any { it != null }
}

private fun createAnswer(): HousenumberAnswer? {
val houseName = houseNameInput?.nonEmptyInput
val houseNumber = houseNumberInput?.nonEmptyInput
val conscriptionNumber = conscriptionNumberInput?.nonEmptyInput
val streetNumber = streetNumberInput?.nonEmptyInput
val blockNumber = blockNumberInput?.nonEmptyInput

return when {
houseName != null && houseNumber != null -> HouseNameAndHouseNumber(houseName, houseNumber)
houseName != null -> HouseName(houseName)
conscriptionNumber != null -> ConscriptionNumber(conscriptionNumber, streetNumber) // streetNumber is optional
blockNumber != null && houseNumber != null -> HouseAndBlockNumber(houseNumber, blockNumber)
houseNumber != null -> HouseNumber(houseNumber)
else -> null
}
else null
}

private val EditText.nonEmptyInput:String? get() {
val input = text.toString().trim()
Expand All @@ -270,7 +300,7 @@ class AddHousenumberForm : AbstractQuestFormAnswerFragment<HousenumberAnswer>()
companion object {
private var lastRealHousenumberAnswer: HousenumberAnswer? = null

private const val IS_HOUSENAME = "is_housename"
private const val INTERFACE_MODE = "interface_mode"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ sealed class HousenumberAnswer
data class ConscriptionNumber(val number: String, val streetNumber: String? = null) : HousenumberAnswer()
data class HouseNumber(val number: String) : HousenumberAnswer()
data class HouseName(val name: String) : HousenumberAnswer()
data class HouseNameAndHouseNumber(val name: String, val number: String) : HousenumberAnswer()
data class HouseAndBlockNumber(val houseNumber: String, val blockNumber: String) : HousenumberAnswer()
object NoHouseNumber : HousenumberAnswer()
object WrongBuildingType : HousenumberAnswer()
67 changes: 67 additions & 0 deletions app/src/main/res/layout/quest_housename_and_housenumber.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/space_8dp"
android:showDividers="middle">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/quest_address_house_name_label"/>

<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:background="@drawable/background_white_corners"
>

<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/houseNameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_housenumber_frame"
app:backgroundTint="@color/traffic_blue"
android:inputType="textCapWords"
android:imeOptions="actionDone"
tools:text="My Cool House"
style="@style/HousenumberEditText"
/>

</me.grantland.widget.AutofitLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/quest_address_house_number_label"/>

<me.grantland.widget.AutofitLayout
android:id="@+id/houseNumberInputContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:background="@drawable/background_white_corners">

<EditText
android:id="@+id/houseNumberInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_housenumber_frame"
app:backgroundTint="@color/traffic_blue"
android:imeOptions="actionDone"
android:textColorHint="#2fff"
android:digits="0123456789.,- /"
style="@style/HousenumberEditText"
/>

</me.grantland.widget.AutofitLayout>

</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ Otherwise, you can download another keyboard in the app store. Popular keyboards
<string name="quest_powerPolesMaterial_concrete">Concrete</string>
<string name="quest_maxspeed_answer_advisory_speed_limit">Just an advisory speed limit…</string>
<string name="quest_address_answer_house_name">No house number but a name…</string>
<string name="quest_address_answer_house_name_and_housenumber">Both a house number and a name…</string>
<string name="quest_address_house_name_label">House name:</string>
<string name="quest_address_house_number_label">House number:</string>
<string name="quest_buildingLevels_roofLevelsLabel2">levels in the roof</string>
<string name="quest_buildingLevels_levelsLabel2">regular levels (not counting the roof)</string>
<string name="quest_cycleway_value_track">cycle track</string>
Expand Down