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_entry_inserter.h" | 5 #import "ios/chrome/browser/ui/history/history_entry_inserter.h" |
6 | 6 |
7 #import "base/ios/weak_nsobject.h" | |
8 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
9 #include "base/mac/scoped_nsobject.h" | |
10 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
11 #include "base/time/time.h" | 9 #include "base/time/time.h" |
12 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" | 10 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
13 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 11 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
14 #import "ios/chrome/browser/ui/history/history_entry_item.h" | 12 #import "ios/chrome/browser/ui/history/history_entry_item.h" |
15 #include "ios/chrome/browser/ui/history/history_util.h" | 13 #include "ios/chrome/browser/ui/history/history_util.h" |
16 #include "url/gurl.h" | 14 #include "url/gurl.h" |
17 | 15 |
| 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 17 #error "This file requires ARC support." |
| 18 #endif |
| 19 |
18 @interface HistoryEntryInserter () { | 20 @interface HistoryEntryInserter () { |
19 // Delegate for the HistoryEntryInserter. | |
20 base::WeakNSProtocol<id<HistoryEntryInserterDelegate>> _delegate; | |
21 // CollectionViewModel in which to insert history entries. | 21 // CollectionViewModel in which to insert history entries. |
22 base::scoped_nsobject<CollectionViewModel> _collectionViewModel; | 22 CollectionViewModel* _collectionViewModel; |
23 // The index of the first section to contain history entries. | 23 // The index of the first section to contain history entries. |
24 NSInteger _firstSectionIndex; | 24 NSInteger _firstSectionIndex; |
25 // Number of assigned section identifiers. | 25 // Number of assigned section identifiers. |
26 NSInteger _sectionIdentifierCount; | 26 NSInteger _sectionIdentifierCount; |
27 // Sorted set of dates that have history entries. | 27 // Sorted set of dates that have history entries. |
28 base::scoped_nsobject<NSMutableOrderedSet> _dates; | 28 NSMutableOrderedSet* _dates; |
29 // Mapping from dates to section identifiers. | 29 // Mapping from dates to section identifiers. |
30 base::scoped_nsobject<NSMutableDictionary> _sectionIdentifiers; | 30 NSMutableDictionary* _sectionIdentifiers; |
31 } | 31 } |
32 | 32 |
33 @end | 33 @end |
34 | 34 |
35 @implementation HistoryEntryInserter | 35 @implementation HistoryEntryInserter |
| 36 @synthesize delegate = _delegate; |
36 | 37 |
37 - (instancetype)initWithModel:(CollectionViewModel*)collectionViewModel { | 38 - (instancetype)initWithModel:(CollectionViewModel*)collectionViewModel { |
38 if ((self = [super init])) { | 39 if ((self = [super init])) { |
39 _collectionViewModel.reset([collectionViewModel retain]); | 40 _collectionViewModel = collectionViewModel; |
40 _firstSectionIndex = [collectionViewModel numberOfSections]; | 41 _firstSectionIndex = [collectionViewModel numberOfSections]; |
41 _dates.reset([[NSMutableOrderedSet alloc] init]); | 42 _dates = [[NSMutableOrderedSet alloc] init]; |
42 _sectionIdentifiers.reset([[NSMutableDictionary dictionary] retain]); | 43 _sectionIdentifiers = [NSMutableDictionary dictionary]; |
43 } | 44 } |
44 return self; | 45 return self; |
45 } | 46 } |
46 | 47 |
47 - (id<HistoryEntryInserterDelegate>)delegate { | |
48 return _delegate; | |
49 } | |
50 | |
51 - (void)setDelegate:(id<HistoryEntryInserterDelegate>)delegate { | |
52 _delegate.reset(delegate); | |
53 } | |
54 | 48 |
55 - (void)insertHistoryEntryItem:(HistoryEntryItem*)item { | 49 - (void)insertHistoryEntryItem:(HistoryEntryItem*)item { |
56 NSInteger sectionIdentifier = | 50 NSInteger sectionIdentifier = |
57 [self sectionIdentifierForTimestamp:item.timestamp]; | 51 [self sectionIdentifierForTimestamp:item.timestamp]; |
58 | 52 |
59 NSComparator objectComparator = ^(id obj1, id obj2) { | 53 NSComparator objectComparator = ^(id obj1, id obj2) { |
60 HistoryEntryItem* firstObject = | 54 HistoryEntryItem* firstObject = |
61 base::mac::ObjCCastStrict<HistoryEntryItem>(obj1); | 55 base::mac::ObjCCastStrict<HistoryEntryItem>(obj1); |
62 HistoryEntryItem* secondObject = | 56 HistoryEntryItem* secondObject = |
63 base::mac::ObjCCastStrict<HistoryEntryItem>(obj2); | 57 base::mac::ObjCCastStrict<HistoryEntryItem>(obj2); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 NSComparator comparator = ^(id obj1, id obj2) { | 113 NSComparator comparator = ^(id obj1, id obj2) { |
120 // Dates are ordered from most to least recent. | 114 // Dates are ordered from most to least recent. |
121 return [obj2 compare:obj1]; | 115 return [obj2 compare:obj1]; |
122 }; | 116 }; |
123 NSUInteger index = [_dates indexOfObject:date | 117 NSUInteger index = [_dates indexOfObject:date |
124 inSortedRange:NSMakeRange(0, [_dates count]) | 118 inSortedRange:NSMakeRange(0, [_dates count]) |
125 options:NSBinarySearchingInsertionIndex | 119 options:NSBinarySearchingInsertionIndex |
126 usingComparator:comparator]; | 120 usingComparator:comparator]; |
127 [_dates insertObject:date atIndex:index]; | 121 [_dates insertObject:date atIndex:index]; |
128 NSInteger insertionIndex = _firstSectionIndex + index; | 122 NSInteger insertionIndex = _firstSectionIndex + index; |
129 CollectionViewTextItem* header = [[[CollectionViewTextItem alloc] | 123 CollectionViewTextItem* header = |
130 initWithType:kItemTypeEnumZero] autorelease]; | 124 [[CollectionViewTextItem alloc] initWithType:kItemTypeEnumZero]; |
131 header.text = | 125 header.text = |
132 base::SysUTF16ToNSString(history::GetRelativeDateLocalized(timestamp)); | 126 base::SysUTF16ToNSString(history::GetRelativeDateLocalized(timestamp)); |
133 [_collectionViewModel insertSectionWithIdentifier:sectionIdentifier | 127 [_collectionViewModel insertSectionWithIdentifier:sectionIdentifier |
134 atIndex:insertionIndex]; | 128 atIndex:insertionIndex]; |
135 [_collectionViewModel setHeader:header | 129 [_collectionViewModel setHeader:header |
136 forSectionWithIdentifier:sectionIdentifier]; | 130 forSectionWithIdentifier:sectionIdentifier]; |
137 [self.delegate historyEntryInserter:self | 131 [self.delegate historyEntryInserter:self |
138 didInsertSectionAtIndex:insertionIndex]; | 132 didInsertSectionAtIndex:insertionIndex]; |
139 return sectionIdentifier; | 133 return sectionIdentifier; |
140 } | 134 } |
(...skipping 15 matching lines...) Expand all Loading... |
156 [_sectionIdentifiers removeObjectForKey:date]; | 150 [_sectionIdentifiers removeObjectForKey:date]; |
157 [_dates removeObject:date]; | 151 [_dates removeObject:date]; |
158 break; | 152 break; |
159 } | 153 } |
160 } | 154 } |
161 [self.delegate historyEntryInserter:self | 155 [self.delegate historyEntryInserter:self |
162 didRemoveSectionAtIndex:sectionIndex]; | 156 didRemoveSectionAtIndex:sectionIndex]; |
163 } | 157 } |
164 | 158 |
165 @end | 159 @end |
OLD | NEW |