OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/history/history_entries_status_item.h" |
| 6 |
| 7 #include "base/ios/weak_nsobject.h" |
| 8 #include "base/mac/foundation_util.h" |
| 9 #import "base/mac/objc_property_releaser.h" |
| 10 #include "components/strings/grit/components_strings.h" |
| 11 #include "ios/chrome/browser/chrome_url_constants.h" |
| 12 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" |
| 13 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 14 #import "ios/chrome/browser/ui/util/label_link_controller.h" |
| 15 #import "ios/chrome/common/string_util.h" |
| 16 #include "ios/chrome/grit/ios_strings.h" |
| 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/l10n/l10n_util_mac.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 // Delegate for HistoryEntriesStatusCell. |
| 22 @protocol HistoryEntriesStatusCellDelegate<NSObject> |
| 23 // Notifies the delegate that |URL| should be opened. |
| 24 - (void)historyEntriesStatusCell:(HistoryEntriesStatusCell*)cell |
| 25 didRequestOpenURL:(const GURL&)URL; |
| 26 @end |
| 27 |
| 28 @interface HistoryEntriesStatusCell () { |
| 29 // Property releaser for HistoryEntriesStatusItem. |
| 30 base::mac::ObjCPropertyReleaser _propertyReleaser_HistoryEntriesStatusCell; |
| 31 // Delegate for the HistoryEntriesStatusCell. Is notified when a link is |
| 32 // tapped. |
| 33 base::WeakNSProtocol<id<HistoryEntriesStatusCellDelegate>> _delegate; |
| 34 } |
| 35 // Redeclare as readwrite. |
| 36 @property(nonatomic, retain, readwrite) |
| 37 LabelLinkController* labelLinkController; |
| 38 // Delegate for the HistoryEntriesStatusCell. Is notified when a link is |
| 39 // tapped. |
| 40 @property(nonatomic, assign) id<HistoryEntriesStatusCellDelegate> delegate; |
| 41 // Sets links on the cell label. |
| 42 - (void)setLinksForSyncURL:(const GURL&)syncURL |
| 43 browsingDataURL:(const GURL&)browsingDataURL; |
| 44 @end |
| 45 |
| 46 @interface HistoryEntriesStatusItem ()<HistoryEntriesStatusCellDelegate> { |
| 47 // Delegate for the HistoryEntriesStatusItem. Is notified when a link is |
| 48 // tapped. |
| 49 base::WeakNSProtocol<id<HistoryEntriesStatusItemDelegate>> _delegate; |
| 50 } |
| 51 @end |
| 52 |
| 53 @implementation HistoryEntriesStatusItem |
| 54 |
| 55 @synthesize entriesStatus = _entriesStatus; |
| 56 @synthesize hidden = _hidden; |
| 57 @synthesize showsOtherBrowsingDataNotice = _showsOtherBrowsingDataNotice; |
| 58 |
| 59 - (instancetype)initWithType:(NSInteger)type { |
| 60 self = [super initWithType:type]; |
| 61 if (self) { |
| 62 _entriesStatus = NO_ENTRIES; |
| 63 _hidden = NO; |
| 64 _showsOtherBrowsingDataNotice = NO; |
| 65 } |
| 66 return self; |
| 67 } |
| 68 |
| 69 - (Class)cellClass { |
| 70 return [HistoryEntriesStatusCell class]; |
| 71 } |
| 72 |
| 73 - (void)configureCell:(HistoryEntriesStatusCell*)cell { |
| 74 [super configureCell:cell]; |
| 75 [cell setDelegate:self]; |
| 76 if (self.hidden) { |
| 77 cell.textLabel.text = nil; |
| 78 return; |
| 79 } |
| 80 |
| 81 NSString* text = nil; |
| 82 GURL syncLearnMoreURL; |
| 83 switch (self.entriesStatus) { |
| 84 case HistoryEntriesStatus::NO_ENTRIES: |
| 85 text = l10n_util::GetNSString(IDS_HISTORY_NO_RESULTS); |
| 86 break; |
| 87 case HistoryEntriesStatus::SYNCED_ENTRIES: |
| 88 text = l10n_util::GetNSString(IDS_IOS_HISTORY_HAS_SYNCED_RESULTS); |
| 89 syncLearnMoreURL = |
| 90 GURL(l10n_util::GetStringUTF8(IDS_IOS_HISTORY_SYNC_LEARN_MORE_URL)); |
| 91 break; |
| 92 case HistoryEntriesStatus::LOCAL_ENTRIES: |
| 93 text = l10n_util::GetNSString(IDS_IOS_HISTORY_NO_SYNCED_RESULTS); |
| 94 syncLearnMoreURL = |
| 95 GURL(l10n_util::GetStringUTF8(IDS_IOS_HISTORY_SYNC_LEARN_MORE_URL)); |
| 96 break; |
| 97 default: |
| 98 NOTREACHED(); |
| 99 break; |
| 100 } |
| 101 |
| 102 GURL otherBrowsingDataURL; |
| 103 if (self.showsOtherBrowsingDataNotice) { |
| 104 NSString* otherBrowsingNotice = |
| 105 l10n_util::GetNSString(IDS_IOS_HISTORY_OTHER_FORMS_OF_HISTORY); |
| 106 text = [NSString stringWithFormat:@"%@ %@", text, otherBrowsingNotice]; |
| 107 otherBrowsingDataURL = GURL(kHistoryMyActivityURL); |
| 108 } |
| 109 |
| 110 cell.textLabel.text = text; |
| 111 [cell setLinksForSyncURL:syncLearnMoreURL |
| 112 browsingDataURL:otherBrowsingDataURL]; |
| 113 } |
| 114 |
| 115 - (void)setDelegate:(id<HistoryEntriesStatusItemDelegate>)delegate { |
| 116 _delegate.reset(delegate); |
| 117 } |
| 118 |
| 119 - (id<HistoryEntriesStatusItemDelegate>)delegate { |
| 120 return _delegate; |
| 121 } |
| 122 |
| 123 - (void)historyEntriesStatusCell:(HistoryEntriesStatusCell*)cell |
| 124 didRequestOpenURL:(const GURL&)URL { |
| 125 [self.delegate historyEntriesStatusItem:self didRequestOpenURL:URL]; |
| 126 } |
| 127 |
| 128 - (BOOL)isEqualToHistoryEntriesStatusItem:(HistoryEntriesStatusItem*)object { |
| 129 return self.entriesStatus == object.entriesStatus && |
| 130 self.hidden == object.hidden && |
| 131 self.showsOtherBrowsingDataNotice == self.showsOtherBrowsingDataNotice; |
| 132 } |
| 133 |
| 134 - (BOOL)isEqual:(id)object { |
| 135 if (self == object) { |
| 136 return YES; |
| 137 } |
| 138 if (![object isKindOfClass:[HistoryEntriesStatusItem class]]) { |
| 139 return NO; |
| 140 } |
| 141 return [self |
| 142 isEqualToHistoryEntriesStatusItem:base::mac::ObjCCastStrict< |
| 143 HistoryEntriesStatusItem>(object)]; |
| 144 } |
| 145 |
| 146 @end |
| 147 |
| 148 @implementation HistoryEntriesStatusCell |
| 149 |
| 150 @synthesize labelLinkController = _labelLinkController; |
| 151 |
| 152 - (instancetype)initWithFrame:(CGRect)frame { |
| 153 self = [super initWithFrame:frame]; |
| 154 if (self) { |
| 155 _propertyReleaser_HistoryEntriesStatusCell.Init( |
| 156 self, [HistoryEntriesStatusCell class]); |
| 157 } |
| 158 return self; |
| 159 } |
| 160 |
| 161 - (void)setLinksForSyncURL:(const GURL&)syncURL |
| 162 browsingDataURL:(const GURL&)browsingDataURL { |
| 163 base::WeakNSObject<HistoryEntriesStatusCell> weakSelf(self); |
| 164 self.labelLinkController = [[[LabelLinkController alloc] |
| 165 initWithLabel:self.textLabel |
| 166 action:^(const GURL& URL) { |
| 167 [[weakSelf delegate] historyEntriesStatusCell:weakSelf |
| 168 didRequestOpenURL:URL]; |
| 169 }] autorelease]; |
| 170 [self.labelLinkController setLinkColor:[[MDCPalette cr_bluePalette] tint500]]; |
| 171 |
| 172 // Remove link delimiters from text and get ranges for links. Both links |
| 173 // must be parsed before being added to the controller because modifying the |
| 174 // label text clears all added links. |
| 175 // The sync URL, if present, will always come before the browsing data URL, |
| 176 // if present. |
| 177 NSRange syncRange; |
| 178 if (syncURL.is_valid()) { |
| 179 self.textLabel.text = ParseStringWithLink(self.textLabel.text, &syncRange); |
| 180 DCHECK(syncRange.location != NSNotFound && syncRange.length); |
| 181 } |
| 182 NSRange otherBrowsingDataRange; |
| 183 if (browsingDataURL.is_valid()) { |
| 184 self.textLabel.text = |
| 185 ParseStringWithLink(self.textLabel.text, &otherBrowsingDataRange); |
| 186 DCHECK(otherBrowsingDataRange.location != NSNotFound && |
| 187 otherBrowsingDataRange.length); |
| 188 } |
| 189 // Add links to the cell. |
| 190 if (syncURL.is_valid()) { |
| 191 [self.labelLinkController addLinkWithRange:syncRange url:syncURL]; |
| 192 } |
| 193 if (browsingDataURL.is_valid()) { |
| 194 [self.labelLinkController addLinkWithRange:otherBrowsingDataRange |
| 195 url:browsingDataURL]; |
| 196 } |
| 197 } |
| 198 |
| 199 - (void)setDelegate:(id<HistoryEntriesStatusCellDelegate>)delegate { |
| 200 _delegate.reset(delegate); |
| 201 } |
| 202 |
| 203 - (id<HistoryEntriesStatusCellDelegate>)delegate { |
| 204 return _delegate; |
| 205 } |
| 206 |
| 207 - (void)prepareForReuse { |
| 208 [super prepareForReuse]; |
| 209 self.labelLinkController = nil; |
| 210 self.delegate = nil; |
| 211 } |
| 212 |
| 213 @end |
OLD | NEW |