Fix theme not being applied live on status row close #961

This commit is contained in:
Thomas Ricouard 2023-02-20 13:00:50 +01:00
parent 2508f98ce1
commit e7fffa07d4
7 changed files with 43 additions and 42 deletions

View file

@ -240,13 +240,13 @@ struct IceCubesApp: App {
CommandGroup(replacing: .textFormatting) { CommandGroup(replacing: .textFormatting) {
Menu("menu.font") { Menu("menu.font") {
Button("menu.font.bigger") { Button("menu.font.bigger") {
if userPreferences.fontSizeScale < 1.5 { if theme.fontSizeScale < 1.5 {
userPreferences.fontSizeScale += 0.1 theme.fontSizeScale += 0.1
} }
} }
Button("menu.font.smaller") { Button("menu.font.smaller") {
if userPreferences.fontSizeScale > 0.5 { if theme.fontSizeScale > 0.5 {
userPreferences.fontSizeScale -= 0.1 theme.fontSizeScale -= 0.1
} }
} }
} }

View file

@ -44,20 +44,20 @@ struct DisplaySettingsView: View {
Section("settings.display.section.font") { Section("settings.display.section.font") {
Picker("settings.display.font", selection: .init(get: { () -> FontState in Picker("settings.display.font", selection: .init(get: { () -> FontState in
if userPreferences.chosenFont?.fontName == "OpenDyslexic-Regular" { if theme.chosenFont?.fontName == "OpenDyslexic-Regular" {
return FontState.openDyslexic return FontState.openDyslexic
} else if userPreferences.chosenFont?.fontName == "AtkinsonHyperlegible-Regular" { } else if theme.chosenFont?.fontName == "AtkinsonHyperlegible-Regular" {
return FontState.hyperLegible return FontState.hyperLegible
} }
return userPreferences.chosenFontData != nil ? FontState.custom : FontState.system return theme.chosenFontData != nil ? FontState.custom : FontState.system
}, set: { newValue in }, set: { newValue in
switch newValue { switch newValue {
case .system: case .system:
userPreferences.chosenFont = nil theme.chosenFont = nil
case .openDyslexic: case .openDyslexic:
userPreferences.chosenFont = UIFont(name: "OpenDyslexic", size: 1) theme.chosenFont = UIFont(name: "OpenDyslexic", size: 1)
case .hyperLegible: case .hyperLegible:
userPreferences.chosenFont = UIFont(name: "Atkinson Hyperlegible", size: 1) theme.chosenFont = UIFont(name: "Atkinson Hyperlegible", size: 1)
case .custom: case .custom:
isFontSelectorPresented = true isFontSelectorPresented = true
} }
@ -68,12 +68,12 @@ struct DisplaySettingsView: View {
} }
.navigationDestination(isPresented: $isFontSelectorPresented, destination: { FontPicker() }) .navigationDestination(isPresented: $isFontSelectorPresented, destination: { FontPicker() })
Toggle("settings.display.font.rounded", isOn: $userPreferences.useSFRoundedFont) Toggle("settings.display.font.rounded", isOn: $theme.useSFRoundedFont)
.disabled(userPreferences.chosenFont != nil) .disabled(theme.chosenFont != nil)
VStack { VStack {
Slider(value: $userPreferences.fontSizeScale, in: 0.5 ... 1.5, step: 0.1) Slider(value: $theme.fontSizeScale, in: 0.5 ... 1.5, step: 0.1)
Text("settings.display.font.scaling-\(String(format: "%.1f", userPreferences.fontSizeScale))") Text("settings.display.font.scaling-\(String(format: "%.1f", theme.fontSizeScale))")
.font(.scaledBody) .font(.scaledBody)
} }
.alignmentGuide(.listRowSeparatorLeading) { d in .alignmentGuide(.listRowSeparatorLeading) { d in

View file

@ -15,15 +15,15 @@ public extension Font {
private static let onMac = ProcessInfo.processInfo.isiOSAppOnMac private static let onMac = ProcessInfo.processInfo.isiOSAppOnMac
private static func customFont(size: CGFloat, relativeTo textStyle: TextStyle) -> Font { private static func customFont(size: CGFloat, relativeTo textStyle: TextStyle) -> Font {
if let chosenFont = UserPreferences.shared.chosenFont { if let chosenFont = Theme.shared.chosenFont {
return .custom(chosenFont.fontName, size: size, relativeTo: textStyle) return .custom(chosenFont.fontName, size: size, relativeTo: textStyle)
} }
return .system(size: size, design: UserPreferences.shared.useSFRoundedFont ? .rounded : .default) return .system(size: size, design: Theme.shared.useSFRoundedFont ? .rounded : .default)
} }
private static func customUIFont(size: CGFloat) -> UIFont { private static func customUIFont(size: CGFloat) -> UIFont {
if let chosenFont = UserPreferences.shared.chosenFont { if let chosenFont = Theme.shared.chosenFont {
return chosenFont.withSize(size) return chosenFont.withSize(size)
} }
@ -31,7 +31,7 @@ public extension Font {
} }
private static func userScaledFontSize(baseSize: CGFloat) -> CGFloat { private static func userScaledFontSize(baseSize: CGFloat) -> CGFloat {
UIFontMetrics.default.scaledValue(for: baseSize * UserPreferences.shared.fontSizeScale) UIFontMetrics.default.scaledValue(for: baseSize * Theme.shared.fontSizeScale)
} }
static var scaledTitle: Font { static var scaledTitle: Font {

View file

@ -16,7 +16,7 @@ public struct FontPicker: UIViewControllerRepresentable {
} }
public func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) { public func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) {
UserPreferences.shared.chosenFont = UIFont(descriptor: viewController.selectedFontDescriptor!, size: 0) Theme.shared.chosenFont = UIFont(descriptor: viewController.selectedFontDescriptor!, size: 0)
dismiss() dismiss()
} }
} }

View file

@ -84,6 +84,24 @@ public class Theme: ObservableObject {
} }
} }
} }
public var chosenFont: UIFont? {
get {
guard let chosenFontData,
let font = try? NSKeyedUnarchiver.unarchivedObject(ofClass: UIFont.self, from: chosenFontData) else { return nil }
return font
}
set {
if let font = newValue,
let data = try? NSKeyedArchiver.archivedData(withRootObject: font, requiringSecureCoding: false)
{
chosenFontData = data
} else {
chosenFontData = nil
}
}
}
@AppStorage("is_previously_set") public var isThemePreviouslySet: Bool = false @AppStorage("is_previously_set") public var isThemePreviouslySet: Bool = false
@AppStorage(ThemeKey.selectedScheme.rawValue) public var selectedScheme: ColorScheme = .dark @AppStorage(ThemeKey.selectedScheme.rawValue) public var selectedScheme: ColorScheme = .dark
@ -98,6 +116,9 @@ public class Theme: ObservableObject {
@AppStorage(ThemeKey.statusDisplayStyle.rawValue) public var statusDisplayStyle: StatusDisplayStyle = .large @AppStorage(ThemeKey.statusDisplayStyle.rawValue) public var statusDisplayStyle: StatusDisplayStyle = .large
@AppStorage(ThemeKey.followSystemColorSchme.rawValue) public var followSystemColorScheme: Bool = true @AppStorage(ThemeKey.followSystemColorSchme.rawValue) public var followSystemColorScheme: Bool = true
@AppStorage(ThemeKey.displayFullUsernameTimeline.rawValue) public var displayFullUsername: Bool = true @AppStorage(ThemeKey.displayFullUsernameTimeline.rawValue) public var displayFullUsername: Bool = true
@AppStorage("font_size_scale") public var fontSizeScale: Double = 1
@AppStorage("chosen_font") public private(set) var chosenFontData: Data?
@AppStorage("font_use_sf_rounded") public var useSFRoundedFont = false
@Published public var avatarPosition: AvatarPosition = .top @Published public var avatarPosition: AvatarPosition = .top
@Published public var avatarShape: AvatarShape = .rounded @Published public var avatarShape: AvatarShape = .rounded

View file

@ -13,7 +13,6 @@ public class UserPreferences: ObservableObject {
@AppStorage("remote_local_timeline") public var remoteLocalTimelines: [String] = [] @AppStorage("remote_local_timeline") public var remoteLocalTimelines: [String] = []
@AppStorage("preferred_browser") public var preferredBrowser: PreferredBrowser = .inAppSafari @AppStorage("preferred_browser") public var preferredBrowser: PreferredBrowser = .inAppSafari
@AppStorage("draft_posts") public var draftsPosts: [String] = [] @AppStorage("draft_posts") public var draftsPosts: [String] = []
@AppStorage("font_size_scale") public var fontSizeScale: Double = 1
@AppStorage("show_translate_button_inline") public var showTranslateButton: Bool = true @AppStorage("show_translate_button_inline") public var showTranslateButton: Bool = true
@AppStorage("is_open_ai_enabled") public var isOpenAIEnabled: Bool = true @AppStorage("is_open_ai_enabled") public var isOpenAIEnabled: Bool = true
@ -26,7 +25,6 @@ public class UserPreferences: ObservableObject {
@AppStorage("app_default_post_visibility") public var appDefaultPostVisibility: Models.Visibility = .pub @AppStorage("app_default_post_visibility") public var appDefaultPostVisibility: Models.Visibility = .pub
@AppStorage("app_default_posts_sensitive") public var appDefaultPostsSensitive = false @AppStorage("app_default_posts_sensitive") public var appDefaultPostsSensitive = false
@AppStorage("autoplay_video") public var autoPlayVideo = true @AppStorage("autoplay_video") public var autoPlayVideo = true
@AppStorage("chosen_font") public private(set) var chosenFontData: Data?
@AppStorage("suppress_dupe_reblogs") public var suppressDupeReblogs: Bool = false @AppStorage("suppress_dupe_reblogs") public var suppressDupeReblogs: Bool = false
@ -48,8 +46,6 @@ public class UserPreferences: ObservableObject {
@AppStorage("swipeactions-use-theme-color") public var swipeActionsUseThemeColor = false @AppStorage("swipeactions-use-theme-color") public var swipeActionsUseThemeColor = false
@AppStorage("swipeactions-icon-style") public var swipeActionsIconStyle: SwipeActionsIconStyle = .iconWithText @AppStorage("swipeactions-icon-style") public var swipeActionsIconStyle: SwipeActionsIconStyle = .iconWithText
@AppStorage("font_use_sf_rounded") public var useSFRoundedFont = false
@AppStorage("requested_review") public var requestedReview = false @AppStorage("requested_review") public var requestedReview = false
public enum SwipeActionsIconStyle: String, CaseIterable { public enum SwipeActionsIconStyle: String, CaseIterable {
@ -112,25 +108,7 @@ public class UserPreferences: ObservableObject {
} }
return count return count
} }
public var chosenFont: UIFont? {
get {
guard let chosenFontData,
let font = try? NSKeyedUnarchiver.unarchivedObject(ofClass: UIFont.self, from: chosenFontData) else { return nil }
return font
}
set {
if let font = newValue,
let data = try? NSKeyedArchiver.archivedData(withRootObject: font, requiringSecureCoding: false)
{
chosenFontData = data
} else {
chosenFontData = nil
}
}
}
@Published public var serverPreferences: ServerPreferences? @Published public var serverPreferences: ServerPreferences?
private init() {} private init() {}

View file

@ -3,6 +3,8 @@ import Models
import SwiftUI import SwiftUI
struct StatusRowTextView: View { struct StatusRowTextView: View {
@EnvironmentObject private var theme: Theme
let status: AnyStatus let status: AnyStatus
let viewModel: StatusRowViewModel let viewModel: StatusRowViewModel