123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package com.reactnativenavigation.layouts;
-
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Color;
- import android.os.Build;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.widget.RelativeLayout;
-
- import com.reactnativenavigation.animation.OnScrollAnimator;
- import com.reactnativenavigation.params.ScreenParams;
- import com.reactnativenavigation.params.ScreenStyleParams;
- import com.reactnativenavigation.utils.SdkSupports;
- import com.reactnativenavigation.views.ContentView;
- import com.reactnativenavigation.views.ScrollDirectionListener;
- import com.reactnativenavigation.views.TopBar;
-
- import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
- import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
-
- public class ScreenImpl extends RelativeLayout implements Screen, ScrollDirectionListener.OnScrollChanged {
-
- private final ScreenParams screenParams;
- private ContentView contentView;
- private TopBar topBar;
- private OnScrollAnimator scrollAnimator;
-
- public ScreenImpl(Context context, ScreenParams screenParams) {
- super(context);
- this.screenParams = screenParams;
-
- createViews();
- setStyle(screenParams.styleParams);
- }
-
- private void createViews() {
- addTopBar();
- addTitleBar();
- addContentView();
- }
-
- private void addTitleBar() {
- topBar.addTitleBarAndSetButtons(screenParams.buttons, screenParams.navigatorEventId);
- topBar.setTitle(screenParams.title);
- }
-
- private void addTopBar() {
- topBar = new TopBar(getContext());
- addView(topBar, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
- }
-
- private void addContentView() {
- contentView = new ContentView(getContext(), screenParams, this);
- RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
- if (!screenParams.styleParams.drawUnderTopBar) {
- params.addRule(RelativeLayout.BELOW, topBar.getId());
- }
- addView(contentView, params);
- contentView.init();
- }
-
- private void setStyle(ScreenStyleParams styleParams) {
- setStatusBarColor(styleParams.statusBarColor);
- setTopBarColor(styleParams.topBarColor);
- setNavigationBarColor(styleParams.navigationBarColor);
- topBar.setTitleBarVisibility(styleParams.titleBarHidden);
- topBar.setVisibility(styleParams.topBarHidden ? GONE : VISIBLE);
- }
-
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- private void setStatusBarColor(ScreenStyleParams.Color statusBarColor) {
- if (!SdkSupports.lollipop()) {
- return;
- }
-
- final Activity context = (Activity) getContext();
- final Window window = context.getWindow();
- if (statusBarColor.hasColor()) {
- window.setStatusBarColor(statusBarColor.getColor());
- } else {
- window.setStatusBarColor(Color.BLACK);
- }
- }
-
- private void setTopBarColor(ScreenStyleParams.Color topBarColor) {
- if (topBarColor.hasColor()) {
- topBar.setBackgroundColor(topBarColor.getColor());
- }
- }
-
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- public void setNavigationBarColor(ScreenStyleParams.Color navigationBarColor) {
- if (!SdkSupports.lollipop()) {
- return;
- }
-
- final Activity context = (Activity) getContext();
- final Window window = context.getWindow();
- if (navigationBarColor.hasColor()) {
- window.setNavigationBarColor(navigationBarColor.getColor());
- } else {
- window.setNavigationBarColor(Color.BLACK);
- }
- }
-
- @Override
- public void onScrollChanged(ScrollDirectionListener.Direction direction) {
- Log.d("TAG", "onScrollChanged: ");
- if (scrollAnimator == null) {
- scrollAnimator = new OnScrollAnimator(topBar, OnScrollAnimator.HideDirection.Up, topBar.getHeight());
- }
- scrollAnimator.onScrollChanged(direction);
- }
-
- @Override
- public View asView() {
- return this;
- }
-
- @Override
- public void ensureUnmountOnDetachedFromWindow() {
- contentView.ensureUnmountOnDetachedFromWindow();
- }
-
- @Override
- public void preventUnmountOnDetachedFromWindow() {
- contentView.preventUnmountOnDetachedFromWindow();
- }
- }
|