summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/NewPostViewController.m
blob: 7b80a525cf6f5254b9e3e908a851534a11629270 (plain) (blame)
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
//
//  NewPostViewController.m
//  Juick
//
//  Created by Vitaly Takmazov on 07.11.13.
//  Copyright (c) 2013 com.juick. All rights reserved.
//

#import "NewPostViewController.h"
#import "MessagesViewController.h"
#import "ColorScheme.h"
#import "MessageInputView.h"
#import "APIClient.h"

@interface NewPostViewController ()

@property (nonatomic, assign) BOOL didSetupConstraints;
@property (nonatomic, assign) int paddingValue;

@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;

@end

@implementation NewPostViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.paddingValue = self.bottomConstraint.constant;
    [self.view setBackgroundColor:[ColorScheme mainBackground]];
    if (_replyTo != nil) {
        MessageInputView *inputView =  (MessageInputView *) self.inputAccessoryView;
        inputView.quoteText.text = _replyTo.text;
        self.title = [NSString stringWithFormat:@"Reply to %@", _replyTo.user.uname];
    } else {
        self.title = @"Post";
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [self.textView becomeFirstResponder];
}

- (IBAction)sendAction:(id)sender {
    if (_replyTo == nil) {
        [[APIClient sharedClient] postMessage:self.textView.text result:^(Message *msg, NSError *err) {
            [self.navigationController popViewControllerAnimated:YES];
            [(MessagesViewController *)self.navigationController.visibleViewController refreshData:YES];
        }];
    } else {
        [[APIClient sharedClient] postReplyToThread:_replyTo.mid inReplyTo:_replyTo.rid text:self.textView.text result:^(Message *msg, NSError *err) {
            [self.navigationController popViewControllerAnimated:YES];
            [(MessagesViewController *)self.navigationController.visibleViewController refreshData:YES];
        }];
    }
}

-(void) keyboardDidShow:(NSNotification *)sender {
    CGRect keyboardRect = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.bottomConstraint.constant = keyboardRect.size.height - self.view.safeAreaInsets.bottom + self.paddingValue;
    [self.view layoutIfNeeded];
}
-(void) keyboardWillHide:(NSNotification *)sender {
    self.bottomConstraint.constant = self.paddingValue;
    [self.view layoutIfNeeded];
}

-(BOOL) canBecomeFirstResponder {
    return YES;
}

- (UIView *) inputAccessoryView {
    if (!_inputAccessoryView) {
        MessageInputView *inputView = (MessageInputView *)[[[NSBundle mainBundle] loadNibNamed:@"MessageInputView" owner:self options:nil] firstObject];
        inputView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        inputView.quoteText.text = self.replyTo.text;
        _inputAccessoryView = inputView;
    }
    return _inputAccessoryView;
}

@end