handle access to selected photos & optimise the process of request photo library permission

pull/381/head
Ryan ZHAO 3 years ago
parent c8e8b94980
commit cf148fe845

@ -92,12 +92,14 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc
} }
func handleLibraryButtonTapped() { func handleLibraryButtonTapped() {
// FIXME: We're not yet handling the case where the user only gives access to selected photos/videos requestLibraryPermissionIfNeeded { [weak self] in
guard requestLibraryPermissionIfNeeded() else { return } DispatchQueue.main.async {
let sendMediaNavController = SendMediaNavigationController.showingMediaLibraryFirst() let sendMediaNavController = SendMediaNavigationController.showingMediaLibraryFirst()
sendMediaNavController.sendMediaNavDelegate = self sendMediaNavController.sendMediaNavDelegate = self
sendMediaNavController.modalPresentationStyle = .fullScreen sendMediaNavController.modalPresentationStyle = .fullScreen
present(sendMediaNavController, animated: true, completion: nil) self?.present(sendMediaNavController, animated: true, completion: nil)
}
}
} }
func handleGIFButtonTapped() { func handleGIFButtonTapped() {
@ -727,43 +729,46 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc
} }
} }
func requestLibraryPermissionIfNeeded() -> Bool { func requestLibraryPermissionIfNeeded(handler: @escaping () -> Void) {
let authorizationStatus: PHAuthorizationStatus let authorizationStatus: PHAuthorizationStatus
if #available(iOS 14, *) { if #available(iOS 14, *) {
authorizationStatus = PHPhotoLibrary.authorizationStatus(for: .readWrite) authorizationStatus = PHPhotoLibrary.authorizationStatus(for: .readWrite)
if (authorizationStatus == .notDetermined) { if (authorizationStatus == .notDetermined) {
PHPhotoLibrary.requestAuthorization(for: .readWrite) { newStatus in // When the user chooses to select photos (which is the .limit status),
switch newStatus { // the PHPhotoUI will present the picker view on the top of the front view.
case .authorized: // Since we have this ScreenLockUI showing when we request premissions,
print("RYAN: Full access.") // the picker view will be presented on the top of ScreenLockUI.
break // However, the ScreenLockUI will dismiss with the permission request alert view,
case .limited: // the picker view then will dissmiss, too. The selection process cannot be finished
print("RYAN: Limited access.") // this way. So we add a flag (isRequestingPermission) to prevent the ScreenLockUI
break // from showing when we request the photo library permission.
case .denied: Environment.shared.isRequestingPermission = true
break PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
default: Environment.shared.isRequestingPermission = false
break if ([PHAuthorizationStatus.authorized, PHAuthorizationStatus.limited].contains(status)) {
handler()
} }
} }
return false
} }
} else { } else {
authorizationStatus = PHPhotoLibrary.authorizationStatus() authorizationStatus = PHPhotoLibrary.authorizationStatus()
if (authorizationStatus == .notDetermined) { if (authorizationStatus == .notDetermined) {
PHPhotoLibrary.requestAuthorization { _ in } PHPhotoLibrary.requestAuthorization { status in
return false if (status == .authorized) {
handler()
}
}
} }
} }
switch authorizationStatus { switch authorizationStatus {
case .authorized, .limited: return true case .authorized, .limited:
handler()
case .denied, .restricted: case .denied, .restricted:
let modal = PermissionMissingModal(permission: "library") { } let modal = PermissionMissingModal(permission: "library") { }
modal.modalPresentationStyle = .overFullScreen modal.modalPresentationStyle = .overFullScreen
modal.modalTransitionStyle = .crossDissolve modal.modalTransitionStyle = .crossDissolve
present(modal, animated: true, completion: nil) present(modal, animated: true, completion: nil)
return false default: return
default: return false
} }
} }

@ -348,6 +348,10 @@ NS_ASSUME_NONNULL_BEGIN
return ScreenLockUIStateNone; return ScreenLockUIStateNone;
} }
if (Environment.shared.isRequestingPermission) {
return ScreenLockUIStateNone;
}
if (Environment.shared.preferences.screenSecurityIsEnabled) { if (Environment.shared.preferences.screenSecurityIsEnabled) {
OWSLogVerbose(@"desiredUIState: screen protection 4."); OWSLogVerbose(@"desiredUIState: screen protection 4.");
return ScreenLockUIStateScreenProtection; return ScreenLockUIStateScreenProtection;

@ -29,6 +29,8 @@
@property (nonatomic, readonly) OWSPreferences *preferences; @property (nonatomic, readonly) OWSPreferences *preferences;
@property (nonatomic, readonly) OWSSounds *sounds; @property (nonatomic, readonly) OWSSounds *sounds;
@property (nonatomic, readonly) OWSWindowManager *windowManager; @property (nonatomic, readonly) OWSWindowManager *windowManager;
// We don't want to cover the window when we request the photo library permission
@property (nonatomic, readwrite) BOOL isRequestingPermission;
@property (class, nonatomic) Environment *shared; @property (class, nonatomic) Environment *shared;

@ -58,6 +58,7 @@ static Environment *sharedEnvironment = nil;
_proximityMonitoringManager = proximityMonitoringManager; _proximityMonitoringManager = proximityMonitoringManager;
_sounds = sounds; _sounds = sounds;
_windowManager = windowManager; _windowManager = windowManager;
_isRequestingPermission = false;
return self; return self;
} }

Loading…
Cancel
Save