2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.dialog
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.app.Activity
|
|
|
|
import android.app.Dialog
|
|
|
|
import android.text.InputType
|
|
|
|
import android.view.View
|
|
|
|
import android.view.WindowManager
|
|
|
|
import android.view.inputmethod.EditorInfo
|
|
|
|
import android.widget.ArrayAdapter
|
|
|
|
import android.widget.AutoCompleteTextView
|
|
|
|
import android.widget.CheckBox
|
|
|
|
import android.widget.Filter
|
|
|
|
import android.widget.TextView
|
|
|
|
|
|
|
|
import java.io.BufferedReader
|
|
|
|
import java.io.InputStreamReader
|
|
|
|
import java.util.ArrayList
|
|
|
|
|
|
|
|
import jp.juggler.subwaytooter.R
|
|
|
|
import jp.juggler.subwaytooter.util.LogCategory
|
2018-01-21 13:46:36 +01:00
|
|
|
import jp.juggler.subwaytooter.util.showToast
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
object LoginForm {
|
|
|
|
private val log = LogCategory("LoginForm")
|
|
|
|
|
|
|
|
private class StringArray : ArrayList<String>()
|
|
|
|
|
|
|
|
@SuppressLint("InflateParams")
|
|
|
|
fun showLoginForm(
|
2018-01-18 19:34:14 +01:00
|
|
|
activity : Activity,
|
|
|
|
instanceArg : String?,
|
|
|
|
onClickOk : (
|
|
|
|
dialog : Dialog,
|
|
|
|
instance : String,
|
|
|
|
bPseudoAccount : Boolean,
|
|
|
|
bInputAccessToken : Boolean
|
|
|
|
) -> Unit
|
2018-01-04 19:52:25 +01:00
|
|
|
) {
|
|
|
|
val view = activity.layoutInflater.inflate(R.layout.dlg_account_add, null, false)
|
2018-09-27 16:21:37 +02:00
|
|
|
val etInstance : AutoCompleteTextView = view.findViewById(R.id.etInstance)
|
|
|
|
val btnOk : View = view.findViewById(R.id.btnOk)
|
|
|
|
val cbPseudoAccount : CheckBox = view.findViewById(R.id.cbPseudoAccount)
|
|
|
|
val cbInputAccessToken : CheckBox = view.findViewById(R.id.cbInputAccessToken)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-09-27 16:21:37 +02:00
|
|
|
cbPseudoAccount.setOnCheckedChangeListener { _, _ ->
|
|
|
|
cbInputAccessToken.isEnabled = ! cbPseudoAccount.isChecked
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
if(instanceArg != null && instanceArg.isNotEmpty()) {
|
|
|
|
etInstance.setText(instanceArg)
|
|
|
|
etInstance.inputType = InputType.TYPE_NULL
|
|
|
|
etInstance.isEnabled = false
|
|
|
|
etInstance.isFocusable = false
|
|
|
|
} else {
|
|
|
|
etInstance.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
|
|
|
|
if(actionId == EditorInfo.IME_ACTION_DONE) {
|
|
|
|
btnOk.performClick()
|
|
|
|
return@OnEditorActionListener true
|
|
|
|
}
|
|
|
|
false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
val dialog = Dialog(activity)
|
|
|
|
dialog.setContentView(view)
|
2018-09-27 16:21:37 +02:00
|
|
|
btnOk.setOnClickListener { _ ->
|
2018-01-04 19:52:25 +01:00
|
|
|
val instance = etInstance.text.toString().trim { it <= ' ' }
|
|
|
|
|
2018-09-27 16:21:37 +02:00
|
|
|
when {
|
|
|
|
instance.isEmpty() -> showToast(activity, true, R.string.instance_not_specified)
|
|
|
|
instance.contains("/") || instance.contains("@") -> showToast(
|
|
|
|
activity,
|
|
|
|
true,
|
|
|
|
R.string.instance_not_need_slash
|
|
|
|
)
|
|
|
|
else -> onClickOk(
|
|
|
|
dialog,
|
|
|
|
instance,
|
|
|
|
cbPseudoAccount.isChecked,
|
|
|
|
cbInputAccessToken.isChecked
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2018-09-27 16:21:37 +02:00
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
view.findViewById<View>(R.id.btnCancel).setOnClickListener { dialog.cancel() }
|
|
|
|
|
|
|
|
val instance_list = ArrayList<String>()
|
|
|
|
try {
|
2018-09-27 16:21:37 +02:00
|
|
|
activity.resources.openRawResource(R.raw.server_list).use { inStream ->
|
|
|
|
val br = BufferedReader(InputStreamReader(inStream, "UTF-8"))
|
2018-01-04 19:52:25 +01:00
|
|
|
while(true) {
|
|
|
|
val s : String = br.readLine()?.trim { it <= ' ' }?.toLowerCase() ?: break
|
|
|
|
if(s.isNotEmpty()) instance_list.add(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
}
|
|
|
|
|
|
|
|
val adapter = object : ArrayAdapter<String>(
|
|
|
|
activity, R.layout.lv_spinner_dropdown, ArrayList()
|
|
|
|
) {
|
|
|
|
|
2018-08-19 06:19:33 +02:00
|
|
|
val nameFilter : Filter = object : Filter() {
|
2018-01-04 19:52:25 +01:00
|
|
|
override fun convertResultToString(value : Any) : CharSequence {
|
|
|
|
return value as String
|
|
|
|
}
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
override fun performFiltering(constraint : CharSequence?) : Filter.FilterResults {
|
2018-01-04 19:52:25 +01:00
|
|
|
val result = Filter.FilterResults()
|
2018-09-27 16:21:37 +02:00
|
|
|
if(constraint?.isNotEmpty() == true) {
|
2018-01-04 19:52:25 +01:00
|
|
|
val key = constraint.toString().toLowerCase()
|
|
|
|
// suggestions リストは毎回生成する必要がある。publishResultsと同時にアクセスされる場合がある
|
|
|
|
val suggestions = StringArray()
|
|
|
|
for(s in instance_list) {
|
|
|
|
if(s.contains(key)) {
|
|
|
|
suggestions.add(s)
|
|
|
|
if(suggestions.size >= 20) break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.values = suggestions
|
|
|
|
result.count = suggestions.size
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2018-09-27 16:21:37 +02:00
|
|
|
override fun publishResults(
|
|
|
|
constraint : CharSequence?,
|
|
|
|
results : Filter.FilterResults?
|
|
|
|
) {
|
2018-01-04 19:52:25 +01:00
|
|
|
clear()
|
2018-01-10 16:47:35 +01:00
|
|
|
val values = results?.values
|
|
|
|
if(values is StringArray) {
|
|
|
|
for(s in values) {
|
2018-01-04 19:52:25 +01:00
|
|
|
add(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getFilter() : Filter {
|
|
|
|
return nameFilter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
adapter.setDropDownViewResource(R.layout.lv_spinner_dropdown)
|
|
|
|
etInstance.setAdapter<ArrayAdapter<String>>(adapter)
|
|
|
|
|
|
|
|
|
|
|
|
dialog.window?.setLayout(
|
|
|
|
WindowManager.LayoutParams.MATCH_PARENT,
|
2018-09-27 16:21:37 +02:00
|
|
|
WindowManager.LayoutParams.WRAP_CONTENT
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
dialog.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|