From 2e727a24b5d857264a5946a6c2fc9848aedf43b3 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 10 Jul 2017 16:04:03 -0400 Subject: [PATCH] Convert home view to programmatic layout. // FREEBIE --- Signal/src/AppDelegate.h | 2 - Signal/src/AppDelegate.m | 9 +- Signal/src/Storyboard/Main.storyboard | 111 +----------------- .../CodeVerificationViewController.m | 13 +- .../ViewControllers/SignalsViewController.h | 8 +- .../ViewControllers/SignalsViewController.m | 106 ++++++++++++++--- Signal/src/network/PushManager.m | 1 - 7 files changed, 106 insertions(+), 144 deletions(-) diff --git a/Signal/src/AppDelegate.h b/Signal/src/AppDelegate.h index a2ceffe5d..915a72dc9 100644 --- a/Signal/src/AppDelegate.h +++ b/Signal/src/AppDelegate.h @@ -10,7 +10,5 @@ extern NSString *const AppDelegateStoryboardMain; @interface AppDelegate : UIResponder -@property (strong, nonatomic) UIWindow *window; -@property (strong, nonatomic) SignalsViewController *signalVC; @end diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index ff438901f..d19020408 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -19,6 +19,7 @@ #import "Release.h" #import "SendExternalFileViewController.h" #import "Signal-Swift.h" +#import "SignalsNavigationController.h" #import "VersionMigrations.h" #import "ViewControllerUtils.h" #import @@ -58,6 +59,8 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; @implementation AppDelegate +@synthesize window = _window; + - (void)applicationDidEnterBackground:(UIApplication *)application { DDLogWarn(@"%@ applicationDidEnterBackground.", self.tag); @@ -808,7 +811,11 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; DDLogInfo(@"Presenting initial root view controller"); if ([TSAccountManager isRegistered]) { - self.window.rootViewController = [[UIStoryboard main] instantiateInitialViewController]; + SignalsViewController *homeView = [SignalsViewController new]; + SignalsNavigationController *navigationController = + [[SignalsNavigationController alloc] initWithRootViewController:homeView]; + self.window.rootViewController = navigationController; + // self.window.rootViewController = [[UIStoryboard main] instantiateInitialViewController]; } else { RegistrationViewController *viewController = [RegistrationViewController new]; UINavigationController *navigationController = diff --git a/Signal/src/Storyboard/Main.storyboard b/Signal/src/Storyboard/Main.storyboard index 7f49e10e0..b69a00d4e 100644 --- a/Signal/src/Storyboard/Main.storyboard +++ b/Signal/src/Storyboard/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -12,95 +12,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -149,25 +60,6 @@ - - - - - - - - - - - - - - - - - - - @@ -372,7 +264,6 @@ - diff --git a/Signal/src/ViewControllers/CodeVerificationViewController.m b/Signal/src/ViewControllers/CodeVerificationViewController.m index b50a49cc4..ab919977d 100644 --- a/Signal/src/ViewControllers/CodeVerificationViewController.m +++ b/Signal/src/ViewControllers/CodeVerificationViewController.m @@ -265,18 +265,13 @@ NS_ASSUME_NONNULL_BEGIN dispatch_async(dispatch_get_main_queue(), ^{ [self stopActivityIndicator]; - UIStoryboard *storyboard = [UIStoryboard main]; - UIViewController *viewController = [storyboard instantiateInitialViewController]; - OWSAssert([viewController isKindOfClass:[SignalsNavigationController class]]); - SignalsNavigationController *navigationController = (SignalsNavigationController *)viewController; + SignalsViewController *homeView = [SignalsViewController new]; + homeView.newlyRegisteredUser = YES; + SignalsNavigationController *navigationController = + [[SignalsNavigationController alloc] initWithRootViewController:homeView]; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.window.rootViewController = navigationController; OWSAssert([navigationController.topViewController isKindOfClass:[SignalsViewController class]]); - - DDLogDebug(@"%@ notifying signals view controller of new user.", self.tag); - SignalsViewController *signalsViewController - = (SignalsViewController *)navigationController.topViewController; - signalsViewController.newlyRegisteredUser = YES; }); }) .catch(^(NSError *_Nonnull error) { diff --git a/Signal/src/ViewControllers/SignalsViewController.h b/Signal/src/ViewControllers/SignalsViewController.h index a1f13bf9f..3cc126370 100644 --- a/Signal/src/ViewControllers/SignalsViewController.h +++ b/Signal/src/ViewControllers/SignalsViewController.h @@ -7,17 +7,17 @@ #import "Contact.h" #import "TSGroupModel.h" -@interface SignalsViewController - : UIViewController +@interface SignalsViewController : UIViewController -@property (nonatomic, retain) IBOutlet UITableView *tableView; -@property (nonatomic, strong) IBOutlet UILabel *emptyBoxLabel; +// TODO: Remove this property. @property (nonatomic) BOOL newlyRegisteredUser; - (void)presentThread:(TSThread *)thread keyboardOnViewAppearing:(BOOL)keyboardOnViewAppearing callOnViewAppearing:(BOOL)callOnViewAppearing; + - (NSNumber *)updateInboxCountLabel; + - (IBAction)composeNew; - (void)presentTopLevelModalViewController:(UIViewController *)viewController diff --git a/Signal/src/ViewControllers/SignalsViewController.m b/Signal/src/ViewControllers/SignalsViewController.m index 2120e2cb4..7377df5b0 100644 --- a/Signal/src/ViewControllers/SignalsViewController.m +++ b/Signal/src/ViewControllers/SignalsViewController.m @@ -29,9 +29,11 @@ #import #define CELL_HEIGHT 72.0f -#define HEADER_HEIGHT 44.0f -@interface SignalsViewController () +@interface SignalsViewController () + +@property (nonatomic) UITableView *tableView; +@property (nonatomic) UILabel *emptyBoxLabel; @property (nonatomic) YapDatabaseConnection *editingDbConnection; @property (nonatomic) YapDatabaseConnection *uiDatabaseConnection; @@ -57,8 +59,10 @@ // Views -@property (weak, nonatomic) IBOutlet ReminderView *missingContactsPermissionView; -@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hideMissingContactsPermissionViewConstraint; +@property (weak, nonatomic) ReminderView *missingContactsPermissionView; +@property (weak, nonatomic) NSLayoutConstraint *hideMissingContactsPermissionViewConstraint; + +@property (nonatomic) TSThread *lastThread; @end @@ -82,6 +86,8 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder { + OWSFail(@"Do not load this from the storyboard."); + self = [super initWithCoder:aDecoder]; if (!self) { return self; @@ -146,10 +152,56 @@ #pragma mark - View Life Cycle -- (void)awakeFromNib +- (void)loadView { - [super awakeFromNib]; + [super loadView]; + + self.view.backgroundColor = [UIColor whiteColor]; + + // TODO: Remove this. [[Environment getCurrent] setSignalsViewController:self]; + + self.navigationItem.rightBarButtonItem = + [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose + target:self + action:@selector(composeNew)]; + + ReminderView *missingContactsPermissionView = [ReminderView new]; + self.missingContactsPermissionView = missingContactsPermissionView; + [self.view addSubview:missingContactsPermissionView]; + [missingContactsPermissionView autoPinWidthToSuperview]; + [missingContactsPermissionView autoPinToTopLayoutGuideOfViewController:self withInset:0]; + self.hideMissingContactsPermissionViewConstraint = + [missingContactsPermissionView autoSetDimension:ALDimensionHeight toSize:0]; + + self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; + self.tableView.delegate = self; + self.tableView.dataSource = self; + [self.view addSubview:self.tableView]; + [self.tableView autoPinWidthToSuperview]; + [self.tableView autoPinToBottomLayoutGuideOfViewController:self withInset:0]; + [self.tableView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:missingContactsPermissionView]; + + UILabel *emptyBoxLabel = [UILabel new]; + self.emptyBoxLabel = emptyBoxLabel; + [self.view addSubview:emptyBoxLabel]; + [emptyBoxLabel autoPinWidthToSuperview]; + [emptyBoxLabel autoPinToTopLayoutGuideOfViewController:self withInset:0]; + [emptyBoxLabel autoPinToBottomLayoutGuideOfViewController:self withInset:0]; + + [self updateReminderView]; +} + +- (void)viewDidLayoutSubviews +{ + [super viewDidLayoutSubviews]; + + DDLogError(@"self.tableView: %@", NSStringFromCGRect(self.tableView.frame)); +} + +- (void)updateReminderView +{ + self.hideMissingContactsPermissionViewConstraint.active = !self.shouldShowMissingContactsPermissionView; } - (void)viewDidLoad { @@ -241,6 +293,7 @@ MessagesViewController *vc = [MessagesViewController new]; TSThread *thread = [self threadForIndexPath:indexPath]; + self.lastThread = thread; [vc configureForThread:thread keyboardOnViewAppearing:NO callOnViewAppearing:NO]; [vc peekSetup]; @@ -258,7 +311,7 @@ [self.navigationController pushViewController:vc animated:NO]; } -- (IBAction)composeNew +- (void)composeNew { MessageComposeTableViewController *viewController = [MessageComposeTableViewController new]; @@ -290,7 +343,7 @@ if ([TSThread numberOfKeysInCollection] > 0) { [self.contactsManager requestSystemContactsOnceWithCompletion:^(NSError *_Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{ - self.hideMissingContactsPermissionViewConstraint.active = !self.shouldShowMissingContactsPermissionView; + [self updateReminderView]; }); }]; } @@ -299,6 +352,27 @@ self.isViewVisible = YES; [self checkIfEmptyView]; + + if (self.lastThread) { + NSIndexPath *indexPathOfLastThread = nil; + NSUInteger numberOfSections = [self.threadMappings numberOfSections]; + for (NSUInteger section = 0; !indexPathOfLastThread && section < numberOfSections; section++) { + NSUInteger numberOfItems = [self.threadMappings numberOfItemsInSection:section]; + for (NSUInteger row = 0; !indexPathOfLastThread && row < numberOfItems; row++) { + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(NSInteger)row inSection:(NSInteger)section]; + TSThread *thread = [self threadForIndexPath:indexPath]; + if ([thread.uniqueId isEqualToString:self.lastThread.uniqueId]) { + indexPathOfLastThread = indexPath; + } + } + } + + if (indexPathOfLastThread) { + [self.tableView scrollToRowAtIndexPath:indexPathOfLastThread + atScrollPosition:UITableViewScrollPositionNone + animated:NO]; + } + } } - (void)viewWillDisappear:(BOOL)animated @@ -367,6 +441,7 @@ - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; + if (self.newlyRegisteredUser) { [self.editingDbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction * _Nonnull transaction) { [self.experienceUpgradeFinder markAllAsSeenWithTransaction:transaction]; @@ -625,6 +700,7 @@ [mvc configureForThread:thread keyboardOnViewAppearing:keyboardOnViewAppearing callOnViewAppearing:callOnViewAppearing]; + self.lastThread = thread; if (self.presentedViewController) { [self.presentedViewController dismissViewControllerAnimated:YES completion:nil]; @@ -695,14 +771,16 @@ } } -#pragma mark - IBAction +#pragma mark - Groupings -- (IBAction)selectedInbox:(id)sender { +- (void)selectedInbox:(id)sender +{ self.viewingThreadsIn = kInboxState; [self changeToGrouping:TSInboxGroup]; } -- (IBAction)selectedArchive:(id)sender { +- (void)selectedArchive:(id)sender +{ self.viewingThreadsIn = kArchiveState; [self changeToGrouping:TSArchiveGroup]; } @@ -810,12 +888,6 @@ [self checkIfEmptyView]; } -- (IBAction)unwindSettingsDone:(UIStoryboardSegue *)segue { -} - -- (IBAction)unwindMessagesView:(UIStoryboardSegue *)segue { -} - - (void)checkIfEmptyView { [_tableView setHidden:NO]; [_emptyBoxLabel setHidden:NO]; diff --git a/Signal/src/network/PushManager.m b/Signal/src/network/PushManager.m index f14ba2e82..24508a3dd 100644 --- a/Signal/src/network/PushManager.m +++ b/Signal/src/network/PushManager.m @@ -155,7 +155,6 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe success:^{ // TODO do we really want to mark them all as read? [self markAllInThreadAsRead:notification.userInfo completionHandler:completionHandler]; - [[[[Environment getCurrent] signalsViewController] tableView] reloadData]; } failure:^(NSError *error) { // TODO Surface the specific error in the notification?