Skip to content

Commit

Permalink
[Follow] Improve FollowManagementUIUpdater logic
Browse files Browse the repository at this point in the history
This is a follow up cl resolving comments on crrev.com/c/3580117

Bug: 1264872
Change-Id: Ie2ed371765fb12d423ab6c6641a6fd1d46ab7b84
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3684910
Reviewed-by: edchin <[email protected]>
Commit-Queue: edchin <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1010304}
  • Loading branch information
Tina Wang authored and Chromium LUCI CQ committed Jun 2, 2022
1 parent 6eda4ba commit f0f001f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "base/mac/foundation_util.h"
#import "ios/chrome/browser/net/crurl.h"
#import "ios/chrome/browser/ui/follow/follow_block_types.h"
#import "ios/chrome/browser/ui/follow/followed_web_channel.h"
#import "ios/chrome/browser/ui/ntp/feed_management/feed_management_navigation_delegate.h"
#import "ios/chrome/browser/ui/ntp/feed_management/follow_management_view_delegate.h"
Expand Down Expand Up @@ -244,17 +243,18 @@ - (void)unfollowTapped {
#pragma mark - FollowManagementUIUpdater

- (void)removeFollowedWebChannel:(FollowedWebChannel*)channel {
for (id cell in self.tableView.visibleCells) {
for (UITableViewCell* cell in self.tableView.visibleCells) {
FollowedWebChannelCell* followedWebChannelCell =
base::mac::ObjCCastStrict<FollowedWebChannelCell>(cell);

if ([followedWebChannelCell.followedWebChannel isEqual:channel]) {
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
[followedWebChannelCell stopAnimatingActivityIndicator];
self.lastUnfollowedWebChannelItem =
base::mac::ObjCCastStrict<FollowedWebChannelItem>(
[self.tableViewModel itemAtIndexPath:indexPath]);
self.indexPathOfLastUnfollowAttempt = indexPath;
[self deleteItemAtIndexPath:indexPath];
[self deleteItemAtIndex:indexPath];
return;
}
}
Expand All @@ -264,7 +264,7 @@ - (void)addFollowedWebChannel:(FollowedWebChannel*)channel {
DCHECK(
[self.lastUnfollowedWebChannelItem.followedWebChannel isEqual:channel]);
[self addItem:self.lastUnfollowedWebChannelItem
AtIndexPath:self.indexPathOfLastUnfollowAttempt];
atIndex:self.indexPathOfLastUnfollowAttempt];
}

#pragma mark - Helpers
Expand All @@ -289,55 +289,12 @@ - (void)requestUnfollowWebChannelAtIndexPath:(NSIndexPath*)indexPath {

self.indexPathOfLastUnfollowAttempt = indexPath;

FollowedWebChannelItem* followedWebChannelItem =
base::mac::ObjCCastStrict<FollowedWebChannelItem>(
[self.tableViewModel itemAtIndexPath:indexPath]);
FollowedWebChannelCell* followedWebChannelCell =
base::mac::ObjCCastStrict<FollowedWebChannelCell>(
[self.tableView cellForRowAtIndexPath:indexPath]);
[followedWebChannelCell startAnimatingActivityIndicator];

__weak FollowManagementViewController* weakSelf = self;
followedWebChannelItem.followedWebChannel.unfollowRequestBlock(
^(BOOL success) {
[followedWebChannelCell stopAnimatingActivityIndicator];
if (success) {
// TODO(crbug.com/1296745): Show success snackbar
// with undo button.
weakSelf.lastUnfollowedWebChannelItem = followedWebChannelItem;
[weakSelf deleteItemAtIndexPath:indexPath];
} else {
// TODO(crbug.com/1296745): Show failure snackbar
// with try again button.
}
});
}

- (void)retryUnfollow {
[self.feedMetricsRecorder recordManagementTappedUnfollowTryAgainOnSnackbar];
[self
requestUnfollowWebChannelAtIndexPath:self.indexPathOfLastUnfollowAttempt];
}

- (void)undoUnfollow {
[self.feedMetricsRecorder
recordManagementTappedRefollowAfterUnfollowOnSnackbar];

// TODO(crbug.com/1296745): Start spinner over UNDO text in snackbar.
FollowedWebChannelItem* unfollowedItem = self.lastUnfollowedWebChannelItem;
__weak FollowManagementViewController* weakSelf = self;
unfollowedItem.followedWebChannel.refollowRequestBlock(^(BOOL success) {
// TODO(crbug.com/1296745): Stop spinner over UNDO text in snackbar.
if (success) {
[weakSelf reinsertLastUnfollowedItem];
} else {
// TODO(crbug.com/1296745): Show undo failure snackbar.
}
});
}

#pragma mark - Helpers

- (void)showOrHideEmptyTableViewBackground {
TableViewModel* model = self.tableViewModel;
NSInteger section =
Expand All @@ -355,7 +312,7 @@ - (void)showOrHideEmptyTableViewBackground {
}

// Deletes item at |indexPath| from both model and UI.
- (void)deleteItemAtIndexPath:(NSIndexPath*)indexPath {
- (void)deleteItemAtIndex:(NSIndexPath*)indexPath {
TableViewModel* model = self.tableViewModel;
NSInteger sectionID =
[model sectionIdentifierForSectionIndex:indexPath.section];
Expand All @@ -369,8 +326,7 @@ - (void)deleteItemAtIndexPath:(NSIndexPath*)indexPath {
[self showOrHideEmptyTableViewBackground];
}

- (void)addItem:(FollowedWebChannelItem*)item
AtIndexPath:(NSIndexPath*)indexPath {
- (void)addItem:(FollowedWebChannelItem*)item atIndex:(NSIndexPath*)indexPath {
TableViewModel* model = self.tableViewModel;
NSInteger sectionID =
[model sectionIdentifierForSectionIndex:indexPath.section];
Expand All @@ -385,23 +341,4 @@ - (void)addItem:(FollowedWebChannelItem*)item
[self showOrHideEmptyTableViewBackground];
}

// Reinserts last unfollowed item into both model and UI.
- (void)reinsertLastUnfollowedItem {
TableViewModel* model = self.tableViewModel;
NSIndexPath* indexPath = self.indexPathOfLastUnfollowAttempt;

NSInteger sectionID =
[model sectionIdentifierForSectionIndex:indexPath.section];
NSUInteger index = [model indexInItemTypeForIndexPath:indexPath];

[model insertItem:self.lastUnfollowedWebChannelItem
inSectionWithIdentifier:sectionID
atIndex:index];
[self.tableView insertRowsAtIndexPaths:@[ indexPath ]
withRowAnimation:UITableViewRowAnimationAutomatic];
self.lastUnfollowedWebChannelItem = nil;
self.indexPathOfLastUnfollowAttempt = nil;
[self showOrHideEmptyTableViewBackground];
}

@end
17 changes: 0 additions & 17 deletions ios/showcase/follow/sc_follow_view_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#import "ios/chrome/browser/net/crurl.h"
#import "ios/chrome/browser/ui/follow/first_follow_favicon_data_source.h"
#import "ios/chrome/browser/ui/follow/first_follow_view_controller.h"
#import "ios/chrome/browser/ui/follow/follow_block_types.h"
#import "ios/chrome/browser/ui/follow/followed_web_channel.h"
#import "ios/chrome/browser/ui/icons/chrome_symbol.h"
#import "ios/chrome/browser/ui/ntp/feed_management/feed_management_follow_delegate.h"
Expand Down Expand Up @@ -186,22 +185,6 @@ - (FollowedWebChannel*)createWebChannelWithTitle:(NSString*)title {
channel.available = YES;
channel.faviconURL =
[[CrURL alloc] initWithNSURL:[NSURL URLWithString:kExampleFaviconURL]];

__weak FollowedWebChannel* weakChannel = channel;
channel.unfollowRequestBlock = ^(RequestCompletionBlock completion) {
// This mimics a successful unfollow on the server.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
[self.followManagementUIUpdater
removeFollowedWebChannel:weakChannel];
});
// This mimics refollowing after a few seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 13 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
[self.followManagementUIUpdater
addFollowedWebChannel:weakChannel];
});
};
return channel;
}

Expand Down

0 comments on commit f0f001f

Please sign in to comment.