From a04f0db29cb280eb45888d3a56967cf9adc6214d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 4 Mar 2018 11:50:14 +0300 Subject: ChatViewController --- Juick/APIClient.h | 1 + Juick/APIClient.m | 21 +++++++++ Juick/Main.storyboard | 59 +++++++++++++++++++++--- Juick/ViewControllers/ChatViewController.h | 15 +++++++ Juick/ViewControllers/ChatViewController.m | 65 +++++++++++++++++++++++++++ Juick/ViewControllers/DialogsViewController.m | 13 ++++++ Juick/Views/BubbleMessageCell.h | 23 ++++++++++ Juick/Views/BubbleMessageCell.m | 43 ++++++++++++++++++ Juick/Views/BubbleMessageCell.xib | 29 ++++++++---- Juick/Views/FixedLabel.h | 13 ++++++ Juick/Views/FixedLabel.m | 21 +++++++++ 11 files changed, 289 insertions(+), 14 deletions(-) create mode 100644 Juick/ViewControllers/ChatViewController.h create mode 100644 Juick/ViewControllers/ChatViewController.m create mode 100644 Juick/Views/BubbleMessageCell.h create mode 100644 Juick/Views/BubbleMessageCell.m create mode 100644 Juick/Views/FixedLabel.h create mode 100644 Juick/Views/FixedLabel.m (limited to 'Juick') diff --git a/Juick/APIClient.h b/Juick/APIClient.h index 668a0a9..46e86c7 100644 --- a/Juick/APIClient.h +++ b/Juick/APIClient.h @@ -20,6 +20,7 @@ -(void) postMessage:(NSString *)text result:(void(^)(Message *, NSError *))callback; -(void) postReplyToThread:(NSNumber *)mid inReplyTo:(NSNumber *)rid text:(NSString *)text result:(void(^)(Message *, NSError *))callback; -(void) fetchChats:(void(^)(NSArray *, NSError *))callback; +-(void) fetchChatWithUser:(NSString *)uname callback:(void(^)(NSArray *, NSError *))callback; +(NSString *) messagesUrl; +(NSString *) threadUrl; diff --git a/Juick/APIClient.m b/Juick/APIClient.m index 5352320..aa5530b 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -122,6 +122,27 @@ }]; }]; } +-(void) fetchChatWithUser:(NSString *)uname callback:(void (^)(NSArray *, NSError *))callback { + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; + NSDictionary *params = @{@"uname": uname}; + [self.backgroundQueue addOperationWithBlock:^{ + [manager GET:@"/pm" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSMutableArray *messages = [NSMutableArray new]; + NSArray *messagesList = (NSArray *)responseObject; + [messagesList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + [messages addObject:[Message yy_modelWithJSON:obj]]; + }]; + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + callback(messages, nil); + }]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + NSLog(@"Error: %@", [error localizedDescription]); + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + callback(nil, error); + }]; + }]; + }]; +} +(NSString *) messagesUrl { diff --git a/Juick/Main.storyboard b/Juick/Main.storyboard index 3383f18..d6698e4 100644 --- a/Juick/Main.storyboard +++ b/Juick/Main.storyboard @@ -231,13 +231,16 @@ - + - + + + + @@ -250,7 +253,24 @@ - + + + + + + + + + + + + + + + + + + @@ -296,6 +316,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -307,7 +356,7 @@ - + @@ -321,6 +370,6 @@ - + diff --git a/Juick/ViewControllers/ChatViewController.h b/Juick/ViewControllers/ChatViewController.h new file mode 100644 index 0000000..7ea8030 --- /dev/null +++ b/Juick/ViewControllers/ChatViewController.h @@ -0,0 +1,15 @@ +// +// ChatViewController.h +// Juick +// +// Created by Vitaly Takmazov on 04/03/2018. +// Copyright © 2018 com.juick. All rights reserved. +// + +#import +#import "User.h" + +@interface ChatViewController : UITableViewController +@property(nonatomic, strong) NSMutableArray *messages; +@property(nonatomic, strong) NSString *uname; +@end diff --git a/Juick/ViewControllers/ChatViewController.m b/Juick/ViewControllers/ChatViewController.m new file mode 100644 index 0000000..f88bfa5 --- /dev/null +++ b/Juick/ViewControllers/ChatViewController.m @@ -0,0 +1,65 @@ +// +// ChatViewController.m +// Juick +// +// Created by Vitaly Takmazov on 04/03/2018. +// Copyright © 2018 com.juick. All rights reserved. +// + +#import "ChatViewController.h" +#import "BubbleMessageCell.h" +#import "APIClient.h" + +@interface ChatViewController () + +@end + +@implementation ChatViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + + [self.tableView registerNib:[UINib nibWithNibName:@"BubbleMessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"bubbleMessageCell"]; + self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; + self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; + self.tableView.allowsSelection = NO; + self.messages = [NSMutableArray array]; + [[APIClient sharedClient] fetchChatWithUser:self.uname callback:^(NSArray *messages, NSError *err) { + if (err == nil) { + [self.messages addObjectsFromArray:[[messages reverseObjectEnumerator] allObjects]]; + NSMutableArray *indexPaths = [NSMutableArray new]; + for (NSUInteger index = 0; index < [messages count]; index++) { + [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]]; + } + [self.tableView beginUpdates]; + [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES]; + [self.tableView endUpdates]; + [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.messages count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; + } + }]; +} + +- (void)didReceiveMemoryWarning { + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +#pragma mark - Table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return [self.messages count]; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + BubbleMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bubbleMessageCell" forIndexPath:indexPath]; + + [cell configureWithMessage:[self.messages objectAtIndex:indexPath.row]]; + + return cell; +} + +@end diff --git a/Juick/ViewControllers/DialogsViewController.m b/Juick/ViewControllers/DialogsViewController.m index 8eea017..878e912 100644 --- a/Juick/ViewControllers/DialogsViewController.m +++ b/Juick/ViewControllers/DialogsViewController.m @@ -7,6 +7,7 @@ // #import "DialogsViewController.h" +#import "ChatViewController.h" #import "ColorScheme.h" #import "ConversationCell.h" #import "APIClient.h" @@ -53,5 +54,17 @@ return cell; } +-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { + [self performSegueWithIdentifier:@"chatSegue" sender:self]; +} + +-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + if ([segue.identifier isEqualToString:@"chatSegue"]) { + Chat *chat = [self.chats objectAtIndex:[self.tableView indexPathForSelectedRow].row]; + ChatViewController *vc = (ChatViewController *)segue.destinationViewController; + [vc setUname:chat.uname]; + } +} + @end diff --git a/Juick/Views/BubbleMessageCell.h b/Juick/Views/BubbleMessageCell.h new file mode 100644 index 0000000..1f5d2e9 --- /dev/null +++ b/Juick/Views/BubbleMessageCell.h @@ -0,0 +1,23 @@ +// +// BubbleMessageCell.h +// Juick +// +// Created by Vitaly Takmazov on 04/03/2018. +// Copyright © 2018 com.juick. All rights reserved. +// + +#import +#import "Message.h" +#import "FixedLabel.h" + +@import TTTAttributedLabel; + +@interface BubbleMessageCell : UITableViewCell +@property (strong, nonatomic) IBOutlet UIImageView *avatarView; +@property (strong, nonatomic) IBOutlet UILabel *unreadMarker; +@property (strong, nonatomic) IBOutlet UILabel *name; +@property (strong, nonatomic) IBOutlet FixedLabel *message; + +-(void) configureWithMessage:(Message *) message; + +@end diff --git a/Juick/Views/BubbleMessageCell.m b/Juick/Views/BubbleMessageCell.m new file mode 100644 index 0000000..b201664 --- /dev/null +++ b/Juick/Views/BubbleMessageCell.m @@ -0,0 +1,43 @@ +// +// BubbleMessageCell.m +// Juick +// +// Created by Vitaly Takmazov on 04/03/2018. +// Copyright © 2018 com.juick. All rights reserved. +// + +#import "BubbleMessageCell.h" +#import "ColorScheme.h" +@import YYWebImage; + +@implementation BubbleMessageCell + +- (void)awakeFromNib { + [super awakeFromNib]; + // Initialization code +} + +- (void)setSelected:(BOOL)selected animated:(BOOL)animated { + [super setSelected:selected animated:animated]; + + // Configure the view for the selected state +} + +-(void) configureWithMessage:(Message *)message { + self.name.text = message.user.uname; + self.message.text = message.text; + self.unreadMarker.text = @""; + [self.avatarView yy_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://i.juick.com/a/%d.png", [message.user.uid intValue]]] options:YYWebImageOptionProgressiveBlur|YYWebImageOptionSetImageWithFadeAnimation]; + self.message.enabledTextCheckingTypes = NSTextCheckingTypeLink; + self.message.backgroundColor = [ColorScheme mainBackground]; + self.message.layer.cornerRadius = 4; + self.message.clipsToBounds = YES; + self.message.textInsets = UIEdgeInsetsMake(4.0f, 8.0f, 4.0f, 8.0f); +} + +-(void) attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url { + [[UIApplication sharedApplication] openURL:url options: + @{} completionHandler:nil]; +} + +@end diff --git a/Juick/Views/BubbleMessageCell.xib b/Juick/Views/BubbleMessageCell.xib index a2b6bc3..b88ab17 100644 --- a/Juick/Views/BubbleMessageCell.xib +++ b/Juick/Views/BubbleMessageCell.xib @@ -1,30 +1,38 @@ - + + + + - + + - + - + + + + + - - - - - + + + + diff --git a/Juick/Views/FixedLabel.h b/Juick/Views/FixedLabel.h new file mode 100644 index 0000000..f05ac94 --- /dev/null +++ b/Juick/Views/FixedLabel.h @@ -0,0 +1,13 @@ +// +// FixedLabel.h +// Juick +// +// Created by Vitaly Takmazov on 04/03/2018. +// Copyright © 2018 com.juick. All rights reserved. +// + +#import + +@interface FixedLabel : TTTAttributedLabel + +@end diff --git a/Juick/Views/FixedLabel.m b/Juick/Views/FixedLabel.m new file mode 100644 index 0000000..24d8648 --- /dev/null +++ b/Juick/Views/FixedLabel.m @@ -0,0 +1,21 @@ +// +// FixedLabel.m +// Juick +// +// Created by Vitaly Takmazov on 04/03/2018. +// Copyright © 2018 com.juick. All rights reserved. +// + +#import "FixedLabel.h" + +@implementation FixedLabel + +/* +// Only override drawRect: if you perform custom drawing. +// An empty implementation adversely affects performance during animation. +- (void)drawRect:(CGRect)rect { + // Drawing code +} +*/ + +@end -- cgit v1.2.3