Fix collapsible state not changing when post edited

This commit is contained in:
Shadowfacts 2023-05-11 14:46:45 -04:00
parent 13ec3366d3
commit 7d3c82f4b7
2 changed files with 21 additions and 2 deletions

View File

@ -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

View File

@ -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
}