mirror of https://github.com/oxen-io/session-ios
Updated the alerts to use the custom styled ones
Removed some more unused code Moved around some files to make them more reusablepull/672/head
parent
40109e0bea
commit
37124c2185
@ -1,17 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface UIViewController (Permissions)
|
||||
|
||||
- (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callback;
|
||||
- (void)ows_askForMediaLibraryPermissions:(void (^)(BOOL granted))callbackParam;
|
||||
- (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callback;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -1,175 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import "UIViewController+Permissions.h"
|
||||
#import "Session-Swift.h"
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
#import <Photos/Photos.h>
|
||||
#import <SignalCoreKit/Threading.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@implementation UIViewController (Permissions)
|
||||
|
||||
- (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callbackParam
|
||||
{
|
||||
OWSLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class));
|
||||
|
||||
// Ensure callback is invoked on main thread.
|
||||
void (^callback)(BOOL) = ^(BOOL granted) {
|
||||
DispatchMainThreadSafe(^{
|
||||
callbackParam(granted);
|
||||
});
|
||||
};
|
||||
|
||||
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
|
||||
OWSLogError(@"Skipping camera permissions request when app is in background.");
|
||||
callback(NO);
|
||||
return;
|
||||
}
|
||||
|
||||
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
|
||||
OWSLogError(@"Camera ImagePicker source not available");
|
||||
callback(NO);
|
||||
return;
|
||||
}
|
||||
|
||||
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
|
||||
if (status == AVAuthorizationStatusDenied) {
|
||||
UIAlertController *alert = [UIAlertController
|
||||
alertControllerWithTitle:NSLocalizedString(@"Session needs camera access to scan QR codes.", @"")
|
||||
message:NSLocalizedString(@"You can enable camera access in your device settings.", @"")
|
||||
preferredStyle:UIAlertControllerStyleAlert];
|
||||
|
||||
UIAlertAction *openSettingsAction =
|
||||
[UIAlertAction actionWithTitle:CommonStrings.openSettingsButton
|
||||
style:UIAlertActionStyleDefault
|
||||
handler:^(UIAlertAction *_Nonnull action) {
|
||||
[[UIApplication sharedApplication] openSystemSettings];
|
||||
callback(NO);
|
||||
}];
|
||||
[alert addAction:openSettingsAction];
|
||||
|
||||
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
|
||||
style:UIAlertActionStyleCancel
|
||||
handler:^(UIAlertAction *action) {
|
||||
callback(NO);
|
||||
}];
|
||||
[alert addAction:dismissAction];
|
||||
|
||||
[self presentAlert:alert];
|
||||
} else if (status == AVAuthorizationStatusAuthorized) {
|
||||
callback(YES);
|
||||
} else if (status == AVAuthorizationStatusNotDetermined) {
|
||||
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
|
||||
completionHandler:callback];
|
||||
} else {
|
||||
OWSLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status);
|
||||
callback(NO);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)ows_askForMediaLibraryPermissions:(void (^)(BOOL granted))callbackParam
|
||||
{
|
||||
OWSLogVerbose(@"[%@] ows_askForMediaLibraryPermissions", NSStringFromClass(self.class));
|
||||
|
||||
// Ensure callback is invoked on main thread.
|
||||
void (^completionCallback)(BOOL) = ^(BOOL granted) {
|
||||
DispatchMainThreadSafe(^{
|
||||
callbackParam(granted);
|
||||
});
|
||||
};
|
||||
|
||||
void (^presentSettingsDialog)(void) = ^(void) {
|
||||
DispatchMainThreadSafe(^{
|
||||
UIAlertController *alert = [UIAlertController
|
||||
alertControllerWithTitle:NSLocalizedString(@"MISSING_MEDIA_LIBRARY_PERMISSION_TITLE",
|
||||
@"Alert title when user has previously denied media library access")
|
||||
message:NSLocalizedString(@"MISSING_MEDIA_LIBRARY_PERMISSION_MESSAGE",
|
||||
@"Alert body when user has previously denied media library access")
|
||||
preferredStyle:UIAlertControllerStyleAlert];
|
||||
|
||||
UIAlertAction *openSettingsAction =
|
||||
[UIAlertAction actionWithTitle:CommonStrings.openSettingsButton
|
||||
style:UIAlertActionStyleDefault
|
||||
handler:^(UIAlertAction *_Nonnull action) {
|
||||
[[UIApplication sharedApplication] openSystemSettings];
|
||||
completionCallback(NO);
|
||||
}];
|
||||
[alert addAction:openSettingsAction];
|
||||
|
||||
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
|
||||
style:UIAlertActionStyleCancel
|
||||
handler:^(UIAlertAction *action) {
|
||||
completionCallback(NO);
|
||||
}];
|
||||
[alert addAction:dismissAction];
|
||||
|
||||
[self presentAlert:alert];
|
||||
});
|
||||
};
|
||||
|
||||
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
|
||||
OWSLogError(@"Skipping media library permissions request when app is in background.");
|
||||
completionCallback(NO);
|
||||
return;
|
||||
}
|
||||
|
||||
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
|
||||
OWSLogError(@"PhotoLibrary ImagePicker source not available");
|
||||
completionCallback(NO);
|
||||
}
|
||||
|
||||
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
|
||||
|
||||
switch (status) {
|
||||
case PHAuthorizationStatusAuthorized: {
|
||||
completionCallback(YES);
|
||||
return;
|
||||
}
|
||||
case PHAuthorizationStatusDenied: {
|
||||
presentSettingsDialog();
|
||||
return;
|
||||
}
|
||||
case PHAuthorizationStatusNotDetermined: {
|
||||
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus newStatus) {
|
||||
if (newStatus == PHAuthorizationStatusAuthorized) {
|
||||
completionCallback(YES);
|
||||
} else {
|
||||
presentSettingsDialog();
|
||||
}
|
||||
}];
|
||||
return;
|
||||
}
|
||||
case PHAuthorizationStatusRestricted: {
|
||||
// when does this happen?
|
||||
OWSFailDebug(@"PHAuthorizationStatusRestricted");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callbackParam
|
||||
{
|
||||
OWSLogVerbose(@"[%@] ows_askForMicrophonePermissions", NSStringFromClass(self.class));
|
||||
|
||||
// Ensure callback is invoked on main thread.
|
||||
void (^callback)(BOOL) = ^(BOOL granted) {
|
||||
DispatchMainThreadSafe(^{
|
||||
callbackParam(granted);
|
||||
});
|
||||
};
|
||||
|
||||
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
|
||||
OWSLogError(@"Skipping microphone permissions request when app is in background.");
|
||||
callback(NO);
|
||||
return;
|
||||
}
|
||||
|
||||
[[AVAudioSession sharedInstance] requestRecordPermission:callback];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -1,145 +0,0 @@
|
||||
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||
|
||||
import UIKit
|
||||
import GRDB
|
||||
import SessionMessagingKit
|
||||
|
||||
@objc public class BlockListUIUtils: NSObject {
|
||||
// MARK: - Block
|
||||
|
||||
/// This method shows an alert to unblock a contact in a ContactThread and will update the `isBlocked` flag of the contact if the user decides to continue
|
||||
///
|
||||
/// **Note:** Make sure to force a config sync in the `completionBlock` if the blocked state was successfully changed
|
||||
@objc public static func showBlockThreadActionSheet(_ threadId: String, from viewController: UIViewController, completionBlock: ((Bool) -> ())? = nil) {
|
||||
let userPublicKey = getUserHexEncodedPublicKey()
|
||||
|
||||
guard threadId != userPublicKey else {
|
||||
completionBlock?(false)
|
||||
return
|
||||
}
|
||||
|
||||
let displayName: String = Profile.displayName(id: threadId)
|
||||
let actionSheet: UIAlertController = UIAlertController(
|
||||
title: String(
|
||||
format: "BLOCK_LIST_BLOCK_USER_TITLE_FORMAT".localized(),
|
||||
self.formatForAlertTitle(displayName: displayName)
|
||||
),
|
||||
message: "BLOCK_USER_BEHAVIOR_EXPLANATION".localized(),
|
||||
preferredStyle: .actionSheet
|
||||
)
|
||||
actionSheet.addAction(UIAlertAction(
|
||||
title: "BLOCK_LIST_BLOCK_BUTTON".localized(),
|
||||
accessibilityIdentifier: "\(type(of: self).self).block",
|
||||
style: .destructive,
|
||||
handler: { _ in
|
||||
Storage.shared.writeAsync(
|
||||
updates: { db in
|
||||
try Contact
|
||||
.fetchOrCreate(db, id: threadId)
|
||||
.with(isBlocked: true)
|
||||
.save(db)
|
||||
},
|
||||
completion: { _, _ in
|
||||
self.showOkAlert(
|
||||
title: "BLOCK_LIST_VIEW_BLOCKED_ALERT_TITLE".localized(),
|
||||
message: String(
|
||||
format: "BLOCK_LIST_VIEW_BLOCKED_ALERT_MESSAGE_FORMAT".localized(),
|
||||
self.formatForAlertMessage(displayName: displayName)
|
||||
),
|
||||
from: viewController,
|
||||
completionBlock: { _ in completionBlock?(true) }
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
))
|
||||
actionSheet.addAction(UIAlertAction(
|
||||
title: CommonStrings.cancelButton,
|
||||
accessibilityIdentifier: "\(type(of: self).self).dismiss",
|
||||
style: .cancel,
|
||||
handler: { _ in completionBlock?(false) }
|
||||
))
|
||||
|
||||
viewController.presentAlert(actionSheet)
|
||||
}
|
||||
|
||||
// MARK: - Unblock
|
||||
|
||||
/// This method shows an alert to unblock a contact in a ContactThread and will update the `isBlocked` flag of the contact if the user decides to continue
|
||||
///
|
||||
/// **Note:** Make sure to force a config sync in the `completionBlock` if the blocked state was successfully changed
|
||||
@objc public static func showUnblockThreadActionSheet(_ threadId: String, from viewController: UIViewController, completionBlock: ((Bool) -> ())? = nil) {
|
||||
let displayName: String = Profile.displayName(id: threadId)
|
||||
let actionSheet: UIAlertController = UIAlertController(
|
||||
title: String(
|
||||
format: "BLOCK_LIST_UNBLOCK_TITLE_FORMAT".localized(),
|
||||
self.formatForAlertTitle(displayName: displayName)
|
||||
),
|
||||
message: nil,
|
||||
preferredStyle: .actionSheet
|
||||
)
|
||||
actionSheet.addAction(UIAlertAction(
|
||||
title: "BLOCK_LIST_UNBLOCK_BUTTON".localized(),
|
||||
accessibilityIdentifier: "\(type(of: self).self).unblock",
|
||||
style: .destructive,
|
||||
handler: { _ in
|
||||
Storage.shared.writeAsync(
|
||||
updates: { db in
|
||||
try Contact
|
||||
.fetchOrCreate(db, id: threadId)
|
||||
.with(isBlocked: false)
|
||||
.save(db)
|
||||
},
|
||||
completion: { _, _ in
|
||||
self.showOkAlert(
|
||||
title: String(
|
||||
format: "BLOCK_LIST_VIEW_UNBLOCKED_ALERT_TITLE_FORMAT".localized(),
|
||||
self.formatForAlertMessage(displayName: displayName)
|
||||
),
|
||||
message: nil,
|
||||
from: viewController,
|
||||
completionBlock: { _ in completionBlock?(false) }
|
||||
)
|
||||
})
|
||||
}
|
||||
))
|
||||
actionSheet.addAction(UIAlertAction(
|
||||
title: CommonStrings.cancelButton,
|
||||
accessibilityIdentifier: "\(type(of: self).self).dismiss",
|
||||
style: .cancel,
|
||||
handler: { _ in completionBlock?(true) }
|
||||
))
|
||||
|
||||
viewController.presentAlert(actionSheet)
|
||||
}
|
||||
|
||||
// MARK: - UI
|
||||
|
||||
@objc public static func showOkAlert(title: String, message: String?, from viewController: UIViewController, completionBlock: @escaping (UIAlertAction) -> ()) {
|
||||
let alertController: UIAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
||||
alertController.addAction(UIAlertAction(
|
||||
title: "BUTTON_OK".localized(),
|
||||
accessibilityIdentifier: "\(type(of: self).self).ok",
|
||||
style: .default,
|
||||
handler: completionBlock
|
||||
))
|
||||
|
||||
viewController.presentAlert(alertController)
|
||||
}
|
||||
|
||||
@objc public static func formatForAlertTitle(displayName: String) -> String {
|
||||
return format(displayName: displayName, maxLength: 20)
|
||||
}
|
||||
|
||||
@objc public static func formatForAlertMessage(displayName: String) -> String {
|
||||
return format(displayName: displayName, maxLength: 127)
|
||||
}
|
||||
|
||||
@objc public static func format(displayName: String, maxLength: Int) -> String {
|
||||
guard displayName.count <= maxLength else {
|
||||
return "\(displayName.substring(to: maxLength))…"
|
||||
}
|
||||
|
||||
return displayName
|
||||
}
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SessionUtilitiesKit
|
||||
|
||||
@objc public class OWSAlerts: NSObject {
|
||||
@objc
|
||||
public class func showAlert(_ alert: UIAlertController) {
|
||||
guard let frontmostViewController = CurrentAppContext().frontmostViewController() else {
|
||||
owsFailDebug("frontmostViewController was unexpectedly nil")
|
||||
return
|
||||
}
|
||||
frontmostViewController.presentAlert(alert)
|
||||
}
|
||||
|
||||
@objc
|
||||
public class func showAlert(title: String) {
|
||||
self.showAlert(title: title, message: nil, buttonTitle: nil)
|
||||
}
|
||||
|
||||
@objc
|
||||
public class func showAlert(title: String?, message: String) {
|
||||
self.showAlert(title: title, message: message, buttonTitle: nil)
|
||||
}
|
||||
|
||||
@objc
|
||||
public class func showAlert(title: String?, message: String? = nil, buttonTitle: String? = nil, buttonAction: ((UIAlertAction) -> Void)? = nil) {
|
||||
guard let fromViewController = CurrentAppContext().frontmostViewController() else {
|
||||
return
|
||||
}
|
||||
showAlert(title: title, message: message, buttonTitle: buttonTitle, buttonAction: buttonAction,
|
||||
fromViewController: fromViewController)
|
||||
}
|
||||
|
||||
@objc
|
||||
public class func showAlert(title: String?, message: String? = nil, buttonTitle: String? = nil, buttonAction: ((UIAlertAction) -> Void)? = nil, fromViewController: UIViewController?) {
|
||||
|
||||
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
||||
|
||||
let actionTitle = buttonTitle ?? NSLocalizedString("BUTTON_OK", comment: "")
|
||||
let okAction = UIAlertAction(title: actionTitle, style: .default, handler: buttonAction)
|
||||
okAction.accessibilityIdentifier = "OWSAlerts.\("ok")"
|
||||
alert.addAction(okAction)
|
||||
fromViewController?.presentAlert(alert)
|
||||
}
|
||||
|
||||
@objc
|
||||
public class func showConfirmationAlert(title: String, message: String? = nil, proceedTitle: String? = nil, proceedAction: @escaping (UIAlertAction) -> Void) {
|
||||
assert(title.count > 0)
|
||||
|
||||
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
||||
alert.addAction(self.cancelAction)
|
||||
|
||||
let actionTitle = proceedTitle ?? NSLocalizedString("BUTTON_OK", comment: "")
|
||||
let okAction = UIAlertAction(title: actionTitle, style: .default, handler: proceedAction)
|
||||
okAction.accessibilityIdentifier = "OWSAlerts.\("ok")"
|
||||
alert.addAction(okAction)
|
||||
|
||||
CurrentAppContext().frontmostViewController()?.presentAlert(alert)
|
||||
}
|
||||
|
||||
@objc
|
||||
public class func showErrorAlert(message: String) {
|
||||
self.showAlert(title: CommonStrings.errorAlertTitle, message: message, buttonTitle: nil)
|
||||
}
|
||||
|
||||
@objc
|
||||
public class var cancelAction: UIAlertAction {
|
||||
let action = UIAlertAction(title: CommonStrings.cancelButton, style: .cancel) { _ in
|
||||
Logger.debug("Cancel item")
|
||||
// Do nothing.
|
||||
}
|
||||
action.accessibilityIdentifier = "OWSAlerts.\("cancel")"
|
||||
return action
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue