Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoniecz committed Jul 31, 2019
1 parent 96d0684 commit a335e76
Showing 1 changed file with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package de.westnordost.streetcomplete.quests.address

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.quests.AbstractQuestFormAnswerFragment
Expand All @@ -16,7 +19,43 @@ class AddAddressStreetForm : AbstractQuestFormAnswerFragment<AddressStreetAnswer
OtherAnswer(R.string.quest_address_street_no_named_streets) { confirmNoName() }
)

private val placeName get() = nameInput?.text?.toString().orEmpty().trim()
private var streetNameInput: EditText? = null
private var placeNameInput: EditText? = null

private val locationName get() = nameInput?.text?.toString().orEmpty().trim()

private var isPlaceName: Boolean = false

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = super.onCreateView(inflater, container, savedInstanceState)

isPlaceName = savedInstanceState?.getBoolean(AddAddressStreetForm.IS_PLACE_NAME) ?: false
setLayout(if(isPlaceName) R.layout.quest_housename else R.layout.quest_housenumber)

return view
}

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

streetNameInput = view.findViewById(R.id.houseNumberInput)
placeNameInput = view.findViewById(R.id.houseNameInput)

val onChanged = TextChangedWatcher { checkIsFormComplete() }
houseNumberInput?.addTextChangedListener(onChanged)
houseNameInput?.addTextChangedListener(onChanged)

// streetNumber is always optional
val input = AddHousenumberForm.getFirstNonNull(blockNumberInput, houseNumberInput, houseNameInput, conscriptionNumberInput)
input?.requestFocus()

initKeyboardButton(view)
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(AddHousenumberForm.IS_HOUSENAME, isHousename)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -25,16 +64,24 @@ class AddAddressStreetForm : AbstractQuestFormAnswerFragment<AddressStreetAnswer
}

override fun onClickOk() {
applyAnswer(StreetName(placeName))
applyAnswer(StreetName(locationName))
}

private fun confirmNoName() {
AlertDialog.Builder(activity!!)
.setTitle(R.string.quest_name_answer_noName_confirmation_title)
.setPositiveButton(R.string.quest_name_noName_confirmation_positive) { _, _ -> applyAnswer(PlaceName("TODO")) } //TODO!
.setPositiveButton(R.string.quest_name_noName_confirmation_positive) { _, _ -> switchToPlaceName() } //TODO!
.setNegativeButton(R.string.quest_generic_confirmation_no, null)
.show()
}

override fun isFormComplete() = placeName.isNotEmpty()
fun switchToPlaceName() {
isPlaceName = true
}

override fun isFormComplete() = locationName.isNotEmpty()

companion object {
private const val IS_PLACE_NAME = "is_place_name"
}
}

0 comments on commit a335e76

Please sign in to comment.