Browse Source

refactoed

Daniel Zlotin 8 years ago
parent
commit
378854bc57

+ 94
- 5
android/app/src/main/java/com/reactnativenavigation/layouts/Screen.java View File

@@ -1,11 +1,100 @@
1 1
 package com.reactnativenavigation.layouts;
2 2
 
3
-import android.view.View;
3
+import android.annotation.TargetApi;
4
+import android.app.Activity;
5
+import android.content.Context;
6
+import android.graphics.Color;
7
+import android.os.Build;
8
+import android.view.Window;
9
+import android.widget.RelativeLayout;
4 10
 
5
-public interface Screen {
6
-    View asView();
11
+import com.reactnativenavigation.animation.OnScrollAnimator;
12
+import com.reactnativenavigation.params.ScreenParams;
13
+import com.reactnativenavigation.params.ScreenStyleParams;
14
+import com.reactnativenavigation.utils.SdkSupports;
15
+import com.reactnativenavigation.views.ScrollDirectionListener;
16
+import com.reactnativenavigation.views.TopBar;
7 17
 
8
-    void preventUnmountOnDetachedFromWindow();
18
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
19
+import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
9 20
 
10
-    void ensureUnmountOnDetachedFromWindow();
21
+public abstract class Screen extends RelativeLayout implements ScrollDirectionListener.OnScrollChanged {
22
+
23
+    protected final ScreenParams screenParams;
24
+    protected TopBar topBar;
25
+    private OnScrollAnimator scrollAnimator;
26
+
27
+    public Screen(Context context, ScreenParams screenParams) {
28
+        super(context);
29
+        this.screenParams = screenParams;
30
+
31
+        createViews();
32
+        setStyle(screenParams.styleParams);
33
+    }
34
+
35
+    private void createViews() {
36
+        createTopBar();
37
+        createTitleBar();
38
+        createContent();
39
+    }
40
+
41
+    protected abstract void createContent();
42
+
43
+    private void createTitleBar() {
44
+        topBar.addTitleBarAndSetButtons(screenParams.buttons, screenParams.navigatorEventId);
45
+        topBar.setTitle(screenParams.title);
46
+    }
47
+
48
+    private void createTopBar() {
49
+        topBar = new TopBar(getContext());
50
+        addView(topBar, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
51
+    }
52
+
53
+    private void setStyle(ScreenStyleParams styleParams) {
54
+        setStatusBarColor(styleParams.statusBarColor);
55
+        setNavigationBarColor(styleParams.navigationBarColor);
56
+        topBar.setStyle(styleParams);
57
+    }
58
+
59
+    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
60
+    private void setStatusBarColor(ScreenStyleParams.Color statusBarColor) {
61
+        if (!SdkSupports.lollipop()) {
62
+            return;
63
+        }
64
+
65
+        final Activity context = (Activity) getContext();
66
+        final Window window = context.getWindow();
67
+        if (statusBarColor.hasColor()) {
68
+            window.setStatusBarColor(statusBarColor.getColor());
69
+        } else {
70
+            window.setStatusBarColor(Color.BLACK);
71
+        }
72
+    }
73
+
74
+    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
75
+    public void setNavigationBarColor(ScreenStyleParams.Color navigationBarColor) {
76
+        if (!SdkSupports.lollipop()) {
77
+            return;
78
+        }
79
+
80
+        final Activity context = (Activity) getContext();
81
+        final Window window = context.getWindow();
82
+        if (navigationBarColor.hasColor()) {
83
+            window.setNavigationBarColor(navigationBarColor.getColor());
84
+        } else {
85
+            window.setNavigationBarColor(Color.BLACK);
86
+        }
87
+    }
88
+
89
+    @Override
90
+    public void onScrollChanged(ScrollDirectionListener.Direction direction) {
91
+        if (scrollAnimator == null) {
92
+            scrollAnimator = new OnScrollAnimator(topBar, OnScrollAnimator.HideDirection.Up, topBar.getHeight());
93
+        }
94
+        scrollAnimator.onScrollChanged(direction);
95
+    }
96
+
97
+    public abstract void ensureUnmountOnDetachedFromWindow();
98
+
99
+    public abstract void preventUnmountOnDetachedFromWindow();
11 100
 }

+ 0
- 130
android/app/src/main/java/com/reactnativenavigation/layouts/ScreenImpl.java View File

@@ -1,130 +0,0 @@
1
-package com.reactnativenavigation.layouts;
2
-
3
-import android.annotation.TargetApi;
4
-import android.app.Activity;
5
-import android.content.Context;
6
-import android.graphics.Color;
7
-import android.os.Build;
8
-import android.view.View;
9
-import android.view.Window;
10
-import android.widget.RelativeLayout;
11
-
12
-import com.reactnativenavigation.animation.OnScrollAnimator;
13
-import com.reactnativenavigation.params.ScreenParams;
14
-import com.reactnativenavigation.params.ScreenStyleParams;
15
-import com.reactnativenavigation.utils.SdkSupports;
16
-import com.reactnativenavigation.views.ContentView;
17
-import com.reactnativenavigation.views.ScrollDirectionListener;
18
-import com.reactnativenavigation.views.TopBar;
19
-
20
-import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
21
-import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
22
-
23
-public class ScreenImpl extends RelativeLayout implements Screen, ScrollDirectionListener.OnScrollChanged {
24
-
25
-    private final ScreenParams screenParams;
26
-    private ContentView contentView;
27
-    private TopBar topBar;
28
-    private OnScrollAnimator scrollAnimator;
29
-
30
-    public ScreenImpl(Context context, ScreenParams screenParams) {
31
-        super(context);
32
-        this.screenParams = screenParams;
33
-
34
-        createViews();
35
-        setStyle(screenParams.styleParams);
36
-    }
37
-
38
-    private void createViews() {
39
-        addTopBar();
40
-        addTitleBar();
41
-        addContentView();
42
-    }
43
-
44
-    private void addTitleBar() {
45
-        topBar.addTitleBarAndSetButtons(screenParams.buttons, screenParams.navigatorEventId);
46
-        topBar.setTitle(screenParams.title);
47
-    }
48
-
49
-    private void addTopBar() {
50
-        topBar = new TopBar(getContext());
51
-        addView(topBar, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
52
-    }
53
-
54
-    private void addContentView() {
55
-        contentView = new ContentView(getContext(), screenParams, this);
56
-        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
57
-        if (!screenParams.styleParams.drawUnderTopBar) {
58
-            params.addRule(RelativeLayout.BELOW, topBar.getId());
59
-        }
60
-        addView(contentView, params);
61
-        contentView.init();
62
-    }
63
-
64
-    private void setStyle(ScreenStyleParams styleParams) {
65
-        setStatusBarColor(styleParams.statusBarColor);
66
-        setTopBarColor(styleParams.topBarColor);
67
-        setNavigationBarColor(styleParams.navigationBarColor);
68
-        topBar.setTitleBarVisibility(styleParams.titleBarHidden);
69
-        topBar.setVisibility(styleParams.topBarHidden ? GONE : VISIBLE);
70
-    }
71
-
72
-    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
73
-    private void setStatusBarColor(ScreenStyleParams.Color statusBarColor) {
74
-        if (!SdkSupports.lollipop()) {
75
-            return;
76
-        }
77
-
78
-        final Activity context = (Activity) getContext();
79
-        final Window window = context.getWindow();
80
-        if (statusBarColor.hasColor()) {
81
-            window.setStatusBarColor(statusBarColor.getColor());
82
-        } else {
83
-            window.setStatusBarColor(Color.BLACK);
84
-        }
85
-    }
86
-
87
-    private void setTopBarColor(ScreenStyleParams.Color topBarColor) {
88
-        if (topBarColor.hasColor()) {
89
-            topBar.setBackgroundColor(topBarColor.getColor());
90
-        }
91
-    }
92
-
93
-    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
94
-    public void setNavigationBarColor(ScreenStyleParams.Color navigationBarColor) {
95
-        if (!SdkSupports.lollipop()) {
96
-            return;
97
-        }
98
-
99
-        final Activity context = (Activity) getContext();
100
-        final Window window = context.getWindow();
101
-        if (navigationBarColor.hasColor()) {
102
-            window.setNavigationBarColor(navigationBarColor.getColor());
103
-        } else {
104
-            window.setNavigationBarColor(Color.BLACK);
105
-        }
106
-    }
107
-
108
-    @Override
109
-    public void onScrollChanged(ScrollDirectionListener.Direction direction) {
110
-        if (scrollAnimator == null) {
111
-            scrollAnimator = new OnScrollAnimator(topBar, OnScrollAnimator.HideDirection.Up, topBar.getHeight());
112
-        }
113
-        scrollAnimator.onScrollChanged(direction);
114
-    }
115
-
116
-    @Override
117
-    public View asView() {
118
-        return this;
119
-    }
120
-
121
-    @Override
122
-    public void ensureUnmountOnDetachedFromWindow() {
123
-        contentView.ensureUnmountOnDetachedFromWindow();
124
-    }
125
-
126
-    @Override
127
-    public void preventUnmountOnDetachedFromWindow() {
128
-        contentView.preventUnmountOnDetachedFromWindow();
129
-    }
130
-}

+ 6
- 6
android/app/src/main/java/com/reactnativenavigation/layouts/ScreenStack.java View File

@@ -31,14 +31,14 @@ public class ScreenStack extends FrameLayout {
31 31
     }
32 32
 
33 33
     private void addScreen(ScreenParams screenParams) {
34
-        Screen screen = new ScreenImpl(getContext(), screenParams);
35
-        addView(screen.asView(), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
34
+        Screen screen = new SingleScreen(getContext(), screenParams);
35
+        addView(screen, new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
36 36
         stack.push(screen);
37 37
     }
38 38
 
39 39
     private void removePreviousWithoutUnmount(Screen previous) {
40 40
         previous.preventUnmountOnDetachedFromWindow();
41
-        removeView(previous.asView());
41
+        removeView(previous);
42 42
     }
43 43
 
44 44
     public void pop() {
@@ -52,11 +52,11 @@ public class ScreenStack extends FrameLayout {
52 52
         readdPrevious(previous);
53 53
 
54 54
         toRemove.ensureUnmountOnDetachedFromWindow();
55
-        removeView(toRemove.asView());
55
+        removeView(toRemove);
56 56
     }
57 57
 
58 58
     private void readdPrevious(Screen previous) {
59
-        addView(previous.asView(), new LayoutParams(MATCH_PARENT, MATCH_PARENT));
59
+        addView(previous, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
60 60
     }
61 61
 
62 62
     public void popToRoot() {
@@ -68,7 +68,7 @@ public class ScreenStack extends FrameLayout {
68 68
     public void destroy() {
69 69
         for (Screen screen : stack) {
70 70
             screen.ensureUnmountOnDetachedFromWindow();
71
-            removeView(screen.asView());
71
+            removeView(screen);
72 72
         }
73 73
         stack.clear();
74 74
     }

+ 45
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreen.java View File

@@ -0,0 +1,45 @@
1
+package com.reactnativenavigation.layouts;
2
+
3
+import android.content.Context;
4
+import android.support.annotation.NonNull;
5
+import android.widget.RelativeLayout;
6
+
7
+import com.reactnativenavigation.params.ScreenParams;
8
+import com.reactnativenavigation.views.ContentView;
9
+
10
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
11
+
12
+public class SingleScreen extends Screen {
13
+
14
+    private ContentView contentView;
15
+
16
+    public SingleScreen(Context context, ScreenParams screenParams) {
17
+        super(context, screenParams);
18
+    }
19
+
20
+    @Override
21
+    protected void createContent() {
22
+        contentView = new ContentView(getContext(), screenParams, this);
23
+        addView(contentView, addBelowTopBar());
24
+        contentView.init();
25
+    }
26
+
27
+    @NonNull
28
+    private LayoutParams addBelowTopBar() {
29
+        LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
30
+        if (!screenParams.styleParams.drawUnderTopBar) {
31
+            params.addRule(RelativeLayout.BELOW, topBar.getId());
32
+        }
33
+        return params;
34
+    }
35
+
36
+    @Override
37
+    public void ensureUnmountOnDetachedFromWindow() {
38
+        contentView.ensureUnmountOnDetachedFromWindow();
39
+    }
40
+
41
+    @Override
42
+    public void preventUnmountOnDetachedFromWindow() {
43
+        contentView.preventUnmountOnDetachedFromWindow();
44
+    }
45
+}

+ 9
- 4
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

@@ -3,6 +3,7 @@ package com.reactnativenavigation.views;
3 3
 import android.content.Context;
4 4
 import android.support.design.widget.AppBarLayout;
5 5
 
6
+import com.reactnativenavigation.params.ScreenStyleParams;
6 7
 import com.reactnativenavigation.params.TitleBarButtonParams;
7 8
 import com.reactnativenavigation.utils.ViewUtils;
8 9
 
@@ -24,11 +25,15 @@ public class TopBar extends AppBarLayout {
24 25
         titleBar.setButtons(buttons, navigatorEventId);
25 26
     }
26 27
 
27
-    public void setTitleBarVisibility(boolean isHidden) {
28
-        titleBar.setVisibility(isHidden ? GONE : VISIBLE);
29
-    }
30
-
31 28
     public void setTitle(String title) {
32 29
         titleBar.setTitle(title);
33 30
     }
31
+
32
+    public void setStyle(ScreenStyleParams styleParams) {
33
+        if (styleParams.topBarColor.hasColor()) {
34
+            setBackgroundColor(styleParams.topBarColor.getColor());
35
+        }
36
+        setVisibility(styleParams.topBarHidden ? GONE : VISIBLE);
37
+        titleBar.setVisibility(styleParams.titleBarHidden ? GONE : VISIBLE);
38
+    }
34 39
 }