-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHSettingsPushNotificationViewController.m
More file actions
150 lines (116 loc) · 5.72 KB
/
SHSettingsPushNotificationViewController.m
File metadata and controls
150 lines (116 loc) · 5.72 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
//
// SHSettingsPushNotificationViewController.m
//
// Copyright (c) 2013 Lodoss. All rights reserved.
//
#import "SHSettingsPushNotificationViewController.h"
#import "SHSettingsPushCell.h"
#import "SHDesignHelper.h"
#import "SHToolbox.h"
#import "SHCommon.h"
#import "SHDataManager.h"
#import "SHLoginManager.h"
#import "NSManagedObjectContext+Extensions.h"
#import "SHGAIHelper.h"
#import "UIColor+RGBColor.h"
@interface SHSettingsPushNotificationViewController () <SHSettingsPushCellDelegate>
{
SHNotificationsMask _currentMask;
BOOL _changesPending;
}
@end
@implementation SHSettingsPushNotificationViewController
static NSArray *notificationTypeMasks;
static NSDictionary *notificationTypeTitles;
+ (void)initialize {
notificationTypeMasks = @[ @(SHFriendMakeNotificationMask), @(SHFriendAcceptNotificationMask), @(SHFriendRejectNotificationMask), @(SHFriendUnfriendNotificationMask), @(SHListShareNotificationMask), @(SHListUnshareNotificationMask), @(SHListUnshareMeNotificationMask), @(SHListChangedNotificationMask), @(SHListRemovedNotificationMask) ];
notificationTypeTitles = @{ @(SHFriendMakeNotificationMask) : NSLocalizedString(@"Somebody wants to be your friend", @"Notification type"),
@(SHFriendAcceptNotificationMask) : NSLocalizedString(@"Somebody confirmed offer", @"Notification type"),
@(SHFriendRejectNotificationMask) : NSLocalizedString(@"Somebody rejected offer", @"Notification type"),
@(SHFriendUnfriendNotificationMask) : NSLocalizedString(@"Somebody removed you from friendlist", @"Notification type"),
@(SHListShareNotificationMask) : NSLocalizedString(@"Friend shared list with you", @"Notification type"),
@(SHListUnshareNotificationMask) : NSLocalizedString(@"Friend removed you from a list", @"Notification type"),
@(SHListUnshareMeNotificationMask) : NSLocalizedString(@"Friend removed himself from your list", @"Notification type"),
@(SHListChangedNotificationMask) : NSLocalizedString(@"Friend changed a list", @"Notification type"),
@(SHListRemovedNotificationMask) : NSLocalizedString(@"Friend removed a list", @"Notification type") };
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.preferredContentSize = CGSizeMake(320, 480);
_currentMask = [SHLoginManager sharedInstance].currentUser.notificationsMask.integerValue;
}
return self;
}
- (NSString *)title {
return NSLocalizedString(@"Notifications", @"Notifications");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[SHGAIHelper sendDefaultTrackerScreenViewWithName:@"PushNotificationsSettings"];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self saveChangesIfNecessary];
}
#pragma mark - Saving changes
- (void)saveChangesIfNecessary {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:_cmd object:nil];
if (!_changesPending) {
return;
}
_changesPending = NO;
NSManagedObjectContext *scratchContext = [[SHDataManager sharedInstance] newScratchContext];
SHUser *scratchUser = (SHUser *)[scratchContext instanceOfManagedObject:[SHLoginManager sharedInstance].currentUser];
if (_currentMask == scratchUser.notificationsMask.integerValue) {
return;
}
[self saveChangesForUser:scratchUser];
}
- (void)saveChangesForUser: (SHUser *)user {
user.notificationsMask = @(_currentMask);
[[SHDataManager sharedInstance] updateUser:user withCompletion:^(NSError *error) {
if (error != nil) {
[SHToolbox showErrorAlertWithTitle:NSLocalizedString(@"Failed to save", @"User save error") andError:error];
}
}];
}
- (void)saveChangesDeferred {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(saveChangesIfNecessary) object:nil];
[self performSelector:@selector(saveChangesIfNecessary) withObject:nil afterDelay:1.0];
}
#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return notificationTypeMasks.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SHSettingsPushCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SHSettingsPushCell"];
if (cell == nil) {
cell = [SHSettingsPushCell cellFromNib];
cell.delegate = self;
}
NSNumber *maskNumber = notificationTypeMasks[indexPath.row];
NSString *title = notificationTypeTitles[maskNumber];
NSInteger maskValue = maskNumber.integerValue;
BOOL isActive = (_currentMask & maskValue) > 0;
[cell setupWithTitle:title on:isActive];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
SHSettingsPushCell *cell = (SHSettingsPushCell *)[tableView cellForRowAtIndexPath:indexPath];
[cell setOn:!cell.on animated:YES];
[self settingPushCell:cell didSetOn:cell.on];
}
- (void)settingPushCell:(SHSettingsPushCell *)cell didSetOn:(BOOL)on {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSInteger maskValue = [notificationTypeMasks[indexPath.row] integerValue];
if (on) {
_currentMask |= maskValue;
} else {
_currentMask &= ~maskValue;
}
_changesPending = YES;
[self saveChangesDeferred];
}
@end