|
| 1 | +package com.enginebai.base.extensions |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.view.View |
| 5 | +import android.view.inputmethod.InputMethodManager |
| 6 | + |
| 7 | +// |
| 8 | +// visibility |
| 9 | +// |
| 10 | +fun View.setVisible(visible: Boolean) { |
| 11 | + visibility = if (visible) View.VISIBLE else View.INVISIBLE |
| 12 | +} |
| 13 | + |
| 14 | +fun View.setExistence(exist: Boolean) { |
| 15 | + visibility = if (exist) View.VISIBLE else View.GONE |
| 16 | +} |
| 17 | + |
| 18 | +inline fun View.showIf(predicate: () -> Boolean): View { |
| 19 | + if (visibility != View.VISIBLE && predicate()) { |
| 20 | + visibility = View.VISIBLE |
| 21 | + } |
| 22 | + return this |
| 23 | +} |
| 24 | + |
| 25 | +inline fun View.hideIf(predicate: () -> Boolean): View { |
| 26 | + if (visibility != View.INVISIBLE && predicate()) { |
| 27 | + visibility = View.VISIBLE |
| 28 | + } |
| 29 | + return this |
| 30 | +} |
| 31 | + |
| 32 | +inline fun View.goneIf(predicate: () -> Boolean): View { |
| 33 | + if (visibility != View.GONE && predicate()) { |
| 34 | + visibility = View.GONE |
| 35 | + } |
| 36 | + return this |
| 37 | +} |
| 38 | + |
| 39 | +fun View.gone() { |
| 40 | + setExistence(false) |
| 41 | +} |
| 42 | + |
| 43 | +fun View.invisible() { |
| 44 | + setVisible(false) |
| 45 | +} |
| 46 | + |
| 47 | +fun View.visible() { |
| 48 | + visibility = View.VISIBLE |
| 49 | +} |
| 50 | + |
| 51 | +fun View.toggleVisible() { |
| 52 | + visibility = if (visibility == View.VISIBLE) View.INVISIBLE else View.VISIBLE |
| 53 | +} |
| 54 | + |
| 55 | +fun View.toggleExistence() { |
| 56 | + visibility = if (visibility == View.VISIBLE) View.GONE else View.VISIBLE |
| 57 | +} |
| 58 | + |
| 59 | +val View.isGone: Boolean get() = (visibility == View.GONE) |
| 60 | +val View.isInvisible: Boolean get() = (visibility == View.INVISIBLE) |
| 61 | +val View.isVisible: Boolean get() = (visibility == View.VISIBLE) |
| 62 | + |
| 63 | +// |
| 64 | +// keyboard |
| 65 | +// |
| 66 | +fun View.showKeyboard() { |
| 67 | + val inputMethodManager = |
| 68 | + context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager |
| 69 | + inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_FORCED) |
| 70 | +} |
| 71 | + |
| 72 | +fun View.hideKeyboard() { |
| 73 | + val inputMethodManager = |
| 74 | + context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager |
| 75 | + inputMethodManager.hideSoftInputFromWindow(this.windowToken, 0) |
| 76 | +} |
| 77 | + |
| 78 | +// |
| 79 | +// dimension conversion |
| 80 | +// |
| 81 | +fun View.px2dp(px: Float): Int { |
| 82 | + return (px / resources.displayMetrics.density + 0.5f).toInt() |
| 83 | +} |
| 84 | + |
| 85 | +fun View.dp2px(dp: Float): Int { |
| 86 | + return (dp * resources.displayMetrics.density + 0.5f).toInt() |
| 87 | +} |
| 88 | + |
| 89 | +fun View.px2sp(px: Float): Int { |
| 90 | + return (px / resources.displayMetrics.scaledDensity + 0.5f).toInt() |
| 91 | +} |
| 92 | + |
| 93 | +fun View.sp2px(sp: Float): Int { |
| 94 | + return (sp * resources.displayMetrics.scaledDensity + 0.5f).toInt() |
| 95 | +} |
0 commit comments