diff --git a/Packages/Pachyderm/Sources/Pachyderm/Utilities/CollapseState.swift b/Packages/Pachyderm/Sources/Pachyderm/Utilities/CollapseState.swift index d574900c..3596f484 100644 --- a/Packages/Pachyderm/Sources/Pachyderm/Utilities/CollapseState.swift +++ b/Packages/Pachyderm/Sources/Pachyderm/Utilities/CollapseState.swift @@ -12,6 +12,7 @@ import Foundation public final class CollapseState: Sendable { public var collapsible: Bool? public var collapsed: Bool? + public var statusPropertiesHash: Int? public var unknown: Bool { collapsible == nil || collapsed == nil diff --git a/Tusker/Extensions/StatusStateResolver.swift b/Tusker/Extensions/StatusStateResolver.swift index 4c5901bd..a91f5472 100644 --- a/Tusker/Extensions/StatusStateResolver.swift +++ b/Tusker/Extensions/StatusStateResolver.swift @@ -9,12 +9,24 @@ import Foundation import Pachyderm +protocol CollapseStateResolving { + var spoilerText: String { get } +} + +extension StatusMO: CollapseStateResolving {} +extension StatusEdit: CollapseStateResolving {} + extension CollapseState { - func resolveFor(status: StatusMO, height: CGFloat, textLength: Int? = nil) { + func resolveFor(status: CollapseStateResolving, height: () -> CGFloat, textLength: Int? = nil) -> Bool { + lazy var newHash = hashStatusProperties(status: status) + guard unknown || statusPropertiesHash != newHash else { + return false + } + let longEnoughToCollapse: Bool if Preferences.shared.collapseLongPosts, - height > 600 || (textLength != nil && textLength! > 500) { + height() > 600 || (textLength != nil && textLength! > 500) { longEnoughToCollapse = true } else { longEnoughToCollapse = false @@ -39,6 +51,12 @@ extension CollapseState { self.collapsible = contentWarningCollapsible || longEnoughToCollapse // use ?? instead of || because the content warnig pref takes priority over length self.collapsed = collapseDueToContentWarning ?? longEnoughToCollapse + self.statusPropertiesHash = newHash + return true } } + +private func hashStatusProperties(status: CollapseStateResolving) -> Int { + status.spoilerText.hashValue +}