package com.reactnativenavigation.views; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.annotation.UiThread; import android.support.v4.content.ContextCompat; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.AttributeSet; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewTreeObserver; import android.widget.TextView; import com.reactnativenavigation.R; import com.reactnativenavigation.activities.BaseReactActivity; import com.reactnativenavigation.core.objects.Button; import com.reactnativenavigation.core.objects.Screen; import com.reactnativenavigation.utils.ContextProvider; import com.reactnativenavigation.utils.ImageUtils; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by guyc on 09/04/16. */ public class RnnToolBar extends Toolbar { private static final int ANIMATE_DURATION = 180; private List mScreens; private AsyncTask mSetupToolbarTask; private Drawable mBackground; private ArrayList mMenuItems; public RnnToolBar(Context context) { super(context); init(); } public RnnToolBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } public RnnToolBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mMenuItems = new ArrayList<>(); mBackground = getBackground(); } public void setScreens(List screens) { mScreens = screens; } public void setStyle(Screen screen) { if (screen.toolBarColor != null) { setBackgroundColor(screen.toolBarColor); } else { resetBackground(); } if (screen.navBarTextColor != null) { setTitleTextColor(screen.navBarTextColor); } else { resetTitleTextColor(); } if (screen.toolBarHidden != null && screen.toolBarHidden) { hideToolbar(); } else { showToolbar(); } } private void resetBackground() { setBackground(mBackground); } private void resetTitleTextColor() { setTitleTextColor(ContextCompat.getColor(getContext(), android.R.color.primary_text_light)); } public void handleOnCreateOptionsMenuAsync() { if (mScreens != null) { setupToolbarButtonsAsync(null, mScreens.get(0)); } } public void setupToolbarButtonsAsync(Screen newScreen) { if (newScreen != null) { this.setupToolbarButtonsAsync(null, newScreen); } } public void setupToolbarButtonsAsync(Screen oldScreen, Screen newScreen) { if (mSetupToolbarTask == null) { mSetupToolbarTask = new SetupToolbarButtonsTask(this, oldScreen, newScreen).execute(); } } public void showToolbar(boolean animated) { ActionBar actionBar = ((AppCompatActivity) getContext()).getSupportActionBar(); if (actionBar != null) { actionBar.setShowHideAnimationEnabled(animated); // We hide the ToolBar's parent (AppBarLayout) since this animates the shadow added by AppBar as well ((View) getParent()).setVisibility(VISIBLE); } } public void hideToolbar(boolean animated) { ActionBar actionBar = ((AppCompatActivity) getContext()).getSupportActionBar(); if (actionBar != null) { actionBar.setShowHideAnimationEnabled(animated); // We hide the ToolBar's parent (AppBarLayout) since this animates the shadow added by AppBar as well ((View) getParent()).setVisibility(GONE); } } private void showToolbar() { showToolbar(false); } private void hideToolbar() { hideToolbar(false); } @SuppressWarnings({"ConstantConditions"}) public void showBackButton(Screen screen) { ActionBar actionBar = ContextProvider.getActivityContext().getSupportActionBar(); Drawable backButton = setupBackButton(screen); actionBar.setHomeAsUpIndicator(backButton); actionBar.setDisplayHomeAsUpEnabled(true); } @SuppressLint("PrivateResource") @SuppressWarnings({"ConstantConditions"}) private Drawable setupBackButton(Screen screen) { Resources resources = getResources(); final Drawable backButton = ResourcesCompat.getDrawable(resources, R.drawable.abc_ic_ab_back_mtrl_am_alpha, ContextProvider.getActivityContext().getTheme()); int tintColor = screen.navBarButtonColor != null ? screen.navBarButtonColor : Color.BLACK; ImageUtils.tint(backButton, tintColor); return backButton; } @SuppressWarnings({"ConstantConditions"}) public void hideBackButton() { ContextProvider.getActivityContext().getSupportActionBar().setDisplayHomeAsUpEnabled(false); } /** * Update the ToolBar from screen. This function sets any properties that are defined * in the screen. * @param screen The currently displayed screen */ @UiThread public void update(@NonNull Screen screen) { ((AppCompatActivity) getContext()).setSupportActionBar(this); setTitle(screen.title); setStyle(screen); } public void updateAndSetButtons(Screen screen) { update(screen); setupToolbarButtonsAsync(screen); } private static class SetupToolbarButtonsTask extends AsyncTask> { private final List