From 908ee925713cbf32128acccf64213baf655bdcb6 Mon Sep 17 00:00:00 2001 From: CraZyLegenD Date: Fri, 13 Nov 2020 13:37:33 +0100 Subject: [PATCH] feat: finally after long time we have an ability to show hide the keyboard, it took em 12 years --- build.gradle | 6 ++--- .../activity/ActivityExtensions.kt | 20 ---------------- .../context/ContextExtensions2.kt | 24 ------------------- .../dialog/DialogExtensions.kt | 13 ---------- .../fragments/FragmentExtensions.kt | 14 ----------- .../insets/InsetsExtensions.kt | 18 +++++++++----- .../views/EditTextExtensions.kt | 24 ++++--------------- .../kotlinextensions/views/ViewExtensions3.kt | 12 +++++++++- 8 files changed, 31 insertions(+), 100 deletions(-) diff --git a/build.gradle b/build.gradle index 531110a9c..87dfbce82 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ ext { retrofit = "2.9.0" rxBindings = "4.0.0" glide = "4.11.0" - constraint_layout = '2.0.2' + constraint_layout = '2.0.4' coroutines = '1.4.0' nav_version = '2.3.1' rx = '3.0.0' @@ -12,7 +12,7 @@ ext { fragment = "1.3.0-beta01" activity = "1.2.0-beta01" appCompat = "1.2.0" - coreKTX = "1.5.0-alpha04" + coreKTX = "1.5.0-alpha05" securityVersion = "1.1.0-alpha02" @@ -43,7 +43,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.0' + classpath 'com.android.tools.build:gradle:4.1.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' } diff --git a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/activity/ActivityExtensions.kt b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/activity/ActivityExtensions.kt index 00ea0347f..a97cd62e8 100644 --- a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/activity/ActivityExtensions.kt +++ b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/activity/ActivityExtensions.kt @@ -435,26 +435,6 @@ fun Activity.enableFullScreen() { } -fun Activity?.hideKeyboardForced() { - this?.currentFocus?.let { currentFocus -> - try { - (currentFocus.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager) - .hideSoftInputFromWindow(currentFocus.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) - } catch (e: Exception) { - } - } -} - -fun Activity?.hideKeyboard() { - (this?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager) - .toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0) -} - -fun Activity?.showKeyboard() { - (this?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager) - .toggleSoftInput(InputMethodManager.SHOW_FORCED, 0) -} - fun Activity.keepScreenOn() { window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) } diff --git a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/context/ContextExtensions2.kt b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/context/ContextExtensions2.kt index e0b965fd4..a8151919f 100644 --- a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/context/ContextExtensions2.kt +++ b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/context/ContextExtensions2.kt @@ -42,30 +42,6 @@ import java.io.File -fun Context.showKeyboard(toFocus: View) { - toFocus.requestFocus() - val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - // imm.showSoftInput(toFocus, InputMethodManager.SHOW_IMPLICIT); - imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY) -} - - -fun Context.hideKeyboard() { - val activity = this as Activity? - if (activity != null) { - val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager? - - if (inputManager != null) { - val v = this.currentFocus - if (v != null) { - inputManager.hideSoftInputFromWindow( - v.windowToken, InputMethodManager.HIDE_NOT_ALWAYS - ) - } - } - } -} - inline fun Context.openEmail(title: String = "Select an email client", onCantHandleAction: () -> Unit = {}) { try { diff --git a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/dialog/DialogExtensions.kt b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/dialog/DialogExtensions.kt index 7daaf7402..ed6fde120 100644 --- a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/dialog/DialogExtensions.kt +++ b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/dialog/DialogExtensions.kt @@ -2,15 +2,12 @@ package com.crazylegend.kotlinextensions.dialog import android.app.Dialog import android.content.Context -import android.content.Context.INPUT_METHOD_SERVICE import android.graphics.Color import android.graphics.Typeface import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.view.View import android.view.Window -import android.view.WindowManager -import android.view.inputmethod.InputMethodManager import android.widget.TextView import androidx.annotation.* import androidx.appcompat.app.AlertDialog @@ -182,16 +179,6 @@ inline fun AlertDialog.Builder.neutralButton(text: String, crossinline handleCli } -/**Method to hide keyboard in dialog only*/ -fun Dialog.hideKeyboard() { - val imm = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager - imm.hideSoftInputFromWindow(currentFocus?.windowToken, 0) -} - -/**Method to show keyboard in dialog only*/ -fun Dialog.showKeyboard() = - window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) - /** * Make the background transparent of the dialog window diff --git a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/fragments/FragmentExtensions.kt b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/fragments/FragmentExtensions.kt index 67186117a..08c4e6fb8 100644 --- a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/fragments/FragmentExtensions.kt +++ b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/fragments/FragmentExtensions.kt @@ -14,7 +14,6 @@ import android.os.Handler import android.os.Looper import android.util.TypedValue import android.view.View -import android.view.inputmethod.InputMethodManager import android.widget.Toast import androidx.annotation.* import androidx.appcompat.app.AlertDialog @@ -554,19 +553,6 @@ fun FragmentManager.applyActions(actions: (fragmentTransaction: FragmentTransact } } -fun Fragment?.hideKeyboard() { - (this?.context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager) - .toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0) -} - -fun Fragment.showKeyboard() { - (context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as? InputMethodManager) - ?.also { inputMethodManager -> - view?.rootView?.apply { post { inputMethodManager.showSoftInput(this, 0) } } - ?: inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0) - } -} - /** * An extension to `postponeEnterTransition` which will resume after a timeout. diff --git a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/insets/InsetsExtensions.kt b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/insets/InsetsExtensions.kt index 4a96349c2..b86f32326 100644 --- a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/insets/InsetsExtensions.kt +++ b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/insets/InsetsExtensions.kt @@ -23,12 +23,18 @@ val View.imeHeight .getInsets(WindowInsetsCompat.Type.ime()).bottom -@RequiresApi(Build.VERSION_CODES.R) -fun View.showKeyboard() = windowInsetsController?.show(WindowInsetsCompat.Type.ime()) - -@RequiresApi(Build.VERSION_CODES.R) -fun View.hideKeyboard() = windowInsetsController?.hide(WindowInsetsCompat.Type.ime()) - +fun View.hideKeyboard() { + ViewCompat.getWindowInsetsController(this)?.hide(WindowInsetsCompat.Type.ime()) +} + +fun View.showKeyboard() { + ViewCompat.getWindowInsetsController(this)?.show(WindowInsetsCompat.Type.ime()) +} + +fun View.showKeyboardAndFocus() { + ViewCompat.getWindowInsetsController(this)?.show(WindowInsetsCompat.Type.ime()) + requestFocus() +} fun View.imeVisibilityListener(onVisibilityChanged: (Boolean) -> Unit) { ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets -> diff --git a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/views/EditTextExtensions.kt b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/views/EditTextExtensions.kt index 13fa44442..f5ae79747 100644 --- a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/views/EditTextExtensions.kt +++ b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/views/EditTextExtensions.kt @@ -3,6 +3,7 @@ package com.crazylegend.kotlinextensions.views import android.annotation.SuppressLint import android.graphics.drawable.Drawable import android.os.Handler +import android.os.Looper import android.text.* import android.view.* import android.view.inputmethod.EditorInfo @@ -14,6 +15,8 @@ import com.crazylegend.kotlinextensions.activity.isKeyboardSubmit import com.crazylegend.kotlinextensions.context.clipboardManager import com.crazylegend.kotlinextensions.context.getCompatDrawable import com.crazylegend.kotlinextensions.context.inputMethodManager +import com.crazylegend.kotlinextensions.insets.hideKeyboard +import com.crazylegend.kotlinextensions.insets.showKeyboard import com.google.android.material.textfield.TextInputEditText import java.net.MalformedURLException import java.net.URL @@ -381,42 +384,25 @@ inline fun EditText.onUnFocused(crossinline block: () -> Unit) { } } -/** - * Shows keyboard for this edit text - */ -fun EditText.showKeyboard() { - requestFocus() - context.inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) -} - /** * Shows keyboard for this edit text with delay * * @param delayMillis - delay in milliseconds before keyboard will be shown */ fun EditText.showKeyboardDelayed(delayMillis: Long) { - Handler().postDelayed({ + Handler(Looper.getMainLooper()).postDelayed({ requestFocus() showKeyboard() }, delayMillis) } -/** - * Hides keyboard for this edit text - */ -fun EditText.hideKeyboard() = - context.inputMethodManager.hideSoftInputFromWindow( - windowToken, - InputMethodManager.HIDE_NOT_ALWAYS - ) - /** * Hides keyboard for this edit text with delay * * @param delayMillis - delay in milliseconds before keyboard will be hided */ fun EditText.hideKeyboardDelayed(delayMillis: Long) = - Handler().postDelayed({ hideKeyboard() }, delayMillis) + Handler(Looper.getMainLooper()).postDelayed({ hideKeyboard() }, delayMillis) @SuppressLint("ClickableViewAccessibility") diff --git a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/views/ViewExtensions3.kt b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/views/ViewExtensions3.kt index 9d147885c..790e4b52c 100644 --- a/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/views/ViewExtensions3.kt +++ b/kotlinextensions/src/main/java/com/crazylegend/kotlinextensions/views/ViewExtensions3.kt @@ -2,6 +2,7 @@ package com.crazylegend.kotlinextensions.views import android.graphics.Rect import android.view.View +import androidx.core.view.ViewCompat import androidx.core.view.isGone import androidx.core.view.isVisible import com.crazylegend.kotlinextensions.context.dp2px @@ -68,4 +69,13 @@ fun View.dp2px(dpValue: Int): Int? { fun View.px2dp(pxValue: Int): Float? { return context?.px2dp(pxValue) -} \ No newline at end of file +} + + +val View.isAppearanceLightNavigationBars + get() = + ViewCompat.getWindowInsetsController(this)?.isAppearanceLightNavigationBars + +val View.isAppearanceLightStatusBars + get() = + ViewCompat.getWindowInsetsController(this)?.isAppearanceLightStatusBars \ No newline at end of file