package me.selinali.tribbble.utils;

import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.support.annotation.ColorRes;
import android.support.v4.view.ViewCompat;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import me.selinali.tribbble.TribbbleApp;

public final class ViewUtils {

  private ViewUtils() {}

  public static int getStatusBarHeight() {
    return getInternalDimension("status_bar_height");
  }

  public static int getNavigationBarHeight() {
    return getInternalDimension("navigation_bar_height");
  }

  private static int getInternalDimension(String name) {
    Resources res = TribbbleApp.context().getResources();
    int resId = res.getIdentifier(name, "dimen", "android");
    return resId != 0 ? res.getDimensionPixelSize(resId) : 0;
  }

  public static int dpToPx(int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
        TribbbleApp.context().getResources().getDisplayMetrics());
  }

  public static void setTopMargin(View view, int topMargin) {
    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
    params.setMargins(params.leftMargin, topMargin, params.rightMargin, params.bottomMargin);
  }

  public static void setBottomMargin(View view, int bottomMargin) {
    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
    params.setMargins(params.leftMargin, params.topMargin, params.rightMargin, bottomMargin);
  }

  public static void setRightMargin(View view, int rightMargin) {
    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
    params.setMargins(params.leftMargin, params.topMargin, rightMargin, params.bottomMargin);
  }

  public static void applyColorFilter(ImageView imageView, @ColorRes int resId) {
    imageView.setColorFilter(new PorterDuffColorFilter(
        TribbbleApp.color(resId), PorterDuff.Mode.SRC_ATOP));
  }

  public static void fadeView(View view, boolean show, long duration) {
    if (show && view.getVisibility() == View.INVISIBLE) {
      ViewCompat.animate(view).alpha(1f).setDuration(duration)
          .withStartAction(() -> view.setVisibility(View.VISIBLE));
    } else if (!show && view.getVisibility() == View.VISIBLE) {
      ViewCompat.animate(view).alpha(0f).setDuration(duration)
          .withEndAction(() -> view.setVisibility(View.INVISIBLE));
    }
  }
}

@@ -290,20 +290,126 @@
 id.view.
-View
+KeyCharacterMap;%0Aimport android.view.KeyEvent;%0Aimport android.view.View;%0Aimport android.view.ViewConfiguration
 ;%0Aimport
@@ -712,38 +712,240 @@
 rHeight() %7B%0A    
-return
+boolean hasMenuKey = ViewConfiguration.get(TribbbleApp.context()).hasPermanentMenuKey();%0A    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);%0A    return (!hasMenuKey && !hasBackKey) ?
  getInternalDime
@@ -966,32 +966,36 @@
 ion_bar_height%22)
+ : 0
 ;%0A  %7D%0A%0A  private
@@ -1441,748 +1441,228 @@
  set
-TopMargin(View view, int topMargin) %7B%0A    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();%0A    params.setMargins(params.leftMargin, topMargin, params.rightMargin, params.bottomMargin);%0A  %7D%0A%0A  public static void setBottomMargin(View view, int bottomMargin) %7B%0A    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();%0A    params.setMargins(params.leftMargin, params.topMargin, params.rightMargin, bottomMargin);%0A  %7D%0A%0A  public static void setRightMargin(View view, int rightMargin) %7B%0A    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();%0A    params.setMargins(params.leftMargin, params.topMargin, rightMargin, params.
+BottomMargin(View view, int bottomMargin) %7B%0A    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();%0A    params.setMargins(params.leftMargin, params.topMargin, params.rightMargin, 
 bott

Check navigation bar height the right way