瀏覽代碼

Avoid asking for calendar permission on app start

master
zbettenbuk 7 年之前
父節點
當前提交
7da26042b3

+ 29
- 0
react/features/calendar-sync/components/MeetingList.native.js 查看文件

@@ -20,6 +20,14 @@ type Props = {
20 20
      */
21 21
     dispatch: Function,
22 22
 
23
+    /**
24
+     * Tells the component if it's being displayed at the moment, or not.
25
+     * Note: as an example, on Android it can happen that the component
26
+     * is rendered but not displayed, because components like ViewPagerAndroid
27
+     * render their children even if they are not visible at the moment.
28
+     */
29
+    displayed: boolean,
30
+
23 31
     /**
24 32
      * The calendar event list.
25 33
      */
@@ -35,6 +43,7 @@ type Props = {
35 43
  * Component to display a list of events from the (mobile) user's calendar.
36 44
  */
37 45
 class MeetingList extends Component<Props> {
46
+    _initialLoaded: boolean
38 47
 
39 48
     /**
40 49
      * Default values for the component's props.
@@ -58,6 +67,26 @@ class MeetingList extends Component<Props> {
58 67
         this._toDateString = this._toDateString.bind(this);
59 68
     }
60 69
 
70
+    /**
71
+     * Implements React Component's componentWillReceiveProps function.
72
+     *
73
+     * @inheritdoc
74
+     */
75
+    componentWillReceiveProps(newProps) {
76
+        // This is a conditional logic to refresh the calendar entries (thus
77
+        // to request access to calendar) on component first receives a
78
+        // displayed=true prop - to avoid requesting calendar access on
79
+        // app start.
80
+        if (!this._initialLoaded
81
+                && newProps.displayed
82
+                && !this.props.displayed) {
83
+            const { dispatch } = this.props;
84
+
85
+            this._initialLoaded = true;
86
+            dispatch(refreshCalendarEntryList());
87
+        }
88
+    }
89
+
61 90
     /**
62 91
      * Implements the React Components's render method.
63 92
      *

+ 10
- 6
react/features/calendar-sync/middleware.js 查看文件

@@ -22,10 +22,10 @@ MiddlewareRegistry.register(store => next => action => {
22 22
     switch (action.type) {
23 23
     case APP_WILL_MOUNT:
24 24
         _ensureDefaultServer(store);
25
-        _fetchCalendarEntries(store);
25
+        _fetchCalendarEntries(store, false);
26 26
         break;
27 27
     case REFRESH_CALENDAR_ENTRY_LIST:
28
-        _fetchCalendarEntries(store);
28
+        _fetchCalendarEntries(store, true);
29 29
         break;
30 30
     case SET_ROOM:
31 31
         _parseAndAddDomain(store);
@@ -38,15 +38,17 @@ MiddlewareRegistry.register(store => next => action => {
38 38
  * Ensures calendar access if possible and resolves the promise if it's granted.
39 39
  *
40 40
  * @private
41
+ * @param {boolean} promptForPermission - Flag to tell the app if it should
42
+ * prompt for a calendar permission if it wasn't granted yet.
41 43
  * @returns {Promise}
42 44
  */
43
-function _ensureCalendarAccess() {
45
+function _ensureCalendarAccess(promptForPermission) {
44 46
     return new Promise((resolve, reject) => {
45 47
         RNCalendarEvents.authorizationStatus()
46 48
             .then(status => {
47 49
                 if (status === 'authorized') {
48 50
                     resolve();
49
-                } else if (status === 'undetermined') {
51
+                } else if (promptForPermission) {
50 52
                     RNCalendarEvents.authorizeEventStore()
51 53
                         .then(result => {
52 54
                             if (result === 'authorized') {
@@ -89,10 +91,12 @@ function _ensureDefaultServer(store) {
89 91
  *
90 92
  * @private
91 93
  * @param {Object} store - The redux store.
94
+ * @param {boolean} promptForPermission - Flag to tell the app if it should
95
+ * prompt for a calendar permission if it wasn't granted yet.
92 96
  * @returns {void}
93 97
  */
94
-function _fetchCalendarEntries(store) {
95
-    _ensureCalendarAccess()
98
+function _fetchCalendarEntries(store, promptForPermission) {
99
+    _ensureCalendarAccess(promptForPermission)
96 100
     .then(() => {
97 101
         const startDate = new Date();
98 102
         const endDate = new Date();

+ 4
- 1
react/features/welcome/components/PagedList.android.js 查看文件

@@ -41,6 +41,7 @@ export default class PagedList extends AbstractPagedList {
41 41
      */
42 42
     render() {
43 43
         const { disabled } = this.props;
44
+        const { pageIndex } = this.state;
44 45
 
45 46
         return (
46 47
             <View
@@ -59,7 +60,9 @@ export default class PagedList extends AbstractPagedList {
59 60
                         <RecentList disabled = { disabled } />
60 61
                     </View>
61 62
                     <View key = { 1 }>
62
-                        <MeetingList disabled = { disabled } />
63
+                        <MeetingList
64
+                            disabled = { disabled }
65
+                            displayed = { pageIndex === 1 } />
63 66
                     </View>
64 67
                 </ViewPagerAndroid>
65 68
                 <View style = { styles.pageIndicatorContainer }>

+ 3
- 1
react/features/welcome/components/PagedList.ios.js 查看文件

@@ -58,7 +58,9 @@ class PagedList extends AbstractPagedList {
58 58
                         onPress = { this._onTabSelected(1) }
59 59
                         selected = { pageIndex === 1 }
60 60
                         title = { t('welcomepage.calendar') } >
61
-                        <MeetingList disabled = { disabled } />
61
+                        <MeetingList
62
+                            disabled = { disabled }
63
+                            displayed = { pageIndex === 1 } />
62 64
                     </TabBarIOS.Item>
63 65
                 </TabBarIOS>
64 66
             </View>

Loading…
取消
儲存