forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwfulPrivateMessageViewController.m
204 lines (177 loc) · 7.18 KB
/
AwfulPrivateMessageViewController.m
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
//
// AwfulPrivateMessageViewController.m
// Awful
//
// Created by me on 8/14/12.
// Copyright (c) 2012 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulPrivateMessageViewController.h"
#import "AwfulActionSheet.h"
#import "AwfulAlertView.h"
#import "AwfulDataStack.h"
#import "AwfulDateFormatters.h"
#import "AwfulHTTPClient.h"
#import "AwfulModels.h"
#import "AwfulPostsView.h"
#import "AwfulPrivateMessageComposeViewController.h"
#import "AwfulSettings.h"
#import "AwfulTheme.h"
#import "AwfulThemingViewController.h"
#import "NSFileManager+UserDirectories.h"
#import "UIViewController+NavigationEnclosure.h"
@interface AwfulPrivateMessageViewController () <AwfulPostsViewDelegate, AwfulThemingViewController,
AwfulPrivateMessageComposeViewControllerDelegate>
@property (nonatomic) AwfulPrivateMessage *privateMessage;
@property (readonly) AwfulPostsView *postsView;
@end
@implementation AwfulPrivateMessageViewController
- (instancetype)initWithPrivateMessage:(AwfulPrivateMessage *)privateMessage
{
if (!(self = [super initWithNibName:nil bundle:nil])) return nil;;
_privateMessage = privateMessage;
self.title = privateMessage.subject;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingsDidChange:)
name:AwfulSettingsDidChangeNotification
object:nil];
return self;
}
- (void)settingsDidChange:(NSNotification *)note
{
if (![self isViewLoaded]) return;
[self configurePostsViewSettings];
}
- (AwfulPostsView *)postsView
{
return (id)self.view;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - AwfulThemingViewController
- (void)retheme
{
self.view.backgroundColor = [AwfulTheme currentTheme].postsViewBackgroundColor;
self.postsView.dark = [AwfulSettings settings].darkTheme;
}
#pragma mark - UIViewController
- (void)loadView
{
AwfulPostsView *view = [AwfulPostsView new];
view.frame = [UIScreen mainScreen].applicationFrame;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
view.delegate = self;
view.stylesheetURL = StylesheetURLForForumWithIDAndSettings(nil, nil);
self.view = view;
[self configurePostsViewSettings];
}
- (void)configurePostsViewSettings
{
self.postsView.showImages = [AwfulSettings settings].showImages;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self retheme];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[AwfulHTTPClient client] readPrivateMessageWithID:self.privateMessage.messageID
andThen:^(NSError *error,
AwfulPrivateMessage *message)
{
[self.postsView reloadPostAtIndex:0];
}];
}
#pragma mark - AwfulPostsViewDelegate
- (NSInteger)numberOfPostsInPostsView:(AwfulPostsView *)postsView
{
return 1;
}
- (NSDictionary *)postsView:(AwfulPostsView *)postsView postAtIndex:(NSInteger)index
{
NSMutableDictionary *dict = [NSMutableDictionary new];
dict[AwfulPostsViewKeys.innerHTML] = self.privateMessage.innerHTML ?: @"";
dict[AwfulPostsViewKeys.beenSeen] = self.privateMessage.seen ?: @NO;
if (self.privateMessage.sentDate) {
NSDateFormatter *formatter = [AwfulDateFormatters formatters].postDateFormatter;
dict[AwfulPostsViewKeys.postDate] = [formatter stringFromDate:self.privateMessage.sentDate];
}
AwfulUser *sender = self.privateMessage.from;
dict[AwfulPostsViewKeys.authorName] = sender.username ?: @"";
if (sender.avatarURL) {
dict[AwfulPostsViewKeys.authorAvatarURL] = [sender.avatarURL absoluteString];
}
if (sender.regdate) {
NSDateFormatter *formatter = [AwfulDateFormatters formatters].regDateFormatter;
dict[AwfulPostsViewKeys.authorRegDate] = [formatter stringFromDate:sender.regdate];
}
return dict;
}
- (NSArray *)whitelistedSelectorsForPostsView:(AwfulPostsView *)postsView
{
return @[ @"showActionsForPostAtIndex:fromRectDictionary:" ];
}
- (void)showActionsForPostAtIndex:(NSNumber *)index fromRectDictionary:(NSDictionary *)rectDict
{
CGRect rect = CGRectMake([rectDict[@"left"] floatValue], [rectDict[@"top"] floatValue],
[rectDict[@"width"] floatValue], [rectDict[@"height"] floatValue]);
if (self.postsView.scrollView.contentOffset.y < 0) {
rect.origin.y -= self.postsView.scrollView.contentOffset.y;
}
rect = [self.postsView convertRect:rect toView:nil];
NSString *title = [NSString stringWithFormat:@"%@'s Message",
self.privateMessage.from.username];
AwfulActionSheet *sheet = [[AwfulActionSheet alloc] initWithTitle:title];
[sheet addButtonWithTitle:@"Reply" block:^{
[[AwfulHTTPClient client] quotePrivateMessageWithID:self.privateMessage.messageID
andThen:^(NSError *error, NSString *bbcode)
{
if (error) {
[AwfulAlertView showWithTitle:@"Could Not Quote Message" error:error
buttonTitle:@"OK"];
} else {
AwfulPrivateMessageComposeViewController *compose;
compose = [AwfulPrivateMessageComposeViewController new];
compose.delegate = self;
[compose setRegardingMessage:self.privateMessage];
[compose setMessageBody:bbcode];
[self presentViewController:[compose enclosingNavigationController] animated:YES
completion:nil];
}
}];
}];
[sheet addButtonWithTitle:@"Forward" block:^{
[[AwfulHTTPClient client] quotePrivateMessageWithID:self.privateMessage.messageID
andThen:^(NSError *error, NSString *bbcode)
{
if (error) {
[AwfulAlertView showWithTitle:@"Could Not Quote Message" error:error
buttonTitle:@"OK"];
} else {
AwfulPrivateMessageComposeViewController *compose;
compose = [AwfulPrivateMessageComposeViewController new];
compose.delegate = self;
[compose setForwardedMessage:self.privateMessage];
[compose setMessageBody:bbcode];
[self presentViewController:[compose enclosingNavigationController] animated:YES
completion:nil];
}
}];
}];
[sheet addCancelButtonWithTitle:@"Cancel"];
[sheet showFromRect:rect inView:self.postsView.window animated:YES];
}
#pragma mark - AwfulPrivateMessageComposeViewControllerDelegate
- (void)privateMessageComposeControllerDidSendMessage:(AwfulPrivateMessageComposeViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:^{
[self.navigationController popViewControllerAnimated:YES];
}];
}
- (void)privateMessageComposeControllerDidCancel:(AwfulPrivateMessageComposeViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end