From 6499148cb3f62e20534b62c181125ebeacfa3438 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 27 Sep 2017 22:24:38 +0300 Subject: cleanup --- Juick/ViewControllers/MessagesViewController.m | 134 ++++++++----------------- 1 file changed, 41 insertions(+), 93 deletions(-) (limited to 'Juick/ViewControllers/MessagesViewController.m') diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m index 95879bf..b877cd1 100644 --- a/Juick/ViewControllers/MessagesViewController.m +++ b/Juick/ViewControllers/MessagesViewController.m @@ -6,58 +6,34 @@ // Copyright (c) 2013 com.juick. All rights reserved. // -#import "SWRevealViewController/SWRevealViewController.h" #import "MessagesViewController.h" +@import YYWebImage; #import "MessageCell.h" #import "Message.h" #import "ColorScheme.h" #import "NewPostViewController.h" +#import "LoginViewController.h" +#import "ThreadViewController.h" #import "NSURL+PathParameters.h" -static NSString *CellIdentifier = @"MessageCell"; - @interface MessagesViewController (); @property(nonatomic, strong) NSMutableArray *messages; @property(nonatomic, assign) Boolean dataLoading; -@property(nonatomic, strong) NSString *path; -@property(nonatomic, strong) NSMutableDictionary *params; -@property(nonatomic, strong) dispatch_queue_t concurrent_queue; -@property (nonatomic, readwrite, retain) UIView *inputAccessoryView; - @end @implementation MessagesViewController -- (id)init { - if (self = [super init]) { - self.concurrent_queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL); - }; - return self; -} - -- (void)loadFromPath:(NSString *)messagesPath withParams:(NSDictionary *)params withTitle:(NSString *)title -{ - [self setPath:messagesPath]; - [self setParams:[[NSMutableDictionary alloc] initWithDictionary:params]]; - self.title = title; - __weak MessagesViewController * weakSelf = self; - dispatch_sync(self.concurrent_queue, ^{ - [self.messages removeAllObjects]; - [weakSelf refreshData]; - }); -} - - (void) refreshData { self.dataLoading = YES; - [Message pullNextFromPath:self.path params:self.params callback:^(NSArray *next) { + [[APIClient sharedClient] pullNextFromPath:self.path params:self.params callback:^(NSArray *next, NSError *err) { NSArray *newMsgs = next; - if ([self.path isEqualToString:[Message threadUrl]]) { + if ([self.path isEqualToString:[APIClient threadUrl]]) { NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue]; NSUInteger count = [next count] - 1; NSUInteger oldCount = [self.messages count]; @@ -84,30 +60,37 @@ static NSString *CellIdentifier = @"MessageCell"; - (void)viewDidLoad { [super viewDidLoad]; + self.dataLoading = NO; [self.view setBackgroundColor:[ColorScheme mainBackground]]; self.messages = [NSMutableArray array]; + self.params = [NSMutableDictionary new]; - SWRevealViewController *revealController = [self revealViewController]; - if (revealController) { - // TODO: add to thread view too - [self.view addGestureRecognizer:[revealController panGestureRecognizer]]; - } - - [self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]; self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 500.0f; self.refreshControl = [UIRefreshControl new]; - [self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];} + [self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged]; + if ([User isAuthenticated]) { + [User checkIsValid:^(BOOL success) { + if (success) { + self.path = [APIClient feedUrl]; + [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge) categories:nil]]; + [self refreshData]; + } else { + [User throwUnableToLogin]; + } + }]; + + } else { + self.path = [APIClient messagesUrl]; + [self refreshData]; + } + self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"profile_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(composePressed)]; + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"settings_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(composePressed)]; +} - (void) composePressed { - CATransition* transition = [CATransition animation]; - transition.duration = 0.3; - transition.type = kCATransitionFade; - transition.subtype = kCATransitionFromTop; - [self.navigationController.view.layer addAnimation:transition forKey:kCATransition]; - [self.navigationController pushViewController:[[NewPostViewController alloc] init] animated:NO]; } - (void)viewDidAppear:(BOOL)animated @@ -137,32 +120,20 @@ static NSString *CellIdentifier = @"MessageCell"; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - __block NSInteger count; - dispatch_sync(self.concurrent_queue, ^{ - count = self.messages.count; - }); - return count; + return self.messages.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; - __block Message *msg; - dispatch_sync(self.concurrent_queue, ^{ - msg = [self.messages objectAtIndex:indexPath.row]; - }); + MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messageCell" forIndexPath:indexPath]; + Message *msg = [self.messages objectAtIndex:indexPath.row]; cell.layer.borderWidth = 6; cell.layer.borderColor = [ColorScheme mainBackground].CGColor; [cell setMessage:msg]; if ([msg.attach length] > 0) { [cell.attach yy_setImageWithURL:[NSURL URLWithString:msg.attach] placeholder:[UIImage imageNamed:@"AttachPlaceholder"] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) { - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - [cell setNeedsUpdateConstraints]; - [cell updateConstraintsIfNeeded]; - [cell setNeedsLayout]; - [cell layoutIfNeeded]; - }]; + [cell setNeedsUpdateConstraints]; + [cell updateConstraintsIfNeeded]; }]; - } else { cell.attach.image = nil; } @@ -171,14 +142,12 @@ static NSString *CellIdentifier = @"MessageCell"; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - if ([self.path isEqualToString:[Message threadUrl]]) + if ([self.path isEqualToString:[APIClient threadUrl]]) return; - __block Message *msg; - dispatch_sync(self.concurrent_queue, ^{ - msg = [self.messages objectAtIndex:indexPath.row]; - }); - MessagesViewController *threadViewController = [[MessagesViewController alloc] init]; - [threadViewController loadFromPath:[Message threadUrl] withParams:[NSDictionary dictionaryWithObjectsAndKeys:msg.mid, @"mid", nil] withTitle:@"Thread"]; + Message *msg = [self.messages objectAtIndex:indexPath.row]; + ThreadViewController *threadViewController = [[ThreadViewController alloc] init]; + [threadViewController setPath:[APIClient threadUrl]]; + [threadViewController setParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:msg.mid, @"mid", nil]]; [self.navigationController pushViewController:threadViewController animated:NO]; } @@ -186,36 +155,15 @@ static NSString *CellIdentifier = @"MessageCell"; CGFloat actualPosition = scrollView_.contentOffset.y; CGFloat contentHeight = scrollView_.contentSize.height - scrollView_.contentSize.height / 2; if (actualPosition >= contentHeight && !self.dataLoading) { - __block Message *lastMsg; - dispatch_sync(self.concurrent_queue, ^{ - lastMsg = [self.messages lastObject]; + Message *lastMsg = [self.messages lastObject]; + if (lastMsg != nil) { NSNumber *lastMid = lastMsg.mid; - if (![self.path isEqualToString:[Message threadUrl]]) { - [self.params setValue:lastMid forKey:@"before_mid"]; + if (![self.path isEqualToString:[APIClient threadUrl]]) { + [self.params setObject:lastMid forKey:@"before_mid"]; } [self refreshData]; - }); - } -} - -- (BOOL) canBecomeFirstResponder { - return [self.path isEqualToString:[Message threadUrl]]; -} - -- (UIView *)inputAccessoryView { - if (!_inputAccessoryView) { - CGRect viewBounds = self.view.bounds; - CGRect frame = CGRectMake(0, viewBounds.size.height - PHFComposeBarViewInitialHeight, viewBounds.size.width, - PHFComposeBarViewInitialHeight); - PHFComposeBarView *_view = [[PHFComposeBarView alloc] initWithFrame:frame]; - _view.backgroundColor = [UIColor whiteColor]; - _view.buttonTintColor = [ColorScheme linkColor]; - _view.maxLinesCount = 4; - _view.utilityButtonImage = [UIImage imageNamed:@"Camera"]; - _view.delegate = self; - _inputAccessoryView = _view; + } } - return _inputAccessoryView; } @end -- cgit v1.2.3