2018-12-01 00:02:18 +01:00
|
|
|
package jp.juggler.util
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.ContextWrapper
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.view.inputmethod.InputMethodManager
|
2019-10-05 14:28:04 +02:00
|
|
|
import android.widget.CompoundButton
|
|
|
|
import jp.juggler.subwaytooter.R
|
|
|
|
import org.xmlpull.v1.XmlPullParser
|
2018-12-01 00:02:18 +01:00
|
|
|
|
2018-12-04 10:59:01 +01:00
|
|
|
private val log = LogCategory("ViewUtils")
|
2018-12-01 00:02:18 +01:00
|
|
|
|
|
|
|
fun View?.scan(callback : (view : View) -> Unit) {
|
|
|
|
this ?: return
|
|
|
|
callback(this)
|
|
|
|
if(this is ViewGroup) {
|
|
|
|
for(i in 0 until this.childCount) {
|
|
|
|
this.getChildAt(i)?.scan(callback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val View?.activity : Activity?
|
|
|
|
get() {
|
|
|
|
var context = this?.context
|
|
|
|
while(context is ContextWrapper) {
|
|
|
|
if(context is Activity) return context
|
|
|
|
context = context.baseContext
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
fun View.hideKeyboard() {
|
|
|
|
try {
|
|
|
|
val imm = this.context?.getSystemService(Context.INPUT_METHOD_SERVICE)
|
|
|
|
if(imm is InputMethodManager) {
|
|
|
|
imm.hideSoftInputFromWindow(this.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
|
|
|
|
} else {
|
2018-12-04 10:59:01 +01:00
|
|
|
log.e("hideKeyboard: can't get InputMethodManager")
|
2018-12-01 00:02:18 +01:00
|
|
|
}
|
|
|
|
} catch(ex : Throwable) {
|
2018-12-04 10:59:01 +01:00
|
|
|
log.trace(ex)
|
2018-12-01 00:02:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun View.showKeyboard() {
|
|
|
|
try {
|
|
|
|
val imm = this.context?.getSystemService(Context.INPUT_METHOD_SERVICE)
|
|
|
|
if(imm is InputMethodManager) {
|
|
|
|
imm.showSoftInput(this, InputMethodManager.HIDE_NOT_ALWAYS)
|
|
|
|
} else {
|
2018-12-04 10:59:01 +01:00
|
|
|
log.e("showKeyboard: can't get InputMethodManager")
|
2018-12-01 00:02:18 +01:00
|
|
|
}
|
|
|
|
} catch(ex : Throwable) {
|
2018-12-04 10:59:01 +01:00
|
|
|
log.trace(ex)
|
2018-12-01 00:02:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:12:03 +01:00
|
|
|
// set visibility VISIBLE or GONE
|
|
|
|
// return true if visible
|
2019-12-15 16:34:46 +01:00
|
|
|
fun <T : View> T?.vg(visible : Boolean) : T? {
|
|
|
|
this?.visibility = if(visible) View.VISIBLE else View.GONE
|
|
|
|
return if(visible) this else null
|
2018-12-01 00:02:18 +01:00
|
|
|
}
|
2019-10-05 14:28:04 +02:00
|
|
|
|
2019-10-05 15:27:12 +02:00
|
|
|
fun ViewGroup.generateLayoutParamsEx() : ViewGroup.LayoutParams? =
|
2019-10-05 14:28:04 +02:00
|
|
|
try {
|
|
|
|
val parser = resources.getLayout(R.layout.generate_params)
|
|
|
|
// Skip everything until the view tag.
|
2019-10-05 15:27:12 +02:00
|
|
|
while(true) {
|
2019-10-05 14:28:04 +02:00
|
|
|
val token = parser.nextToken()
|
|
|
|
if(token == XmlPullParser.START_TAG) break
|
|
|
|
}
|
|
|
|
generateLayoutParams(parser)
|
2019-10-05 15:27:12 +02:00
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.e(ex, "generateLayoutParamsEx failed")
|
2019-10-05 14:28:04 +02:00
|
|
|
null
|
|
|
|
}
|
|
|
|
|
|
|
|
// isChecked with skipping animation
|
2019-10-05 15:27:12 +02:00
|
|
|
var CompoundButton.isCheckedNoAnime : Boolean
|
2019-10-05 14:28:04 +02:00
|
|
|
get() = isChecked
|
2019-10-05 15:27:12 +02:00
|
|
|
set(value) {
|
2019-10-05 14:28:04 +02:00
|
|
|
isChecked = value
|
|
|
|
jumpDrawablesToCurrentState()
|
|
|
|
}
|