Skip to content

Commit

Permalink
Handle remaining shares on post settings screen
Browse files Browse the repository at this point in the history
  • Loading branch information
wargcm committed Jul 11, 2023
1 parent 48b8a39 commit ee476a0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ class AztecPostViewController: UIViewController, PostEditor {
PostCoordinator.shared.cancelAnyPendingSaveOf(post: post)
addObservers(toPost: post)
registerMediaObserver()
disableSocialConnectionsIfNecessary()
}

required init?(coder aDecoder: NSCoder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ class GutenbergViewController: UIViewController, PostEditor, FeaturedImageDelega

PostCoordinator.shared.cancelAnyPendingSaveOf(post: post)
self.navigationBarManager.delegate = self
disableSocialConnectionsIfNecessary()
}

required init?(coder aDecoder: NSCoder) {
Expand Down
18 changes: 18 additions & 0 deletions WordPress/Classes/ViewRelated/Post/PostEditor+JetpackSocial.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 34 additions & 2 deletions WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ @interface PostSettingsViewController () <UITextFieldDelegate,
@property (nonatomic, strong) WPAndDeviceMediaLibraryDataSource *mediaDataSource;
@property (nonatomic, strong) NSArray *publicizeConnections;
@property (nonatomic, strong) NSArray<PublicizeConnection *> *unsupportedConnections;
@property (nonatomic, strong) NSMutableArray<NSNumber *> *enabledConnections;

@property (nonatomic, strong) NoResultsViewController *noResultsView;
@property (nonatomic, strong) NSObject *mediaLibraryChangeObserverKey;
Expand Down Expand Up @@ -112,6 +113,7 @@ - (instancetype)initWithPost:(AbstractPost *)aPost
if (self) {
self.apost = aPost;
self.unsupportedConnections = @[];
self.enabledConnections = [NSMutableArray array];
}
return self;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<PublicizeConnection *> *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 {
Expand Down Expand Up @@ -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];
}
}
}
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -7488,6 +7490,7 @@
83C972DF281C45AB0049E1FE /* Post+BloggingPrompts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Post+BloggingPrompts.swift"; sourceTree = "<group>"; };
83DC5C452A4B769000DAA422 /* JetpackSocialSettingsRemainingSharesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackSocialSettingsRemainingSharesView.swift; sourceTree = "<group>"; };
83E1E5582A58B5C2000B576F /* JetpackSocialError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackSocialError.swift; sourceTree = "<group>"; };
83E1E55E2A5CB8BF000B576F /* PostEditor+JetpackSocial.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PostEditor+JetpackSocial.swift"; sourceTree = "<group>"; };
83EF3D7C2937E969000AF9BF /* SharedDataIssueSolverTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedDataIssueSolverTests.swift; sourceTree = "<group>"; };
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; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down

0 comments on commit ee476a0

Please sign in to comment.