session-ios/SessionShareExtension/SAEScreenLockViewController.m

207 lines
5.6 KiB
Mathematica
Raw Permalink Normal View History

2018-04-19 17:45:13 +02:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "SAEScreenLockViewController.h"
#import "UIColor+OWS.h"
2020-11-11 07:45:50 +01:00
#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
2020-11-20 04:04:56 +01:00
#import <SessionUtilitiesKit/AppContext.h>
2018-04-19 17:45:13 +02:00
NS_ASSUME_NONNULL_BEGIN
@interface SAEScreenLockViewController () <ScreenLockViewDelegate>
@property (nonatomic, readonly, weak) id<ShareViewDelegate> shareViewDelegate;
@property (nonatomic) BOOL hasShownAuthUIOnce;
@property (nonatomic) BOOL isShowingAuthUI;
@end
#pragma mark -
@implementation SAEScreenLockViewController
- (instancetype)initWithShareViewDelegate:(id<ShareViewDelegate>)shareViewDelegate
{
self = [super init];
if (!self) {
return self;
}
_shareViewDelegate = shareViewDelegate;
self.delegate = self;
return self;
}
- (void)loadView
{
[super loadView];
2020-03-17 06:18:53 +01:00
UIView.appearance.tintColor = LKColors.text;
2020-01-20 03:20:27 +01:00
2021-05-05 07:49:03 +02:00
// Gradient background
2020-01-20 03:20:27 +01:00
self.view.backgroundColor = UIColor.clearColor;
CAGradientLayer *layer = [CAGradientLayer new];
layer.frame = UIScreen.mainScreen.bounds;
2020-08-26 04:02:27 +02:00
UIColor *gradientStartColor = LKAppModeUtilities.isLightMode ? [UIColor colorWithRGBHex:0xFCFCFC] : [UIColor colorWithRGBHex:0x171717];
UIColor *gradientEndColor = LKAppModeUtilities.isLightMode ? [UIColor colorWithRGBHex:0xFFFFFF] : [UIColor colorWithRGBHex:0x121212];
layer.colors = @[ (id)gradientStartColor.CGColor, (id)gradientEndColor.CGColor ];
2020-01-20 03:20:27 +01:00
[self.view.layer insertSublayer:layer atIndex:0];
2021-05-05 07:49:03 +02:00
// Navigation bar background color
2020-01-20 03:20:27 +01:00
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
navigationBar.shadowImage = [UIImage new];
[navigationBar setTranslucent:NO];
2020-03-17 06:18:53 +01:00
navigationBar.barTintColor = LKColors.navigationBarBackground;
2020-01-20 03:20:27 +01:00
2021-05-05 07:49:03 +02:00
// Title
2020-01-20 03:20:27 +01:00
UILabel *titleLabel = [UILabel new];
2021-05-06 02:36:58 +02:00
titleLabel.text = NSLocalizedString(@"vc_share_title", @"");
2020-03-17 06:18:53 +01:00
titleLabel.textColor = LKColors.text;
titleLabel.font = [UIFont boldSystemFontOfSize:LKValues.veryLargeFontSize];
2020-01-20 03:20:27 +01:00
self.navigationItem.titleView = titleLabel;
2021-05-05 07:49:03 +02:00
// Close button
2020-01-20 03:20:27 +01:00
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"X"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissPressed:)];
2020-03-17 06:18:53 +01:00
closeButton.tintColor = LKColors.text;
2020-01-20 03:20:27 +01:00
self.navigationItem.leftBarButtonItem = closeButton;
2018-04-19 17:45:13 +02:00
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self ensureUI];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self ensureUI];
// Auto-show the auth UI f
if (!self.hasShownAuthUIOnce) {
self.hasShownAuthUIOnce = YES;
[self tryToPresentAuthUIToUnlockScreenLock];
}
}
- (void)dealloc
{
// Surface memory leaks by logging the deallocation of view controllers.
OWSLogVerbose(@"Dealloc: %@", self.class);
2018-04-19 17:45:13 +02:00
}
- (void)tryToPresentAuthUIToUnlockScreenLock
{
OWSAssertIsOnMainThread();
if (self.isShowingAuthUI) {
// We're already showing the auth UI; abort.
return;
}
OWSLogInfo(@"try to unlock screen lock");
2018-04-19 17:45:13 +02:00
self.isShowingAuthUI = YES;
[OWSScreenLock.sharedManager tryToUnlockScreenLockWithSuccess:^{
OWSAssertIsOnMainThread();
OWSLogInfo(@"unlock screen lock succeeded.");
2018-04-19 17:45:13 +02:00
self.isShowingAuthUI = NO;
[self.shareViewDelegate shareViewWasUnlocked];
}
failure:^(NSError *error) {
OWSAssertIsOnMainThread();
OWSLogInfo(@"unlock screen lock failed.");
2018-04-19 17:45:13 +02:00
self.isShowingAuthUI = NO;
[self ensureUI];
[self showScreenLockFailureAlertWithMessage:error.localizedDescription];
}
unexpectedFailure:^(NSError *error) {
OWSAssertIsOnMainThread();
OWSLogInfo(@"unlock screen lock unexpectedly failed.");
2018-04-19 17:45:13 +02:00
self.isShowingAuthUI = NO;
// Local Authentication isn't working properly.
// This isn't covered by the docs or the forums but in practice
// it appears to be effective to retry again after waiting a bit.
dispatch_async(dispatch_get_main_queue(), ^{
[self ensureUI];
});
}
cancel:^{
OWSAssertIsOnMainThread();
OWSLogInfo(@"unlock screen lock cancelled.");
2018-04-19 17:45:13 +02:00
self.isShowingAuthUI = NO;
[self ensureUI];
}];
[self ensureUI];
}
- (void)ensureUI
{
[self updateUIWithState:ScreenLockUIStateScreenLock isLogoAtTop:NO animated:NO];
}
- (void)showScreenLockFailureAlertWithMessage:(NSString *)message
{
OWSAssertIsOnMainThread();
[OWSAlerts showAlertWithTitle:NSLocalizedString(@"SCREEN_LOCK_UNLOCK_FAILED",
@"Title for alert indicating that screen lock could not be unlocked.")
message:message
buttonTitle:nil
buttonAction:^(UIAlertAction *action) {
// After the alert, update the UI.
[self ensureUI];
}
fromViewController:self];
}
- (void)dismissPressed:(id)sender
{
OWSLogDebug(@"tapped dismiss share button");
2018-04-19 17:45:13 +02:00
[self cancelShareExperience];
}
- (void)cancelShareExperience
{
[self.shareViewDelegate shareViewWasCancelled];
}
#pragma mark - ScreenLockViewDelegate
- (void)unlockButtonWasTapped
{
OWSAssertIsOnMainThread();
OWSLogInfo(@"unlockButtonWasTapped");
2018-04-19 17:45:13 +02:00
[self tryToPresentAuthUIToUnlockScreenLock];
}
@end
NS_ASSUME_NONNULL_END