-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSupportRequestViewController.m
More file actions
749 lines (649 loc) · 39.7 KB
/
Copy pathSupportRequestViewController.m
File metadata and controls
749 lines (649 loc) · 39.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// SupportRequestViewController.m
// EZCompleteUI v1.3
//
// Changes from v1.2:
// - Added "Include coin usage" toggle — when on, fetches the user's coin
// usage log from the get-usage-log edge function and attaches it as a
// CSV file (coin_usage.csv) to the support email. Opens in Numbers/Excel
// making it easy to filter and discuss specific charges in disputes.
// - Coin balance + total coins spent summary always included in email body
// regardless of the toggle, since it's lightweight and always useful.
// - Send button disables and shows a spinner in the nav bar while the usage
// fetch is in flight — re-enables on completion or error.
// - mailto: fallback notes CSV attachment was omitted (same pattern as log).
// - Added #import "EZAuthManager.h" (was missing — needed for JWT to call
// the get-usage-log edge function).
// - Added #import "EZEntitlementManager.h" for coinBalance inline summary.
// - sendTapped: canSendMail now gates the send PATH, not whether to send at all
// - Fallback to mailto: URL when MFMailComposeViewController is unavailable
// (fixes jailbroken iOS 15 where canSendMail returns NO despite mail being configured)
//
// Changes from v1.0:
// - System message no longer truncated in settings snapshot (full content included)
//
// Changes from (new):
// - Initial implementation: support/feedback form with settings snapshot
// - Recipient email loaded from EZKeyVault (never in source)
// - Settings snapshot excludes all API key fields (vault and legacy UserDefaults)
// - Optional debug log attachment behind explicit user permission switch
// - App version + build read from main bundle Info.plist
// - MFMailComposeViewController handles send; controller dismissed on any result
#import "SupportRequestViewController.h"
#import "EZKeyVault.h"
#import "EZAuthManager.h"
#import "EZEntitlementManager.h"
#import "helpers.h"
#import <MessageUI/MessageUI.h>
// Keys we explicitly must never include in the settings snapshot.
// Covers both the current vault-backed names and any un-migrated legacy names.
static NSArray<NSString *> *EZSensitiveUserDefaultsKeys(void) {
return @[@"apiKey", @"elevenKey", @"openAIKey", @"elevenLabsKey",
@"apikey", @"api_key", @"elevenlabs_key"];
}
@interface SupportRequestViewController () <MFMailComposeViewControllerDelegate,
UITextViewDelegate>
// ── UI ─────────────────────────────────────────────────────────────────────
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITextView *messageTextView;
@property (nonatomic, strong) UISwitch *includeLogSwitch;
@property (nonatomic, strong) UISwitch *includeUsageSwitch;
@property (nonatomic, strong) UILabel *settingsPreviewLabel;
@end
@implementation SupportRequestViewController
// ─────────────────────────────────────────────────────────────────────────────
// MARK: - Lifecycle
// ─────────────────────────────────────────────────────────────────────────────
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Support & Feedback";
self.view.backgroundColor = [UIColor systemBackgroundColor];
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancelTapped)];
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Send"
style:UIBarButtonItemStyleDone
target:self
action:@selector(sendTapped)];
[self setupUI];
// Dismiss keyboard on tap outside the text view
UITapGestureRecognizer *dismissTap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
dismissTap.cancelsTouchesInView = NO;
[self.scrollView addGestureRecognizer:dismissTap];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// ─────────────────────────────────────────────────────────────────────────────
// MARK: - UI Setup
// ─────────────────────────────────────────────────────────────────────────────
- (void)setupUI {
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.scrollView.alwaysBounceVertical = YES;
[self.view addSubview:self.scrollView];
CGFloat contentWidth = self.view.frame.size.width - 32;
CGFloat y = 20;
// ── Intro label ──────────────────────────────────────────────────────────
UILabel *introLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, y, contentWidth, 0)];
introLabel.text =
@"Describe your issue or feedback below. Your current app settings "
"(no API keys) will be included automatically to help diagnose problems.";
introLabel.font = [UIFont systemFontOfSize:14];
introLabel.textColor = [UIColor secondaryLabelColor];
introLabel.numberOfLines = 0;
[introLabel sizeToFit];
introLabel.frame = CGRectMake(16, y, contentWidth, introLabel.frame.size.height);
[self.scrollView addSubview:introLabel];
y += introLabel.frame.size.height + 14;
// ── Message text view ────────────────────────────────────────────────────
UILabel *messageLabel = [self makeSectionLabel:@"Your message:" y:&y];
(void)messageLabel; // used for layout side-effect
self.messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(16, y, contentWidth, 160)];
self.messageTextView.font = [UIFont systemFontOfSize:15];
self.messageTextView.layer.cornerRadius = 10;
self.messageTextView.layer.borderWidth = 1.0;
self.messageTextView.layer.borderColor = [UIColor systemGray4Color].CGColor;
self.messageTextView.backgroundColor = [UIColor secondarySystemBackgroundColor];
self.messageTextView.textContainerInset = UIEdgeInsetsMake(10, 8, 10, 8);
self.messageTextView.delegate = self;
// Placeholder text — cleared on first edit via delegate
self.messageTextView.text = @"Describe your issue or feedback here...";
self.messageTextView.textColor = [UIColor placeholderTextColor];
[self.scrollView addSubview:self.messageTextView];
y += 170;
// ── Separator ────────────────────────────────────────────────────────────
UIView *separator1 = [[UIView alloc] initWithFrame:CGRectMake(16, y, contentWidth, 0.5)];
separator1.backgroundColor = [UIColor separatorColor];
[self.scrollView addSubview:separator1];
y += 16;
// ── Include debug log toggle ─────────────────────────────────────────────
UILabel *logToggleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, y, contentWidth - 60, 22)];
logToggleLabel.text = @"Include debug log";
logToggleLabel.font = [UIFont systemFontOfSize:15];
logToggleLabel.textColor = [UIColor labelColor];
[self.scrollView addSubview:logToggleLabel];
self.includeLogSwitch = [[UISwitch alloc] init];
CGSize switchSize = self.includeLogSwitch.intrinsicContentSize;
self.includeLogSwitch.frame =
CGRectMake(contentWidth - switchSize.width + 16, y - 1, switchSize.width, switchSize.height);
self.includeLogSwitch.on = NO;
[self.scrollView addSubview:self.includeLogSwitch];
y += 30;
UILabel *logHintLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, y, contentWidth, 0)];
logHintLabel.text =
@"The debug log records model calls and errors. It does not contain message content.";
logHintLabel.font = [UIFont systemFontOfSize:12];
logHintLabel.textColor = [UIColor secondaryLabelColor];
logHintLabel.numberOfLines = 0;
[logHintLabel sizeToFit];
logHintLabel.frame = CGRectMake(16, y, contentWidth, logHintLabel.frame.size.height);
[self.scrollView addSubview:logHintLabel];
y += logHintLabel.frame.size.height + 16;
// ── Separator ────────────────────────────────────────────────────────────
UIView *separatorUsage = [[UIView alloc] initWithFrame:CGRectMake(16, y, contentWidth, 0.5)];
separatorUsage.backgroundColor = [UIColor separatorColor];
[self.scrollView addSubview:separatorUsage];
y += 16;
// ── Include coin usage toggle ─────────────────────────────────────────────
UILabel *usageToggleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, y, contentWidth - 60, 22)];
usageToggleLabel.text = @"Include coin usage";
usageToggleLabel.font = [UIFont systemFontOfSize:15];
usageToggleLabel.textColor = [UIColor labelColor];
[self.scrollView addSubview:usageToggleLabel];
self.includeUsageSwitch = [[UISwitch alloc] init];
CGSize usageSwitchSize = self.includeUsageSwitch.intrinsicContentSize;
self.includeUsageSwitch.frame =
CGRectMake(contentWidth - usageSwitchSize.width + 16, y - 1,
usageSwitchSize.width, usageSwitchSize.height);
self.includeUsageSwitch.on = YES; // default on — most useful for support requests
[self.scrollView addSubview:self.includeUsageSwitch];
y += 30;
UILabel *usageHintLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, y, contentWidth, 0)];
usageHintLabel.text =
@"Attaches your coin usage history as a CSV file. "
"Helps resolve any questions about charges — you can open it in Numbers or Excel.";
usageHintLabel.font = [UIFont systemFontOfSize:12];
usageHintLabel.textColor = [UIColor secondaryLabelColor];
usageHintLabel.numberOfLines = 0;
[usageHintLabel sizeToFit];
usageHintLabel.frame = CGRectMake(16, y, contentWidth, usageHintLabel.frame.size.height);
[self.scrollView addSubview:usageHintLabel];
y += usageHintLabel.frame.size.height + 16;
// ── Settings snapshot preview ─────────────────────────────────────────────
[self makeSectionLabel:@"Settings that will be included:" y:&y];
self.settingsPreviewLabel =
[[UILabel alloc] initWithFrame:CGRectMake(16, y, contentWidth, 0)];
self.settingsPreviewLabel.text = [self buildSettingsSnapshotDisplayString];
self.settingsPreviewLabel.font = [UIFont monospacedSystemFontOfSize:11
weight:UIFontWeightRegular];
self.settingsPreviewLabel.textColor = [UIColor secondaryLabelColor];
self.settingsPreviewLabel.numberOfLines = 0;
self.settingsPreviewLabel.backgroundColor =
[UIColor colorWithDynamicProvider:^UIColor *(UITraitCollection *tc) {
return tc.userInterfaceStyle == UIUserInterfaceStyleDark
? [UIColor colorWithWhite:0.12 alpha:1.0]
: [UIColor colorWithWhite:0.95 alpha:1.0];
}];
self.settingsPreviewLabel.layer.cornerRadius = 8;
self.settingsPreviewLabel.clipsToBounds = YES;
[self.settingsPreviewLabel sizeToFit];
// Ensure it spans the full content width despite sizeToFit
self.settingsPreviewLabel.frame =
CGRectMake(16, y, contentWidth, self.settingsPreviewLabel.frame.size.height + 16);
// Add a small left inset via layer — UILabel doesn't have textContainerInset,
// so we just add a small invisible view as a left margin
// (achieved by padding the text itself below in buildSettingsSnapshotDisplayString)
[self.scrollView addSubview:self.settingsPreviewLabel];
y += self.settingsPreviewLabel.frame.size.height + 24;
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, y);
}
/// Tiny helper that adds a bold section label and advances y.
- (UILabel *)makeSectionLabel:(NSString *)text y:(CGFloat *)y {
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(16, *y, self.view.frame.size.width - 32, 20)];
label.text = text;
label.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold];
label.textColor = [UIColor secondaryLabelColor];
[self.scrollView addSubview:label];
*y += 26;
return label;
}
// ─────────────────────────────────────────────────────────────────────────────
// MARK: - Settings Snapshot
// ─────────────────────────────────────────────────────────────────────────────
/// Builds the plain-text settings block that goes into the email.
/// Reads only from NSUserDefaults — vault keys are never touched here.
- (NSString *)buildSettingsSnapshot {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray<NSString *> *sensitiveKeys = EZSensitiveUserDefaultsKeys();
// Collect the keys we actually care about presenting
NSDictionary<NSString *, NSString *> *knownSettings = @{
@"selectedModel" : @"Selected Model",
@"temperature" : @"Temperature",
@"frequency" : @"Freq Penalty",
@"webSearchEnabled" : @"Web Search",
@"webSearchLocation": @"Web Search Location",
@"soraModel" : @"Sora Model",
@"soraSize" : @"Sora Resolution",
@"soraDuration" : @"Sora Duration",
@"elevenVoiceID" : @"ElevenLabs Voice ID",
@"systemMessage" : @"System Message",
};
// Ordered display sequence
NSArray<NSString *> *displayOrder = @[
@"selectedModel", @"temperature", @"frequency",
@"webSearchEnabled", @"webSearchLocation",
@"soraModel", @"soraSize", @"soraDuration",
@"elevenVoiceID", @"systemMessage"
];
NSMutableString *snapshot = [NSMutableString string];
[snapshot appendString:@"── App Settings ──────────────────────────\n"];
// App version
NSDictionary *infoPlist = [NSBundle mainBundle].infoDictionary;
NSString *appVersion = infoPlist[@"CFBundleShortVersionString"] ?: @"Unknown";
NSString *buildNumber = infoPlist[@"CFBundleVersion"] ?: @"?";
[snapshot appendFormat:@"App Version : %@ (build %@)\n", appVersion, buildNumber];
// iOS + device
UIDevice *device = [UIDevice currentDevice];
[snapshot appendFormat:@"iOS : %@\n", device.systemVersion];
[snapshot appendFormat:@"Device : %@\n", device.model];
[snapshot appendString:@"\n"];
for (NSString *key in displayOrder) {
// Double-check against sensitive list — belt AND suspenders
if ([sensitiveKeys containsObject:key]) continue;
NSString *displayName = knownSettings[key] ?: key;
id value = [defaults objectForKey:key];
NSString *valueString = @"(not set)";
if ([key isEqualToString:@"systemMessage"]) {
// Include the full system message — important context when debugging
NSString *sysMsg = [defaults stringForKey:key] ?: @"";
valueString = sysMsg.length > 0 ? sysMsg : @"(not set)";
} else if ([key isEqualToString:@"webSearchEnabled"]) {
valueString = [defaults boolForKey:key] ? @"ON" : @"OFF";
} else if ([key isEqualToString:@"temperature"] ||
[key isEqualToString:@"frequency"]) {
valueString = value ? [NSString stringWithFormat:@"%.2f",
[defaults floatForKey:key]] : @"(not set)";
} else if ([key isEqualToString:@"soraDuration"]) {
NSInteger dur = [defaults integerForKey:key];
valueString = dur > 0 ? [NSString stringWithFormat:@"%lds", (long)dur] : @"(not set)";
} else {
valueString = ([value isKindOfClass:[NSString class]] && ((NSString *)value).length > 0)
? (NSString *)value : @"(not set)";
}
// Align the colon column for readability
NSString *paddedName = [displayName stringByPaddingToLength:20
withString:@" "
startingAtIndex:0];
[snapshot appendFormat:@"%@: %@\n", paddedName, valueString];
}
// Keychain presence indicators (no values, just "saved" or "not set")
[snapshot appendString:@"\n── Key Storage (vault presence only) ────\n"];
[snapshot appendFormat:@"OpenAI Key : %@\n",
[EZKeyVault hasKeyForIdentifier:EZVaultKeyOpenAI] ? @"saved in vault" : @"not saved"];
[snapshot appendFormat:@"ElevenLabs Key : %@\n",
[EZKeyVault hasKeyForIdentifier:EZVaultKeyElevenLabs] ? @"saved in vault" : @"not saved"];
return [snapshot copy];
}
/// Version shown in the settings preview label (indented for readability).
- (NSString *)buildSettingsSnapshotDisplayString {
NSString *snapshot = [self buildSettingsSnapshot];
// Add a little left padding since UILabel has no textContainerInset
NSMutableString *padded = [NSMutableString string];
for (NSString *line in [snapshot componentsSeparatedByString:@"\n"]) {
[padded appendFormat:@" %@\n", line];
}
return [padded copy];
}
// ─────────────────────────────────────────────────────────────────────────────
// MARK: - Actions
// ─────────────────────────────────────────────────────────────────────────────
- (void)cancelTapped {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)sendTapped {
// Validate user has entered something beyond the placeholder
BOOL hasMessage = self.messageTextView.textColor != [UIColor placeholderTextColor]
&& self.messageTextView.text.length > 0;
if (!hasMessage) {
UIAlertController *alert =
[UIAlertController alertControllerWithTitle:@"Message Required"
message:@"Please describe your issue or feedback before sending."
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
return;
}
// Load recipient from vault — never hardcoded in this file
NSString *recipientEmail = [EZKeyVault loadKeyForIdentifier:EZVaultKeySupportEmail];
if (!recipientEmail.length) {
UIAlertController *alert =
[UIAlertController alertControllerWithTitle:@"Support Unavailable"
message:@"Support contact could not be loaded. "
"Please update the app and try again."
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
EZLog(EZLogLevelError, @"SUPPORT", @"EZVaultKeySupportEmail not found in vault");
return;
}
if (self.includeUsageSwitch.isOn) {
// Disable Send and show a spinner while we fetch the usage log
self.navigationItem.rightBarButtonItem.enabled = NO;
UIActivityIndicatorView *fetchSpinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
[fetchSpinner startAnimating];
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:fetchSpinner];
[self fetchUsageCSVWithCompletion:^(NSData *csvData) {
dispatch_async(dispatch_get_main_queue(), ^{
// Restore the Send button regardless of outcome
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Send"
style:UIBarButtonItemStyleDone
target:self
action:@selector(sendTapped)];
[self composeMailTo:recipientEmail csvAttachment:csvData];
});
}];
} else {
[self composeMailTo:recipientEmail csvAttachment:nil];
}
}
// ─────────────────────────────────────────────────────────────────────────────
// MARK: - Usage CSV Fetch
// ─────────────────────────────────────────────────────────────────────────────
/// Fetches up to 500 rows from get-usage-log and formats them as UTF-8 CSV.
/// Calls completion with nil if the user is not signed in or the fetch fails —
/// the mail is still sent, just without the attachment.
- (void)fetchUsageCSVWithCompletion:(void (^)(NSData *_Nullable csvData))completion {
NSString *accessToken = [EZAuthManager shared].accessToken;
if (!accessToken.length) {
EZLog(EZLogLevelWarning, @"SUPPORT", @"No auth token — skipping usage CSV attachment");
completion(nil);
return;
}
static NSString *const kUsageEdgeURL =
@"https://spuoimtqofhbdzosrbng.supabase.co/functions/v1/get-usage-log";
NSURLComponents *components = [NSURLComponents componentsWithString:kUsageEdgeURL];
components.queryItems = @[
[NSURLQueryItem queryItemWithName:@"page" value:@"0"],
[NSURLQueryItem queryItemWithName:@"limit" value:@"500"],
];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:components.URL];
req.HTTPMethod = @"GET";
req.timeoutInterval = 20;
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[req setValue:[NSString stringWithFormat:@"Bearer %@", accessToken]
forHTTPHeaderField:@"Authorization"];
[[[NSURLSession sharedSession] dataTaskWithRequest:req
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error || !data) {
EZLogf(EZLogLevelError, @"SUPPORT", @"Usage fetch error: %@",
error.localizedDescription);
completion(nil);
return;
}
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray<NSDictionary *> *rows = nil;
if ([json isKindOfClass:[NSDictionary class]]) {
id rowsValue = ((NSDictionary *)json)[@"rows"];
if ([rowsValue isKindOfClass:[NSArray class]]) rows = rowsValue;
} else if ([json isKindOfClass:[NSArray class]]) {
rows = json;
}
if (!rows.count) {
completion(nil);
return;
}
NSData *csvData = [self buildCSVFromRows:rows];
completion(csvData);
}] resume];
}
/// Converts usage log rows into UTF-8 CSV data.
- (NSData *)buildCSVFromRows:(NSArray<NSDictionary *> *)rows {
NSMutableString *csv = [NSMutableString string];
// Header row
[csv appendString:@"Date,Feature,Coins Charged,Balance After,Quantity,Status,Prompt\n"];
// Simple CSV value escaper — wraps in quotes and doubles any internal quotes
NSString * (^escape)(id) = ^NSString *(id value) {
NSString *stringValue = @"";
if (value && value != (id)kCFNull) {
if ([value isKindOfClass:[NSString class]]) {
stringValue = (NSString *)value;
} else {
stringValue = [value description];
}
}
// Wrap in double quotes; escape internal double quotes by doubling them
NSString *escaped = [stringValue stringByReplacingOccurrencesOfString:@"\""
withString:@"\"\""];
return [NSString stringWithFormat:@"\"%@\"", escaped];
};
for (NSDictionary *row in rows) {
// Trim ISO timestamp to readable local form
NSString *isoDate = row[@"created_at"] ?: @"";
NSString *dateStr = isoDate.length >= 19 ? [isoDate substringToIndex:19] : isoDate;
NSString *feature = row[@"feature"] ?: @"";
NSString *coins = row[@"coins_charged"] ? [row[@"coins_charged"] description] : @"";
NSString *balance = row[@"running_balance"] ? [row[@"running_balance"] description] : @"";
NSString *quantity = row[@"quantity"] ? [row[@"quantity"] description] : @"";
NSString *status = row[@"status"] ?: @"";
NSString *prompt = row[@"prompt"] ?: @"";
[csv appendFormat:@"%@,%@,%@,%@,%@,%@,%@\n",
escape(dateStr), escape(feature), coins, balance,
quantity, escape(status), escape(prompt)];
}
return [csv dataUsingEncoding:NSUTF8StringEncoding];
}
// ─────────────────────────────────────────────────────────────────────────────
// MARK: - Mail Composition
// ─────────────────────────────────────────────────────────────────────────────
/// Builds and presents the mail composer. csvAttachment may be nil if the
/// usage fetch was skipped or failed — email is still sent without it.
- (void)composeMailTo:(NSString *)recipientEmail csvAttachment:(nullable NSData *)csvData {
// Build app version string for the subject line
NSDictionary *infoPlist = [NSBundle mainBundle].infoDictionary;
NSString *appVersion = infoPlist[@"CFBundleShortVersionString"] ?: @"?";
NSString *subject = [NSString stringWithFormat:@"EZCompleteUI v%@ — Support Request",
appVersion];
// Coin balance summary — always inline in the body regardless of CSV toggle
NSInteger coinBalance = [EZEntitlementManager shared].coinBalance;
NSString *tier = [EZEntitlementManager shared].currentTier ?: @"unknown";
// Build the shared email body
NSMutableString *body = [NSMutableString string];
[body appendString:@"USER MESSAGE\n"];
[body appendString:@"----------------------------------------\n"];
[body appendString:self.messageTextView.text];
[body appendString:@"\n\n\n"];
[body appendFormat:@"── Coin Summary ──────────────────────────\n"];
[body appendFormat:@"Current Balance : %ld coins\n", (long)coinBalance];
[body appendFormat:@"Subscription Tier : %@\n", tier];
if (csvData) {
[body appendString:@"Coin Usage CSV : attached (coin_usage.csv)\n"];
}
[body appendString:@"\n"];
[body appendString:[self buildSettingsSnapshot]];
// ── Path 1: MFMailComposeViewController (preferred — supports attachments) ─
if ([MFMailComposeViewController canSendMail]) {
if (self.includeLogSwitch.isOn) {
NSString *logPath = EZLogGetPath();
NSString *logContent = [NSString stringWithContentsOfFile:logPath
encoding:NSUTF8StringEncoding
error:nil];
if (logContent.length > 0) {
NSUInteger maxLogBytes = 50 * 1024;
if (logContent.length > maxLogBytes) {
logContent = [logContent substringFromIndex:logContent.length - maxLogBytes];
logContent = [@"[Log truncated to last 50 KB]\n\n" stringByAppendingString:logContent];
}
[body appendString:@"\n\n-- Debug Log -------------------------\n"];
[body appendString:logContent];
} else {
[body appendString:@"\n\n-- Debug Log -------------------------\n"];
[body appendString:@"(log file is empty or could not be read)\n"];
}
}
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
mailVC.mailComposeDelegate = self;
[mailVC setToRecipients:@[recipientEmail]];
[mailVC setSubject:subject];
[mailVC setMessageBody:body isHTML:NO];
// Attach coin usage CSV if available
if (csvData) {
[mailVC addAttachmentData:csvData
mimeType:@"text/csv"
fileName:@"coin_usage.csv"];
EZLog(EZLogLevelInfo, @"SUPPORT", @"Coin usage CSV attached to support email");
}
[self presentViewController:mailVC animated:YES completion:nil];
EZLog(EZLogLevelInfo, @"SUPPORT", @"Mail composer presented (MFMailComposeViewController)");
return;
}
// ── Path 2: mailto: URL fallback ─────────────────────────────────────────
EZLog(EZLogLevelInfo, @"SUPPORT",
@"canSendMail returned NO — falling back to mailto: URL");
if (self.includeLogSwitch.isOn) {
[body appendString:@"\n\n-- Debug Log -------------------------\n"];
[body appendString:@"[Log omitted: not supported via mailto: fallback. "
"Please send a follow-up with the log from Settings > Helper Stats.]\n"];
}
if (csvData) {
[body appendString:@"\n\n-- Coin Usage CSV --------------------\n"];
[body appendString:@"[CSV attachment omitted: not supported via mailto: fallback. "
"Please send a follow-up requesting your usage log.]\n"];
}
NSCharacterSet *mailtoAllowed = [NSCharacterSet
characterSetWithCharactersInString:
@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"];
NSString *encodedSubject = [subject
stringByAddingPercentEncodingWithAllowedCharacters:mailtoAllowed];
NSString *encodedBody = [body
stringByAddingPercentEncodingWithAllowedCharacters:mailtoAllowed];
NSString *mailtoURLString = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@",
recipientEmail, encodedSubject, encodedBody];
NSURL *mailtoURL = [NSURL URLWithString:mailtoURLString];
if (!mailtoURL || ![[UIApplication sharedApplication] canOpenURL:mailtoURL]) {
UIAlertController *noMailAlert =
[UIAlertController alertControllerWithTitle:@"No Mail App Found"
message:@"Could not find a mail app to send with. "
"Please install or configure a mail app and try again."
preferredStyle:UIAlertControllerStyleAlert];
[noMailAlert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:noMailAlert animated:YES completion:nil];
EZLog(EZLogLevelError, @"SUPPORT", @"mailto: URL also unavailable — no mail client found");
return;
}
[[UIApplication sharedApplication] openURL:mailtoURL options:@{} completionHandler:^(BOOL success) {
if (success) {
EZLog(EZLogLevelInfo, @"SUPPORT", @"mailto: URL opened successfully");
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
} else {
EZLog(EZLogLevelError, @"SUPPORT", @"mailto: URL failed to open");
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *failAlert =
[UIAlertController alertControllerWithTitle:@"Could Not Open Mail"
message:@"The mail app could not be opened. "
"Please try again or send feedback manually."
preferredStyle:UIAlertControllerStyleAlert];
[failAlert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:failAlert animated:YES completion:nil];
});
}
}];
}
- (void)dismissKeyboard {
[self.view endEditing:YES];
}
// ─────────────────────────────────────────────────────────────────────────────
// MARK: - MFMailComposeViewControllerDelegate
// ─────────────────────────────────────────────────────────────────────────────
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(nullable NSError *)error {
[controller dismissViewControllerAnimated:YES completion:^{
switch (result) {
case MFMailComposeResultSent:
EZLog(EZLogLevelInfo, @"SUPPORT", @"Support email sent successfully");
// Dismiss the support VC itself now that the mail is sent
[self dismissViewControllerAnimated:YES completion:nil];
break;
case MFMailComposeResultCancelled:
EZLog(EZLogLevelInfo, @"SUPPORT", @"Mail composer cancelled by user");
// User cancelled the mail sheet — stay on the support VC so they can try again
break;
case MFMailComposeResultFailed:
EZLogf(EZLogLevelError, @"SUPPORT", @"Mail send failed: %@", error.localizedDescription);
[self showSendError:error];
break;
case MFMailComposeResultSaved:
EZLog(EZLogLevelInfo, @"SUPPORT", @"Support email saved to drafts");
[self dismissViewControllerAnimated:YES completion:nil];
break;
}
}];
}
- (void)showSendError:(nullable NSError *)error {
NSString *detail = error.localizedDescription ?: @"Unknown error.";
UIAlertController *alert =
[UIAlertController alertControllerWithTitle:@"Send Failed"
message:[NSString stringWithFormat:
@"The email could not be sent. %@", detail]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
// ─────────────────────────────────────────────────────────────────────────────
// MARK: - UITextViewDelegate (placeholder simulation)
// ─────────────────────────────────────────────────────────────────────────────
- (void)textViewDidBeginEditing:(UITextView *)textView {
if (textView == self.messageTextView &&
textView.textColor == [UIColor placeholderTextColor]) {
textView.text = @"";
textView.textColor = [UIColor labelColor];
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView == self.messageTextView && textView.text.length == 0) {
textView.text = @"Describe your issue or feedback here...";
textView.textColor = [UIColor placeholderTextColor];
}
}
// ─────────────────────────────────────────────────────────────────────────────
// MARK: - Keyboard Avoidance
// ─────────────────────────────────────────────────────────────────────────────
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, keyboardFrame.size.height, 0);
self.scrollView.contentInset = insets;
self.scrollView.scrollIndicatorInsets = insets;
}
- (void)keyboardWillHide:(NSNotification *)notification {
self.scrollView.contentInset = UIEdgeInsetsZero;
self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
@end