// // StatusStateResolver.swift // Tusker // // Created by Shadowfacts on 9/15/20. // Copyright © 2020 Shadowfacts. All rights reserved. // import Foundation import Pachyderm protocol CollapseStateResolving { var spoilerText: String { get } } extension StatusMO: CollapseStateResolving {} extension StatusEdit: CollapseStateResolving {} extension CollapseState { func resolveFor(status: CollapseStateResolving, height: () -> CGFloat, textLength: Int? = nil) -> Bool { let newHash = hashStatusProperties(status: status) guard unknown || statusPropertiesHash != newHash else { return false } let longEnoughToCollapse: Bool if Preferences.shared.collapseLongPosts, height() > 600 || (textLength != nil && textLength! > 500) { longEnoughToCollapse = true } else { longEnoughToCollapse = false } let contentWarningCollapsible = !status.spoilerText.isEmpty let collapseDueToContentWarning: Bool? if contentWarningCollapsible { let lowercased = status.spoilerText.lowercased() let opposite = Preferences.shared.oppositeCollapseKeywords.contains { lowercased.contains($0.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()) } if Preferences.shared.expandAllContentWarnings { collapseDueToContentWarning = opposite } else { collapseDueToContentWarning = !opposite } } else { collapseDueToContentWarning = nil } 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 }