diff --git a/syMessaging/syMessaging/AppDelegate.m b/syMessaging/syMessaging/AppDelegate.m index df51120a5..44357d361 100644 --- a/syMessaging/syMessaging/AppDelegate.m +++ b/syMessaging/syMessaging/AppDelegate.m @@ -103,7 +103,7 @@ #pragma mark - Split view - (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController { - if ([secondaryViewController isKindOfClass:[UINavigationController class]] && [[(UINavigationController *)secondaryViewController topViewController] isKindOfClass:[RoomViewController class]] && ([(RoomViewController *)[(UINavigationController *)secondaryViewController topViewController] detailItem] == nil)) { + if ([secondaryViewController isKindOfClass:[UINavigationController class]] && [[(UINavigationController *)secondaryViewController topViewController] isKindOfClass:[RoomViewController class]] && ([(RoomViewController *)[(UINavigationController *)secondaryViewController topViewController] roomId] == nil)) { // Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded. return YES; } else { diff --git a/syMessaging/syMessaging/Base.lproj/Main.storyboard b/syMessaging/syMessaging/Base.lproj/Main.storyboard index 39858c731..baf98c1ac 100644 --- a/syMessaging/syMessaging/Base.lproj/Main.storyboard +++ b/syMessaging/syMessaging/Base.lproj/Main.storyboard @@ -182,6 +182,9 @@ + @@ -192,6 +195,7 @@ + @@ -201,6 +205,7 @@ + @@ -222,6 +227,7 @@ + @@ -416,24 +422,48 @@ - + - + + + + + + + + + + + + + @@ -445,10 +475,17 @@ + + + + + + + - + diff --git a/syMessaging/syMessaging/LoginViewController.m b/syMessaging/syMessaging/LoginViewController.m index c737ad600..25db0dbb7 100644 --- a/syMessaging/syMessaging/LoginViewController.m +++ b/syMessaging/syMessaging/LoginViewController.m @@ -34,6 +34,8 @@ NSString* const defaultHomeserver = @"http://www.matrix.org"; @property (weak, nonatomic) IBOutlet UIButton *loginBtn; @property (weak, nonatomic) IBOutlet UIButton *createAccountBtn; +@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator; + @end @implementation LoginViewController @@ -179,6 +181,8 @@ NSString* const defaultHomeserver = @"http://www.matrix.org"; return YES; } +#pragma mark - + - (IBAction)onButtonPressed:(id)sender { [self dismissKeyboard]; @@ -188,8 +192,12 @@ NSString* const defaultHomeserver = @"http://www.matrix.org"; if (matrix.homeServer) { + [_activityIndicator startAnimating]; + [matrix.homeServer loginWithUser:matrix.userLogin andPassword:_passWordTextField.text success:^(MXLoginResponse *credentials){ + [_activityIndicator stopAnimating]; + // Report credentials [matrix setUserId:credentials.user_id]; [matrix setAccessToken:credentials.access_token]; @@ -197,6 +205,8 @@ NSString* const defaultHomeserver = @"http://www.matrix.org"; [self dismissViewControllerAnimated:YES completion:nil]; } failure:^(NSError *error){ + [_activityIndicator stopAnimating]; + NSLog(@"Login failed: %@", error); //Alert user [[AppDelegate theDelegate] showErrorAsAlert:error]; diff --git a/syMessaging/syMessaging/MatrixHandler.h b/syMessaging/syMessaging/MatrixHandler.h index 616398d19..476cff6bf 100644 --- a/syMessaging/syMessaging/MatrixHandler.h +++ b/syMessaging/syMessaging/MatrixHandler.h @@ -19,15 +19,19 @@ @interface MatrixHandler : NSObject @property (strong, nonatomic) MXHomeServer *homeServer; +@property (strong, nonatomic) MXData *mxData; + @property (strong, nonatomic) NSString *homeServerURL; @property (strong, nonatomic) NSString *userLogin; @property (strong, nonatomic) NSString *userId; @property (strong, nonatomic) NSString *accessToken; +@property (nonatomic,readonly) BOOL isLogged; +@property (nonatomic,readonly) BOOL isInitialSyncDone; + + (id)sharedHandler; -- (BOOL)isLogged; - (void)logout; @end diff --git a/syMessaging/syMessaging/MatrixHandler.m b/syMessaging/syMessaging/MatrixHandler.m index e5bb4b882..da21c83fd 100644 --- a/syMessaging/syMessaging/MatrixHandler.m +++ b/syMessaging/syMessaging/MatrixHandler.m @@ -22,9 +22,8 @@ static MatrixHandler *sharedHandler = nil; @interface MatrixHandler () @property (strong, nonatomic) MXSession *mxSession; -@property (strong, nonatomic) MXData *mxData; -@property BOOL isInitialSyncDone; +@property (nonatomic,readwrite) BOOL isInitialSyncDone; @end @@ -65,7 +64,7 @@ static MatrixHandler *sharedHandler = nil; if (self.mxSession) { self.mxData = [[MXData alloc] initWithMatrixSession:self.mxSession]; [self.mxData start:^{ - _isInitialSyncDone = YES; + self.isInitialSyncDone = YES; } failure:^(NSError *error) { NSLog(@"Initial Sync failed: %@", error); //Alert user @@ -79,7 +78,7 @@ static MatrixHandler *sharedHandler = nil; self.mxData = nil; [self.mxSession close]; self.mxSession = nil; - _isInitialSyncDone = NO; + self.isInitialSyncDone = NO; } - (void)dealloc { diff --git a/syMessaging/syMessaging/RecentsViewController.m b/syMessaging/syMessaging/RecentsViewController.m index 13c0482ba..27301fb0d 100644 --- a/syMessaging/syMessaging/RecentsViewController.m +++ b/syMessaging/syMessaging/RecentsViewController.m @@ -17,9 +17,24 @@ #import "RecentsViewController.h" #import "RoomViewController.h" -@interface RecentsViewController () +#import "AppDelegate.h" +#import "MatrixHandler.h" -@property NSMutableArray *objects; +@interface RecentsTableViewCell : UITableViewCell + +@property (weak, nonatomic) IBOutlet UILabel *roomTitle; +@property (weak, nonatomic) IBOutlet UILabel *lastEventDescription; +@property (weak, nonatomic) IBOutlet UILabel *recentDate; + +@end + +@implementation RecentsTableViewCell +@end + +@interface RecentsViewController () +@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator; + +@property NSArray *recents; @end @implementation RecentsViewController @@ -40,6 +55,15 @@ UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; self.navigationItem.rightBarButtonItem = addButton; self.roomViewController = (RoomViewController *)[[self.splitViewController.viewControllers lastObject] topViewController]; + + // Add activity indicator + [self.view addSubview:_activityIndicator]; + _activityIndicator.center = self.view.center; + [self.view bringSubviewToFront:_activityIndicator]; +} + +- (void)dealloc { + self.recents = nil; } - (void)didReceiveMemoryWarning { @@ -47,13 +71,57 @@ // Dispose of any resources that can be recreated. } -- (void)insertNewObject:(id)sender { - if (!self.objects) { - self.objects = [[NSMutableArray alloc] init]; +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; + + // Refresh recents table + [self refresh]; + [[MatrixHandler sharedHandler] addObserver:self forKeyPath:@"isInitialSyncDone" options:0 context:nil]; +} + +- (void)viewWillDisappear:(BOOL)animated { + [super viewWillDisappear:animated]; + [[MatrixHandler sharedHandler] removeObserver:self forKeyPath:@"isInitialSyncDone"]; +} + +#pragma mark - recents + +- (void)refresh { + [_activityIndicator startAnimating]; + + MatrixHandler *mxHandler = [MatrixHandler sharedHandler]; + if ([mxHandler isInitialSyncDone] || [mxHandler isLogged] == NO) { + // Update recents + if (mxHandler.mxData) { + self.recents = mxHandler.mxData.recents; + } else { + self.recents = nil; + } + + [self.tableView reloadData]; + [_activityIndicator stopAnimating]; + } +} + +- (void)insertNewObject:(id)sender { +// if (!self.recents) { +// self.recents = [[NSMutableArray alloc] init]; +// } +// [self.recents insertObject:[NSDate date] atIndex:0]; +// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; +// [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; +} + +#pragma mark - KVO + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if ([@"isInitialSyncDone" isEqualToString:keyPath]) + { + dispatch_async(dispatch_get_main_queue(), ^{ + [self refresh]; + }); } - [self.objects insertObject:[NSDate date] atIndex:0]; - NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; - [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } #pragma mark - Segues @@ -61,18 +129,17 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"showDetail"]) { NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; - NSDate *object = self.objects[indexPath.row]; + MXEvent *mxEvent = self.recents[indexPath.row]; UIViewController *controller; if ([[segue destinationViewController] isKindOfClass:[UINavigationController class]]) { controller = [[segue destinationViewController] topViewController]; - } - else { + } else { controller = [segue destinationViewController]; } if ([controller isKindOfClass:[RoomViewController class]]) { - [(RoomViewController *)controller setDetailItem:object]; + [(RoomViewController *)controller setRoomId:mxEvent.room_id]; } controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem; @@ -87,14 +154,32 @@ } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return self.objects.count; + return self.recents.count; +} + +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath +{ + return 70; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; + RecentsTableViewCell *cell = (RecentsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"RecentsCell" forIndexPath:indexPath]; - NSDate *object = self.objects[indexPath.row]; - cell.textLabel.text = [object description]; + MXEvent *mxEvent = self.recents[indexPath.row]; + MXRoomData *mxRoomData = [[[MatrixHandler sharedHandler] mxData] getRoomData:mxEvent.room_id]; + + cell.roomTitle.text = [mxRoomData room_id]; // TODO use room display name + cell.lastEventDescription.text = [mxEvent description]; + + NSDate *date = [NSDate dateWithTimeIntervalSince1970:mxEvent.ts]; + NSString *dateFormat = @"MMM dd HH:mm"; + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0]]]; + [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; + [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; + [dateFormatter setDateFormat:dateFormat]; + cell.recentDate.text = [dateFormatter stringFromDate:date]; + return cell; } @@ -105,8 +190,8 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { - [self.objects removeObjectAtIndex:indexPath.row]; - [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; +// [self.recents removeObjectAtIndex:indexPath.row]; +// [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } diff --git a/syMessaging/syMessaging/RoomViewController.h b/syMessaging/syMessaging/RoomViewController.h index 5e095ce48..782da00fa 100644 --- a/syMessaging/syMessaging/RoomViewController.h +++ b/syMessaging/syMessaging/RoomViewController.h @@ -18,7 +18,8 @@ @interface RoomViewController : UIViewController -@property (strong, nonatomic) id detailItem; +@property (strong, nonatomic) NSString *roomId; + @property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel; @end diff --git a/syMessaging/syMessaging/RoomViewController.m b/syMessaging/syMessaging/RoomViewController.m index 8bc19ce17..02fc4a499 100644 --- a/syMessaging/syMessaging/RoomViewController.m +++ b/syMessaging/syMessaging/RoomViewController.m @@ -16,27 +16,30 @@ #import "RoomViewController.h" +#import "MatrixHandler.h" + @interface RoomViewController () +@property (strong, nonatomic) MXRoomData *mxRoomData; + @end @implementation RoomViewController #pragma mark - Managing the detail item -- (void)setDetailItem:(id)newDetailItem { - if (_detailItem != newDetailItem) { - _detailItem = newDetailItem; - - // Update the view. - [self configureView]; - } +- (void)setDetailItem:(NSString*)roomId { + _roomId = roomId; + + // Update the view. + [self configureView]; } - (void)configureView { // Update the user interface for the detail item. - if (self.detailItem) { - self.detailDescriptionLabel.text = [self.detailItem description]; + if (self.roomId) { + self.mxRoomData = [[[MatrixHandler sharedHandler] mxData] getRoomData:self.roomId]; + self.detailDescriptionLabel.text = [self.mxRoomData.lastEvent event_id]; } } @@ -46,6 +49,10 @@ [self configureView]; } +- (void)dealloc { + _mxRoomData = nil; +} + - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.