Commit 7aa2027583dc8d6ca20d372caa8a9ae1ac86714d

Authored by nvtu11790@gmail.com
1 parent 923306f25e

Add API get recent tweets list in SNS Screen

Showing 14 changed files with 295 additions and 26 deletions Side-by-side Diff

LifeLog/LifeLog.xcodeproj/project.pbxproj
... ... @@ -57,6 +57,7 @@
57 57 E99E13AD1F336F4500C78787 /* CollectionView.xib in Resources */ = {isa = PBXBuildFile; fileRef = E99E13AC1F336F4500C78787 /* CollectionView.xib */; };
58 58 E99E13B11F33720600C78787 /* LabelCollectionViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E99E13AF1F33720600C78787 /* LabelCollectionViewCell.m */; };
59 59 E99E13B21F33720600C78787 /* LabelCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = E99E13B01F33720600C78787 /* LabelCollectionViewCell.xib */; };
  60 + E9D909211F3D559D004CF99F /* TweetObject.m in Sources */ = {isa = PBXBuildFile; fileRef = E9D909201F3D559D004CF99F /* TweetObject.m */; };
60 61 E9F73DCC1F338A0F004A3A6F /* HistoryListTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E9F73DCA1F338A0F004A3A6F /* HistoryListTableViewCell.m */; };
61 62 E9F73DCD1F338A0F004A3A6F /* HistoryListTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = E9F73DCB1F338A0F004A3A6F /* HistoryListTableViewCell.xib */; };
62 63 /* End PBXBuildFile section */
... ... @@ -167,6 +168,8 @@
167 168 E99E13AE1F33720600C78787 /* LabelCollectionViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LabelCollectionViewCell.h; sourceTree = "<group>"; };
168 169 E99E13AF1F33720600C78787 /* LabelCollectionViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LabelCollectionViewCell.m; sourceTree = "<group>"; };
169 170 E99E13B01F33720600C78787 /* LabelCollectionViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LabelCollectionViewCell.xib; sourceTree = "<group>"; };
  171 + E9D9091F1F3D559D004CF99F /* TweetObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TweetObject.h; sourceTree = "<group>"; };
  172 + E9D909201F3D559D004CF99F /* TweetObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TweetObject.m; sourceTree = "<group>"; };
170 173 E9F73DC91F338A0F004A3A6F /* HistoryListTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HistoryListTableViewCell.h; sourceTree = "<group>"; };
171 174 E9F73DCA1F338A0F004A3A6F /* HistoryListTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HistoryListTableViewCell.m; sourceTree = "<group>"; };
172 175 E9F73DCB1F338A0F004A3A6F /* HistoryListTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = HistoryListTableViewCell.xib; sourceTree = "<group>"; };
... ... @@ -238,6 +241,8 @@
238 241 E9373E491F35AA180059355A /* HistoryObject.m */,
239 242 E9373E4B1F361A230059355A /* HistoryGraphObject.h */,
240 243 E9373E4C1F361A230059355A /* HistoryGraphObject.m */,
  244 + E9D9091F1F3D559D004CF99F /* TweetObject.h */,
  245 + E9D909201F3D559D004CF99F /* TweetObject.m */,
241 246 );
242 247 name = Model;
243 248 sourceTree = "<group>";
... ... @@ -727,6 +732,7 @@
727 732 6E8DA0F71F2BAC3400CCE941 /* TodayViewController.m in Sources */,
728 733 6E7F93451F35872B00EFBC71 /* UIScrollView+TPKeyboardAvoidingAdditions.m in Sources */,
729 734 E9682E2E1F39675A00FE05A2 /* RankingTableViewCell.m in Sources */,
  735 + E9D909211F3D559D004CF99F /* TweetObject.m in Sources */,
730 736 6E48AF0D1F2F9D3300C1D184 /* CustomTextField.m in Sources */,
731 737 E99E13AB1F336F3600C78787 /* CollectionView.m in Sources */,
732 738 6E84E3B01F27A975001EB88E /* HomeViewController.m in Sources */,
LifeLog/LifeLog/Entities.h
... ... @@ -11,6 +11,7 @@
11 11  
12 12 #import <MBProgressHUD/MBProgressHUD.h>
13 13  
  14 +#import "TweetObject.h"
14 15 #import "HistoryGraphObject.h"
15 16 #import "HistoryObject.h"
16 17 #import "User.h"
LifeLog/LifeLog/HistoryViewController.m
... ... @@ -150,16 +150,19 @@
150 150 if(self.tableListHistory.alpha == 0.0) {
151 151 [[ServerAPI server] requestHistory:token atDate:_curDate withType:type andMode:mode CompletionHandler:^(HistoryObject *object, NSError *error) {
152 152 HistoryViewController __weak *weakSelf = self;
  153 + dispatch_async(dispatch_get_main_queue(), ^{
  154 + if(hudView != nil) {
  155 + [hudView hideAnimated:true];
  156 + }
  157 + });
153 158 if(error == nil) {
154 159 _curHisObj = object;
155 160 dispatch_async(dispatch_get_main_queue(), ^{
156   - [hudView hideAnimated:true];
157 161 [weakSelf updateView];
158 162 });
159 163 }
160 164 else {
161 165 dispatch_async(dispatch_get_main_queue(), ^{
162   - [hudView hideAnimated:true];
163 166 NSString *message = [error.userInfo objectForKey:@"message"];
164 167 [Utilities showErrorMessage:message withViewController:weakSelf];
165 168 });
166 169  
167 170  
... ... @@ -185,16 +188,19 @@
185 188 else {
186 189 [[ServerAPI server] requestHistoryList:token withType:type andMode:mode AtPage:1 CompletionHandler:^(NSMutableArray *object, NSError *error) {
187 190 HistoryViewController __weak *weakSelf = self;
  191 + dispatch_async(dispatch_get_main_queue(), ^{
  192 + if(hudView != nil) {
  193 + [hudView hideAnimated:true];
  194 + }
  195 + });
188 196 if(error == nil) {
189 197 _curHisList = object;
190 198 dispatch_async(dispatch_get_main_queue(), ^{
191   - [hudView hideAnimated:true];
192 199 [weakSelf.tableListHistory reloadData];
193 200 });
194 201 }
195 202 else {
196 203 dispatch_async(dispatch_get_main_queue(), ^{
197   - [hudView hideAnimated:true];
198 204 NSString *message = [error.userInfo objectForKey:@"message"];
199 205 [Utilities showErrorMessage:message withViewController:weakSelf];
200 206 });
LifeLog/LifeLog/SNSRecentTopicTableViewCell.h
... ... @@ -12,7 +12,7 @@
12 12  
13 13 @property (weak, nonatomic) IBOutlet UIImageView *imgAvatar;
14 14 @property (weak, nonatomic) IBOutlet UILabel *lblDateTime;
15   -@property (weak, nonatomic) IBOutlet UILabel *lblLocation;
  15 +@property (weak, nonatomic) IBOutlet UILabel *lblUsername;
16 16 @property (weak, nonatomic) IBOutlet UILabel *lblDes;
17 17 @property (weak, nonatomic) IBOutlet UILabel *lblMode;
18 18 @property (weak, nonatomic) IBOutlet UILabel *lblDistance;
LifeLog/LifeLog/SNSRecentTopicTableViewCell.xib
... ... @@ -5,7 +5,6 @@
5 5 </device>
6 6 <dependencies>
7 7 <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
8   - <capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
9 8 <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
10 9 </dependencies>
11 10 <objects>
12 11  
13 12  
14 13  
... ... @@ -15,14 +14,14 @@
15 14 <rect key="frame" x="0.0" y="0.0" width="320" height="100"/>
16 15 <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
17 16 <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">
18   - <rect key="frame" x="0.0" y="0.0" width="320" height="99.5"/>
  17 + <rect key="frame" x="0.0" y="0.0" width="320" height="100"/>
19 18 <autoresizingMask key="autoresizingMask"/>
20 19 <subviews>
21   - <imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="avatar_default" translatesAutoresizingMaskIntoConstraints="NO" id="ybP-aK-loM">
22   - <rect key="frame" x="8" y="17.5" width="50" height="65"/>
  20 + <imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="avatar_default" translatesAutoresizingMaskIntoConstraints="NO" id="ybP-aK-loM">
  21 + <rect key="frame" x="8" y="10" width="50" height="80"/>
23 22 <constraints>
24   - <constraint firstAttribute="width" secondItem="ybP-aK-loM" secondAttribute="height" multiplier="10:13" id="6rL-og-ZNA"/>
25 23 <constraint firstAttribute="width" constant="50" id="Bkh-J5-a8V"/>
  24 + <constraint firstAttribute="height" constant="80" id="C55-ta-Dpa"/>
26 25 </constraints>
27 26 </imageView>
28 27 <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Sv2-7A-ngx">
29 28  
... ... @@ -129,8 +128,8 @@
129 128 <outlet property="lblDes" destination="rfd-Mx-5Wv" id="cHd-S3-6NO"/>
130 129 <outlet property="lblDistance" destination="Qs8-Mr-CzW" id="19w-MP-GTq"/>
131 130 <outlet property="lblDuration" destination="JHq-wA-TUm" id="cKs-J1-eJa"/>
132   - <outlet property="lblLocation" destination="3XR-JQ-EfG" id="xnZ-Ab-KhF"/>
133 131 <outlet property="lblMode" destination="w7w-q1-yZi" id="uke-pj-NlL"/>
  132 + <outlet property="lblUsername" destination="3XR-JQ-EfG" id="Enq-ZC-f2b"/>
134 133 </connections>
135 134 <point key="canvasLocation" x="25" y="113"/>
136 135 </tableViewCell>
LifeLog/LifeLog/SNSViewController.h
... ... @@ -8,7 +8,13 @@
8 8  
9 9 #import <UIKit/UIKit.h>
10 10  
11   -@interface SNSViewController : UIViewController
  11 +@interface SNSViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
  12 + BOOL _firstTime;
  13 + BOOL _isLoading;
  14 + BOOL _isEndOfResult;
  15 + int _curPage;
  16 + NSMutableArray * _curTweetList;
  17 +}
12 18  
13 19 @property (weak, nonatomic) IBOutlet UITableView *tableRecent;
14 20  
LifeLog/LifeLog/SNSViewController.m
... ... @@ -8,6 +8,9 @@
8 8  
9 9 #import "SNSViewController.h"
10 10 #import "SNSRecentTopicTableViewCell.h"
  11 +#import "ServerAPI.h"
  12 +#import "Utilities.h"
  13 +#import <SDWebImage/UIImageView+WebCache.h>
11 14  
12 15 @interface SNSViewController ()
13 16  
14 17  
... ... @@ -19,7 +22,14 @@
19 22 [super viewDidLoad];
20 23 // Do any additional setup after loading the view from its nib.
21 24 self.title = NSLocalizedString(@"lifelog.tapbar.sns", nil);
  25 + _firstTime = true;
  26 + _curPage = 1;
  27 + _isEndOfResult = false;
22 28  
  29 + _curTweetList = [[NSMutableArray alloc] init];
  30 +
  31 + [self callRequestToUpdateData];
  32 +
23 33 //register nib for table view
24 34 [self.tableRecent registerNib:[UINib nibWithNibName:@"SNSRecentTopicTableViewCell" bundle:nil] forCellReuseIdentifier:@"RecentTopicCell"];
25 35 }
26 36  
27 37  
28 38  
29 39  
... ... @@ -32,27 +42,97 @@
32 42  
33 43 #pragma mark UITableView Delegate
34 44 - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
35   - // if(_curHisList == nil || _curHisList.count == 0) {
36   - // UILabel * noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
37   - // noDataLabel.text = @"No data available";
38   - // noDataLabel.backgroundColor = [UIColor clearColor];
39   - // noDataLabel.textColor = [UIColor whiteColor];
40   - // noDataLabel.textAlignment = NSTextAlignmentCenter;
41   - // tableView.backgroundView = noDataLabel;
42   - // tableView.backgroundView.layer.zPosition -= 1;
43   - // return 0;
44   - // }
45   - // tableView.backgroundView = nil;
  45 + if((_curTweetList == nil || _curTweetList.count == 0) && !_firstTime) {
  46 + UILabel * noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
  47 + noDataLabel.text = @"No data available";
  48 + noDataLabel.backgroundColor = [UIColor clearColor];
  49 + noDataLabel.textColor = [UIColor whiteColor];
  50 + noDataLabel.textAlignment = NSTextAlignmentCenter;
  51 + tableView.backgroundView = noDataLabel;
  52 + tableView.backgroundView.layer.zPosition -= 1;
  53 + return 0;
  54 + }
  55 + tableView.backgroundView = nil;
46 56 return 1;
47 57 }
48 58  
49 59 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
50   - return 6;
  60 + return _curTweetList.count;
51 61 }
52 62  
53   -- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  63 +- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
54 64 SNSRecentTopicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecentTopicCell"];
  65 + TweetObject *object = [_curTweetList objectAtIndex:indexPath.row];
  66 + if(object.avatarLink && ![object.avatarLink isKindOfClass:[NSNull class]]) {
  67 + [cell.imgAvatar sd_setImageWithURL:[NSURL URLWithString:[Utilities getImageLink:object.avatarLink]]];
  68 + }
  69 + cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm"];
  70 + cell.lblUsername.text = object.userName;
  71 + cell.lblDes.text = object.content;
  72 + cell.lblMode.text = object.mode;
  73 + cell.lblDistance.text = [NSString stringWithFormat:@"%.0f m", object.distance];
  74 + cell.lblDuration.text = object.time;
55 75 return cell;
  76 +}
  77 +
  78 +- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  79 + NSInteger lastRowIndex = [tableView numberOfRowsInSection:0] - 1;
  80 + if (indexPath.row == lastRowIndex) {
  81 + // This is the last cell
  82 + if(!_isLoading) {
  83 + _curPage += 1;
  84 + [self callRequestToUpdateData];
  85 + }
  86 + }
  87 +}
  88 +#pragma mark Private Function
  89 +
  90 +-(void) callRequestToUpdateData {
  91 + _isLoading = true;
  92 + NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
  93 + MBProgressHUD *hudView = nil;
  94 + if(_curPage == 1) {
  95 + hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
  96 + }
  97 + [[ServerAPI server] requestRecentlyTweetsList:token withPage:_curPage CompletionHandler:^(NSArray *array, NSError *error){
  98 + SNSViewController __weak *weakSelf = self;
  99 + _isLoading = false;
  100 + dispatch_async(dispatch_get_main_queue(), ^{
  101 + if(hudView != nil) {
  102 + [hudView hideAnimated:true];
  103 + }
  104 + });
  105 + if(error == nil) {
  106 + if(_curPage == 1) {
  107 + [_curTweetList removeAllObjects];
  108 + }
  109 + if(array.count != 0) {
  110 + [_curTweetList addObjectsFromArray:array];
  111 + dispatch_async(dispatch_get_main_queue(), ^{
  112 + [weakSelf.tableRecent reloadData];
  113 + });
  114 + }
  115 + else {
  116 + _isEndOfResult = true;
  117 + _curPage = MAX(1, _curPage - 1);
  118 + }
  119 + }
  120 + else {
  121 + _curPage = MAX(1, _curPage - 1);
  122 + dispatch_async(dispatch_get_main_queue(), ^{
  123 + NSString *message = [error.userInfo objectForKey:@"message"];
  124 + [Utilities showErrorMessage:message withViewController:weakSelf];
  125 + });
  126 + }
  127 +
  128 + }];
  129 +}
  130 +
  131 +-(void) resetData {
  132 + _isLoading = false;
  133 + _isEndOfResult = false;
  134 + _firstTime = true;
  135 + _curPage = 1;
56 136 }
57 137  
58 138 @end
LifeLog/LifeLog/ServerAPI.h
... ... @@ -22,8 +22,12 @@
22 22 - (void)forgetPass:(NSString *)email CompletionHandler:(void (^)(NSError *)) completion;
23 23 - (void)confirmForgetPass:(NSString *)email withConfirm:(NSString *)confirm CompletionHandler:(void (^)(NSError *)) completion;
24 24  
  25 +#pragma mark - History Screen Function
25 26 - (void) requestHistory:(NSString *)token atDate:(NSDate *)date withType:(int)type andMode:(int) mode CompletionHandler:(void (^)(HistoryObject *, NSError *)) completion;
26 27 - (void) requestHistoryGraph:(NSString *)token withType:(int)type andMode:(int) mode CompletionHandler:(void (^)(HistoryGraphObject *, NSError *)) completion;
27 28 - (void) requestHistoryList:(NSString *)token withType:(int)type andMode:(int) mode AtPage:(int) page CompletionHandler:(void (^)(NSMutableArray *, NSError *)) completion;
  29 +
  30 +#pragma mark - SNS Screen Function
  31 +- (void) requestRecentlyTweetsList:(NSString *)token withPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion;
28 32 @end
LifeLog/LifeLog/ServerAPI.m
... ... @@ -350,7 +350,7 @@
350 350 }];
351 351 }
352 352  
353   -- (void) requestHistoryList:(NSString *)token withType:(int)type andMode:(int) mode AtPage:(int) page CompletionHandler:(void (^)(NSMutableArray *, NSError *)) completion {
  353 +- (void) requestHistoryList:(NSString *)token withType:(int)type andMode:(int) mode AtPage:(int) page CompletionHandler:(void (^)(NSArray *, NSError *)) completion {
354 354 NSString *url = [kServerAddress stringByAppendingFormat:@"/api/history/list/%@/%d?page=%d&record=50", [self convertIntToString:type], mode, page];
355 355 NSLog(@"requestHistoryList link %@", url);
356 356 [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) {
... ... @@ -384,6 +384,49 @@
384 384 if (message == nil) {
385 385 message = @"Unknown error";
386 386 }
  387 + NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}];
  388 + completion(nil, errorObject);
  389 + }
  390 + }
  391 + else
  392 + {
  393 + completion(nil, error);
  394 + }
  395 + }];
  396 +}
  397 +
  398 +#pragma mark - SNS Screen Function
  399 +- (void) requestRecentlyTweetsList:(NSString *)token withPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion {
  400 + NSString *url = [kServerAddress stringByAppendingFormat:@"/api/sns/%d", page];
  401 + NSLog(@"requestRecentlyTweetsList link %@", url);
  402 + [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) {
  403 +
  404 + if (completion == NULL) {
  405 + return ;
  406 + }
  407 +
  408 + if (error == nil)
  409 + {
  410 + NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error];
  411 + NSLog(@"%@", dataResult);
  412 + int status = [dataResult[@"status"] intValue];
  413 + if (status == 1) { // status = 1 success
  414 + if(dataResult[@"result"] != nil) {
  415 + NSArray * array = dataResult[@"result"];
  416 + NSMutableArray * arrayTweets = [[NSMutableArray alloc] init];
  417 + for(NSDictionary * dict in array) {
  418 + TweetObject * object = [[TweetObject alloc] initWithData:dict];
  419 + [arrayTweets addObject:object];
  420 + }
  421 + completion(arrayTweets, nil);
  422 + }
  423 + else {
  424 + NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}];
  425 + completion(nil, errorObject);
  426 + }
  427 + }
  428 + else {
  429 + NSString *message = dataResult[@"message"];
387 430 NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}];
388 431 completion(nil, errorObject);
389 432 }
LifeLog/LifeLog/TweetObject.h
  1 +//
  2 +// TweetObject.h
  3 +// LifeLog
  4 +//
  5 +// Created by nvtu on 8/11/17.
  6 +// Copyright ยฉ 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import <Foundation/Foundation.h>
  10 +
  11 +@interface TweetObject : NSObject
  12 +
  13 +@property (nonatomic) int tweetID;
  14 +@property (nonatomic) int groupID;
  15 +@property (nonatomic) int userID;
  16 +@property (nonatomic) float distance;
  17 +
  18 +@property (nonatomic, strong) NSString * content;
  19 +@property (nonatomic, strong) NSString * mode;
  20 +@property (nonatomic, strong) NSString * time;
  21 +@property (nonatomic, strong) NSString * userName;
  22 +@property (nonatomic, strong) NSString * avatarLink;
  23 +@property (nonatomic, strong) NSDate * createDate;
  24 +
  25 +-(id) initWithData : (NSDictionary *) dict;
  26 +
  27 +@end
LifeLog/LifeLog/TweetObject.m
  1 +//
  2 +// TweetObject.m
  3 +// LifeLog
  4 +//
  5 +// Created by nvtu on 8/11/17.
  6 +// Copyright ยฉ 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import "TweetObject.h"
  10 +#import "Utilities.h"
  11 +
  12 +@implementation TweetObject
  13 +
  14 +-(id) initWithData : (NSDictionary *) dict {
  15 + if([dict objectForKey:@"id"] != nil) {
  16 + self.tweetID = [dict[@"id"] intValue];
  17 + }
  18 + if([dict objectForKey:@"group_id"] != nil) {
  19 + self.groupID = [dict[@"group_id"] intValue];
  20 + }
  21 + if([dict objectForKey:@"tweet_content"] != nil) {
  22 + self.content = dict[@"tweet_content"];
  23 + }
  24 + if([dict objectForKey:@"mode"] != nil) {
  25 + int modeInt = [dict[@"mode"] intValue];
  26 + switch (modeInt) {
  27 + case 2:
  28 + self.mode = @"running";
  29 + break;
  30 + case 3:
  31 + self.mode = @"bike";
  32 + break;
  33 + default:
  34 + self.mode = @"walking";
  35 + break;
  36 + }
  37 + }
  38 + if([dict objectForKey:@"distance"] != nil) {
  39 + self.distance = [dict[@"distance"] floatValue];
  40 + }
  41 + if([dict objectForKey:@"time"] != nil) {
  42 + self.time = dict[@"time"];
  43 + }
  44 + if([dict objectForKey:@"user_id"] != nil) {
  45 + self.userID = [dict[@"user_id"] intValue];
  46 + }
  47 + if([dict objectForKey:@"username"] != nil) {
  48 + self.userName = dict[@"username"];
  49 + }
  50 + if([dict objectForKey:@"profile_image"] != nil) {
  51 + self.avatarLink = dict[@"profile_image"];
  52 + }
  53 + if([dict objectForKey:@"created_at"] != nil) {
  54 + self.createDate = dict[@"created_at"];
  55 + }
  56 + if([dict objectForKey:@"created_at"] != nil) {
  57 + NSString *dateString = dict[@"created_at"];
  58 + self.createDate = [Utilities dateFromString:dateString withFormat:@"yyyy-MM-dd hh:mm:ss"];
  59 + }
  60 + else {
  61 + self.createDate = [NSDate date];
  62 + }
  63 + return self;
  64 +}
  65 +
  66 +@end
LifeLog/LifeLog/Utilities.h
... ... @@ -13,11 +13,16 @@
13 13 + (NSString *)addCommaFromNumber:(NSInteger)number;
14 14 + (UIColor *)convertHecToColor:(int) hex;
15 15 + (void)showErrorMessage:(NSString *)message withViewController:(UIViewController *)vc;
  16 ++ (NSString *) getImageLink : (NSString *) path;
16 17  
17 18 //share function
18 19 + (void) shareFacebook : (NSString *) content withViewController:(UIViewController *)vc;
19 20 + (void) shareTwitter : (NSString *) content withViewController:(UIViewController *)vc;
20 21 + (void) shareLine : (NSString *) content withViewController:(UIViewController *)vc;
21 22 + (void) shareEmail : (NSString *) content withViewController:(UIViewController *)vc;
  23 +
  24 +//convert date time
  25 ++ (NSDate *) dateFromString : (NSString *) dateString withFormat: (NSString *) format;
  26 ++ (NSString *) stringFromDate : (NSDate *) date withFormat: (NSString *) format;
22 27 @end
LifeLog/LifeLog/Utilities.m
... ... @@ -10,6 +10,7 @@
10 10 #import <LineKit/Line.h>
11 11  
12 12 #import "Utilities.h"
  13 +#import "ServerAPI.h"
13 14  
14 15 @implementation Utilities
15 16 + (NSString *)addCommaFromNumber:(NSInteger)number
... ... @@ -51,6 +52,11 @@
51 52 }
52 53 }
53 54  
  55 ++ (NSString *) getImageLink : (NSString *) path {
  56 + NSString * link = kServerAddress;
  57 + return [link stringByAppendingString:path];
  58 +}
  59 +
54 60 + (void) shareFacebook : (NSString *) content withViewController:(UIViewController *)vc {
55 61 if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
56 62 {
... ... @@ -118,5 +124,24 @@
118 124 urlEmail = [urlEmail stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
119 125 [[UIApplication sharedApplication] openURL: [NSURL URLWithString: urlEmail]];
120 126 }
  127 +
  128 +#pragma mark convert date time
  129 ++ (NSDate *) dateFromString : (NSString *) dateString withFormat: (NSString *) format {
  130 + NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  131 + [dateFormat setDateFormat:format];
  132 + NSDate *date = [dateFormat dateFromString:dateString];
  133 + if(date == NULL) {
  134 + return [NSDate date];
  135 + }
  136 + return date;
  137 +}
  138 +
  139 ++ (NSString *) stringFromDate : (NSDate *) date withFormat: (NSString *) format {
  140 + NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  141 + [dateFormat setDateFormat:format];
  142 + NSString *dateString = [dateFormat stringFromDate:date];
  143 + return dateString;
  144 +}
  145 +
121 146 @end
... ... @@ -6,5 +6,6 @@
6 6 pod 'MBProgressHUD', '~> 1.0.0'
7 7 pod 'Charts'
8 8 pod 'LineKit'
  9 + pod 'SDWebImage'
9 10 end