diff --git a/kotlin-extensions/src/main/java/ru/touchin/extensions/View.kt b/kotlin-extensions/src/main/java/ru/touchin/extensions/View.kt index 6c59e27..52fdfa5 100644 --- a/kotlin-extensions/src/main/java/ru/touchin/extensions/View.kt +++ b/kotlin-extensions/src/main/java/ru/touchin/extensions/View.kt @@ -24,15 +24,3 @@ fun View.setOnRippleClickListener(listener: () -> Unit) { setOnClickListener { listener() } } } - -fun View.showSoftInput() { - requestFocus() - val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - inputManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) -} - -fun View.hideSoftInput() { - clearFocus() - val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - inputManager.hideSoftInputFromWindow(windowToken, 0) -} \ No newline at end of file diff --git a/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java b/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java index 1204a53..e9f85ba 100644 --- a/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java +++ b/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java @@ -38,7 +38,7 @@ import ru.touchin.roboswag.core.observables.storable.NonNullStorable; public final class PreferenceUtils { /** - * Creates {@link Storable} that stores string into {@link SharedPreferences}. + * Creates {@link Storable} that stores string into {@link SharedPreferences}. Default value is null * * @param name Name of preference; * @param preferences Preferences to store value; diff --git a/utils/src/main/java/ru/touchin/roboswag/components/utils/UiUtils.kt b/utils/src/main/java/ru/touchin/roboswag/components/utils/UiUtils.kt index 642520c..d21acd6 100644 --- a/utils/src/main/java/ru/touchin/roboswag/components/utils/UiUtils.kt +++ b/utils/src/main/java/ru/touchin/roboswag/components/utils/UiUtils.kt @@ -205,6 +205,10 @@ object UiUtils { * @param view [View] to get ID from; * @return Readable ID. */ + @Deprecated( + message = "Use extension instead", + replaceWith = ReplaceWith("view.getViewIdString()") + ) fun getViewIdString(view: View): String = try { view.resources.getResourceName(view.id) } catch (exception: Resources.NotFoundException) { @@ -214,6 +218,10 @@ object UiUtils { /** * Hides device keyboard for target activity. */ + @Deprecated( + message = "Use extension instead", + replaceWith = ReplaceWith("activity.hideSoftInput()") + ) fun hideSoftInput(activity: Activity) { activity.currentFocus?.let(this::hideSoftInput) } @@ -221,6 +229,10 @@ object UiUtils { /** * Hides device keyboard for target view. */ + @Deprecated( + message = "Use extension instead", + replaceWith = ReplaceWith("view.hideSoftInput()") + ) fun hideSoftInput(view: View) { view.clearFocus() val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager @@ -234,6 +246,10 @@ object UiUtils { * * @param view View to get focus for input from keyboard. */ + @Deprecated( + message = "Use extension instead", + replaceWith = ReplaceWith("view.showSoftInput()") + ) fun showSoftInput(view: View) { view.requestFocus() val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager diff --git a/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt b/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt new file mode 100644 index 0000000..041f926 --- /dev/null +++ b/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt @@ -0,0 +1,48 @@ +package ru.touchin.roboswag.components.utils + +import android.app.Activity +import android.content.Context +import android.content.res.Resources +import android.view.View +import android.view.inputmethod.InputMethodManager + +/** + * Returns string representation of [View]'s ID. + * + * @param view [View] to get ID from; + * @return Readable ID. + */ +fun View.getViewIdString(): String = try { + resources.getResourceName(id) +} catch (exception: Resources.NotFoundException) { + id.toString() +} + +/** + * Hides device keyboard for target activity. + */ +fun Activity.hideSoftInput() { + currentFocus?.hideSoftInput() +} + +/** + * Hides device keyboard for target view. + */ +fun View.hideSoftInput() { + clearFocus() + val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + inputManager.hideSoftInputFromWindow(windowToken, 0) +} + +/** + * Shows device keyboard over [Activity] and focuses [View]. + * Do NOT use it if keyboard is over [android.app.Dialog] - it won't work as they have different [Activity.getWindow]. + * Do NOT use it if you are not sure that view is already added on screen. + * + * @param view View to get focus for input from keyboard. + */ +fun View.showSoftInput() { + requestFocus() + val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + inputManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) +} \ No newline at end of file