-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PostSettingsViewController.m
1436 lines (1210 loc) · 57.2 KB
/
PostSettingsViewController.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
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "PostSettingsViewController.h"
#import "PostSettingsViewController_Internal.h"
#import "FeaturedImageViewController.h"
#import "Media.h"
#import "PostFeaturedImageCell.h"
#import "SettingsSelectionViewController.h"
#import "SharingDetailViewController.h"
#import "WPTableViewActivityCell.h"
#import "CoreDataStack.h"
#import "MediaService.h"
#import "WPProgressTableViewCell.h"
#import <Photos/Photos.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
#import <Reachability/Reachability.h>
#import "WPGUIConstants.h"
#import <WordPressShared/NSString+XMLExtensions.h>
#import <WordPressShared/WPTextFieldTableViewCell.h>
#import "WordPress-Swift.h"
@import Gridicons;
@import WordPressShared;
@import WordPressKit;
@import WordPressUI;
typedef NS_ENUM(NSInteger, PostSettingsRow) {
PostSettingsRowCategories = 0,
PostSettingsRowTags,
PostSettingsRowAuthor,
PostSettingsRowPublishDate,
PostSettingsRowStatus,
PostSettingsRowVisibility,
PostSettingsRowPassword,
PostSettingsRowFormat,
PostSettingsRowFeaturedImage,
PostSettingsRowFeaturedImageAdd,
PostSettingsRowFeaturedImageRemove,
PostSettingsRowFeaturedLoading,
PostSettingsRowShareConnection,
PostSettingsRowShareMessage,
PostSettingsRowSlug,
PostSettingsRowExcerpt,
PostSettingsRowSocialNoConnections,
PostSettingsRowSocialRemainingShares
};
static CGFloat CellHeight = 44.0f;
static CGFloat LoadingIndicatorHeight = 28.0f;
static NSString *const PostSettingsAnalyticsTrackingSource = @"post_settings";
static NSString *const TableViewActivityCellIdentifier = @"TableViewActivityCellIdentifier";
static NSString *const TableViewProgressCellIdentifier = @"TableViewProgressCellIdentifier";
static NSString *const TableViewFeaturedImageCellIdentifier = @"TableViewFeaturedImageCellIdentifier";
static NSString *const TableViewStickyPostCellIdentifier = @"TableViewStickyPostCellIdentifier";
static NSString *const TableViewGenericCellIdentifier = @"TableViewGenericCellIdentifier";
@interface PostSettingsViewController () <UITextFieldDelegate,
UIImagePickerControllerDelegate, UINavigationControllerDelegate,
UIPopoverControllerDelegate,
PostCategoriesViewControllerDelegate, PostFeaturedImageCellDelegate,
FeaturedImageViewControllerDelegate>
@property (nonatomic, strong) AbstractPost *apost;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UIButton *passwordVisibilityButton;
@property (nonatomic, strong) NSArray *postMetaSectionRows;
@property (nonatomic, strong) NSArray *visibilityList;
@property (nonatomic, strong) NSArray *formatsList;
@property (nonatomic, strong) UIImage *featuredImage;
@property (nonatomic, strong) NSData *animatedFeaturedImageData;
@property (nonatomic, readonly) CGSize featuredImageSize;
@property (assign) BOOL textFieldDidHaveFocusBeforeOrientationChange;
@property (nonatomic, strong) NSArray *publicizeConnections;
@property (nonatomic, strong) NSArray<PublicizeConnection *> *unsupportedConnections;
@property (nonatomic, strong) NSMutableArray<NSNumber *> *enabledConnections;
@property (nonatomic, strong) NSDateFormatter *postDateFormatter;
#pragma mark - Properties: Services
@property (nonatomic, strong, readonly) BlogService *blogService;
@property (nonatomic, strong, readonly) SharingService *sharingService;
#pragma mark - Properties: Reachability
@property (nonatomic, strong, readwrite) Reachability *internetReachability;
@end
@implementation PostSettingsViewController
#pragma mark - Initialization and dealloc
- (void)dealloc
{
[self.internetReachability stopNotifier];
[self removeMediaObserver];
}
- (instancetype)initWithPost:(AbstractPost *)aPost
{
self = [super initWithStyle:UITableViewStyleInsetGrouped];
if (self) {
self.apost = aPost;
self.unsupportedConnections = @[];
self.enabledConnections = [NSMutableArray array];
}
return self;
}
#pragma mark - UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self.apost isKindOfClass:[Page class]]) {
self.title = NSLocalizedString(@"Page Settings", @"The title of the Page Settings screen.");
} else {
self.title = NSLocalizedString(@"Post Settings", @"The title of the Post Settings screen.");
}
DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd));
[WPStyleGuide configureColorsForView:self.view andTableView:self.tableView];
[WPStyleGuide configureAutomaticHeightRowsFor:self.tableView];
self.visibilityList = @[NSLocalizedString(@"Public", @"Privacy setting for posts set to 'Public' (default). Should be the same as in core WP."),
NSLocalizedString(@"Password protected", @"Privacy setting for posts set to 'Password protected'. Should be the same as in core WP."),
NSLocalizedString(@"Private", @"Privacy setting for posts set to 'Private'. Should be the same as in core WP.")];
[self setupFormatsList];
[self setupPublicizeConnections];
[self.tableView registerNib:[UINib nibWithNibName:@"WPTableViewActivityCell" bundle:nil] forCellReuseIdentifier:TableViewActivityCellIdentifier];
[self.tableView registerClass:[WPProgressTableViewCell class] forCellReuseIdentifier:TableViewProgressCellIdentifier];
[self.tableView registerClass:[PostFeaturedImageCell class] forCellReuseIdentifier:TableViewFeaturedImageCellIdentifier];
[self.tableView registerClass:[SwitchTableViewCell class] forCellReuseIdentifier:TableViewStickyPostCellIdentifier];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TableViewGenericCellIdentifier];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 44.0)]; // add some vertical padding
self.tableView.cellLayoutMarginsFollowReadableWidth = YES;
// Compensate for the first section's height of 1.0f
self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0, 0, 0);
self.tableView.accessibilityIdentifier = @"SettingsTable";
self.isUploadingMedia = NO;
_blogService = [[BlogService alloc] initWithCoreDataStack:[ContextManager sharedInstance]];
[self setupPostDateFormatter];
[WPAnalytics track:WPAnalyticsStatPostSettingsShown];
// It's recommended to keep this call near the end of the initial setup, since we don't want
// reachability callbacks to trigger before such initial setup completes.
//
[self setupReachability];
[self setupStandaloneEditor];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:NO];
[self.navigationController setToolbarHidden:YES];
[self setupPublicizeConnections]; // Refresh in case the user disconnects from unsupported services.
[self configureMetaSectionRows];
[self reloadData];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (self.isStandalone) {
if (!self.isStandaloneEditorDismissingAfterSave) {
[self.apost.managedObjectContext refreshObject:self.apost mergeChanges:NO];
}
} else {
[self.apost.managedObjectContext performBlock:^{
[self.apost.managedObjectContext save:nil];
}];
}
}
- (void)didReceiveMemoryWarning
{
DDLogWarn(@"%@ %@", self, NSStringFromSelector(_cmd));
[super didReceiveMemoryWarning];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
if ([self.passwordTextField isFirstResponder]) {
self.textFieldDidHaveFocusBeforeOrientationChange = YES;
}
}
#pragma mark - Password Field
- (void)togglePasswordVisibility
{
NSAssert(_passwordTextField, @"The password text field should be set here.");
self.passwordTextField.secureTextEntry = !self.passwordTextField.secureTextEntry;
[self refreshPasswordVisibilityButton];
}
- (void)refreshPasswordVisibilityButton
{
NSAssert(_passwordTextField, @"The password text field should be set here.");
NSAssert(_passwordVisibilityButton, @"The password visibility button should be set here.");
UIImage *icon;
BOOL passwordIsVisible = !self.passwordTextField.secureTextEntry;
if (passwordIsVisible) {
icon = [UIImage gridiconOfType:GridiconTypeVisible];
} else {
icon = [UIImage gridiconOfType:GridiconTypeNotVisible];
}
[self.passwordVisibilityButton setImage:icon forState:UIControlStateNormal];
[self.passwordVisibilityButton sizeToFit];
}
#pragma mark - Additional setup
- (void)setupFormatsList
{
self.formatsList = self.post.blog.sortedPostFormatNames;
}
- (void)setupPublicizeConnections
{
// Separate Twitter connections if the service is unsupported.
PublicizeService *twitterService = [PublicizeService lookupPublicizeServiceNamed:@"twitter"
inContext:self.apost.managedObjectContext];
if (!twitterService || [twitterService isSupported]) {
return;
}
NSMutableArray<PublicizeConnection *> *supportedConnections = [NSMutableArray new];
NSMutableArray<PublicizeConnection *> *unsupportedConnections = [NSMutableArray new];
for (PublicizeConnection *connection in self.post.blog.sortedConnections) {
if ([connection.service isEqualToString:twitterService.serviceID]) {
[unsupportedConnections addObject:connection];
continue;
}
[supportedConnections addObject:connection];
if (![self.post publicizeConnectionDisabledForKeyringID:connection.keyringConnectionID]
&& ![self.enabledConnections containsObject:connection.keyringConnectionID]) {
[self.enabledConnections addObject:connection.keyringConnectionID];
}
}
self.publicizeConnections = supportedConnections;
self.unsupportedConnections = unsupportedConnections;
}
- (void)setupReachability
{
self.internetReachability = [Reachability reachabilityForInternetConnection];
__weak __typeof(self) weakSelf = self;
self.internetReachability.reachableBlock = ^void(Reachability * __unused reachability) {
[weakSelf internetIsReachableAgain];
};
[self.internetReachability startNotifier];
}
- (void)setupPostDateFormatter
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.timeZone = [self.apost.blog timeZone];
self.postDateFormatter = dateFormatter;
}
#pragma mark - Reachability handling
- (void)internetIsReachableAgain
{
[self synchUnavailableData];
}
- (void)synchUnavailableData
{
__weak __typeof(self) weakSelf = self;
if (self.formatsList.count == 0) {
[self synchPostFormatsAndDo:^{
// DRM: if we ever start synchronizing anything else that could affect the table data
// aside from the post formats, we will need reload the table view only once all of the
// synchronization calls complete.
//
[[weakSelf tableView] reloadData];
}];
}
}
- (void)synchPostFormatsAndDo:(void(^)(void))completionBlock
{
__weak __typeof(self) weakSelf = self;
[self.blogService syncPostFormatsForBlog:self.apost.blog success:^{
[weakSelf setupFormatsList];
completionBlock();
} failure:^(NSError * _Nonnull __unused error) {
completionBlock();
}];
}
// sync the latest state of Twitter.
- (void)syncPublicizeServices
{
__weak __typeof(self) weakSelf = self;
[self.sharingService syncPublicizeServicesForBlog:self.apost.blog success:^{
[weakSelf setupPublicizeConnections];
} failure:nil];
}
#pragma mark - Instance Methods
- (void)setApost:(AbstractPost *)apost
{
if ([apost isEqual:_apost]) {
return;
}
_apost = apost;
}
- (Post *)post
{
if ([self.apost isKindOfClass:[Post class]]) {
return (Post *)self.apost;
}
return nil;
}
- (void)endEditingAction:(id)sender
{
if (self.passwordTextField) {
[self.passwordTextField resignFirstResponder];
}
}
- (void)endEditingForTextFieldAction:(id)sender
{
[self.passwordTextField endEditing:YES];
}
- (void)reloadData
{
self.passwordTextField.text = self.apost.password;
[self configureSections];
[self.tableView reloadData];
}
#pragma mark - TextField Delegate Methods
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (self.textFieldDidHaveFocusBeforeOrientationChange) {
self.textFieldDidHaveFocusBeforeOrientationChange = NO;
return NO;
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == self.passwordTextField) {
self.apost.password = textField.text;
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#pragma mark - UITableView Delegate
- (void)configureSections
{
NSNumber *stickyPostSection = @(PostSettingsSectionStickyPost);
NSNumber *disabledTwitterSection = @(PostSettingsSectionDisabledTwitter);
NSNumber *remainingSharesSection = @(PostSettingsSectionSharesRemaining);
NSMutableArray *sections = [@[ @(PostSettingsSectionTaxonomy),
@(PostSettingsSectionMeta),
@(PostSettingsSectionFormat),
@(PostSettingsSectionFeaturedImage),
stickyPostSection,
@(PostSettingsSectionShare),
disabledTwitterSection,
remainingSharesSection,
@(PostSettingsSectionMoreOptions) ] mutableCopy];
// Remove sticky post section for self-hosted non Jetpack site
// and non admin user
//
if (![self.apost.blog supports:BlogFeatureWPComRESTAPI] && !self.apost.blog.isAdmin) {
[sections removeObject:stickyPostSection];
}
if (self.unsupportedConnections.count == 0) {
[sections removeObject:disabledTwitterSection];
}
if (![self showRemainingShares]) {
[sections removeObject:remainingSharesSection];
}
self.sections = [sections copy];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (!self.sections) {
[self configureSections];
}
return [self.sections count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger sec = [[self.sections objectAtIndex:section] integerValue];
if (sec == PostSettingsSectionTaxonomy) {
return 2;
} else if (sec == PostSettingsSectionMeta) {
return [self.postMetaSectionRows count];
} else if (sec == PostSettingsSectionFormat) {
return 1;
} else if (sec == PostSettingsSectionFeaturedImage) {
return 1;
} else if (sec == PostSettingsSectionStickyPost) {
return 1;
} else if (sec == PostSettingsSectionShare) {
return [self numberOfRowsForShareSection];
} else if (sec == PostSettingsSectionDisabledTwitter) {
return self.unsupportedConnections.count;
} else if (sec == PostSettingsSectionSharesRemaining) {
return 1;
} else if (sec == PostSettingsSectionMoreOptions) {
return 2;
}
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSInteger sec = [[self.sections objectAtIndex:section] integerValue];
if (sec == PostSettingsSectionTaxonomy) {
return NSLocalizedString(@"Taxonomy", @"Label for the Taxonomy area (categories, keywords, ...) in post settings.");
} else if (sec == PostSettingsSectionMeta) {
return NSLocalizedString(@"Publish", @"Label for the publish (verb) button. Tapping publishes a draft post.");
} else if (sec == PostSettingsSectionFormat) {
return NSLocalizedString(@"Post Format", @"For setting the format of a post.");
} else if (sec == PostSettingsSectionFeaturedImage) {
return NSLocalizedString(@"Featured Image", @"Label for the Featured Image area in post settings.");
} else if (sec == PostSettingsSectionStickyPost) {
return NSLocalizedString(@"Mark as Sticky", @"Label for the Mark as Sticky option in post settings.");
} else if (sec == PostSettingsSectionShare && [self numberOfRowsForShareSection] > 0) {
return NSLocalizedString(@"Jetpack Social", @"Label for the Sharing section in post Settings. Should be the same as WP core.");
} else if (sec == PostSettingsSectionDisabledTwitter) {
return NSLocalizedStringWithDefaultValue(@"postSettings.section.disabledTwitter.header",
nil,
[NSBundle mainBundle],
@"Twitter Auto-Sharing Is No Longer Available",
@"Section title for the disabled Twitter service in the Post Settings screen");
} else if (sec == PostSettingsSectionMoreOptions) {
return NSLocalizedString(@"More Options", @"Label for the More Options area in post settings. Should use the same translation as core WP.");
}
return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
NSInteger sec = [[self.sections objectAtIndex:section] integerValue];
if (sec == PostSettingsSectionDisabledTwitter) {
TwitterDeprecationTableFooterView *footerView = [[TwitterDeprecationTableFooterView alloc] init];
footerView.presentingViewController = self;
footerView.source = @"post_settings";
return footerView;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ([self tableView:tableView numberOfRowsInSection:section] == 0) {
return CGFLOAT_MIN;
} else {
return UITableViewAutomaticDimension;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if ([self tableView:tableView numberOfRowsInSection:section] == 0) {
return CGFLOAT_MIN;
} else {
return UITableViewAutomaticDimension;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger sectionId = [[self.sections objectAtIndex:indexPath.section] integerValue];
if (sectionId == PostSettingsSectionFeaturedImage) {
if ([self isUploadingMedia]) {
return CellHeight + (2.f * PostFeaturedImageCellMargin);
} else if (self.featuredImage) {
return self.featuredImageSize.height + 2.f * PostFeaturedImageCellMargin;
} else {
return LoadingIndicatorHeight + 2.f * PostFeaturedImageCellMargin;
}
}
if (sectionId == PostSettingsSectionMeta) {
NSInteger row = [[self.postMetaSectionRows objectAtIndex:indexPath.row] integerValue];
if (row == PostSettingsRowPassword) {
return CellHeight;
}
}
return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger sec = [[self.sections objectAtIndex:indexPath.section] integerValue];
UITableViewCell *cell;
if (sec == PostSettingsSectionTaxonomy) {
cell = [self configureTaxonomyCellForIndexPath:indexPath];
} else if (sec == PostSettingsSectionMeta) {
cell = [self configureMetaPostMetaCellForIndexPath:indexPath];
} else if (sec == PostSettingsSectionFormat) {
cell = [self configurePostFormatCellForIndexPath:indexPath];
} else if (sec == PostSettingsSectionFeaturedImage) {
cell = [self configureFeaturedImageCellForIndexPath:indexPath];
} else if (sec == PostSettingsSectionStickyPost) {
cell = [self configureStickyPostCellForIndexPath:indexPath];
} else if (sec == PostSettingsSectionShare || sec == PostSettingsSectionDisabledTwitter) {
cell = [self showNoConnection] ? [self configureNoConnectionCell] : [self configureShareCellForIndexPath:indexPath];
} else if (sec == PostSettingsSectionSharesRemaining) {
cell = [self configureRemainingSharesCell];
} else if (sec == PostSettingsSectionMoreOptions) {
cell = [self configureMoreOptionsCellForIndexPath:indexPath];
}
return cell ?: [UITableViewCell new];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSInteger sec = [[self.sections objectAtIndex:indexPath.section] integerValue];
if (cell.tag == PostSettingsRowCategories) {
[self showCategoriesSelection];
} else if (cell.tag == PostSettingsRowTags) {
[self showTagsPicker];
} else if (cell.tag == PostSettingsRowPublishDate) {
[self showPublishSchedulingController];
} else if (cell.tag == PostSettingsRowStatus) {
[self showPostStatusSelector];
} else if (cell.tag == PostSettingsRowVisibility) {
[self showPostVisibilitySelector];
} else if (cell.tag == PostSettingsRowAuthor) {
[self showPostAuthorSelector];
} else if (cell.tag == PostSettingsRowFormat) {
[self showPostFormatSelector];
} else if (cell.tag == PostSettingsRowFeaturedImage) {
[self showFeaturedImageSelector];
} else if (cell.tag == PostSettingsRowFeaturedImageAdd) {
[self showFeaturedImageSelector];
} else if (cell.tag == PostSettingsRowFeaturedImageRemove) {
[self showFeaturedImageRemoveOrRetryActionAtIndexPath:indexPath];
} else if (sec == PostSettingsSectionDisabledTwitter) {
[self showShareDetailForIndexPath:indexPath];
} else if (cell.tag == PostSettingsRowShareConnection) {
[self toggleShareConnectionForIndexPath:indexPath];
} else if (cell.tag == PostSettingsRowShareMessage) {
[self showEditShareMessageController];
} else if (cell.tag == PostSettingsRowSlug) {
[self showEditSlugController];
} else if (cell.tag == PostSettingsRowExcerpt) {
[self showEditExcerptController];
}
}
- (NSInteger)numberOfRowsForShareSection
{
if ([self.apost.status isEqualToString:@"private"]) {
return 0;
}
if (self.apost.blog.supportsPublicize && self.publicizeConnections.count > 0) {
// One row per publicize connection plus an extra row for the publicze message
return self.publicizeConnections.count + 1;
}
return [self showNoConnection] ? 1 : 0;
}
- (UITableViewCell *)configureTaxonomyCellForIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self getWPTableViewDisclosureCell];
if (indexPath.row == PostSettingsRowCategories) {
// Categories
cell.textLabel.text = NSLocalizedString(@"Categories", @"Label for the categories field. Should be the same as WP core.");
cell.detailTextLabel.text = [NSString decodeXMLCharactersIn:[self.post categoriesText]];
cell.tag = PostSettingsRowCategories;
cell.accessibilityIdentifier = @"Categories";
} else if (indexPath.row == PostSettingsRowTags) {
// Tags
cell.textLabel.text = NSLocalizedString(@"Tags", @"Label for the tags field. Should be the same as WP core.");
cell.detailTextLabel.text = self.post.tags;
cell.tag = PostSettingsRowTags;
cell.accessibilityIdentifier = @"Tags";
}
return cell;
}
- (void)configureMetaSectionRows
{
NSMutableArray *metaRows = [[NSMutableArray alloc] init];
if (self.apost.isMultiAuthorBlog) {
[metaRows addObject:@(PostSettingsRowAuthor)];
}
[metaRows addObjectsFromArray:@[ @(PostSettingsRowPublishDate),
@(PostSettingsRowStatus),
@(PostSettingsRowVisibility) ]];
if (self.apost.password) {
[metaRows addObject:@(PostSettingsRowPassword)];
}
self.postMetaSectionRows = [metaRows copy];
}
- (UITableViewCell *)configureMetaPostMetaCellForIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSInteger row = [[self.postMetaSectionRows objectAtIndex:indexPath.row] integerValue];
if (row == PostSettingsRowAuthor) {
// Author
cell = [self getWPTableViewDisclosureCell];
cell.textLabel.text = NSLocalizedString(@"Author", @"The author of the post or page.");
cell.accessibilityIdentifier = @"SetAuthor";
cell.detailTextLabel.text = [self.apost authorNameForDisplay];
cell.tag = PostSettingsRowAuthor;
} else if (row == PostSettingsRowPublishDate) {
// Publish date
cell = [self getWPTableViewDisclosureCell];
if (self.apost.dateCreated && ![self.apost shouldPublishImmediately]) {
if ([self.apost hasFuturePublishDate]) {
cell.textLabel.text = NSLocalizedString(@"Scheduled for", @"Scheduled for [date]");
} else {
cell.textLabel.text = NSLocalizedString(@"Published on", @"Published on [date]");
}
cell.detailTextLabel.text = [self.postDateFormatter stringFromDate:self.apost.dateCreated];
} else {
cell.textLabel.text = NSLocalizedString(@"Publish Date", @"Label for the publish date button.");
cell.detailTextLabel.text = NSLocalizedString(@"Immediately", @"");
}
if ([self.apost.status isEqualToString:PostStatusPrivate]) {
[cell disable];
} else {
[cell enable];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.tag = PostSettingsRowPublishDate;
} else if (row == PostSettingsRowStatus) {
// Publish Status
cell = [self getWPTableViewDisclosureCell];
cell.textLabel.text = NSLocalizedString(@"Status", @"The status of the post. Should be the same as in core WP.");
cell.accessibilityIdentifier = @"Status";
cell.detailTextLabel.text = self.apost.statusTitle;
if ([self.apost.status isEqualToString:PostStatusPrivate]) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} else {
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
cell.tag = PostSettingsRowStatus;
} else if (row == PostSettingsRowVisibility) {
// Visibility
cell = [self getWPTableViewDisclosureCell];
cell.textLabel.text = NSLocalizedString(@"Visibility", @"The visibility settings of the post. Should be the same as in core WP.");
cell.detailTextLabel.text = [self.apost titleForVisibility];
cell.tag = PostSettingsRowVisibility;
cell.accessibilityIdentifier = @"Visibility";
} else if (row == PostSettingsRowPassword) {
cell = [self configurePasswordCell];
}
return cell;
}
- (UITableViewCell *)configurePasswordCell
{
// Password
WPTextFieldTableViewCell *textCell = [self getWPTableViewTextFieldCell];
textCell.textLabel.text = NSLocalizedString(@"Password", @"Label for the password field. Should be the same as WP core.");
textCell.textField.textColor = [UIColor murielText];
textCell.textField.text = self.apost.password;
textCell.textField.attributedPlaceholder = nil;
textCell.textField.placeholder = NSLocalizedString(@"Enter a password", @"");
textCell.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textCell.textField.secureTextEntry = YES;
textCell.tag = PostSettingsRowPassword;
self.passwordTextField = textCell.textField;
self.passwordTextField.accessibilityIdentifier = @"Password Value";
[self configureVisibilityButtonForPasswordCell:textCell];
return textCell;
}
- (void)configureVisibilityButtonForPasswordCell:(WPTextFieldTableViewCell *)textCell
{
self.passwordVisibilityButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.passwordVisibilityButton.tintColor = [UIColor murielNeutral20];
[self.passwordVisibilityButton addTarget:self action:@selector(togglePasswordVisibility) forControlEvents:UIControlEventTouchUpInside];
[self refreshPasswordVisibilityButton];
textCell.accessoryView = self.passwordVisibilityButton;
}
- (UITableViewCell *)configurePostFormatCellForIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self getWPTableViewDisclosureCell];
cell.textLabel.text = NSLocalizedString(@"Post Format", @"The post formats available for the post. Should be the same as in core WP.");
if (self.post.postFormatText.length > 0) {
cell.detailTextLabel.text = self.post.postFormatText;
} else {
cell.detailTextLabel.text = NSLocalizedString(@"Unavailable",
@"Message to show in the post-format cell when the post format is not available");
}
cell.tag = PostSettingsRowFormat;
cell.accessibilityIdentifier = @"Post Format";
return cell;
}
- (UITableViewCell *)configureFeaturedImageCellForIndexPath:(NSIndexPath *)indexPath
{
if (!self.apost.featuredImage && !self.isUploadingMedia) {
return [self cellForSetFeaturedImage];
} else if (self.isUploadingMedia || self.apost.featuredImage.remoteStatus == MediaRemoteStatusPushing) {
// Is featured Image set on the post and it's being pushed to the server?
if (!self.isUploadingMedia) {
self.isUploadingMedia = YES;
[self setupObservingOfMedia:self.apost.featuredImage];
}
self.featuredImage = nil;
return [self cellForFeaturedImageUploadProgressAtIndexPath:indexPath];
} else if (self.apost.featuredImage && self.apost.featuredImage.remoteStatus == MediaRemoteStatusFailed) {
// Do we have an feature image set and for some reason the upload failed?
return [self cellForFeaturedImageError];
} else {
NSURL *featuredURL = [self urlForFeaturedImage];
if (!featuredURL) {
return [self cellForSetFeaturedImage];
}
return [self cellForFeaturedImageWithURL:featuredURL atIndexPath:indexPath];
}
}
- (UITableViewCell *)configureStickyPostCellForIndexPath:(NSIndexPath *)indexPath
{
__weak __typeof(self) weakSelf = self;
SwitchTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:TableViewStickyPostCellIdentifier];
cell.name = NSLocalizedString(@"Stick post to the front page", @"This is the cell title.");
cell.on = self.post.isStickyPost;
cell.onChange = ^(BOOL newValue) {
[WPAnalytics trackEvent:WPAnalyticsEventEditorPostStickyChanged properties:@{@"via": @"settings"}];
weakSelf.post.isStickyPost = newValue;
};
return cell;
}
- (UITableViewCell *)cellForSetFeaturedImage
{
UITableViewCell *cell = [self makeSetFeaturedImageCell];
cell.tag = PostSettingsRowFeaturedImageAdd;
return cell;
}
- (UITableViewCell *)cellForFeaturedImageError
{
WPTableViewActivityCell *activityCell = [self getWPTableViewActivityCell];
activityCell.textLabel.text = NSLocalizedString(@"Upload failed. Tap for options.", @"Description to show on post setting for a featured image that failed to upload.");
activityCell.tag = PostSettingsRowFeaturedImageRemove;
return activityCell;
}
- (UITableViewCell *)cellForFeaturedImageUploadProgressAtIndexPath:(NSIndexPath *)indexPath
{
self.progressCell = [self.tableView dequeueReusableCellWithIdentifier:TableViewProgressCellIdentifier forIndexPath:indexPath];
[WPStyleGuide configureTableViewCell:self.progressCell];
[self.progressCell setProgress:self.featuredImageProgress];
self.progressCell.tag = PostSettingsRowFeaturedLoading;
return self.progressCell;
}
- (UITableViewCell *)cellForFeaturedImageWithURL:(nonnull NSURL *)featuredURL atIndexPath:(NSIndexPath *)indexPath
{
PostFeaturedImageCell *featuredImageCell = [self.tableView dequeueReusableCellWithIdentifier:TableViewFeaturedImageCellIdentifier forIndexPath:indexPath];
featuredImageCell.delegate = self;
[WPStyleGuide configureTableViewCell:featuredImageCell];
[featuredImageCell setImageWithURL:featuredURL inPost:self.apost withSize:self.featuredImageSize];
featuredImageCell.tag = PostSettingsRowFeaturedImage;
return featuredImageCell;
}
- (nullable NSURL *)urlForFeaturedImage {
NSURL *featuredURL = self.apost.featuredImage.absoluteLocalURL;
if (!featuredURL || ![featuredURL checkResourceIsReachableAndReturnError:nil]) {
featuredURL = [NSURL URLWithString:self.apost.featuredImage.remoteURL];
}
if (!featuredURL) {
featuredURL = self.apost.featuredImageURLForDisplay;
}
return featuredURL;
}
- (UITableViewCell *)configureSocialCellForIndexPath:(NSIndexPath *)indexPath
connection:(PublicizeConnection *)connection
canEditSharing:(BOOL)canEditSharing
section:(NSInteger)section
{
BOOL isJetpackSocialEnabled = [RemoteFeature enabled:RemoteFeatureFlagJetpackSocialImprovements];
UITableViewCell *cell = [self getWPTableViewImageAndAccessoryCell];
UIImage *image = [WPStyleGuide socialIconFor:connection.service];
if (isJetpackSocialEnabled) {
image = [image resizedImageWithContentMode:UIViewContentModeScaleAspectFill
bounds:CGSizeMake(28.0, 28.0)
interpolationQuality:kCGInterpolationDefault];
}
[cell.imageView setImage:image];
cell.imageView.alpha = 1.0;
if (canEditSharing && !isJetpackSocialEnabled) {
cell.imageView.tintColor = [WPStyleGuide tintColorForConnectedService: connection.service];
} else if (!canEditSharing && isJetpackSocialEnabled) {
cell.imageView.alpha = 0.36;
}
cell.textLabel.text = connection.externalDisplay;
cell.textLabel.enabled = canEditSharing;
if (connection.isBroken) {
cell.accessoryView = section == PostSettingsSectionShare ?
[WPStyleGuide sharingCellWarningAccessoryImageView] :
[WPStyleGuide sharingCellErrorAccessoryImageView];
} else {
UISwitch *switchAccessory = [[UISwitch alloc] initWithFrame:CGRectZero];
// This interaction is handled at a cell level
switchAccessory.userInteractionEnabled = NO;
switchAccessory.on = ![self.post publicizeConnectionDisabledForKeyringID:connection.keyringConnectionID];
switchAccessory.enabled = canEditSharing;
cell.accessoryView = switchAccessory;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag = PostSettingsRowShareConnection;
cell.accessibilityIdentifier = [NSString stringWithFormat:@"%@ %@", connection.service, connection.externalDisplay];
return cell;
}
- (UITableViewCell *)configureDisclosureCellWithSharing:(BOOL)canEditSharing
{
UITableViewCell *cell = [self getWPTableViewDisclosureCell];
cell.textLabel.text = NSLocalizedString(@"Message", @"Label for the share message field on the post settings.");
cell.textLabel.enabled = canEditSharing;
cell.detailTextLabel.text = self.post.publicizeMessage ? self.post.publicizeMessage : self.post.titleForDisplay;
cell.detailTextLabel.enabled = canEditSharing;
cell.tag = PostSettingsRowShareMessage;
cell.accessibilityIdentifier = @"Customize the message";
return cell;
}
- (UITableViewCell *)configureShareCellForIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
BOOL canEditSharing = [self userCanEditSharing];
NSInteger sec = [[self.sections objectAtIndex:indexPath.section] integerValue];
NSArray<PublicizeConnection *> *connections = sec == PostSettingsSectionShare ? self.publicizeConnections : self.unsupportedConnections;
if (indexPath.row < connections.count) {
PublicizeConnection *connection = connections[indexPath.row];
if ([RemoteFeature enabled:RemoteFeatureFlagJetpackSocialImprovements]) {
BOOL hasRemainingShares = self.enabledConnections.count < [self remainingSocialShares];
BOOL isSwitchOn = ![self.post publicizeConnectionDisabledForKeyringID:connection.keyringConnectionID];
canEditSharing = canEditSharing && (hasRemainingShares || isSwitchOn);
}
cell = [self configureSocialCellForIndexPath:indexPath
connection:connection
canEditSharing:canEditSharing
section:sec];
} else {
cell = [self configureDisclosureCellWithSharing:canEditSharing];
}
cell.userInteractionEnabled = canEditSharing;
return cell;
}
- (UITableViewCell *)configureMoreOptionsCellForIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return [self configureSlugCellForIndexPath:indexPath];
} else {
return [self configureExcerptCellForIndexPath:indexPath];
}
}
- (UITableViewCell *)configureSlugCellForIndexPath:(NSIndexPath *)indexPath
{
WPTableViewCell *cell = [self getWPTableViewDisclosureCell];
cell.textLabel.text = NSLocalizedString(@"Slug", @"Label for the slug field. Should be the same as WP core.");
cell.detailTextLabel.text = self.apost.slugForDisplay;
cell.tag = PostSettingsRowSlug;
cell.accessibilityIdentifier = @"Slug";
return cell;
}
- (UITableViewCell *)configureExcerptCellForIndexPath:(NSIndexPath *)indexPath
{
WPTableViewCell *cell = [self getWPTableViewDisclosureCell];
cell.textLabel.text = NSLocalizedString(@"Excerpt", @"Label for the excerpt field. Should be the same as WP core.");
cell.detailTextLabel.text = self.apost.mt_excerpt;
cell.tag = PostSettingsRowExcerpt;
cell.accessibilityIdentifier = @"Excerpt";
return cell;
}
- (WPTableViewCell *)getWPTableViewDisclosureCell
{
static NSString *WPTableViewDisclosureCellIdentifier = @"WPTableViewDisclosureCellIdentifier";
WPTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:WPTableViewDisclosureCellIdentifier];
if (!cell) {
cell = [[WPTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:WPTableViewDisclosureCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[WPStyleGuide configureTableViewCell:cell];
}