Add text watcher to format phone number

This commit is contained in:
moezbhatti
2019-06-29 01:47:57 -04:00
parent 042f7645df
commit 7674b37dae
2 changed files with 53 additions and 0 deletions
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.feature.blocking.numbers
import android.telephony.PhoneNumberUtils
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import timber.log.Timber
import java.util.*
class BlockedNumberTextWatcher(private val editText: EditText) : TextWatcher {
init {
editText.addTextChangedListener(this)
}
override fun afterTextChanged(s: Editable?) = Unit
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
val formatted = PhoneNumberUtils.formatNumber(s?.toString(), Locale.getDefault().country)
Timber.v("onTextChanged=$s, formatted=$formatted")
if (s?.toString() != formatted && formatted != null) {
editText.setText(formatted)
editText.setSelection(formatted.length)
}
}
fun dispose() {
editText.removeTextChangedListener(this)
}
}
@@ -75,12 +75,14 @@ class BlockedNumbersController : QkController<BlockedNumbersView, BlockedNumbers
override fun showAddDialog() {
val layout = LayoutInflater.from(activity).inflate(R.layout.blocked_numbers_add_dialog, null)
val textWatcher = BlockedNumberTextWatcher(layout.input)
val dialog = AlertDialog.Builder(activity!!)
.setView(layout)
.setPositiveButton(R.string.blocked_numbers_dialog_block) { _, _ ->
saveAddressSubject.onNext(layout.input.text.toString())
}
.setNegativeButton(R.string.button_cancel) { _, _ -> }
.setOnDismissListener { textWatcher.dispose() }
dialog.show()
}