1// Copyright 2018 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/reading_list/reading_list_list_item_factory.h"
6
7#include "base/mac/foundation_util.h"
8#include "base/strings/sys_string_conversions.h"
9#include "components/reading_list/core/reading_list_entry.h"
10#include "components/url_formatter/url_formatter.h"
11#import "ios/chrome/browser/ui/reading_list/reading_list_list_item_custom_action_factory.h"
12#import "ios/chrome/browser/ui/reading_list/reading_list_list_item_util.h"
13#import "ios/chrome/browser/ui/reading_list/reading_list_table_view_item.h"
14#import "ios/chrome/browser/ui/reading_list/reading_list_utils.h"
15
16#if !defined(__has_feature) || !__has_feature(objc_arc)
17#error "This file requires ARC support."
18#endif
19
20@interface ReadingListListItemFactory ()
21
22// The factory supplying custom accessibility actions to the items.
23@property(nonatomic, readonly, strong)
24    ReadingListListItemCustomActionFactory* customActionFactory;
25
26@end
27
28@implementation ReadingListListItemFactory
29@synthesize customActionFactory = _customActionFactory;
30
31- (instancetype)init {
32  if (self = [super init]) {
33    _customActionFactory =
34        [[ReadingListListItemCustomActionFactory alloc] init];
35  }
36  return self;
37}
38
39#pragma mark Accessors
40
41- (void)setAccessibilityDelegate:
42    (id<ReadingListListItemAccessibilityDelegate>)accessibilityDelegate {
43  self.customActionFactory.accessibilityDelegate = accessibilityDelegate;
44}
45
46- (id<ReadingListListItemAccessibilityDelegate>)accessibilityDelegate {
47  return self.customActionFactory.accessibilityDelegate;
48}
49
50#pragma mark Public
51
52- (ListItem<ReadingListListItem>*)cellItemForReadingListEntry:
53    (const ReadingListEntry*)entry {
54  ListItem<ReadingListListItem>* item =
55      [[ReadingListTableViewItem alloc] initWithType:0];
56  item.title = base::SysUTF8ToNSString(entry->Title());
57  const GURL& URL = entry->URL();
58  item.entryURL = URL;
59  item.faviconPageURL =
60      entry->DistilledURL().is_valid() ? entry->DistilledURL() : URL;
61  item.distillationState =
62      reading_list::UIStatusFromModelStatus(entry->DistilledState());
63  BOOL hasDistillationDetails =
64      entry->DistilledState() == ReadingListEntry::PROCESSED &&
65      entry->DistillationSize() != 0 && entry->DistillationTime() != 0;
66  int64_t distillationDate =
67      hasDistillationDetails ? entry->DistillationTime() : 0;
68  item.distillationDateText =
69      GetReadingListCellDistillationDateText(distillationDate);
70  int64_t distillationSize =
71      hasDistillationDetails ? entry->DistillationSize() : 0;
72  item.distillationSizeText =
73      GetReadingListCellDistillationSizeText(distillationSize);
74  item.customActionFactory = self.customActionFactory;
75  return item;
76}
77
78@end
79