chromium/ios/chrome/browser/ui/settings/notifications/notifications_view_controller.mm

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/chrome/browser/ui/settings/notifications/notifications_view_controller.h"

#import "base/apple/foundation_util.h"
#import "base/metrics/user_metrics.h"
#import "base/metrics/user_metrics_action.h"
#import "ios/chrome/browser/commerce/model/push_notification/push_notification_feature.h"
#import "ios/chrome/browser/shared/public/features/features.h"
#import "ios/chrome/browser/shared/ui/table_view/cells/table_view_switch_cell.h"
#import "ios/chrome/browser/shared/ui/table_view/cells/table_view_switch_item.h"
#import "ios/chrome/browser/shared/ui/table_view/cells/table_view_text_header_footer_item.h"
#import "ios/chrome/browser/shared/ui/table_view/table_view_utils.h"
#import "ios/chrome/browser/ui/settings/notifications/notifications_constants.h"
#import "ios/chrome/browser/ui/settings/notifications/notifications_view_controller_delegate.h"
#import "ios/chrome/grit/ios_strings.h"
#import "ui/base/l10n/l10n_util.h"

namespace {

typedef NS_ENUM(NSInteger, SectionIdentifier) {
  SectionIdentifierNotificationsPriceTracking = kSectionIdentifierEnumZero,
  SectionIdentifierNotificationsContent,
  SectionIdentifierNotificationsTips,
};

}  // namespace

@interface NotificationsViewController ()

// All the items for the price notifications section received by mediator.
@property(nonatomic, strong) TableViewItem* priceTrackingItem;
// All the items for the content notifications section received by mediator.
@property(nonatomic, strong) TableViewItem* contentNotificationsItem;
// All the items for the tips notifications section received by mediator.
@property(nonatomic, strong) TableViewSwitchItem* tipsNotificationsItem;
// Tips Notifications footer item received by the mediator.
@property(nonatomic, strong)
    TableViewHeaderFooterItem* tipsNotificationsFooterItem;

@end

@implementation NotificationsViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.title = l10n_util::GetNSString(IDS_IOS_NOTIFICATIONS_TITLE);
  self.tableView.accessibilityIdentifier = kNotificationsTableViewId;
  [self loadModel];
}

#pragma mark - SettingsControllerProtocol

- (void)reportDismissalUserAction {
  base::RecordAction(
      base::UserMetricsAction("MobilePriceNotificationsSettingsClose"));
}

- (void)reportBackUserAction {
  base::RecordAction(
      base::UserMetricsAction("MobilePriceNotificationsSettingsBack"));
}

#pragma mark - LegacyChromeTableViewController

- (void)loadModel {
  [super loadModel];

  TableViewModel* model = self.tableViewModel;
  [model addSectionWithIdentifier:SectionIdentifierNotificationsPriceTracking];
  if (IsPriceNotificationsEnabled()) {
    [model addItem:self.priceTrackingItem
        toSectionWithIdentifier:SectionIdentifierNotificationsPriceTracking];
  }
  if ([self isContentNotificationEnabled]) {
    [model addSectionWithIdentifier:SectionIdentifierNotificationsContent];
    [model addItem:self.contentNotificationsItem
        toSectionWithIdentifier:SectionIdentifierNotificationsContent];
  }
  if (IsIOSTipsNotificationsEnabled()) {
    [model addSectionWithIdentifier:SectionIdentifierNotificationsTips];
    [model addItem:self.tipsNotificationsItem
        toSectionWithIdentifier:SectionIdentifierNotificationsTips];
    [model setFooter:self.tipsNotificationsFooterItem
        forSectionWithIdentifier:SectionIdentifierNotificationsTips];
  }
}

#pragma mark - UITableViewDataSource

- (UITableViewCell*)tableView:(UITableView*)tableView
        cellForRowAtIndexPath:(NSIndexPath*)indexPath {
  UITableViewCell* cell = [super tableView:tableView
                     cellForRowAtIndexPath:indexPath];
  if ([cell isKindOfClass:[TableViewSwitchCell class]]) {
    TableViewSwitchCell* switchCell =
        base::apple::ObjCCastStrict<TableViewSwitchCell>(cell);
    [switchCell.switchView addTarget:self
                              action:@selector(switchAction:)
                    forControlEvents:UIControlEventValueChanged];
    TableViewItem* item = [self.tableViewModel itemAtIndexPath:indexPath];
    switchCell.switchView.tag = item.type;
  }
  return cell;
}

#pragma mark - UIViewController

- (void)didMoveToParentViewController:(UIViewController*)parent {
  [super didMoveToParentViewController:parent];
  if (!parent) {
    [self.presentationDelegate notificationsViewControllerDidRemove:self];
  }
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView*)tableView
    didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
  TableViewModel* model = self.tableViewModel;
  TableViewItem* selectedItem = [model itemAtIndexPath:indexPath];
  [self.modelDelegate didSelectItem:selectedItem];
}

#pragma mark - Private

// Called when switch is toggled.
- (void)switchAction:(UISwitch*)sender {
  NSIndexPath* indexPath =
      [self.tableViewModel indexPathForItemType:sender.tag];
  DCHECK(indexPath);
  TableViewSwitchItem* switchItem =
      base::apple::ObjCCastStrict<TableViewSwitchItem>(
          [self.tableViewModel itemAtIndexPath:indexPath]);
  DCHECK(switchItem);
  [self.modelDelegate didToggleSwitchItem:switchItem withValue:sender.isOn];
}

@end