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..7592cc1 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 @@ -23,7 +23,6 @@ import android.app.Activity import android.app.Application import android.content.Context import android.content.Intent -import android.content.res.Resources import android.os.Build import android.util.DisplayMetrics import android.util.TypedValue @@ -33,7 +32,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewConfiguration import android.view.ViewGroup -import android.view.inputmethod.InputMethodManager import androidx.annotation.LayoutRes import ru.touchin.roboswag.components.utils.spans.getSpannedTextWithUrls @@ -205,27 +203,30 @@ object UiUtils { * @param view [View] to get ID from; * @return Readable ID. */ - fun getViewIdString(view: View): String = try { - view.resources.getResourceName(view.id) - } catch (exception: Resources.NotFoundException) { - view.id.toString() - } + @Deprecated( + message = "Use extension instead", + replaceWith = ReplaceWith("view.getViewIdString()") + ) + fun getViewIdString(view: View): String = view.getViewIdString() /** * Hides device keyboard for target activity. */ - fun hideSoftInput(activity: Activity) { - activity.currentFocus?.let(this::hideSoftInput) - } + @Deprecated( + message = "Use extension instead", + replaceWith = ReplaceWith("activity.hideSoftInput()") + ) + fun hideSoftInput(activity: Activity) = activity.hideSoftInput() + /** * Hides device keyboard for target view. */ - fun hideSoftInput(view: View) { - view.clearFocus() - val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - inputManager.hideSoftInputFromWindow(view.windowToken, 0) - } + @Deprecated( + message = "Use extension instead", + replaceWith = ReplaceWith("view.hideSoftInput()") + ) + fun hideSoftInput(view: View) = view.hideSoftInput() /** * Shows device keyboard over [Activity] and focuses [View]. @@ -234,11 +235,11 @@ object UiUtils { * * @param view View to get focus for input from keyboard. */ - fun showSoftInput(view: View) { - view.requestFocus() - val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) - } + @Deprecated( + message = "Use extension instead", + replaceWith = ReplaceWith("view.showSoftInput()") + ) + fun showSoftInput(view: View) = view.showSoftInput() } 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