use NSNotification to propagate reader setting changes

This commit is contained in:
Satindar Dhillon 2022-07-03 22:30:29 -07:00
parent fb361096fa
commit 6b5f8b7443
3 changed files with 45 additions and 13 deletions

View file

@ -15,12 +15,18 @@ import Views
public var fontSizeButtons: some View {
Group {
Button(
action: { storedFontSize = min(storedFontSize + 2, 28) },
action: {
storedFontSize = min(storedFontSize + 2, 28)
NSNotification.readerSettingsChanged()
},
label: { Text("Increase Reader Font Size") }
)
Button(
action: { storedFontSize = max(storedFontSize - 2, 10) },
action: {
storedFontSize = max(storedFontSize - 2, 10)
NSNotification.readerSettingsChanged()
},
label: { Text("Decrease Reader Font Size")
}
)
@ -30,13 +36,19 @@ import Views
public var marginSizeButtons: some View {
Group {
Button(
action: { storedMaxWidthPercentage = max(storedMaxWidthPercentage - 10, 40) },
action: {
storedMaxWidthPercentage = max(storedMaxWidthPercentage - 10, 40)
NSNotification.readerSettingsChanged()
},
label: { Text("Increase Reader Margin")
}
)
Button(
action: { storedMaxWidthPercentage = min(storedMaxWidthPercentage + 10, 100) },
action: {
storedMaxWidthPercentage = min(storedMaxWidthPercentage + 10, 100)
NSNotification.readerSettingsChanged()
},
label: { Text("Decrease Reader Margin")
}
)
@ -46,12 +58,18 @@ import Views
public var lineSpacingButtons: some View {
Group {
Button(
action: { storedLineSpacing = min(storedLineSpacing + 25, 300) },
action: {
storedLineSpacing = min(storedLineSpacing + 25, 300)
NSNotification.readerSettingsChanged()
},
label: { Text("Increase Reader Line Spacing") }
)
Button(
action: { storedLineSpacing = max(storedLineSpacing - 25, 100) },
action: {
storedLineSpacing = max(storedLineSpacing - 25, 100)
NSNotification.readerSettingsChanged()
},
label: { Text("Decrease Reader Line Spacing") }
)
// .keyboardShortcut("l")
@ -78,12 +96,20 @@ import Views
ForEach(WebFont.allCases, id: \.self) { font in
Text(font.displayValue).tag(font.rawValue)
}
// TODO: fix this since it doesn't work
.onChange(of: preferredFont) { _ in
NSNotification.readerSettingsChanged()
}
}
Toggle(
isOn: $prefersHighContrastText,
label: { Text("High Contrast Text") }
)
// TODO: fix this since it doesn't work
.onChange(of: prefersHighContrastText) { _ in
NSNotification.readerSettingsChanged()
}
}
}
}

View file

@ -233,6 +233,10 @@ struct WebReaderContainerView: View {
.formSheet(isPresented: $showPreferencesPopover, useSmallDetent: false) {
webPreferencesPopoverView
}
#else
.onReceive(NSNotification.readerSettingsChangedPublisher) { _ in
readerSettingsChangedTransactionID = UUID()
}
#endif
.onDisappear {
// Clear the shared webview content when exiting

View file

@ -1,10 +1,3 @@
//
// NSNotification+Operation.swift
//
//
// Created by Jackson Harper on 1/31/22.
//
import Foundation
import Models
@ -12,6 +5,7 @@ public extension NSNotification {
static let PushJSONArticle = Notification.Name("PushJSONArticle")
static let OperationSuccess = Notification.Name("OperationSuccess")
static let OperationFailure = Notification.Name("OperationFailure")
static let ReaderSettingsChanged = Notification.Name("ReaderSettingsChanged")
static var pushFeedItemPublisher: NotificationCenter.Publisher {
NotificationCenter.default.publisher(for: PushJSONArticle)
@ -25,6 +19,10 @@ public extension NSNotification {
NotificationCenter.default.publisher(for: OperationFailure)
}
static var readerSettingsChangedPublisher: NotificationCenter.Publisher {
NotificationCenter.default.publisher(for: ReaderSettingsChanged)
}
internal var operationMessage: String? {
if let message = userInfo?["message"] as? String {
return message
@ -47,4 +45,8 @@ public extension NSNotification {
static func operationFailed(message: String) {
NotificationCenter.default.post(name: NSNotification.OperationFailure, object: nil, userInfo: ["message": message])
}
static func readerSettingsChanged() {
NotificationCenter.default.post(name: NSNotification.ReaderSettingsChanged, object: nil)
}
}