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