From 5bad313e77dca6d7526fa54d07e64b81f1cefd0c Mon Sep 17 00:00:00 2001 From: Miltiadis Vasilakis Date: Mon, 20 Jan 2025 09:13:34 +0200 Subject: [PATCH] Remove ItemDetailState.Data attributedTitle property --- .../Detail/ItemDetail/ItemDetailDataCreator.swift | 5 ----- .../Detail/ItemDetail/Models/ItemDetailState.swift | 2 -- .../ViewModels/ItemDetailActionHandler.swift | 9 +++------ .../Views/ItemDetailCollectionViewHandler.swift | 14 +++++++++++--- .../Views/ItemDetailViewController.swift | 1 + ZoteroTests/SyncActionsSpec.swift | 4 ---- 6 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Zotero/Scenes/Detail/ItemDetail/ItemDetailDataCreator.swift b/Zotero/Scenes/Detail/ItemDetail/ItemDetailDataCreator.swift index 92eb46967..45b5fb796 100644 --- a/Zotero/Scenes/Detail/ItemDetail/ItemDetailDataCreator.swift +++ b/Zotero/Scenes/Detail/ItemDetail/ItemDetailDataCreator.swift @@ -32,7 +32,6 @@ struct ItemDetailDataCreator { fileStorage: FileStorage, urlDetector: UrlDetector, htmlAttributedStringConverter: HtmlAttributedStringConverter, - titleFont: UIFont, doiDetector: (String) -> Bool ) throws -> (ItemDetailState.Data, [Attachment], [Note], [Tag]) { switch type { @@ -48,7 +47,6 @@ struct ItemDetailDataCreator { fileStorage: fileStorage, urlDetector: urlDetector, htmlAttributedStringConverter: htmlAttributedStringConverter, - titleFont: titleFont, doiDetector: doiDetector ) } @@ -73,7 +71,6 @@ struct ItemDetailDataCreator { let attachments: [Attachment] = child.flatMap({ [$0] }) ?? [] let data = ItemDetailState.Data( title: "", - attributedTitle: .init(string: ""), type: itemType, localizedType: localizedType, creators: [:], @@ -103,7 +100,6 @@ struct ItemDetailDataCreator { fileStorage: FileStorage, urlDetector: UrlDetector, htmlAttributedStringConverter: HtmlAttributedStringConverter, - titleFont: UIFont, doiDetector: (String) -> Bool ) throws -> (ItemDetailState.Data, [Attachment], [Note], [Tag]) { guard let localizedType = schemaController.localized(itemType: item.rawType) else { @@ -173,7 +169,6 @@ struct ItemDetailDataCreator { let tags = item.tags.sorted(byKeyPath: "tag.name").map(Tag.init) let data = ItemDetailState.Data( title: item.baseTitle, - attributedTitle: htmlAttributedStringConverter.convert(text: item.baseTitle, baseAttributes: [.font: titleFont]), type: item.rawType, localizedType: localizedType, creators: creators, diff --git a/Zotero/Scenes/Detail/ItemDetail/Models/ItemDetailState.swift b/Zotero/Scenes/Detail/ItemDetail/Models/ItemDetailState.swift index ba0b60d0b..04e8cd6f7 100644 --- a/Zotero/Scenes/Detail/ItemDetail/Models/ItemDetailState.swift +++ b/Zotero/Scenes/Detail/ItemDetail/Models/ItemDetailState.swift @@ -173,7 +173,6 @@ struct ItemDetailState: ViewModelState { struct Data: Equatable { var title: String - var attributedTitle: NSAttributedString var type: String var localizedType: String var creators: OrderedDictionary @@ -221,7 +220,6 @@ struct ItemDetailState: ViewModelState { let date = Date() return Data( title: "", - attributedTitle: .init(string: ""), type: "", localizedType: "", creators: [:], diff --git a/Zotero/Scenes/Detail/ItemDetail/ViewModels/ItemDetailActionHandler.swift b/Zotero/Scenes/Detail/ItemDetail/ViewModels/ItemDetailActionHandler.swift index 82c64b330..215a1b0e1 100644 --- a/Zotero/Scenes/Detail/ItemDetail/ViewModels/ItemDetailActionHandler.swift +++ b/Zotero/Scenes/Detail/ItemDetail/ViewModels/ItemDetailActionHandler.swift @@ -182,7 +182,6 @@ final class ItemDetailActionHandler: ViewModelActionHandler, BackgroundDbProcess fileStorage: self.fileStorage, urlDetector: self.urlDetector, htmlAttributedStringConverter: htmlAttributedStringConverter, - titleFont: viewModel.state.titleFont, doiDetector: FieldKeys.Item.isDoi ) @@ -196,7 +195,6 @@ final class ItemDetailActionHandler: ViewModelActionHandler, BackgroundDbProcess fileStorage: self.fileStorage, urlDetector: self.urlDetector, htmlAttributedStringConverter: htmlAttributedStringConverter, - titleFont: viewModel.state.titleFont, doiDetector: FieldKeys.Item.isDoi ) @@ -257,7 +255,6 @@ final class ItemDetailActionHandler: ViewModelActionHandler, BackgroundDbProcess fileStorage: self.fileStorage, urlDetector: self.urlDetector, htmlAttributedStringConverter: htmlAttributedStringConverter, - titleFont: viewModel.state.titleFont, doiDetector: FieldKeys.Item.isDoi ) @@ -887,11 +884,11 @@ final class ItemDetailActionHandler: ViewModelActionHandler, BackgroundDbProcess DDLogError("ItemDetailActionHandler: schema controller doesn't contain title key for item type \(viewModel.state.data.type)") return } - guard title != viewModel.state.data.attributedTitle else { return } + let htmlTitle = htmlAttributedStringConverter.convert(attributedString: title) + guard htmlTitle != viewModel.state.data.title else { return } update(viewModel: viewModel) { state in - state.data.attributedTitle = title - state.data.title = htmlAttributedStringConverter.convert(attributedString: title) + state.data.title = htmlTitle state.reload = .row(.title) } diff --git a/Zotero/Scenes/Detail/ItemDetail/Views/ItemDetailCollectionViewHandler.swift b/Zotero/Scenes/Detail/ItemDetail/Views/ItemDetailCollectionViewHandler.swift index 4bd621d23..77ddc2190 100644 --- a/Zotero/Scenes/Detail/ItemDetail/Views/ItemDetailCollectionViewHandler.swift +++ b/Zotero/Scenes/Detail/ItemDetail/Views/ItemDetailCollectionViewHandler.swift @@ -98,6 +98,7 @@ final class ItemDetailCollectionViewHandler: NSObject { private var maxNonemptyTitleWidth: CGFloat = 0 private var dataSource: UICollectionViewDiffableDataSource! private let updateQueue: DispatchQueue + private unowned let htmlAttributedStringConverter: HtmlAttributedStringConverter private weak var fileDownloader: AttachmentDownloader? weak var delegate: ItemDetailCollectionViewHandlerDelegate? @@ -111,9 +112,16 @@ final class ItemDetailCollectionViewHandler: NSObject { // MARK: - Lifecycle - init(collectionView: UICollectionView, containerWidth: CGFloat, viewModel: ViewModel, fileDownloader: AttachmentDownloader?) { + init( + collectionView: UICollectionView, + containerWidth: CGFloat, + viewModel: ViewModel, + htmlAttributedStringConverter: HtmlAttributedStringConverter, + fileDownloader: AttachmentDownloader? + ) { self.collectionView = collectionView self.viewModel = viewModel + self.htmlAttributedStringConverter = htmlAttributedStringConverter self.fileDownloader = fileDownloader observer = PublishSubject() disposeBag = DisposeBag() @@ -180,8 +188,8 @@ final class ItemDetailCollectionViewHandler: NSObject { switch row { case .title: - let title = viewModel.state.data.attributedTitle - return collectionView.dequeueConfiguredReusableCell(using: titleRegistration, for: indexPath, item: (title, isEditing)) + let attributedTitle = htmlAttributedStringConverter.convert(text: viewModel.state.data.title, baseAttributes: [.font: viewModel.state.titleFont]) + return collectionView.dequeueConfiguredReusableCell(using: titleRegistration, for: indexPath, item: (attributedTitle, isEditing)) case .creator(let creator): return collectionView.dequeueConfiguredReusableCell(using: fieldRegistration, for: indexPath, item: (.creator(creator), titleWidth)) diff --git a/Zotero/Scenes/Detail/ItemDetail/Views/ItemDetailViewController.swift b/Zotero/Scenes/Detail/ItemDetail/Views/ItemDetailViewController.swift index 91c13aad1..5d4517c45 100644 --- a/Zotero/Scenes/Detail/ItemDetail/Views/ItemDetailViewController.swift +++ b/Zotero/Scenes/Detail/ItemDetail/Views/ItemDetailViewController.swift @@ -35,6 +35,7 @@ final class ItemDetailViewController: UIViewController { collectionView: collectionView, containerWidth: width, viewModel: viewModel, + htmlAttributedStringConverter: controllers.htmlAttributedStringConverter, fileDownloader: controllers.userControllers?.fileDownloader ) collectionViewHandler.delegate = self diff --git a/ZoteroTests/SyncActionsSpec.swift b/ZoteroTests/SyncActionsSpec.swift index c5d1d1c8b..feee8b28a 100644 --- a/ZoteroTests/SyncActionsSpec.swift +++ b/ZoteroTests/SyncActionsSpec.swift @@ -96,7 +96,6 @@ final class SyncActionsSpec: QuickSpec { try! dbStorage.perform(request: EditCollectionDbRequest(libraryId: .group(1234123), key: "BBBBBBBB", name: "New name", parentKey: nil), on: .main) let data = ItemDetailState.Data( title: "New title", - attributedTitle: .init(string: "New title"), type: "magazineArticle", localizedType: "Magazine Article", creators: [:], @@ -107,7 +106,6 @@ final class SyncActionsSpec: QuickSpec { ) let snapshot = ItemDetailState.Data( title: "Bachelor thesis", - attributedTitle: .init(string: "Bachelor thesis"), type: "thesis", localizedType: "Thesis", creators: [:], @@ -355,7 +353,6 @@ final class SyncActionsSpec: QuickSpec { try! dbStorage.perform(request: EditCollectionDbRequest(libraryId: .group(1234123), key: "BBBBBBBB", name: "New name", parentKey: nil), on: .main) let data = ItemDetailState.Data( title: "New title", - attributedTitle: .init(string: "New title"), type: "magazineArticle", localizedType: "Magazine Article", creators: [:], @@ -366,7 +363,6 @@ final class SyncActionsSpec: QuickSpec { ) let snapshot = ItemDetailState.Data( title: "Bachelor thesis", - attributedTitle: .init(string: "Bachelor thesis"), type: "thesis", localizedType: "Thesis", creators: [:],