From ee476a042f5221b3de13d9b6a67a64f4da18a8d9 Mon Sep 17 00:00:00 2001 From: Chris McGraw <2454408+wargcm@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:35:42 -0400 Subject: [PATCH] Handle remaining shares on post settings screen --- .../AztecPostViewController.swift | 1 + .../Gutenberg/GutenbergViewController.swift | 1 + .../Post/PostEditor+JetpackSocial.swift | 18 ++++++++++ ...SettingsViewController+JetpackSocial.swift | 17 +++++++++ .../Post/PostSettingsViewController.m | 36 +++++++++++++++++-- WordPress/WordPress.xcodeproj/project.pbxproj | 6 ++++ 6 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 WordPress/Classes/ViewRelated/Post/PostEditor+JetpackSocial.swift diff --git a/WordPress/Classes/ViewRelated/Aztec/ViewControllers/AztecPostViewController.swift b/WordPress/Classes/ViewRelated/Aztec/ViewControllers/AztecPostViewController.swift index 535db4eb771a..c5c5cc67989a 100644 --- a/WordPress/Classes/ViewRelated/Aztec/ViewControllers/AztecPostViewController.swift +++ b/WordPress/Classes/ViewRelated/Aztec/ViewControllers/AztecPostViewController.swift @@ -504,6 +504,7 @@ class AztecPostViewController: UIViewController, PostEditor { PostCoordinator.shared.cancelAnyPendingSaveOf(post: post) addObservers(toPost: post) registerMediaObserver() + disableSocialConnectionsIfNecessary() } required init?(coder aDecoder: NSCoder) { diff --git a/WordPress/Classes/ViewRelated/Gutenberg/GutenbergViewController.swift b/WordPress/Classes/ViewRelated/Gutenberg/GutenbergViewController.swift index 7ea35979c2b3..0fc511053b6d 100644 --- a/WordPress/Classes/ViewRelated/Gutenberg/GutenbergViewController.swift +++ b/WordPress/Classes/ViewRelated/Gutenberg/GutenbergViewController.swift @@ -338,6 +338,7 @@ class GutenbergViewController: UIViewController, PostEditor, FeaturedImageDelega PostCoordinator.shared.cancelAnyPendingSaveOf(post: post) self.navigationBarManager.delegate = self + disableSocialConnectionsIfNecessary() } required init?(coder aDecoder: NSCoder) { diff --git a/WordPress/Classes/ViewRelated/Post/PostEditor+JetpackSocial.swift b/WordPress/Classes/ViewRelated/Post/PostEditor+JetpackSocial.swift new file mode 100644 index 000000000000..e13f2911cb80 --- /dev/null +++ b/WordPress/Classes/ViewRelated/Post/PostEditor+JetpackSocial.swift @@ -0,0 +1,18 @@ + +extension PostEditor { + + func disableSocialConnectionsIfNecessary() { + guard FeatureFlag.jetpackSocial.enabled, + let post = self.post as? Post, + let remainingShares = self.post.blog.sharingLimit?.remaining, + let connections = self.post.blog.sortedConnections as? [PublicizeConnection], + remainingShares < connections.count else { + return + } + + for connection in connections { + post.disablePublicizeConnectionWithKeyringID(connection.keyringConnectionID) + } + } + +} diff --git a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController+JetpackSocial.swift b/WordPress/Classes/ViewRelated/Post/PostSettingsViewController+JetpackSocial.swift index f19daf336f67..ff5030d72307 100644 --- a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController+JetpackSocial.swift +++ b/WordPress/Classes/ViewRelated/Post/PostSettingsViewController+JetpackSocial.swift @@ -66,6 +66,23 @@ extension PostSettingsViewController { return hostController.view } + // MARK: - Social share cells + + @objc func userCanEditSharing() -> Bool { + guard let post = self.apost as? Post else { + return false + } + guard FeatureFlag.jetpackSocial.enabled else { + return post.canEditPublicizeSettings() + } + + return post.canEditPublicizeSettings() && remainingSocialShares() > 0 + } + + @objc func remainingSocialShares() -> Int { + self.apost.blog.sharingLimit?.remaining ?? .max + } + } // MARK: - Private methods diff --git a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m b/WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m index f2527725dc2f..fe0e0cf632d6 100644 --- a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m +++ b/WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m @@ -78,6 +78,7 @@ @interface PostSettingsViewController () *unsupportedConnections; +@property (nonatomic, strong) NSMutableArray *enabledConnections; @property (nonatomic, strong) NoResultsViewController *noResultsView; @property (nonatomic, strong) NSObject *mediaLibraryChangeObserverKey; @@ -112,6 +113,7 @@ - (instancetype)initWithPost:(AbstractPost *)aPost if (self) { self.apost = aPost; self.unsupportedConnections = @[]; + self.enabledConnections = [NSMutableArray array]; } return self; } @@ -255,6 +257,11 @@ - (void)setupPublicizeConnections } [supportedConnections addObject:connection]; + + if (![self.post publicizeConnectionDisabledForKeyringID:connection.keyringConnectionID] + && ![self.enabledConnections containsObject:connection.keyringConnectionID]) { + [self.enabledConnections addObject:connection.keyringConnectionID]; + } } self.publicizeConnections = supportedConnections; @@ -927,13 +934,19 @@ - (UITableViewCell *)configureDisclosureCellWithSharing:(BOOL)canEditSharing - (UITableViewCell *)configureShareCellForIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; - BOOL canEditSharing = [self.post canEditPublicizeSettings]; + BOOL canEditSharing = [self userCanEditSharing]; NSInteger sec = [[self.sections objectAtIndex:indexPath.section] integerValue]; NSArray *connections = sec == PostSettingsSectionShare ? self.publicizeConnections : self.unsupportedConnections; if (indexPath.row < connections.count) { + PublicizeConnection *connection = connections[indexPath.row]; + if ([Feature enabled:FeatureFlagJetpackSocial]) { + BOOL hasRemainingShares = self.enabledConnections.count < [self remainingSocialShares]; + BOOL isSwitchOn = ![self.post publicizeConnectionDisabledForKeyringID:connection.keyringConnectionID]; + canEditSharing = canEditSharing && (hasRemainingShares || isSwitchOn); + } cell = [self configureSocialCellForIndexPath:indexPath - connection:connections[indexPath.row] + connection:connection canEditSharing:canEditSharing section:sec]; } else { @@ -1170,8 +1183,18 @@ - (void)toggleShareConnectionForIndexPath:(NSIndexPath *) indexPath [cellSwitch setOn:!cellSwitch.on animated:YES]; if (cellSwitch.on) { [self.post enablePublicizeConnectionWithKeyringID:connection.keyringConnectionID]; + + if ([Feature enabled:FeatureFlagJetpackSocial]) { + [self.enabledConnections addObject:connection.keyringConnectionID]; + [self reloadSocialSectionComparingValue:[self remainingSocialShares]]; + } } else { [self.post disablePublicizeConnectionWithKeyringID:connection.keyringConnectionID]; + + if ([Feature enabled:FeatureFlagJetpackSocial]) { + [self.enabledConnections removeObject:connection.keyringConnectionID]; + [self reloadSocialSectionComparingValue:[self remainingSocialShares] - 1]; + } } } } @@ -1414,6 +1437,15 @@ - (UITableViewCell *)configureRemainingSharesCell return cell; } +- (void)reloadSocialSectionComparingValue:(NSUInteger)value +{ + if (self.enabledConnections.count == value) { + NSUInteger sharingSection = [self.sections indexOfObject:@(PostSettingsSectionShare)]; + NSIndexSet *sharingSectionSet = [NSIndexSet indexSetWithIndex:sharingSection]; + [self.tableView reloadSections:sharingSectionSet withRowAnimation:UITableViewRowAnimationNone]; + } +} + #pragma mark - WPMediaPickerViewControllerDelegate methods - (UIViewController *)emptyViewControllerForMediaPickerController:(WPMediaPickerViewController *)picker diff --git a/WordPress/WordPress.xcodeproj/project.pbxproj b/WordPress/WordPress.xcodeproj/project.pbxproj index f0db765d52c8..39a1d4d42cd1 100644 --- a/WordPress/WordPress.xcodeproj/project.pbxproj +++ b/WordPress/WordPress.xcodeproj/project.pbxproj @@ -2110,6 +2110,8 @@ 83DC5C472A4B769000DAA422 /* JetpackSocialSettingsRemainingSharesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83DC5C452A4B769000DAA422 /* JetpackSocialSettingsRemainingSharesView.swift */; }; 83E1E5592A58B5C2000B576F /* JetpackSocialError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83E1E5582A58B5C2000B576F /* JetpackSocialError.swift */; }; 83E1E55A2A58B5C2000B576F /* JetpackSocialError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83E1E5582A58B5C2000B576F /* JetpackSocialError.swift */; }; + 83E1E55F2A5CB8BF000B576F /* PostEditor+JetpackSocial.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83E1E55E2A5CB8BF000B576F /* PostEditor+JetpackSocial.swift */; }; + 83E1E5602A5CB8BF000B576F /* PostEditor+JetpackSocial.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83E1E55E2A5CB8BF000B576F /* PostEditor+JetpackSocial.swift */; }; 83EF3D7B2937D703000AF9BF /* SharedDataIssueSolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = FED65D78293511E4008071BF /* SharedDataIssueSolver.swift */; }; 83EF3D7F2937F08C000AF9BF /* SharedDataIssueSolverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83EF3D7C2937E969000AF9BF /* SharedDataIssueSolverTests.swift */; }; 83F3E26011275E07004CD686 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 83F3E25F11275E07004CD686 /* MapKit.framework */; }; @@ -7488,6 +7490,7 @@ 83C972DF281C45AB0049E1FE /* Post+BloggingPrompts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Post+BloggingPrompts.swift"; sourceTree = ""; }; 83DC5C452A4B769000DAA422 /* JetpackSocialSettingsRemainingSharesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackSocialSettingsRemainingSharesView.swift; sourceTree = ""; }; 83E1E5582A58B5C2000B576F /* JetpackSocialError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackSocialError.swift; sourceTree = ""; }; + 83E1E55E2A5CB8BF000B576F /* PostEditor+JetpackSocial.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PostEditor+JetpackSocial.swift"; sourceTree = ""; }; 83EF3D7C2937E969000AF9BF /* SharedDataIssueSolverTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedDataIssueSolverTests.swift; sourceTree = ""; }; 83F3E25F11275E07004CD686 /* MapKit.framework */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; }; 83F3E2D211276371004CD686 /* CoreLocation.framework */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; }; @@ -14796,6 +14799,7 @@ C3E2462826277B7700B99EA6 /* PostAuthorSelectorViewController.swift */, E13ACCD31EE5672100CCE985 /* PostEditor.swift */, E17FEAD7221490F7006E1D2D /* PostEditorAnalyticsSession.swift */, + 83E1E55E2A5CB8BF000B576F /* PostEditor+JetpackSocial.swift */, 91DCE84521A6A7F50062F134 /* PostEditor+MoreOptions.swift */, 91DCE84721A6C58C0062F134 /* PostEditor+Publish.swift */, 7E504D4921A5B8D400E341A8 /* PostEditorNavigationBarManager.swift */, @@ -21587,6 +21591,7 @@ 405BFB21223B37A000CD5BEA /* FollowersCountStatsRecordValue+CoreDataProperties.swift in Sources */, D8212CC720AA85C1008E8AE8 /* ReaderHeaderAction.swift in Sources */, 405B53FB1F83C369002E19BF /* FancyAlerts+VerificationPrompt.swift in Sources */, + 83E1E55F2A5CB8BF000B576F /* PostEditor+JetpackSocial.swift in Sources */, E6D2E1651B8AAD7E0000ED14 /* ReaderSiteStreamHeader.swift in Sources */, F126FE0020A33BDB0010EB6E /* VideoUploadProcessor.swift in Sources */, E166FA1B1BB0656B00374B5B /* PeopleCellViewModel.swift in Sources */, @@ -24629,6 +24634,7 @@ F4F9D5F2290993D400502576 /* MigrationWelcomeViewModel.swift in Sources */, FABB22ED2602FC2C00C8785C /* EditorFactory.swift in Sources */, FABB22EE2602FC2C00C8785C /* NotificationSiteSubscriptionViewController.swift in Sources */, + 83E1E5602A5CB8BF000B576F /* PostEditor+JetpackSocial.swift in Sources */, FABB22EF2602FC2C00C8785C /* PostingActivityDay.swift in Sources */, C7A09A4B28401B7B003096ED /* QRLoginService.swift in Sources */, FABB22F02602FC2C00C8785C /* ReaderTeamTopic.swift in Sources */,