-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewTweetViewController.m
More file actions
152 lines (124 loc) · 5.87 KB
/
NewTweetViewController.m
File metadata and controls
152 lines (124 loc) · 5.87 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
//
// NewTweetViewController.m
// TwitterClient
//
// Created by Jessica Ko on 3/29/14.
// Copyright (c) 2014 Jessica Ko. All rights reserved.
//
#import "NewTweetViewController.h"
#import "HomeViewController.h"
#import "DetailViewController.h"
#import "MBProgressHUD.h"
#import "Client.h"
#import "LeftNavViewController.h"
#import "MyProfileViewController.h"
#import "MentionsViewController.h"
@interface NewTweetViewController ()
- (IBAction)onCancelButton:(id)sender;
- (IBAction)onTweetButton:(id)sender;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *countDown;
@end
@implementation NewTweetViewController
static int maximumNumCharacters = 140;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.delegate = self;
NSLog(@"%@", self.replyTo);
if ([self.replyTo length] > 0) {
self.textView.text = [NSString stringWithFormat:@"@%@ ", self.replyTo];
}
[self.textView becomeFirstResponder];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
textView.backgroundColor = [UIColor colorWithRed:250 green:250 blue:250 alpha:1.f];
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
textView.backgroundColor = [UIColor whiteColor];
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSCharacterSet *doneButtonCharacterSet = [NSCharacterSet newlineCharacterSet];
NSRange replacementTextRange = [text rangeOfCharacterFromSet:doneButtonCharacterSet];
NSUInteger location = replacementTextRange.location;
self.countDown.text = [NSString stringWithFormat:@"%i", maximumNumCharacters - textView.text.length - 1];
if (textView.text.length + text.length > maximumNumCharacters){
if (location != NSNotFound){
[textView resignFirstResponder];
}
return NO;
}
else if (location != NSNotFound){
[textView resignFirstResponder];
return NO;
}
return YES;
}
- (IBAction)onCancelButton:(id)sender {
if ([self.backTo isEqualToString:@"detailview"]) {
DetailViewController *detail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detail.tweet = self.savedTweet;
NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[vcs insertObject:detail atIndex:[vcs count]-1];
[self.navigationController setViewControllers:vcs animated:NO];
[self.navigationController popViewControllerAnimated:YES];
} else {
HomeViewController *homeViewController = [[HomeViewController alloc] init];
LeftNavViewController *leftMenuViewController = [[LeftNavViewController alloc] init];
MyProfileViewController *profileViewController = [[MyProfileViewController alloc] init];
MentionsViewController *mentionsViewController = [[MentionsViewController alloc] init];
MenuSliderViewController *slidingMenuContainer = [[MenuSliderViewController alloc] initWithRootViewController:homeViewController leftViewController:leftMenuViewController profileController:profileViewController mentionsController:mentionsViewController];
NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[vcs insertObject:slidingMenuContainer atIndex:[vcs count]-1];
[self.navigationController setViewControllers:vcs animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}
}
- (IBAction)onTweetButton:(id)sender {
if (self.textView.text.length > 0) {
Client *client = [Client instance];
NSDictionary *param = [[NSDictionary alloc] initWithObjectsAndKeys:self.textView.text, @"status", nil];
[client tweetWithSuccess:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
HomeViewController *homeViewController = [[HomeViewController alloc] init];
LeftNavViewController *leftMenuViewController = [[LeftNavViewController alloc] init];
MyProfileViewController *profileViewController = [[MyProfileViewController alloc] init];
MentionsViewController *mentionsViewController = [[MentionsViewController alloc] init];
MenuSliderViewController *slidingMenuContainer = [[MenuSliderViewController alloc] initWithRootViewController:homeViewController leftViewController:leftMenuViewController profileController:profileViewController mentionsController:mentionsViewController];
homeViewController.theNewTweet = self.textView.text;
homeViewController.currentTweets = self.savedTweets;
NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[vcs insertObject:slidingMenuContainer atIndex:[vcs count]-1];
[self.navigationController setViewControllers:vcs animated:NO];
[self.navigationController popViewControllerAnimated:YES];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Tweeting...";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
}
}
@end