Browse Source

Adapt TopTabs to new API (#2540)

Guy Carmeli 7 years ago
parent
commit
82d69ee277
No account linked to committer's email address

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ComponentViewController.java View File

60
     public void onViewAppeared() {
60
     public void onViewAppeared() {
61
         super.onViewAppeared();
61
         super.onViewAppeared();
62
         ensureViewIsCreated();
62
         ensureViewIsCreated();
63
-        applyOnParentStack(parentController -> {
63
+        applyOnParentController(parentController -> {
64
             parentController.clearOptions();
64
             parentController.clearOptions();
65
             parentController.applyOptions(options, component);
65
             parentController.applyOptions(options, component);
66
         });
66
         });
89
     public void mergeOptions(Options options) {
89
     public void mergeOptions(Options options) {
90
         this.options.mergeWith(options);
90
         this.options.mergeWith(options);
91
         component.applyOptions(this.options);
91
         component.applyOptions(this.options);
92
-        applyOnParentStack(parentController -> parentController.applyOptions(this.options, component));
92
+        applyOnParentController(parentController -> parentController.applyOptions(this.options, component));
93
     }
93
     }
94
 
94
 
95
     Options getOptions() {
95
     Options getOptions() {

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ParentController.java View File

3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
 import android.support.annotation.Nullable;
5
 import android.support.annotation.Nullable;
6
+import android.support.v4.view.ViewPager;
6
 import android.view.ViewGroup;
7
 import android.view.ViewGroup;
7
 
8
 
8
 import com.reactnativenavigation.parse.Options;
9
 import com.reactnativenavigation.parse.Options;
58
     void clearOptions() {
59
     void clearOptions() {
59
 
60
 
60
     }
61
     }
62
+
63
+    public void setupTopTabsWithViewPager(ViewPager viewPager) {
64
+
65
+    }
61
 }
66
 }

+ 8
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java View File

3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
 import android.support.annotation.RestrictTo;
5
 import android.support.annotation.RestrictTo;
6
+import android.support.v4.view.ViewPager;
6
 import android.view.View;
7
 import android.view.View;
7
 
8
 
8
 import com.facebook.react.bridge.Promise;
9
 import com.facebook.react.bridge.Promise;
61
 		}
62
 		}
62
 	}
63
 	}
63
 
64
 
64
-    public void pop(final Promise promise) {
65
+    void pop(final Promise promise) {
65
         if (!canPop()) {
66
         if (!canPop()) {
66
             Navigator.rejectPromise(promise);
67
             Navigator.rejectPromise(promise);
67
             return;
68
             return;
77
         finishPopping(exitingView, poppedTop, promise);
78
         finishPopping(exitingView, poppedTop, promise);
78
     }
79
     }
79
 
80
 
80
-	public void animatePop(final Promise promise) {
81
+	private void animatePop(final Promise promise) {
81
 		if (!canPop()) {
82
 		if (!canPop()) {
82
 			Navigator.rejectPromise(promise);
83
 			Navigator.rejectPromise(promise);
83
 			return;
84
 			return;
177
 	public Collection<ViewController> getChildControllers() {
178
 	public Collection<ViewController> getChildControllers() {
178
 		return stack.values();
179
 		return stack.values();
179
 	}
180
 	}
181
+
182
+    @Override
183
+    public void setupTopTabsWithViewPager(ViewPager viewPager) {
184
+        stackLayout.setupTopTabsWithViewPager(viewPager);
185
+    }
180
 }
186
 }

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java View File

50
 		return activity;
50
 		return activity;
51
 	}
51
 	}
52
 
52
 
53
-	protected void applyOnParentStack(Task<ParentController> task) {
53
+	protected void applyOnParentController(Task<ParentController> task) {
54
         if (parentController != null) {
54
         if (parentController != null) {
55
             task.run(parentController);
55
             task.run(parentController);
56
         }
56
         }

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/toptabs/TopTabController.java View File

33
 
33
 
34
     @Override
34
     @Override
35
     public void applyOptions(Options options) {
35
     public void applyOptions(Options options) {
36
-        applyOnParentStack(parentController -> parentController.applyOptions(options));
36
+        applyOnParentController(parentController -> parentController.applyOptions(options));
37
     }
37
     }
38
 
38
 
39
     @Override
39
     @Override

+ 2
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/toptabs/TopTabsController.java View File

2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
+import android.support.v4.view.ViewPager;
5
 import android.view.ViewGroup;
6
 import android.view.ViewGroup;
6
 
7
 
7
 import com.reactnativenavigation.parse.Options;
8
 import com.reactnativenavigation.parse.Options;
48
     @Override
49
     @Override
49
     public void onViewAppeared() {
50
     public void onViewAppeared() {
50
         applyOptions(options);
51
         applyOptions(options);
52
+        applyOnParentController(parentController -> parentController.setupTopTabsWithViewPager((ViewPager) getView()));
51
         performOnCurrentTab(ViewController::onViewAppeared);
53
         performOnCurrentTab(ViewController::onViewAppeared);
52
     }
54
     }
53
 
55
 

+ 0
- 44
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/toptabs/TopTabsViewPager.java View File

1
-package com.reactnativenavigation.viewcontrollers.toptabs;
2
-
3
-import android.annotation.SuppressLint;
4
-import android.content.Context;
5
-import android.support.v4.view.ViewPager;
6
-import android.view.ViewGroup;
7
-
8
-import com.reactnativenavigation.viewcontrollers.ComponentViewController;
9
-import com.reactnativenavigation.viewcontrollers.ViewController;
10
-
11
-import java.util.List;
12
-
13
-import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
14
-
15
-@SuppressLint("ViewConstructor")
16
-public class TopTabsViewPager extends ViewPager {
17
-    private static final int OFFSCREEN_PAGE_LIMIT = 99;
18
-    private List<ViewController> tabs;
19
-
20
-    public TopTabsViewPager(Context context, List<ViewController> tabs, TopTabsAdapter adapter) {
21
-        super(context);
22
-        this.tabs = tabs;
23
-        init(adapter);
24
-    }
25
-
26
-    private void init(TopTabsAdapter adapter) {
27
-        setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
28
-        for (ViewController tab : tabs) {
29
-            addView(tab.getView(), new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
30
-        }
31
-        setAdapter(adapter);
32
-        addOnPageChangeListener(adapter);
33
-    }
34
-
35
-    public void destroy() {
36
-        for (ViewController tab : tabs) {
37
-            tab.destroy();
38
-        }
39
-    }
40
-
41
-    public void sendOnNavigationButtonPressed(String id) {
42
-        ((ComponentViewController.IReactView) tabs.get(getCurrentItem()).getView()).sendOnNavigationButtonPressed(id);
43
-    }
44
-}

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/views/StackLayout.java View File

2
 
2
 
3
 import android.content.Context;
3
 import android.content.Context;
4
 import android.support.annotation.RestrictTo;
4
 import android.support.annotation.RestrictTo;
5
+import android.support.v4.view.ViewPager;
5
 import android.widget.RelativeLayout;
6
 import android.widget.RelativeLayout;
6
 
7
 
7
 import com.reactnativenavigation.parse.Options;
8
 import com.reactnativenavigation.parse.Options;
43
     public void clearOptions() {
44
     public void clearOptions() {
44
         topBar.clear();
45
         topBar.clear();
45
     }
46
     }
47
+
48
+    public void setupTopTabsWithViewPager(ViewPager viewPager) {
49
+        topBar.setupTopTabsWithViewPager(viewPager);
50
+    }
46
 }
51
 }

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

5
 import android.graphics.Typeface;
5
 import android.graphics.Typeface;
6
 import android.support.annotation.Nullable;
6
 import android.support.annotation.Nullable;
7
 import android.support.design.widget.AppBarLayout;
7
 import android.support.design.widget.AppBarLayout;
8
+import android.support.v4.view.ViewPager;
8
 import android.support.v7.widget.Toolbar;
9
 import android.support.v7.widget.Toolbar;
9
 import android.util.Log;
10
 import android.util.Log;
10
 import android.view.Menu;
11
 import android.view.Menu;
20
 import com.reactnativenavigation.parse.Fraction;
21
 import com.reactnativenavigation.parse.Fraction;
21
 import com.reactnativenavigation.parse.Number;
22
 import com.reactnativenavigation.parse.Number;
22
 import com.reactnativenavigation.parse.Options;
23
 import com.reactnativenavigation.parse.Options;
23
-import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsViewPager;
24
 
24
 
25
 import java.util.ArrayList;
25
 import java.util.ArrayList;
26
 
26
 
146
         return titleBar;
146
         return titleBar;
147
     }
147
     }
148
 
148
 
149
-    public void setupTopTabsWithViewPager(TopTabsViewPager viewPager) {
149
+    public void setupTopTabsWithViewPager(ViewPager viewPager) {
150
         initTopTabs();
150
         initTopTabs();
151
         topTabs.setupWithViewPager(viewPager);
151
         topTabs.setupWithViewPager(viewPager);
152
     }
152
     }

+ 30
- 30
lib/android/app/src/main/java/com/reactnativenavigation/views/TopTabsLayout.java View File

2
 
2
 
3
 import android.annotation.SuppressLint;
3
 import android.annotation.SuppressLint;
4
 import android.content.Context;
4
 import android.content.Context;
5
-import android.support.annotation.RestrictTo;
6
 import android.support.v4.view.ViewPager;
5
 import android.support.v4.view.ViewPager;
7
-import android.view.View;
8
 import android.view.ViewGroup;
6
 import android.view.ViewGroup;
9
 import android.widget.RelativeLayout;
7
 import android.widget.RelativeLayout;
10
 
8
 
11
 import com.reactnativenavigation.parse.Options;
9
 import com.reactnativenavigation.parse.Options;
10
+import com.reactnativenavigation.viewcontrollers.ComponentViewController.IReactView;
12
 import com.reactnativenavigation.viewcontrollers.ViewController;
11
 import com.reactnativenavigation.viewcontrollers.ViewController;
13
 import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsAdapter;
12
 import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsAdapter;
14
-import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsViewPager;
15
 
13
 
16
 import java.util.List;
14
 import java.util.List;
17
 
15
 
16
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
17
+import static android.widget.RelativeLayout.BELOW;
18
+
18
 @SuppressLint("ViewConstructor")
19
 @SuppressLint("ViewConstructor")
19
-public class TopTabsLayout extends RelativeLayout implements Component, TitleBarButton.OnClickListener {
20
+public class TopTabsLayout extends ViewPager implements Component, TitleBarButton.OnClickListener {
20
 
21
 
21
-    private TopBar topBar;
22
-    private TopTabsViewPager viewPager;
22
+    private static final int OFFSCREEN_PAGE_LIMIT = 99;
23
+    private List<ViewController> tabs;
23
 
24
 
24
     public TopTabsLayout(Context context, List<ViewController> tabs, TopTabsAdapter adapter) {
25
     public TopTabsLayout(Context context, List<ViewController> tabs, TopTabsAdapter adapter) {
25
         super(context);
26
         super(context);
26
-        viewPager = new TopTabsViewPager(context, tabs, adapter);
27
-        topBar = new TopBar(context, this);
28
-        topBar.setId(View.generateViewId());
29
-
30
-        initViews();
27
+        this.tabs = tabs;
28
+        initTabs(adapter);
31
     }
29
     }
32
 
30
 
33
-    private void initViews() {
34
-        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
35
-        layoutParams.addRule(BELOW, topBar.getId());
36
-        addView(topBar, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
37
-        addView(viewPager, layoutParams);
38
-        topBar.setupTopTabsWithViewPager(viewPager);
31
+    private void initTabs(TopTabsAdapter adapter) {
32
+        setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
33
+        for (ViewController tab : tabs) {
34
+            addView(tab.getView(), new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
35
+        }
36
+        setAdapter(adapter);
37
+        addOnPageChangeListener(adapter);
39
     }
38
     }
40
 
39
 
41
     @Override
40
     @Override
45
 
44
 
46
     @Override
45
     @Override
47
     public void drawBehindTopBar() {
46
     public void drawBehindTopBar() {
48
-
47
+        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
48
+        layoutParams.removeRule(BELOW);
49
+        setLayoutParams(layoutParams);
49
     }
50
     }
50
 
51
 
51
     @Override
52
     @Override
52
     public void drawBelowTopBar(TopBar topBar) {
53
     public void drawBelowTopBar(TopBar topBar) {
53
-
54
-    }
55
-
56
-    @RestrictTo(RestrictTo.Scope.TESTS)
57
-    public ViewPager getViewPager() {
58
-        return viewPager;
54
+        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
55
+        layoutParams.addRule(BELOW, topBar.getId());
56
+        setLayoutParams(layoutParams);
59
     }
57
     }
60
 
58
 
61
     public void switchToTab(int index) {
59
     public void switchToTab(int index) {
62
-        viewPager.setCurrentItem(index);
63
-    }
64
-
65
-    public int getCurrentItem() {
66
-        return viewPager.getCurrentItem();
60
+        setCurrentItem(index);
67
     }
61
     }
68
 
62
 
69
     @Override
63
     @Override
70
     public void onPress(String buttonId) {
64
     public void onPress(String buttonId) {
71
-        viewPager.sendOnNavigationButtonPressed(buttonId);
65
+        ((IReactView) tabs.get(getCurrentItem()).getView()).sendOnNavigationButtonPressed(buttonId);
66
+    }
67
+
68
+    public void destroy() {
69
+        for (ViewController tab : tabs) {
70
+            tab.destroy();
71
+        }
72
     }
72
     }
73
 }
73
 }

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/TopTabsViewControllerTest.java View File

133
     }
133
     }
134
 
134
 
135
     private IReactView tab(TopTabsLayout topTabs, final int index) {
135
     private IReactView tab(TopTabsLayout topTabs, final int index) {
136
-        return (IReactView) ((ViewGroup) topTabs.getViewPager().getChildAt(index)).getChildAt(0);
136
+        return (IReactView) ((ViewGroup) topTabs.getChildAt(index)).getChildAt(0);
137
     }
137
     }
138
 }
138
 }