Closed phlippieb closed 6 years ago
@phlippieb
How about utilize Extension?
extension Permission {
func requestWithAnalytics(_ callback: @escaping Permission.Callback) {
request { [weak self] (status: PermissionStatus) in
self?.recordAnalytics(status: status)
// Call the client callback normally.
callback(status)
}
}
private func recordAnalytics(status: PermissionStatus) {
let granted = (status == .authorized)
switch self.type {
case .camera: // (send analytics)
// etc
}
}
}
Permission.camera.requestWithAnalytics { status in
// other callback code
}
Of course! Brilliant. Thanks :)
Use case:
Whenever a user actions on a permission request, I need to send analytics. Rather than implementing this in every callback in my code, I tried to subclass Permission and override
request
to do that automatically. Basically:And then when I need to request a permission, I go
(Note that this is a minimal example; I use many different permission types in many places, and would really prefer not to paste the same analytics code in every single callback.)
However, I'm stuck on initializing instances of Permission using my subclass type. Obviously, this part doesn't work:
PermissionWithAnalytics.camera
. It returns a Permission instance, not a PermissionWithAnalytics, becausecamera
is a var on Permission. But I can't overridecamera
since it's (1) a static var, not a class var, and (2) there are no publicly exposed initializers.Any advice would be greatly appreciated.