From 6df5f7fb088539b9b48fb39ba52ac3eb8a1446dd Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 15 Sep 2020 21:37:08 -0400 Subject: [PATCH] Add preferences for auto-expanding CW'd posts and disabling long post collapsing See #105 --- Tusker.xcodeproj/project.pbxproj | 4 +++ Tusker/Extensions/StatusStateResolver.swift | 30 +++++++++++++++++++ Tusker/Preferences/Preferences.swift | 12 ++++++++ .../Preferences/BehaviorPrefsView.swift | 13 ++++++++ .../Status/BaseStatusTableViewCell.swift | 22 ++++---------- 5 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 Tusker/Extensions/StatusStateResolver.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index a4421c03..90b8874f 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -146,6 +146,7 @@ D6531DF0246B867E000F9538 /* GifvAttachmentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6531DEF246B867E000F9538 /* GifvAttachmentViewController.swift */; }; D6538945214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */; }; D65F613423AEAB6600F3CFD3 /* OnboardingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65F613323AEAB6600F3CFD3 /* OnboardingTests.swift */; }; + D6620ACE2511A0ED00312CA0 /* StatusStateResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6620ACD2511A0ED00312CA0 /* StatusStateResolver.swift */; }; D663625D2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */; }; D663625F2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */; }; D663626221360B1900C9CBA2 /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663626121360B1900C9CBA2 /* Preferences.swift */; }; @@ -474,6 +475,7 @@ D65F613323AEAB6600F3CFD3 /* OnboardingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingTests.swift; sourceTree = ""; }; D65F613523AFD65900F3CFD3 /* Ambassador.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Ambassador.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D65F613723AFD65D00F3CFD3 /* Embassy.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Embassy.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + D6620ACD2511A0ED00312CA0 /* StatusStateResolver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusStateResolver.swift; sourceTree = ""; }; D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ConversationMainStatusTableViewCell.xib; sourceTree = ""; }; D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationMainStatusTableViewCell.swift; sourceTree = ""; }; D663626121360B1900C9CBA2 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = ""; }; @@ -1109,6 +1111,7 @@ D690797224A4EF9700023A34 /* UIBezierPath+Helpers.swift */, D6D3F4C324FDB6B700EC4A6A /* View+ConditionalModifier.swift */, D6D4CC90250D2C3100FCCF8D /* UIAccessibility.swift */, + D6620ACD2511A0ED00312CA0 /* StatusStateResolver.swift */, ); path = Extensions; sourceTree = ""; @@ -1904,6 +1907,7 @@ D677284824ECBCB100C732D3 /* ComposeView.swift in Sources */, D68232F72464F4FD00325FB8 /* ComposeDrawingViewController.swift in Sources */, 04586B4122B2FFB10021BD04 /* PreferencesView.swift in Sources */, + D6620ACE2511A0ED00312CA0 /* StatusStateResolver.swift in Sources */, D6BC9DB5232D4CE3002CA326 /* NotificationsMode.swift in Sources */, D68015402401A6BA00D6103B /* ComposingPrefsView.swift in Sources */, D67895BC24671E6D00D4CD9E /* PKDrawing+Render.swift in Sources */, diff --git a/Tusker/Extensions/StatusStateResolver.swift b/Tusker/Extensions/StatusStateResolver.swift new file mode 100644 index 00000000..60079c51 --- /dev/null +++ b/Tusker/Extensions/StatusStateResolver.swift @@ -0,0 +1,30 @@ +// +// StatusStateResolver.swift +// Tusker +// +// Created by Shadowfacts on 9/15/20. +// Copyright © 2020 Shadowfacts. All rights reserved. +// + +import Foundation +import Pachyderm + +extension StatusState { + + func resolveFor(status: StatusMO, text: String?) { + let longEnoughToCollapse: Bool + if Preferences.shared.collapseLongPosts, + let text = text, + text.count > 500 { + longEnoughToCollapse = true + } else { + longEnoughToCollapse = false + } + + let contentWarningCollapsible = !status.spoilerText.isEmpty + + self.collapsible = contentWarningCollapsible || longEnoughToCollapse + self.collapsed = longEnoughToCollapse || (!Preferences.shared.expandAllContentWarnings && contentWarningCollapsible) + } + +} diff --git a/Tusker/Preferences/Preferences.swift b/Tusker/Preferences/Preferences.swift index 21cc0075..ffda4e3a 100644 --- a/Tusker/Preferences/Preferences.swift +++ b/Tusker/Preferences/Preferences.swift @@ -55,6 +55,12 @@ class Preferences: Codable, ObservableObject { self.openLinksInApps = try container.decode(Bool.self, forKey: .openLinksInApps) self.useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari) self.inAppSafariAutomaticReaderMode = try container.decode(Bool.self, forKey: .inAppSafariAutomaticReaderMode) + if container.contains(.expandAllContentWarnings) { + self.expandAllContentWarnings = try container.decode(Bool.self, forKey: .expandAllContentWarnings) + } + if container.contains(.collapseLongPosts) { + self.collapseLongPosts = try container.decode(Bool.self, forKey: .collapseLongPosts) + } self.showFavoriteAndReblogCounts = try container.decode(Bool.self, forKey: .showFavoriteAndReblogCounts) self.defaultNotificationsMode = try container.decode(NotificationsMode.self, forKey: .defaultNotificationsType) @@ -84,6 +90,8 @@ class Preferences: Codable, ObservableObject { try container.encode(openLinksInApps, forKey: .openLinksInApps) try container.encode(useInAppSafari, forKey: .useInAppSafari) try container.encode(inAppSafariAutomaticReaderMode, forKey: .inAppSafariAutomaticReaderMode) + try container.encode(expandAllContentWarnings, forKey: .expandAllContentWarnings) + try container.encode(collapseLongPosts, forKey: .collapseLongPosts) try container.encode(showFavoriteAndReblogCounts, forKey: .showFavoriteAndReblogCounts) try container.encode(defaultNotificationsMode, forKey: .defaultNotificationsType) @@ -114,6 +122,8 @@ class Preferences: Codable, ObservableObject { @Published var openLinksInApps = true @Published var useInAppSafari = true @Published var inAppSafariAutomaticReaderMode = false + @Published var expandAllContentWarnings = false + @Published var collapseLongPosts = true // MARK: Digital Wellness @Published var showFavoriteAndReblogCounts = true @@ -142,6 +152,8 @@ class Preferences: Codable, ObservableObject { case openLinksInApps case useInAppSafari case inAppSafariAutomaticReaderMode + case expandAllContentWarnings + case collapseLongPosts case showFavoriteAndReblogCounts case defaultNotificationsType diff --git a/Tusker/Screens/Preferences/BehaviorPrefsView.swift b/Tusker/Screens/Preferences/BehaviorPrefsView.swift index 5eb85331..b3e05f9b 100644 --- a/Tusker/Screens/Preferences/BehaviorPrefsView.swift +++ b/Tusker/Screens/Preferences/BehaviorPrefsView.swift @@ -14,6 +14,7 @@ struct BehaviorPrefsView: View { var body: some View { List { linksSection + contentWarningsSection } .insetOrGroupedListStyle() .navigationBarTitle(Text("Behavior")) @@ -32,6 +33,18 @@ struct BehaviorPrefsView: View { }.disabled(!preferences.useInAppSafari) } } + + var contentWarningsSection: some View { + Section(header: Text("Content Warnings")) { + Toggle(isOn: $preferences.expandAllContentWarnings) { + Text("Expand All Content Warnings") + } + + Toggle(isOn: $preferences.collapseLongPosts) { + Text("Collapse Long Posts") + } + } + } } #if DEBUG diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index eb03b1f4..01bbfbea 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -170,25 +170,13 @@ class BaseStatusTableViewCell: UITableViewCell { updateStatusIconsForPreferences(status) if state.unknown { - collapsible = !status.spoilerText.isEmpty - var shouldCollapse = collapsible - if !shouldCollapse, - let text = contentTextView.text, - text.count > 500 { - collapsible = true - shouldCollapse = true + state.resolveFor(status: status, text: contentTextView.text) + if state.collapsible! && showStatusAutomatically { + state.collapsed = false } - if collapsible && showStatusAutomatically { - shouldCollapse = false - } - setCollapsed(shouldCollapse, animated: false) - - state.collapsible = collapsible - state.collapsed = shouldCollapse - } else { - collapsible = state.collapsible! - setCollapsed(state.collapsed!, animated: false) } + collapsible = state.collapsible! + setCollapsed(state.collapsed!, animated: false) } func updateStatusState(status: StatusMO) {