Sfoglia il codice sorgente

[RN] Make the calendar the default tab when there are calendar entries fetched.

master
Bettenbuk Zoltan 7 anni fa
parent
commit
6a1e9e256d

+ 52
- 8
react/features/base/react/components/native/AbstractPagedList.js Vedi File

@@ -55,16 +55,26 @@ export default class AbstractPagedList extends Component<Props, State> {
55 55
     constructor(props: Props) {
56 56
         super(props);
57 57
 
58
-        // props.defaultPage may point to a non existing page if some of the
59
-        // pages are disabled.
60
-        const maxPageIndex
61
-            = props.pages.filter(page => page.component).length - 1;
62
-
63 58
         this.state = {
64
-            pageIndex: Math.max(0, Math.min(maxPageIndex, props.defaultPage))
59
+            pageIndex: this._validatePageIndex(props.defaultPage)
65 60
         };
66 61
     }
67 62
 
63
+    /**
64
+     * Implements React {@code Component}'s componentWillReceiveProps.
65
+     *
66
+     * @inheritdoc
67
+     */
68
+    componentWillReceiveProps(newProps: Props) {
69
+        const { defaultPage } = newProps;
70
+
71
+        if (defaultPage !== this.props.defaultPage) {
72
+            // Default page changed due to a redux update. This is likely to
73
+            // happen after APP_WILL_MOUNT. So we update the active tab.
74
+            this._platformSpecificPageSelect(defaultPage);
75
+        }
76
+    }
77
+
68 78
     /**
69 79
      * Renders the component.
70 80
      *
@@ -95,6 +105,20 @@ export default class AbstractPagedList extends Component<Props, State> {
95 105
         );
96 106
     }
97 107
 
108
+    _platformSpecificPageSelect: number => void
109
+
110
+    /**
111
+     * Method to be overriden by the components implementing this abstract class
112
+     * to handle platform specific actions on page select.
113
+     *
114
+     * @protected
115
+     * @param {number} pageIndex - The selected page index.
116
+     * @returns {void}
117
+     */
118
+    _platformSpecificPageSelect(pageIndex) {
119
+        this._selectPage(pageIndex);
120
+    }
121
+
98 122
     _renderPagedList: boolean => React$Node;
99 123
 
100 124
     _selectPage: number => void;
@@ -107,13 +131,15 @@ export default class AbstractPagedList extends Component<Props, State> {
107 131
      * @returns {void}
108 132
      */
109 133
     _selectPage(pageIndex: number) {
134
+        const validatedPageIndex = this._validatePageIndex(pageIndex);
135
+
110 136
         this.setState({
111
-            pageIndex
137
+            pageIndex: validatedPageIndex
112 138
         });
113 139
 
114 140
         // The page's Component may have a refresh(dispatch) function which we
115 141
         // invoke when the page is selected.
116
-        const selectedPage = this.props.pages[pageIndex];
142
+        const selectedPage = this.props.pages[validatedPageIndex];
117 143
 
118 144
         if (selectedPage && selectedPage.component) {
119 145
             const { refresh } = selectedPage.component;
@@ -121,4 +147,22 @@ export default class AbstractPagedList extends Component<Props, State> {
121 147
             typeof refresh === 'function' && refresh(this.props.dispatch);
122 148
         }
123 149
     }
150
+
151
+    _validatePageIndex: number => number
152
+
153
+    /**
154
+     * Validates the requested page index and returns a safe value.
155
+     *
156
+     * @private
157
+     * @param {number} pageIndex - The requested page index.
158
+     * @returns {number}
159
+     */
160
+    _validatePageIndex(pageIndex) {
161
+        // pageIndex may point to a non existing page if some of the pages are
162
+        // disabled (their component property is undefined).
163
+        const maxPageIndex
164
+            = this.props.pages.filter(page => page.component).length - 1;
165
+
166
+        return Math.max(0, Math.min(maxPageIndex, pageIndex));
167
+    }
124 168
 }

+ 12
- 0
react/features/base/react/components/native/PagedList.android.js Vedi File

@@ -86,6 +86,18 @@ class PagedList extends AbstractPagedList {
86 86
         }
87 87
     }
88 88
 
89
+    /**
90
+     * Platform specific actions to run on page select.
91
+     *
92
+     * @private
93
+     * @param {number} pageIndex - The selected page index.
94
+     * @returns {void}
95
+     */
96
+    _platformSpecificPageSelect(pageIndex) {
97
+        this._viewPager.setPage(pageIndex);
98
+        this._selectPage(pageIndex);
99
+    }
100
+
89 101
     /**
90 102
      * Renders a single page of the page list.
91 103
      *

+ 1
- 3
react/features/calendar-sync/middleware.js Vedi File

@@ -66,11 +66,9 @@ CALENDAR_ENABLED
66 66
                     && store.dispatch(addKnownDomains(knownDomains));
67 67
             }
68 68
 
69
-            const result = next(action);
70
-
71 69
             _fetchCalendarEntries(store, false, false);
72 70
 
73
-            return result;
71
+            return next(action);
74 72
         }
75 73
 
76 74
         case REFRESH_CALENDAR: {

+ 27
- 3
react/features/welcome/components/WelcomePageLists.js Vedi File

@@ -2,6 +2,7 @@
2 2
 
3 3
 import React, { Component } from 'react';
4 4
 import { Platform } from 'react-native';
5
+import { connect } from 'react-redux';
5 6
 
6 7
 import { translate } from '../../base/i18n';
7 8
 import { PagedList } from '../../base/react';
@@ -10,6 +11,11 @@ import { RecentList } from '../../recent-list';
10 11
 
11 12
 type Props = {
12 13
 
14
+    /**
15
+     * True if the calendar feature has fetched entries, false otherwise
16
+     */
17
+    _hasCalendarEntries: boolean,
18
+
13 19
     /**
14 20
      * Renders the lists disabled.
15 21
      */
@@ -74,15 +80,33 @@ class WelcomePageLists extends Component<Props> {
74 80
      * @inheritdoc
75 81
      */
76 82
     render() {
77
-        const { disabled } = this.props;
83
+        const { disabled, _hasCalendarEntries } = this.props;
78 84
 
79 85
         return (
80 86
             <PagedList
81
-                defaultPage = { 0 }
87
+                defaultPage = { _hasCalendarEntries ? 1 : 0 }
82 88
                 disabled = { disabled }
83 89
                 pages = { this.pages } />
84 90
         );
85 91
     }
86 92
 }
87 93
 
88
-export default translate(WelcomePageLists);
94
+/**
95
+ * Maps (parts of) the redux state to the React {@code Component} props of
96
+ * {@code WelcomePageLists}.
97
+ *
98
+ * @param {Object} state - The redux state.
99
+ * @protected
100
+ * @returns {{
101
+ *     _hasCalendarEntries: boolean
102
+ * }}
103
+ */
104
+function _mapStateToProps(state: Object) {
105
+    const { events } = state['features/calendar-sync'];
106
+
107
+    return {
108
+        _hasCalendarEntries: Boolean(events && events.length)
109
+    };
110
+}
111
+
112
+export default translate(connect(_mapStateToProps)(WelcomePageLists));

Loading…
Annulla
Salva