Skip to content

Commit

Permalink
Remove ItemDetailState.Data attributedTitle property
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak committed Jan 20, 2025
1 parent d0bfd3c commit 5bad313
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
5 changes: 0 additions & 5 deletions Zotero/Scenes/Detail/ItemDetail/ItemDetailDataCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -48,7 +47,6 @@ struct ItemDetailDataCreator {
fileStorage: fileStorage,
urlDetector: urlDetector,
htmlAttributedStringConverter: htmlAttributedStringConverter,
titleFont: titleFont,
doiDetector: doiDetector
)
}
Expand All @@ -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: [:],
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions Zotero/Scenes/Detail/ItemDetail/Models/ItemDetailState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Creator>
Expand Down Expand Up @@ -221,7 +220,6 @@ struct ItemDetailState: ViewModelState {
let date = Date()
return Data(
title: "",
attributedTitle: .init(string: ""),
type: "",
localizedType: "",
creators: [:],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
)

Expand Down Expand Up @@ -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
)

Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ final class ItemDetailCollectionViewHandler: NSObject {
private var maxNonemptyTitleWidth: CGFloat = 0
private var dataSource: UICollectionViewDiffableDataSource<SectionType, Row>!
private let updateQueue: DispatchQueue
private unowned let htmlAttributedStringConverter: HtmlAttributedStringConverter
private weak var fileDownloader: AttachmentDownloader?
weak var delegate: ItemDetailCollectionViewHandlerDelegate?

Expand All @@ -111,9 +112,16 @@ final class ItemDetailCollectionViewHandler: NSObject {

// MARK: - Lifecycle

init(collectionView: UICollectionView, containerWidth: CGFloat, viewModel: ViewModel<ItemDetailActionHandler>, fileDownloader: AttachmentDownloader?) {
init(
collectionView: UICollectionView,
containerWidth: CGFloat,
viewModel: ViewModel<ItemDetailActionHandler>,
htmlAttributedStringConverter: HtmlAttributedStringConverter,
fileDownloader: AttachmentDownloader?
) {
self.collectionView = collectionView
self.viewModel = viewModel
self.htmlAttributedStringConverter = htmlAttributedStringConverter
self.fileDownloader = fileDownloader
observer = PublishSubject()
disposeBag = DisposeBag()
Expand Down Expand Up @@ -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])

This comment has been minimized.

Copy link
@michalrentka

michalrentka Jan 24, 2025

Contributor

This conversion used to be slow at some point, that's why it was cached in Data. Is this alright now? Won't it lag on older devices for example?

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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class ItemDetailViewController: UIViewController {
collectionView: collectionView,
containerWidth: width,
viewModel: viewModel,
htmlAttributedStringConverter: controllers.htmlAttributedStringConverter,
fileDownloader: controllers.userControllers?.fileDownloader
)
collectionViewHandler.delegate = self
Expand Down
4 changes: 0 additions & 4 deletions ZoteroTests/SyncActionsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [:],
Expand All @@ -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: [:],
Expand Down Expand Up @@ -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: [:],
Expand All @@ -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: [:],
Expand Down

0 comments on commit 5bad313

Please sign in to comment.