forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwfulProfileViewController.m
More file actions
207 lines (183 loc) · 7 KB
/
Copy pathAwfulProfileViewController.m
File metadata and controls
207 lines (183 loc) · 7 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
//
// AwfulProfileViewController.m
// Awful
//
// Created by Nolan Waite on 2012-12-30.
// Copyright (c) 2012 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulProfileViewController.h"
#import "AwfulAlertView.h"
#import "AwfulDateFormatters.h"
#import "AwfulHTTPClient.h"
#import "AwfulModels.h"
#import "AwfulSettings.h"
#import "NSManagedObject+Awful.h"
#import "SVProgressHUD.h"
@interface AwfulProfileViewController () <UIWebViewDelegate>
@property (readonly, nonatomic) UIWebView *webView;
@property (nonatomic) AwfulUser *user;
@end
@implementation AwfulProfileViewController
- (void)setUserID:(NSString *)userID
{
if ([_userID isEqualToString:userID]) return;
_userID = [userID copy];
if (!_userID) {
self.user = nil;
return;
}
self.user = [AwfulUser firstMatchingPredicate:@"userID = %@", _userID];
}
- (void)renderUser
{
if (!self.user) return;
self.title = self.user.username;
NSDateFormatter *regdateFormatter = [AwfulDateFormatters formatters].regDateFormatter;
NSDateFormatter *lastPostFormatter = [NSDateFormatter new];
lastPostFormatter.locale = regdateFormatter.locale;
lastPostFormatter.dateFormat = @"MMM d, yyyy HH:mm";
NSMutableArray *contactInfo = [NSMutableArray new];
if ([self.user.aimName length] > 0) {
[contactInfo addObject:@{ @"service": @"AIM", @"address": self.user.aimName }];
}
if ([self.user.icqName length] > 0) {
[contactInfo addObject:@{ @"service": @"ICQ", @"address": self.user.icqName }];
}
if ([self.user.yahooName length] > 0) {
[contactInfo addObject:@{ @"service": @"Yahoo!", @"address": self.user.yahooName }];
}
if ([self.user.homepageURL length] > 0) {
[contactInfo addObject:@{ @"service": @"Homepage", @"address": self.user.homepageURL }];
}
NSMutableArray *additionalInfo = [NSMutableArray new];
if ([self.user.location length] > 0) {
[additionalInfo addObject:@{ @"kind": @"Location", @"info": self.user.location }];
}
if ([self.user.interests length] > 0) {
[additionalInfo addObject:@{ @"kind": @"Interests", @"info": self.user.interests }];
}
if ([self.user.occupation length] > 0) {
[additionalInfo addObject:@{ @"kind": @"Occupation", @"info": self.user.occupation }];
}
NSMutableDictionary *userDict = [@{
@"customTitle": self.user.customTitle ?: [NSNull null],
@"postCount": self.user.postCount ?: @0,
@"username": self.user.username ?: @"",
@"postRate": self.user.postRate ?: @"",
@"lastPost": [lastPostFormatter stringFromDate:self.user.lastPost] ?: [NSNull null],
@"regdate": [regdateFormatter stringFromDate:self.user.regdate] ?: [NSNull null],
@"gender": self.user.gender ?: @"porpoise",
@"aboutMe": self.user.aboutMe ?: @"",
@"anyContactInformation": @([contactInfo count] > 0),
@"contactInformation": contactInfo,
@"additionalInformation": additionalInfo,
@"profilePictureURL": self.user.profilePictureURL ?: [NSNull null],
} mutableCopy];
if ([userDict[@"customTitle"] isEqual:@"<br/>"]) userDict[@"customTitle"] = [NSNull null];
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:userDict options:0 error:&error];
if (!data) {
NSLog(@"error serializing user dict %@: %@", userDict, error);
return;
}
NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *js = [NSString stringWithFormat:@"Awful.render(%@)", json];
[self.webView stringByEvaluatingJavaScriptFromString:js];
}
- (UIWebView *)webView
{
return (UIWebView *)self.view;
}
- (void)stopObserving
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AwfulSettingsDidChangeNotification
object:nil];
}
- (void)updateDarkTheme
{
NSString *js = [NSString stringWithFormat:@"Awful.dark(%@)",
[AwfulSettings settings].darkTheme ? @"true" : @"false"];
[self.webView stringByEvaluatingJavaScriptFromString:js];
}
- (void)settingsChanged:(NSNotification *)note
{
NSArray *changed = note.userInfo[AwfulSettingsDidChangeSettingsKey];
if ([changed containsObject:AwfulSettingsKeys.darkTheme]) [self updateDarkTheme];
}
#pragma mark - UIViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (!(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) return nil;
self.title = @"Profile";
return self;
}
- (void)loadView
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
webView.delegate = self;
webView.scalesPageToFit = YES;
webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
webView.dataDetectorTypes = UIDataDetectorTypeNone;
NSURL *url = [[NSBundle mainBundle] URLForResource:@"profile-view" withExtension:@"html"];
NSError *error;
NSString *html = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&error];
if (!html) {
NSLog(@"error loading profile-view.html: %@", error);
return;
}
[webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
self.view = webView;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self updateDarkTheme];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(settingsChanged:)
name:AwfulSettingsDidChangeNotification
object:nil];
[self renderUser];
[[AwfulHTTPClient client] profileUserWithID:self.userID
andThen:^(NSError *error, AwfulUser *user)
{
if (error) {
NSLog(@"error fetching user profile for %@: %@", self.userID, error);
if (!self.user) {
[AwfulAlertView showWithTitle:@"Network Error" error:error buttonTitle:@"OK"];
}
return;
}
self.user = user;
[self renderUser];
}];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.webView.scrollView flashScrollIndicators];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self stopObserving];
[super viewWillDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return YES;
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
- (void)dealloc
{
if ([self isViewLoaded]) self.webView.delegate = nil;
[self stopObserving];
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self updateDarkTheme];
[self renderUser];
}
@end