forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweetSendViewController.m
More file actions
executable file
·292 lines (262 loc) · 11.6 KB
/
TweetSendViewController.m
File metadata and controls
executable file
·292 lines (262 loc) · 11.6 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// TweetSendViewController.m
// Coding_iOS
//
// Created by 王 原闯 on 14-9-1.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import <AssetsLibrary/AssetsLibrary.h>
#import "TweetSendViewController.h"
#import "TweetSendTextCell.h"
#import "TweetSendImagesCell.h"
#import "Coding_NetAPIManager.h"
#import "UsersViewController.h"
#import "Helper.h"
#import "TweetSendLocationViewController.h"
#import "TweetSendLocation.h"
#import <TPKeyboardAvoiding/TPKeyboardAvoidingTableView.h>
@interface TweetSendViewController ()<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, QBImagePickerControllerDelegate, UIScrollViewDelegate>
@property (strong, nonatomic) UITableView *myTableView;
@property (strong, nonatomic) Tweet *curTweet;;
@end
@implementation TweetSendViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_curTweet = [Tweet tweetForSend];
_locationData = _curTweet.locationData;
[self.navigationItem setLeftBarButtonItem:[UIBarButtonItem itemWithBtnTitle:@"取消" target:self action:@selector(cancelBtnClicked:)] animated:YES];
UIBarButtonItem *buttonItem = [UIBarButtonItem itemWithBtnTitle:@"发送" target:self action:@selector(sendTweet)];
[self.navigationItem setRightBarButtonItem:buttonItem animated:YES];
@weakify(self);
RAC(self.navigationItem.rightBarButtonItem, enabled) =
[RACSignal combineLatest:@[RACObserve(self, curTweet.tweetContent),
RACObserve(self, curTweet.tweetImages)] reduce:^id (NSString *mdStr){
@strongify(self);
return @(![self isEmptyTweet]);
}];
self.title = [NSString stringWithFormat:@"发冒泡"];
// 添加myTableView
_myTableView = ({
TPKeyboardAvoidingTableView *tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor clearColor];
tableView.dataSource = self;
tableView.delegate = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[tableView registerClass:[TweetSendTextCell class] forCellReuseIdentifier:kCellIdentifier_TweetSendText];
[tableView registerClass:[TweetSendImagesCell class] forCellReuseIdentifier:kCellIdentifier_TweetSendImages];
[self.view addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
tableView;
});
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self inputViewBecomeFirstResponder];
}
- (BOOL)inputViewBecomeFirstResponder{
TweetSendTextCell *cell = (TweetSendTextCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
if ([cell respondsToSelector:@selector(becomeFirstResponder)]) {
[cell becomeFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setLocationData:(TweetSendLocationResponse *)locationData
{
_locationData = locationData;
_curTweet.locationData = locationData;
[self.myTableView reloadData];
}
#pragma mark Table M
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
__weak typeof(self) weakSelf = self;
if (indexPath.row == 0) {
TweetSendTextCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_TweetSendText forIndexPath:indexPath];
cell.tweetContentView.text = _curTweet.tweetContent;
[cell setLocationStr:self.locationData.displayLocaiton];
cell.textValueChangedBlock = ^(NSString *valueStr){
weakSelf.curTweet.tweetContent = valueStr;
};
cell.photoBtnBlock = ^(){
[weakSelf showActionForPhoto];
};
cell.locationBtnBlock = ^(){
TweetSendLocationViewController *vc = [[TweetSendLocationViewController alloc] init];
vc.responseData = self.locationData;
UINavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
[weakSelf presentViewController:nav animated:YES completion:nil];
};
return cell;
}else {
TweetSendImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_TweetSendImages forIndexPath:indexPath];
cell.curTweet = _curTweet;
cell.addPicturesBlock = ^(){
[self showActionForPhoto];
};
cell.deleteTweetImageBlock = ^(TweetImage *toDelete){
[weakSelf.curTweet deleteATweetImage:toDelete];
[weakSelf.myTableView reloadData];
};
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat cellHeight = 0;
if (indexPath.row == 0) {
cellHeight = [TweetSendTextCell cellHeight];
}else if(indexPath.row == 1){
cellHeight = [TweetSendImagesCell cellHeightWithObj:_curTweet];
}
return cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark UIActionSheet M
- (void)showActionForPhoto{
@weakify(self);
[[UIActionSheet bk_actionSheetCustomWithTitle:nil buttonTitles:@[@"拍照", @"从相册选择"] destructiveTitle:nil cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
@strongify(self);
[self photoActionSheet:sheet DismissWithButtonIndex:index];
}] showInView:self.view];
}
- (void)photoActionSheet:(UIActionSheet *)sheet DismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
// 拍照
if (![Helper checkCameraAuthorizationStatus]) {
return;
}else if (_curTweet.tweetImages.count >= 6) {
kTipAlert(@"最多只可选择6张照片,已经选满了。先去掉一张照片再拍照呗~");
return;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;//设置可编辑
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];//进入照相界面
}else if (buttonIndex == 1){
// 相册
if (![Helper checkPhotoLibraryAuthorizationStatus]) {
return;
}
QBImagePickerController *imagePickerController = [[QBImagePickerController alloc] init];
[imagePickerController.selectedAssetURLs removeAllObjects];
[imagePickerController.selectedAssetURLs addObjectsFromArray:self.curTweet.selectedAssetURLs];
imagePickerController.filterType = QBImagePickerControllerFilterTypePhotos;
imagePickerController.delegate = self;
imagePickerController.allowsMultipleSelection = YES;
imagePickerController.maximumNumberOfSelection = 9;
UINavigationController *navigationController = [[BaseNavigationController alloc] initWithRootViewController:imagePickerController];
[self presentViewController:navigationController animated:YES completion:NULL];
}
}
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *pickerImage = [info objectForKey:UIImagePickerControllerOriginalImage];
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary writeImageToSavedPhotosAlbum:[pickerImage CGImage] orientation:(ALAssetOrientation)pickerImage.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {
[self.curTweet addASelectedAssetURL:assetURL];
[self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}];
[picker dismissViewControllerAnimated:YES completion:^{}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark QBImagePickerControllerDelegate
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets{
NSMutableArray *selectedAssetURLs = [NSMutableArray new];
[imagePickerController.selectedAssetURLs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[selectedAssetURLs addObject:obj];
}];
@weakify(self);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.curTweet.selectedAssetURLs = selectedAssetURLs;
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
[self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
});
});
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark Nav Btn M
- (void)cancelBtnClicked:(id)sender{
if ([self isEmptyTweet] && !_curTweet.locationData) {//有位置
[Tweet deleteSendData];
[self dismissSelf];
}else{
__weak typeof(self) weakSelf = self;
[[UIActionSheet bk_actionSheetCustomWithTitle:@"是否保存草稿" buttonTitles:@[@"保存"] destructiveTitle:@"不保存" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
if (index == 0) {
[weakSelf.curTweet saveSendData];
}else if (index == 1){
[Tweet deleteSendData];
}else{
return ;
}
[weakSelf dismissSelf];
}] showInView:self.view];
}
}
- (void)dismissSelf{
[self.view endEditing:YES];
TweetSendTextCell *cell = (TweetSendTextCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
if (cell.footerToolBar) {
[cell.footerToolBar removeFromSuperview];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)isEmptyTweet{
BOOL isEmptyTweet = YES;
if ((_curTweet.tweetContent && ![_curTweet.tweetContent isEmptyOrListening])//内容不为空
|| _curTweet.tweetImages.count > 0)//有照片
{
isEmptyTweet = NO;
}
return isEmptyTweet;
}
- (void)sendTweet{
_curTweet.tweetContent = [_curTweet.tweetContent aliasedString];
if (_sendNextTweet) {
_sendNextTweet(_curTweet);
}
[self dismissSelf];
}
- (void)enableNavItem:(BOOL)isEnable{
self.navigationItem.leftBarButtonItem.enabled = isEnable;
self.navigationItem.rightBarButtonItem.enabled = isEnable;
}
- (void)dealloc
{
_myTableView.delegate = nil;
_myTableView.dataSource = nil;
}
#pragma mark
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (scrollView == self.myTableView) {
[self.view endEditing:YES];
}
}
@end