8
8
import Foundation
9
9
import SwiftUI
10
10
11
- //MARK: Storage
12
- struct PermissionStore {
13
- private init ( ) { }
11
+ //MARK: - Storage
12
+ /**
13
+ The global shared storage for PermissionsSwiftUI
14
+ */
15
+ public struct PermissionStore {
16
+ //MARK: Init and Singleton Management
17
+ /**
18
+ Initalizes and returns a new instance of `PermissionStore`
19
+
20
+ The `PermissionStore` initliazer accepts no parameters, instead, set properties after intialization:
21
+ ```
22
+ let store = PermissionStore()
23
+ store.mainTexts.headerText = "PermissionsSwiftUI is the best library"
24
+ */
25
+ public init ( ) { }
14
26
//a private singleton instance that allows read & write, but for this file's methods only
15
27
fileprivate static var mutableShared = PermissionStore ( )
16
28
//Read only singleton exposed to other parts of program
@@ -19,99 +31,127 @@ struct PermissionStore {
19
31
mutableShared
20
32
}
21
33
}
22
- var permissions : [ PermissionType ] = [ ]
34
+ ///A global array of permissions that configures the permissions to request
35
+ public var permissions : [ PermissionType ] = [ ]
23
36
var permissionsToAsk : [ PermissionType ] {
24
37
FilterPermissions . filterForShouldAskPermission ( for: permissions)
25
38
}
26
- var mainTexts = MainTexts ( )
27
- var allButtonColors = AllButtonColors ( )
28
- var autoDismissModal : Bool = true
29
- var autoDismissAlert : Bool = true
30
- var autoCheckModalAuth : Bool = true
31
- var autoCheckAlertAuth : Bool = true
32
- var onAppear : ( ( ) -> Void ) ?
33
- var onDisappear : ( ( ) -> Void ) ?
34
- struct MainTexts {
35
- var headerText : String = " Need Permissions "
36
- var headerDescription : String = """
39
+ //MARK: Secondary Components
40
+ ///The text for text label components, including header and descriptions
41
+ public var mainTexts = MainTexts ( )
42
+ ///The color configuration for permission allow buttons
43
+ public var allButtonColors = AllButtonColors ( )
44
+ ///Whether to auto dismiss the modal after last permission is allowed
45
+ public var autoDismissModal : Bool = true
46
+ ///Whether to auto dismiss the alert after last permission is allowed
47
+ public var autoDismissAlert : Bool = true
48
+ ///Whether to auto check for authorization status before showing, and show the view only if permission is in `notDetermined`
49
+ public var autoCheckModalAuth : Bool = true
50
+ ///Whether to auto check for authorization status before showing, and show the view only if permission is in `notDetermined`
51
+ public var autoCheckAlertAuth : Bool = true
52
+ ///Override point for executing action when PermissionsSwiftUI view appears
53
+ public var onAppear : ( ( ) -> Void ) ?
54
+ ///Override point for executing action when PermissionsSwiftUI view disappears
55
+ public var onDisappear : ( ( ) -> Void ) ?
56
+ public struct MainTexts {
57
+ ///Text to display for header text
58
+ public var headerText : String = " Need Permissions "
59
+ ///Text to display for header description text
60
+ public var headerDescription : String = """
37
61
In order for you use certain features of this app, you need to give permissions. See description for each permission
38
62
"""
39
- var bottomDescription : String = """
63
+ ///Text to display for bottom part description text
64
+ public var bottomDescription : String = """
40
65
Permission are necessary for all the features and functions to work properly. If not allowed, you have to enable permissions in settings
41
66
"""
42
67
}
43
- var cameraPermission = JMPermission (
68
+ //MARK: Permission Components
69
+ ///The displayed text and image icon for the camera permission
70
+ public var cameraPermission = JMPermission (
44
71
imageIcon: AnyView ( Image ( systemName: " camera.fill " ) ) ,
45
72
title: " Camera " ,
46
73
description: " Allow to use your camera " , authorized: false )
47
-
48
- var locationPermission = JMPermission (
74
+ ///The displayed text and image icon for the location permission
75
+ public var locationPermission = JMPermission (
49
76
imageIcon: AnyView ( Image ( systemName: " location.fill.viewfinder " ) ) ,
50
77
title: " Location " ,
51
78
description: " Allow to access your location " , authorized: false
52
79
)
53
- var locationAlwaysPermission = JMPermission (
80
+ ///The displayed text and image icon for the location always permission
81
+ public var locationAlwaysPermission = JMPermission (
54
82
imageIcon: AnyView ( Image ( systemName: " location.fill.viewfinder " ) ) ,
55
83
title: " Location Always " ,
56
84
description: " Allow to access your location " , authorized: false
57
85
)
58
- var photoPermission = JMPermission (
86
+ ///The displayed text and image icon for the photo library permission
87
+ public var photoPermission = JMPermission (
59
88
imageIcon: AnyView ( Image ( systemName: " photo " ) ) ,
60
89
title: " Photo Library " ,
61
90
description: " Allow to access your photos " , authorized: false
62
91
)
63
- var microphonePermisson = JMPermission (
92
+ ///The displayed text and image icon for the microphone permission
93
+ public var microphonePermisson = JMPermission (
64
94
imageIcon: AnyView ( Image ( systemName: " mic.fill " ) ) ,
65
95
title: " Microphone " ,
66
96
description: " Allow to record with microphone " , authorized: false
67
97
)
68
- var notificationPermission = JMPermission (
98
+ ///The displayed text and image icon for the notification center permission
99
+ public var notificationPermission = JMPermission (
69
100
imageIcon: AnyView ( Image ( systemName: " bell.fill " ) ) ,
70
101
title: " Notification " ,
71
102
description: " Allow to send notifications " , authorized: false
72
103
)
73
- var calendarPermisson = JMPermission (
104
+ ///The displayed text and image icon for the calendar permission
105
+ public var calendarPermisson = JMPermission (
74
106
imageIcon: AnyView ( Image ( systemName: " calendar " ) ) ,
75
107
title: " Calendar " ,
76
108
description: " Allow to access calendar " , authorized: false
77
109
)
78
- var bluetoothPermission = JMPermission (
110
+ ///The displayed text and image icon for the bluetooth permission
111
+ public var bluetoothPermission = JMPermission (
79
112
imageIcon: AnyView ( Image ( systemName: " wave.3.left.circle.fill " ) ) ,
80
113
title: " Bluetooth " ,
81
114
description: " Allow to use bluetooth " , authorized: false
82
115
)
83
- var trackingPermission = JMPermission (
116
+ ///The displayed text and image icon for the permission to track across apps and websites
117
+ public var trackingPermission = JMPermission (
84
118
imageIcon: AnyView ( Image ( systemName: " person.circle.fill " ) ) ,
85
119
title: " Tracking " ,
86
120
description: " Allow to track your data " , authorized: false
87
121
)
88
- var contactsPermission = JMPermission (
122
+ ///The displayed text and image icon for the contact permission
123
+ public var contactsPermission = JMPermission (
89
124
imageIcon: AnyView ( Image ( systemName: " book.fill " ) ) ,
90
125
title: " Contacts " ,
91
126
description: " Allow to access your contacts " , authorized: false
92
127
)
93
- var motionPermission = JMPermission (
128
+ ///The displayed text and image icon for the motion permission
129
+ public var motionPermission = JMPermission (
94
130
imageIcon: AnyView ( Image ( systemName: " hare.fill " ) ) ,
95
131
title: " Motion " ,
96
132
description: " Allow to access your motion sensor data " , authorized: false
97
133
)
98
- var remindersPermission = JMPermission (
134
+ ///The displayed text and image icon for the reminders permission
135
+ public var remindersPermission = JMPermission (
99
136
imageIcon: AnyView ( Image ( systemName: " list.bullet.rectangle " ) ) ,
100
137
title: " Reminders " ,
101
138
description: " Allow to access your reminders " , authorized: false
102
139
)
103
- var speechPermission = JMPermission (
140
+ ///The displayed text and image icon for the speech recognition permission
141
+ public var speechPermission = JMPermission (
104
142
imageIcon: AnyView ( Image ( systemName: " rectangle.3.offgrid.bubble.left.fill " ) ) ,
105
143
title: " Speech " ,
106
144
description: " Allow to access speech recognition " , authorized: false
107
145
)
108
- var healthPermission = JMPermission ( imageIcon: AnyView ( Image ( systemName: " heart.fill " ) ) ,
109
- title: " Health " ,
110
- description: " Allow to access your health information " ,
111
- authorized: false )
146
+ ///The displayed text and image icon for the health permission
147
+ public var healthPermission = JMPermission (
148
+ imageIcon: AnyView ( Image ( systemName: " heart.fill " ) ) ,
149
+ title: " Health " ,
150
+ description: " Allow to access your health information " ,
151
+ authorized: false )
112
152
113
153
}
114
- // MARK: Updating methods
154
+ // MARK: - Updating methods
115
155
extension PermissionStore {
116
156
//Used for unit testing, need to reset storage before each subtest
117
157
static func resetPermissionsModelStore( ) {
@@ -123,6 +163,7 @@ extension PermissionStore{
123
163
property ( & PermissionStore. mutableShared, value)
124
164
}
125
165
}
166
+ // MARK: - Button Customizations
126
167
/**
127
168
`AllButtonColors` encapsulates the color configuration for all states of the allow button
128
169
@@ -132,19 +173,15 @@ extension PermissionStore{
132
173
3. Pass in the `AllButtonColors` struct previously into the proper parameter
133
174
*/
134
175
public struct AllButtonColors {
135
- public var buttonIdle = ButtonColor ( foregroundColor: Color ( . systemBlue) ,
136
- backgroundColor: Color ( . systemGray5) )
137
- public var buttonAllowed = ButtonColor ( foregroundColor: Color ( . white) ,
138
- backgroundColor: Color ( . systemBlue) )
139
- public var buttonDenied = ButtonColor ( foregroundColor: Color ( . white) ,
140
- backgroundColor: Color ( . systemRed) )
176
+ //MARK: Creating New Button Configs
141
177
/**
142
178
- parameters:
143
179
- buttonIdle: The button color configuration for the default, idle state
144
180
- buttonAllowed: The button color configuration for the highlighted, allowed state
145
181
- buttonDenied: The button color configuration for the user explicitly denied state
146
182
*/
147
183
public init ( buttonIdle: ButtonColor , buttonAllowed: ButtonColor , buttonDenied: ButtonColor ) {
184
+ self . init ( )
148
185
self . buttonIdle = buttonIdle
149
186
self . buttonAllowed = buttonAllowed
150
187
self . buttonDenied = buttonDenied
@@ -154,23 +191,47 @@ public struct AllButtonColors{
154
191
- buttonIdle: The button color configuration for the default, idle state
155
192
*/
156
193
public init ( buttonIdle: ButtonColor ) {
194
+ self . init ( )
157
195
self . buttonIdle = buttonIdle
158
196
}
159
197
/**
160
198
- parameters:
161
199
- buttonAllowed: The button color configuration for the highlighted, allowed state
162
200
*/
163
201
public init ( buttonAllowed: ButtonColor ) {
202
+ self . init ( )
164
203
self . buttonAllowed = buttonAllowed
165
204
}
166
205
/**
167
206
- parameters:
168
207
- buttonDenied: The button color configuration for the user explicitly denied state
169
208
*/
170
209
public init ( buttonDenied: ButtonColor ) {
210
+ self . init ( )
171
211
self . buttonDenied = buttonDenied
172
212
}
173
- public init ( ) { }
213
+
214
+ public init ( primaryColor: Color ? = nil , tertiaryColor: Color ? = nil ) {
215
+ self . primaryColor = primaryColor ?? Color ( . systemBlue)
216
+ self . tertiaryColor = tertiaryColor ?? Color ( . systemRed)
217
+ self . buttonIdle = ButtonColor ( foregroundColor: self . primaryColor,
218
+ backgroundColor: Color ( . systemGray5) )
219
+ self . buttonAllowed = ButtonColor ( foregroundColor: Color ( . white) ,
220
+ backgroundColor: self . primaryColor)
221
+ self . buttonDenied = ButtonColor ( foregroundColor: Color ( . white) ,
222
+ backgroundColor: self . tertiaryColor)
223
+ }
224
+
225
+ //MARK: Button Color States
226
+
227
+ var primaryColor : Color
228
+ var tertiaryColor : Color
229
+ ///The button color configuration under idle status defined by a `ButtonColor` struct
230
+ public var buttonIdle : ButtonColor
231
+ ///The button color configuration under allowed status defined by a `ButtonColor` struct
232
+ public var buttonAllowed : ButtonColor
233
+ ///The button color configuration under denied status defined by a `ButtonColor` struct
234
+ public var buttonDenied : ButtonColor
174
235
}
175
236
176
237
/**
@@ -180,10 +241,7 @@ public struct AllButtonColors{
180
241
To customize
181
242
*/
182
243
public struct ButtonColor {
183
- //The color of type `Color` for the foreground text
184
- public var foregroundColor : Color
185
- //The color of type `Color` for the foreground text
186
- public var backgroundColor : Color
244
+ // MARK: Creating New Button Color
187
245
/**
188
246
- parameters:
189
247
- foregroundColor: The color of type `Color` for the foreground text
@@ -193,4 +251,10 @@ public struct ButtonColor{
193
251
self . foregroundColor = foregroundColor
194
252
self . backgroundColor = backgroundColor
195
253
}
254
+ //MARK: Properties
255
+ ///The color of type `Color` for the foreground text
256
+ public var foregroundColor : Color
257
+ ///The color of type `Color` for the foreground text
258
+ public var backgroundColor : Color
259
+
196
260
}
0 commit comments