-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathMonsterViewerViewController.m
More file actions
284 lines (229 loc) · 12.4 KB
/
Copy pathMonsterViewerViewController.m
File metadata and controls
284 lines (229 loc) · 12.4 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// MonsterViewerViewController.m
// BranchMonsterFactory
//
// Created by Alex Austin on 9/6/14.
// Copyright (c) 2014 Branch, Inc All rights reserved.
//
@import Branch;
#import "BranchUniversalObject+MonsterHelpers.h"
#import "BranchInfoViewController.h"
#import "NetworkProgressBar.h"
#import "MonsterViewerViewController.h"
#import "MonsterPartsFactory.h"
@interface MonsterViewerViewController () /*<UITextViewDelegate>*/
@property (strong, nonatomic)BranchUniversalObject *viewingMonster;
@property (strong, nonatomic) NetworkProgressBar *progressBar;
@property (strong, nonatomic) NSDictionary *monsterMetadata;
@property (strong, nonatomic) NSString *monsterName;
@property (strong, nonatomic) NSString *monsterDescription;
@property (strong, nonatomic) NSDecimalNumber *price;
@property (weak, nonatomic) IBOutlet UIView *botLayerOneColor;
@property (weak, nonatomic) IBOutlet UIImageView *botLayerTwoBody;
@property (weak, nonatomic) IBOutlet UIImageView *botLayerThreeFace;
@property (weak, nonatomic) IBOutlet UILabel *txtName;
@property (weak, nonatomic) IBOutlet UILabel *txtDescription;
@property (weak, nonatomic) IBOutlet UIButton *cmdChange;
@property (weak, nonatomic) IBOutlet UIButton *cmdInfo;
@property (weak, nonatomic) IBOutlet UITextView *shareTextView;
@property NSString *shareURL;
@end
#pragma mark - MonsterViewerViewController
@implementation MonsterViewerViewController
static CGFloat MONSTER_HEIGHT = 0.4f;
@synthesize pasteConfiguration;
- (void)viewDidLoad {
[super viewDidLoad];
[self.botLayerOneColor setBackgroundColor:[MonsterPartsFactory colorForIndex:[self.viewingMonster getColorIndex]]];
[self.botLayerTwoBody setImage:[MonsterPartsFactory imageForBody:[self.viewingMonster getBodyIndex]]];
[self.botLayerThreeFace setImage:[MonsterPartsFactory imageForFace:[self.viewingMonster getFaceIndex]]];
self.monsterName = [self.viewingMonster getMonsterName];
if (!self.monsterName) self.monsterName = @"None";
NSInteger priceInt = arc4random_uniform(4) + 1;
NSString *priceString = [NSString stringWithFormat:@"%1.2f", (float)priceInt];
_price = [NSDecimalNumber decimalNumberWithString:priceString];
self.monsterDescription = [self.viewingMonster getMonsterDescription];
[self.txtName setText:self.monsterName];
[self.txtDescription setText:self.monsterDescription];
self.monsterMetadata = [[NSDictionary alloc]
initWithObjects:@[
[NSNumber numberWithInteger:[self.viewingMonster getColorIndex]],
[NSNumber numberWithInteger:[self.viewingMonster getBodyIndex]],
[NSNumber numberWithInteger:[self.viewingMonster getFaceIndex]],
self.monsterName]
forKeys:@[
@"color_index",
@"body_index",
@"face_index",
@"monster_name"]];
[self.cmdChange.layer setCornerRadius:3.0];
[self.cmdInfo.layer setCornerRadius:3.0];
self.progressBar = [[NetworkProgressBar alloc] initWithFrame:self.view.frame andMessage:@"preparing your Branchster.."];
[self.progressBar show];
[self.view addSubview:self.progressBar];
[self.viewingMonster registerViewWithCallback:^(NSDictionary *params, NSError *error) {
NSLog(@"Monster %@ was viewed. params: %@", self.viewingMonster.getMonsterName, params);
}];
[self.progressBar hide];
[self setViewingMonster:self.viewingMonster]; //not awesome, but it triggers the setter
if (@available(iOS 16.0, *)) {
UIPasteControlConfiguration *pcConfig = [UIPasteControlConfiguration new];
pcConfig.baseBackgroundColor = [UIColor colorWithRed:81.0f/255.0f green:131.0f/255.0f blue:212.0f/255.0f alpha:1.0f];
pcConfig.displayMode = UIPasteControlDisplayModeLabelOnly;
CGRect rectPC = CGRectMake(15, (self.view.bounds.size.height - 53), 68, 34);
UIPasteControl *pc = [[UIPasteControl alloc] initWithConfiguration:pcConfig];
pc.frame = rectPC;
pc.target = self;
[self.view addSubview:pc];
pasteConfiguration = [[UIPasteConfiguration alloc] initWithAcceptableTypeIdentifiers:@[UTTypeURL.identifier]];
}
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.monsterName.length <= 0) self.monsterName = @"Nameless Monster";
[[Branch getInstance] userCompletedAction:@"Product View" withState:@{
@"sku": self.monsterName,
@"price": self.price,
@"currency": @"USD"
}];
}
- (void)pasteItemProviders:(NSArray<NSItemProvider *> *)itemProviders {
if (@available(iOS 16, *)) {
[[Branch getInstance] passPasteItemProviders:itemProviders];
}
}
- (BOOL)canPasteItemProviders:(NSArray<NSItemProvider *> *)itemProviders {
for (NSItemProvider* item in itemProviders)
if (@available(iOS 14.0, *)) {
if ( [item hasItemConformingToTypeIdentifier: UTTypeURL.identifier] )
return true;
}
return false;
}
-(void) setViewingMonster: (BranchUniversalObject *)monster {
_viewingMonster = monster;
//and every time it gets set, I need to create a new url
BranchLinkProperties *linkProperties = [[BranchLinkProperties alloc] init];
linkProperties.feature = @"monster_sharing";
linkProperties.channel = @"twitter";
monster.title = [NSString stringWithFormat:@"My Branchster: %@", self.monsterName];
monster.contentDescription = self.monsterDescription;
monster.imageUrl = [NSString stringWithFormat:@"https://s3-us-west-1.amazonaws.com/branchmonsterfactory/%hd%hd%hd.png", (short)[self.viewingMonster getColorIndex], (short)[self.viewingMonster getBodyIndex], (short)[self.viewingMonster getFaceIndex]];
[self.viewingMonster getShortUrlWithLinkProperties:linkProperties andCallback:^(NSString *url, NSError *error) {
if (!error) {
self.shareURL = url;
NSLog(@"new monster url created: %@", self.shareURL);
self.shareTextView.text = url;
}
}];
}
-(IBAction)shareSheet:(id)sender {
BNCCommerceEvent *commerceEvent = [[BNCCommerceEvent alloc] init];
commerceEvent.revenue = self.price;
commerceEvent.currency = @"USD";
BNCProduct* branchester = [BNCProduct new];
if (self.monsterName.length <= 0) self.monsterName = @"Nameless Monster";
branchester.sku = self.monsterName;
branchester.price = self.price;
branchester.quantity = @1;
branchester.variant = @"X-Tra Hairy";
branchester.brand = @"Branch";
branchester.category = BNCProductCategoryAnimalSupplies;
branchester.name = self.monsterName;
commerceEvent.products = [NSArray arrayWithObject:branchester];
[[Branch getInstance] userCompletedAction:BNCAddToCartEvent withState:@{
@"sku": self.monsterName,
@"price": self.price,
@"currency": @"USD"
}];
[self.viewingMonster
showShareSheetWithShareText:@"Share Your Monster!"
completion:^(NSString * _Nullable activityType, BOOL completed) {
if (completed) {
// [[Branch getInstance] userCompletedAction:BNCAddToCartEvent];
[[Branch getInstance] sendCommerceEvent:commerceEvent
metadata:nil
withCompletion:^ (NSDictionary *response, NSError *error) {
if (error) { }
}];
}
}];
[UIMenuController sharedMenuController].menuVisible = NO;
[self.shareTextView resignFirstResponder];
}
-(IBAction)copyShareURL:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = self.shareTextView.text;
}
- (IBAction)cmdChangeClick:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (NSDictionary *)prepareFBDict:(NSString *)url {
return [[NSDictionary alloc] initWithObjects:@[
[NSString stringWithFormat:@"My Branchster: %@", self.monsterName],
self.monsterDescription,
self.monsterDescription,
url,
[NSString stringWithFormat:@"https://s3-us-west-1.amazonaws.com/branchmonsterfactory/%hd%hd%hd.png", (short)[self.viewingMonster getColorIndex], (short)[self.viewingMonster getBodyIndex], (short)[self.viewingMonster getFaceIndex]]]
forKeys:@[
@"name",
@"caption",
@"description",
@"link",
@"picture"]];
}
// This function serves to dynamically generate the dictionary parameters to embed in the Branch link
// These are the parameters that will be available in the callback of init user session if
// a user clicked the link and was deep linked
- (NSDictionary *)prepareBranchDict {
return [[NSDictionary alloc] initWithObjects:@[
[NSNumber numberWithInteger:[self.viewingMonster getColorIndex]],
[NSNumber numberWithInteger:[self.viewingMonster getBodyIndex]],
[NSNumber numberWithInteger:[self.viewingMonster getFaceIndex]],
self.monsterName,
@"true",
[NSString stringWithFormat:@"My Branchster: %@", self.monsterName],
self.monsterDescription,
[NSString stringWithFormat:@"https://s3-us-west-1.amazonaws.com/branchmonsterfactory/%hd%hd%hd.png", (short)[self.viewingMonster getColorIndex], (short)[self.viewingMonster getBodyIndex], (short)[self.viewingMonster getFaceIndex]]]
forKeys:@[
@"color_index",
@"body_index",
@"face_index",
@"monster_name",
@"monster",
@"$og_title",
@"$og_description",
@"$og_image_url"]];
}
- (void)viewDidLayoutSubviews {
[self adjustMonsterPicturesForScreenSize];
}
- (void)adjustMonsterPicturesForScreenSize {
[self.botLayerOneColor setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.botLayerTwoBody setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.botLayerThreeFace setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.txtDescription setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.cmdChange setTranslatesAutoresizingMaskIntoConstraints:NO];
CGRect screenSize = [[UIScreen mainScreen] bounds];
CGFloat widthRatio = self.botLayerOneColor.frame.size.width/self.botLayerOneColor.frame.size.height;
CGFloat newHeight = screenSize.size.height;
newHeight = newHeight * MONSTER_HEIGHT;
CGFloat newWidth = widthRatio * newHeight;
CGRect newFrame = CGRectMake(
(screenSize.size.width-newWidth)/2,
self.botLayerOneColor.frame.origin.y,
newWidth,
newHeight
);
self.botLayerOneColor.frame = newFrame;
self.botLayerTwoBody.frame = newFrame;
self.botLayerThreeFace.frame = newFrame;
CGRect textFrame = self.txtDescription.frame;
textFrame.origin.y = newFrame.origin.y + newFrame.size.height + 8;
self.txtDescription.frame = textFrame;
CGRect cmdFrame = self.cmdChange.frame;
cmdFrame.origin.x = newFrame.origin.x + newFrame.size.width;
self.cmdChange.frame = cmdFrame;
[self.view layoutSubviews];
}
@end