forked from salesforce-marketingcloud/LearningAppIos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCTagsViewController.m
153 lines (117 loc) · 3.4 KB
/
MCTagsViewController.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
/*
* Copyright (c) 2016, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
#import "MCTagsViewController.h"
// Libraries
#import "ETPush.h"
#import "ETAnalytics.h"
// Models
#import "MCTag.h"
// Table cell
#import "MCTagTableCell.h"
static NSString *cellIdentifier = @"MCTagTableCell";
@interface MCTagsViewController () <MCTagTableCellDelegate>
/**
Array of all tag object
*/
@property(nonatomic, strong) NSMutableArray *tags;
/**
Text field, new tag name.
*/
@property (weak, nonatomic) IBOutlet UITextField *tagName;
@end
@implementation MCTagsViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
PushManager returns the list of tags as a NSSet collection
*/
NSSet *setOfTags = [[ETPush pushManager] getTags];
/**
Init mutable array
*/
self.tags = [[NSMutableArray alloc]initWithCapacity:setOfTags.count];
MCTag *tag;
/**
Create object MCTag and add to tags array
*/
for (NSString* nameTag in [setOfTags allObjects]) {
tag = [[MCTag alloc] init];
tag.name = nameTag;
tag.on = true;
[self.tags addObject:tag];
}
[ETAnalytics trackPageView:@"data://TagViewControllerLoaded" andTitle:@"Tag View Loaded" andItem:nil andSearch:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tags count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MCTagTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cell configureWithTag:self.tags[indexPath.row] indexPath:indexPath];
cell.delegate = self;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
#pragma mark - <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
#pragma mark - tag cell view delegate
- (void) notificationReceive:(BOOL)recive indexPath:(NSIndexPath*)indexPath {
MCTag *tag = self.tags[indexPath.row];
tag.on = recive;
/**
Add or remove tags for the current device.
*/
if (recive) {
[[ETPush pushManager] addTag:tag.name];
} else {
[[ETPush pushManager] removeTag:tag.name];
}
}
/**
Adds a new tag to the SDK so that it is sent to the server and be able to receive push notifications.
Reloads the table to display the tag.
@param sender An ID of a component of the user interface.
@return An action for the method to display in the view.
*/
- (IBAction)newTag:(id)sender {
/**
Tags don't have to be defined inside of MobilePush before using them. You can create them at will.
Create new tag.
*/
MCTag *tag = [[MCTag alloc]init];
tag.name = self.tagName.text;
tag.on = YES;
/**
Add tag in tags array
*/
[self.tags addObject:tag];
/**
Add tags for the current device.
*/
[[ETPush pushManager] addTag:tag.name];
self.tagName.text = @"";
/**
Reload table
*/
[self.tableView reloadData];
}
@end