From 2b23e848d041ee84da8b5c2e2ef33517594b0b8f Mon Sep 17 00:00:00 2001 From: Felix Schwarz Date: Thu, 23 Nov 2023 12:56:55 +0100 Subject: [PATCH] - UniversalItemListCell: add new Content.iconWidth property to control icon width in the universal cell, with fallback to defaults - OCShareRole+UniversalItemListCellContentProvider: adapt icon width to size of option icons in ShareViewController --- ...UniversalItemListCellContentProvider.swift | 1 + .../Cells/UniversalItemListCell.swift | 47 ++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/ownCloudAppShared/Client/Collection Views/Cells/UniversalItemListCell Content Providers/OCShareRole+UniversalItemListCellContentProvider.swift b/ownCloudAppShared/Client/Collection Views/Cells/UniversalItemListCell Content Providers/OCShareRole+UniversalItemListCellContentProvider.swift index 433879709..74dc4c16b 100644 --- a/ownCloudAppShared/Client/Collection Views/Cells/UniversalItemListCell Content Providers/OCShareRole+UniversalItemListCellContentProvider.swift +++ b/ownCloudAppShared/Client/Collection Views/Cells/UniversalItemListCell Content Providers/OCShareRole+UniversalItemListCellContentProvider.swift @@ -25,6 +25,7 @@ extension OCShareRole: UniversalItemListCellContentProvider { if let icon = OCSymbol.icon(forSymbolName: symbolName) { content.icon = .icon(image: icon) + content.iconWidth = UniversalItemListCell.defaultIconSize.width / 2 } content.title = .text(localizedName) diff --git a/ownCloudAppShared/Client/Collection Views/Cells/UniversalItemListCell.swift b/ownCloudAppShared/Client/Collection Views/Cells/UniversalItemListCell.swift index 1ae92129f..92b36e21e 100644 --- a/ownCloudAppShared/Client/Collection Views/Cells/UniversalItemListCell.swift +++ b/ownCloudAppShared/Client/Collection Views/Cells/UniversalItemListCell.swift @@ -49,6 +49,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { icon = content.icon iconDisabled = content.iconDisabled + iconWidth = content.iconWidth details = content.details @@ -102,6 +103,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { var title: Title? var icon: Icon? var iconDisabled: Bool = false + var iconWidth: CGFloat? var details: [SegmentViewItem]? @@ -127,7 +129,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { return view }() - private let iconSize : CGSize = CGSize(width: 40, height: 40) + static public let defaultIconSize : CGSize = CGSize(width: 40, height: 40) public let thumbnailSize : CGSize = CGSize(width: 60, height: 60) // when changing size, also update .iconView.fallbackSize open var iconView: ResourceViewHost = ResourceViewHost(fallbackSize: CGSize(width: 60, height: 60)) // when changing size, also update .thumbnailSize @@ -306,10 +308,12 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { hasSecondaryDetailView = false } + let iconWidthConstraint = updateIconWidth(content?.iconWidth, defaultWidth: (iconViewHeight / 0.75)) // 4:3 + constraints = [ iconView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: horizontalMargin), iconView.trailingAnchor.constraint(equalTo: titleLabel.leadingAnchor, constant: -spacing), - iconView.widthAnchor.constraint(equalToConstant: floor(iconViewHeight / 0.75)), // 4:3 + iconWidthConstraint, iconView.heightAnchor.constraint(equalToConstant: iconViewHeight), iconView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: verticalIconMargin), iconView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -verticalIconMargin), @@ -328,7 +332,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { let verticalLabelMargin : CGFloat = 10 let verticalIconMargin : CGFloat = 10 let spacing : CGFloat = 15 - let iconViewWidth : CGFloat = floor(iconSize.width / 2) + let iconViewWidth : CGFloat = floor(type(of: self).defaultIconSize.width / 2) let titleDetailSpacing: CGFloat = 15 titleLabel.numberOfLines = 1 @@ -341,10 +345,12 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { hasSecondaryDetailView = false } + let iconWidthConstraint = updateIconWidth(content?.iconWidth, defaultWidth: iconViewWidth) + constraints = [ iconView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: horizontalMargin), iconView.trailingAnchor.constraint(equalTo: titleLabel.leadingAnchor, constant: -spacing), - iconView.widthAnchor.constraint(equalToConstant: iconViewWidth), + iconWidthConstraint, iconView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: verticalIconMargin), iconView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -verticalIconMargin), @@ -362,7 +368,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { let verticalLabelMargin : CGFloat = 10 let verticalIconMargin : CGFloat = 10 let spacing : CGFloat = 15 - let iconViewWidth : CGFloat = iconSize.width + let iconViewWidth : CGFloat = type(of: self).defaultIconSize.width let verticalLabelMarginFromCenter : CGFloat = 1 titleLabel.numberOfLines = 1 @@ -377,10 +383,12 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { truncationMode = .truncateTail + let iconWidthConstraint = updateIconWidth(content?.iconWidth, defaultWidth: iconViewWidth) + constraints = [ iconView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: horizontalMargin), iconView.trailingAnchor.constraint(equalTo: titleLabel.leadingAnchor, constant: -spacing), - iconView.widthAnchor.constraint(equalToConstant: iconViewWidth), + iconWidthConstraint, iconView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: verticalIconMargin), iconView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -verticalIconMargin), @@ -406,6 +414,28 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { } } + private var iconWidthConstraint: NSLayoutConstraint? + private var lastIconWidth: CGFloat? + private var defaultIconWidthForCellLayout: CGFloat? // default width for current cell layout + private func updateIconWidth(_ newWidth: CGFloat?, defaultWidth: CGFloat? = nil) -> NSLayoutConstraint { + if let iconWidthConstraint { + iconWidthConstraint.isActive = false + } + + if let defaultWidth { + // Store default width for this cell type if one is provided + defaultIconWidthForCellLayout = defaultWidth + } + + // Fall back to default icon size if necessary + let effectiveWidth = newWidth ?? defaultIconWidthForCellLayout ?? type(of: self).defaultIconSize.width + + let widthConstraint = iconView.widthAnchor.constraint(equalToConstant: effectiveWidth) + iconWidthConstraint = widthConstraint + + return widthConstraint + } + // MARK: - Content var title: NSAttributedString? { didSet { @@ -502,6 +532,11 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell { } } + if content?.iconWidth != lastIconWidth { + updateIconWidth(content?.iconWidth).isActive = true + lastIconWidth = content?.iconWidth + } + iconView.request = iconRequest if let iconViewProvider { iconView.activeViewProvider = iconViewProvider