TWListViewController.m 5.49 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
//
// TWListViewController.m
// TwitterKitExample
//
// Created by DongSoo Lee on 2015. 12. 8..
// Copyright © 2015년 DongSoo Lee. All rights reserved.
//

#import "TWListViewController.h"
#import <TwitterKit/TwitterKit.h>
#import "TWTimelineViewController.h"
#import "TWComposeViewController.h"

@interface TWListViewController()

@property (assign, nonatomic) NSInteger tweetCount;
@property (strong, nonatomic) NSArray *tweetArray;

@property (strong, nonatomic) UITableView *tableView;

@end

@implementation TWListViewController

- (void) viewDidLoad {
[super viewDidLoad];

self.edgesForExtendedLayout = UIRectEdgeNone;

self.title = @"트위터 싱글뷰";

TWTRTweetView *tweetView = [[TWTRTweetView alloc] init];
tweetView.showActionButtons = YES;

__weak typeof(self) wself = self;
TWTRAPIClient *client = [[TWTRAPIClient alloc] init];
[client loadTweetWithID:@"20" completion:^(TWTRTweet * _Nullable tweet, NSError * _Nullable error) {
if (tweet) {
[tweetView configureWithTweet:tweet];

[wself.view addSubview:tweetView];

[wself congifureTableView:tweetView.frame];
} else {
NSLog(@"Failed to load tweet: %@", [error localizedDescription]);
}
}];

[client loadTweetsWithIDs:@[@"20"] completion:^(NSArray * _Nullable tweets, NSError * _Nullable error) {
if (tweets) {
wself.tweetArray = [NSArray arrayWithArray:tweets];

wself.tweetCount = tweets.count;
[wself.tableView reloadData];
} else {
NSLog(@"Failed to load tweet: %@", [error localizedDescription]);
}
}];

UIImage *image = [UIImage imageNamed:@"TwitterKitResources.bundle/twtr-icn-logo.png"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(moveToCompose) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *temp = [[UIBarButtonItem alloc] initWithCustomView:button];

NSArray *rightItemArray = @[temp
, [[UIBarButtonItem alloc] initWithTitle:@"타임라인" style:UIBarButtonItemStylePlain target:self action:@selector(moveToTimeline)]];
[self.navigationItem setRightBarButtonItems:rightItemArray];

[self addWriteViewController];
}

- (void) moveToTimeline {
TWTimelineViewController *vc = [[TWTimelineViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}

- (void) moveToCompose {
TWComposeViewController *vc = [[TWComposeViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}

- (void) moveToWrite {
TWTRSession *session = [Twitter sharedInstance].sessionStore.session;

TWTRCardConfiguration *card = [TWTRCardConfiguration appCardConfigurationWithPromoImage:nil iPhoneAppID:@"12345" iPadAppID:nil googlePlayAppID:nil];
TWTRComposerViewController *vc = [[TWTRComposerViewController alloc] initWithUserID:session.userID cardConfiguration:card];

vc.delegate = self;

[self.navigationController presentViewController:vc animated:YES completion:nil];
}

- (void) addWriteViewController {
UIImage *image = [UIImage imageNamed:@"TwitterKitResources.bundle/twtr-icn-logo-white.png"];
UIButton *tweetWriteButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - image.size.width*2 - 20, self.view.bounds.size.height - image.size.height*2 - 120, image.size.width*2, image.size.height*2)];
[tweetWriteButton setBackgroundImage:image forState:UIControlStateNormal];
[tweetWriteButton addTarget:self action:@selector(moveToWrite) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tweetWriteButton];
}

- (void) congifureTableView:(CGRect) frame {

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, frame.origin.y+frame.size.height+50
, self.view.bounds.size.width, 300)];
[self.view addSubview:self.tableView];

self.tableView.delegate = self;
self.tableView.dataSource = self;

[self.tableView registerClass:[TWTRTweetTableViewCell class] forCellReuseIdentifier:@"tweetTableViewCell"];
}

- (TWTRTweetTableViewCell *) configureTableViewCell:(TWTRTweetTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *) indexPath {

if (cell) {
[cell.tweetView configureWithTweet:self.tweetArray[indexPath.row]];
cell.tweetView.showActionButtons = YES;

return cell;
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:@"tweetTableViewCell"];
[cell.tweetView configureWithTweet:self.tweetArray[indexPath.row]];
cell.tweetView.showActionButtons = YES;

return cell;
}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
TWTRTweetTableViewCell *cell = [self configureTableViewCell:nil forRowAtIndexPath:indexPath];

return cell.tweetView.frame.size.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.tweetCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TWTRTweetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tweetTableViewCell" forIndexPath:indexPath];

cell = [self configureTableViewCell:cell forRowAtIndexPath:indexPath];

return cell;
}

@end