7
7
8
8
import MapKit
9
9
import SwiftUI
10
+ import UserNotifications
11
+
10
12
struct PermissionSection : View {
11
13
@Environment ( \. colorScheme) var colorScheme
12
14
@Binding var showing : Bool
@@ -43,7 +45,7 @@ struct PermissionSectionCell: View {
43
45
@Binding var showing : Bool
44
46
@EnvironmentObject var store : PermissionStore
45
47
@EnvironmentObject var schemaStore : PermissionSchemaStore
46
-
48
+
47
49
//Empty unauthorized array means all permissions have been interacted
48
50
var shouldAutoDismiss : Bool { FilterPermissions . filterForUnauthorized ( with: store. permissions, store: schemaStore) . isEmpty}
49
51
@@ -104,7 +106,7 @@ struct PermissionSectionCell: View {
104
106
}
105
107
else {
106
108
AllowButtonSection ( action: handlePermissionRequest, allowButtonStatus: $allowButtonStatus)
107
- . animation ( . default)
109
+ . animation ( . default)
108
110
}
109
111
110
112
}
@@ -115,44 +117,77 @@ struct PermissionSectionCell: View {
115
117
116
118
func handlePermissionRequest( ) {
117
119
permissionManager = permission. getPermissionManager ( ) ? . init ( permissionType: permission)
118
- permissionManager!. requestPermission { authorized, error in
119
- let result = JMResult ( permissionType: permission,
120
- authorizationStatus: permissionManager!. authorizationStatus,
121
- error: error)
122
- schemaStore. permissionComponentsStore. getPermissionComponent ( for: permission) { permissionComponent in
123
- permissionComponent. interacted = true
124
- if authorized {
125
- allowButtonStatus = . allowed
126
- permissionComponent. authorized = true
127
- ( schemaStore. successfulPermissions? . append ( result) ) ?? ( schemaStore. successfulPermissions = [ result] )
128
- }
129
- else {
130
- allowButtonStatus = . denied
131
- permissionComponent. authorized = false
132
- ( schemaStore. erroneousPermissions? . append ( result) ) ?? ( schemaStore. erroneousPermissions = [ result] )
120
+ guard let permissionManager = permissionManager else {
121
+ return
122
+ }
123
+ permissionManager. requestPermission { authorized, error in
124
+ var result : JMResult !
125
+ if permission == . notification {
126
+ getNotificationAuthorizationStatus {
127
+ result = JMResult ( permissionType: permission,
128
+ authorizationStatus: $0,
129
+ error: error)
130
+ setStatus ( authorized: authorized, result: result)
133
131
}
134
132
}
135
- DispatchQueue . main. async {
136
- schemaStore. objectWillChange. send ( )
133
+ else {
134
+ result = JMResult ( permissionType: permission,
135
+ authorizationStatus: permissionManager. authorizationStatus,
136
+ error: error)
137
+ setStatus ( authorized: authorized, result: result)
138
+ }
139
+
140
+ }
141
+ }
142
+
143
+ func setStatus( authorized: Bool , result: JMResult ) {
144
+ schemaStore. permissionComponentsStore. getPermissionComponent ( for: permission) { permissionComponent in
145
+ permissionComponent. interacted = true
146
+ if authorized {
147
+ allowButtonStatus = . allowed
148
+ permissionComponent. authorized = true
149
+ ( schemaStore. successfulPermissions? . append ( result) ) ?? ( schemaStore. successfulPermissions = [ result] )
150
+ }
151
+ else {
152
+ allowButtonStatus = . denied
153
+ permissionComponent. authorized = false
154
+ ( schemaStore. erroneousPermissions? . append ( result) ) ?? ( schemaStore. erroneousPermissions = [ result] )
155
+ }
156
+ }
157
+ DispatchQueue . main. async {
158
+ schemaStore. objectWillChange. send ( )
159
+ }
160
+ //Backward compatibility - autoDismissAlert, autoDismissModal, and autoDismiss are all acceptable ways to trigger condition
161
+ if shouldAutoDismiss &&
162
+
163
+ //Current view style is alert and autoDismissAlert is true
164
+ ( ( schemaStore. permissionViewStyle == . alert &&
165
+ store. autoDismissAlert) ||
166
+ //Current view style is modal and autoDismissModal is true
167
+ ( schemaStore. permissionViewStyle == . modal &&
168
+ store. autoDismissModal) ) ||
169
+ store. configStore. autoDismiss {
170
+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.8 ) {
171
+ showing = false
172
+ guard let handler = store. configStore. onDisappearHandler else { return }
173
+ handler ( schemaStore. successfulPermissions ?? nil , schemaStore. erroneousPermissions ?? nil )
174
+ }
175
+ }
176
+ }
177
+ func getNotificationAuthorizationStatus( completion: @escaping ( AuthorizationStatus ) -> ( ) ) {
178
+ UNUserNotificationCenter . current ( ) . getNotificationSettings { settings in
179
+ switch settings. authorizationStatus{
180
+ case . authorized:
181
+ completion ( . authorized)
182
+ case . denied:
183
+ completion ( . denied)
184
+ case . notDetermined:
185
+ completion ( . notDetermined)
186
+ case . provisional:
187
+ completion ( . limited)
188
+ default :
189
+ completion ( . denied)
137
190
}
138
- //Backward compatibility - autoDismissAlert, autoDismissModal, and autoDismiss are all acceptable ways to trigger condition
139
- if shouldAutoDismiss &&
140
-
141
- //Current view style is alert and autoDismissAlert is true
142
- ( ( schemaStore. permissionViewStyle == . alert &&
143
- store. autoDismissAlert) ||
144
- //Current view style is modal and autoDismissModal is true
145
- ( schemaStore. permissionViewStyle == . modal &&
146
- store. autoDismissModal) ) ||
147
- store. configStore. autoDismiss {
148
- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.8 ) {
149
- showing = false
150
- guard let handler = store. configStore. onDisappearHandler else { return }
151
- handler ( schemaStore. successfulPermissions ?? nil , schemaStore. erroneousPermissions ?? nil )
152
- }
153
- }
154
- permissionManager = nil
155
-
156
191
}
157
192
}
158
193
}
0 commit comments