SoftInput method moved to UiUtils

This commit is contained in:
Denis Karmyshakov 2018-03-21 16:51:31 +03:00
parent 7e35087c0a
commit bbc73d3d12
7 changed files with 56 additions and 83 deletions

View File

@ -29,6 +29,7 @@ import android.view.MenuItem;
import android.view.View;
import ru.touchin.roboswag.components.navigation.activities.BaseActivity;
import ru.touchin.roboswag.components.utils.UiUtils;
/**
* Created by Gavriil Sitnikov on 11/03/16.
@ -206,7 +207,7 @@ public class SimpleActionBarDrawerToggle extends ActionBarDrawerToggle
@Override
public void onDrawerClosed(@NonNull final View view) {
if (isInvalidateOptionsMenuSupported) {
activity.supportInvalidateOptionsMenu();
activity.invalidateOptionsMenu();
}
}
@ -221,9 +222,9 @@ public class SimpleActionBarDrawerToggle extends ActionBarDrawerToggle
@Override
public void onDrawerOpened(@NonNull final View drawerView) {
activity.hideSoftInput();
UiUtils.OfViews.hideSoftInput(activity);
if (isInvalidateOptionsMenuSupported) {
activity.supportInvalidateOptionsMenu();
activity.invalidateOptionsMenu();
}
}

View File

@ -19,21 +19,14 @@
package ru.touchin.roboswag.components.navigation.activities;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.ArraySet;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import java.util.ArrayList;
import java.util.Set;
import ru.touchin.roboswag.components.utils.UiUtils;
import ru.touchin.roboswag.core.log.Lc;
@ -45,7 +38,7 @@ import ru.touchin.roboswag.core.log.Lc;
public abstract class BaseActivity extends AppCompatActivity {
@NonNull
private final ArrayList<OnBackPressedListener> onBackPressedListeners = new ArrayList<>();
private final Set<OnBackPressedListener> onBackPressedListeners = new ArraySet<>();
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
@ -101,59 +94,6 @@ public abstract class BaseActivity extends AppCompatActivity {
super.onDestroy();
}
/**
* Hides device keyboard that is showing over {@link Activity}.
* Do NOT use it if keyboard is over {@link android.app.Dialog} - it won't work as they have different {@link Activity#getWindow()}.
*/
public void hideSoftInput() {
if (getCurrentFocus() == null) {
return;
}
final InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
getWindow().getDecorView().requestFocus();
}
/**
* Shows device keyboard over {@link Activity} and focuses {@link View}.
* Do NOT use it if keyboard is over {@link android.app.Dialog} - it won't work as they have different {@link Activity#getWindow()}.
* Do NOT use it if you are not sure that view is already added on screen.
* Better use it onStart of element if view is part of it or onConfigureNavigation if view is part of navigation.
*
* @param view View to get focus for input from keyboard.
*/
public void showSoftInput(@NonNull final View view) {
view.requestFocus();
final InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
/**
* Return the color value associated with a particular resource ID.
* Starting in {@link android.os.Build.VERSION_CODES#M}, the returned
* color will be styled for the specified Context's theme.
*
* @param resId The resource id to search for data;
* @return int A single color value in the form 0xAARRGGBB.
*/
@ColorInt
public int getColorCompat(@ColorRes final int resId) {
return ContextCompat.getColor(this, resId);
}
/**
* Returns a drawable object associated with a particular resource ID.
* Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the
* returned drawable will be styled for the specified Context's theme.
*
* @param resId The resource id to search for data;
* @return Drawable An object that can be used to draw this resource.
*/
@Nullable
public Drawable getDrawableCompat(@DrawableRes final int resId) {
return ContextCompat.getDrawable(this, resId);
}
public void addOnBackPressedListener(@NonNull final OnBackPressedListener onBackPressedListener) {
onBackPressedListeners.add(onBackPressedListener);
}
@ -169,12 +109,7 @@ public abstract class BaseActivity extends AppCompatActivity {
return;
}
}
if (getSupportFragmentManager().getBackStackEntryCount() <= 1) {
supportFinishAfterTransition();
} else {
getSupportFragmentManager().popBackStack();
}
super.onBackPressed();
}
/*

View File

@ -51,7 +51,7 @@ import ru.touchin.roboswag.core.utils.ShouldNotHappenException;
* @param <TState> Type of object which is representing it's fragment state;
* @param <TActivity> Type of {@link FragmentActivity} where fragment could be attached to.
*/
public class ViewControllerFragment<TState extends Parcelable, TActivity extends FragmentActivity> extends ViewFragment<TActivity> {
public class ViewControllerFragment<TActivity extends FragmentActivity, TState extends Parcelable> extends ViewFragment<TActivity> {
private static final String VIEW_CONTROLLER_CLASS_EXTRA = "VIEW_CONTROLLER_CLASS_EXTRA";
private static final String VIEW_CONTROLLER_STATE_EXTRA = "VIEW_CONTROLLER_STATE_EXTRA";
@ -105,7 +105,7 @@ public class ViewControllerFragment<TState extends Parcelable, TActivity extends
@Nullable
private ViewController viewController;
private Class<ViewController<TActivity, ViewControllerFragment<TState, TActivity>, TState>> viewControllerClass;
private Class<ViewController<TActivity, ViewControllerFragment<TActivity, TState>, TState>> viewControllerClass;
private TState state;
@Nullable
private ActivityResult pendingActivityResult;
@ -127,7 +127,7 @@ public class ViewControllerFragment<TState extends Parcelable, TActivity extends
setHasOptionsMenu(!isChildFragment());
//noinspection unchecked
viewControllerClass = (Class<ViewController<TActivity, ViewControllerFragment<TState, TActivity>, TState>>)
viewControllerClass = (Class<ViewController<TActivity, ViewControllerFragment<TActivity, TState>, TState>>)
getArguments().getSerializable(VIEW_CONTROLLER_CLASS_EXTRA);
state = savedInstanceState != null
? savedInstanceState.getParcelable(VIEW_CONTROLLER_STATE_EXTRA)

View File

@ -10,7 +10,7 @@ abstract class DefaultViewController<TActivity : FragmentActivity, TState : Parc
@LayoutRes layoutRes: Int,
creationContext: CreationContext,
savedInstanceState: Bundle?
) : ViewController<TActivity, ViewControllerFragment<TState, TActivity>, TState>(
) : ViewController<TActivity, ViewControllerFragment<TActivity, TState>, TState>(
creationContext,
savedInstanceState
) {

View File

@ -51,7 +51,7 @@ import ru.touchin.roboswag.core.log.Lc;
*/
public class ViewController<
TActivity extends FragmentActivity,
TFragment extends ViewControllerFragment<TState, TActivity>,
TFragment extends ViewControllerFragment<TActivity, TState>,
TState extends Parcelable> implements LifecycleOwner {
@NonNull
@ -276,7 +276,7 @@ public class ViewController<
* Callback from parent fragment.
*/
public void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) {
UiUtils.UI_LIFECYCLE_LC_GROUP.i(Lc.getCodePoint(this));
// do nothing
}
/**

View File

@ -54,7 +54,7 @@ open class ViewControllerNavigation<TActivity : FragmentActivity>(
* @param TState Type of state of fragment.
*/
fun <TState : Parcelable> pushViewController(
viewControllerClass: Class<out ViewController<TActivity, ViewControllerFragment<TState, TActivity>, TState>>,
viewControllerClass: Class<out ViewController<TActivity, ViewControllerFragment<TActivity, TState>, TState>>,
state: TState,
addToStack: Boolean = true,
transactionSetup: ((FragmentTransaction) -> Unit)? = null
@ -82,7 +82,7 @@ open class ViewControllerNavigation<TActivity : FragmentActivity>(
* @param TTargetFragment Type of target fragment.
*/
fun <TState : Parcelable, TTargetFragment : Fragment> pushViewControllerForResult(
viewControllerClass: Class<out ViewController<TActivity, ViewControllerFragment<TState, TActivity>, TState>>,
viewControllerClass: Class<out ViewController<TActivity, ViewControllerFragment<TActivity, TState>, TState>>,
state: TState,
targetFragment: TTargetFragment,
targetRequestCode: Int,
@ -109,7 +109,7 @@ open class ViewControllerNavigation<TActivity : FragmentActivity>(
* @param TState Type of state of fragment.
*/
fun <TState : Parcelable> setViewControllerAsTop(
viewControllerClass: Class<out ViewController<TActivity, ViewControllerFragment<TState, TActivity>, TState>>,
viewControllerClass: Class<out ViewController<TActivity, ViewControllerFragment<TActivity, TState>, TState>>,
state: TState,
transactionSetup: ((FragmentTransaction) -> Unit)? = null
) {
@ -134,7 +134,7 @@ open class ViewControllerNavigation<TActivity : FragmentActivity>(
* @param TState Type of state of fragment.
*/
fun <TState : Parcelable> setInitialViewController(
viewControllerClass: Class<out ViewController<TActivity, ViewControllerFragment<TState, TActivity>, TState>>,
viewControllerClass: Class<out ViewController<TActivity, ViewControllerFragment<TActivity, TState>, TState>>,
state: TState,
transactionSetup: ((FragmentTransaction) -> Unit)? = null
) {

View File

@ -41,6 +41,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
@ -307,6 +308,42 @@ public final class UiUtils {
}
}
/**
* Hides device keyboard for target activity.
*/
public static void hideSoftInput(@NonNull final Activity activity) {
final View focusedView = activity.getCurrentFocus();
if (focusedView != null) {
hideSoftInput(focusedView);
}
}
/**
* Hides device keyboard for target view.
*/
public static void hideSoftInput(@NonNull final View view) {
view.clearFocus();
final InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null) {
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
/**
* Shows device keyboard over {@link Activity} and focuses {@link View}.
* Do NOT use it if keyboard is over {@link android.app.Dialog} - it won't work as they have different {@link 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.
*/
public static void showSoftInput(@NonNull final View view) {
view.requestFocus();
final InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null) {
inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
private OfViews() {
}