Clean up Share Extension theme override management

This commit is contained in:
Sasha Weiss
2025-12-03 09:43:44 -08:00
committed by GitHub
parent 2f3c6b19e8
commit 3327a6e2d8
3 changed files with 53 additions and 34 deletions

View File

@@ -31,11 +31,9 @@ public enum AppContextType: CaseIterable, CustomStringConvertible {
public protocol AppContext {
var type: AppContextType { get }
var isMainApp: Bool { get }
var isMainAppAndActive: Bool { get }
@MainActor
var isMainAppAndActiveIsolated: Bool { get }
var isNSE: Bool { get }
/// Whether the user is using a right-to-left language like Arabic.
var isRTL: Bool { get }
var isRunningTests: Bool { get }
@@ -99,6 +97,31 @@ public protocol AppContext {
var debugLogsDirPath: String { get }
}
extension AppContext {
public var isMainApp: Bool {
return switch type {
case .main: true
case .nse, .share: false
}
}
public var isNSE: Bool {
switch type {
case .nse: true
case .main, .share: false
}
}
public var isShareExtension: Bool {
switch type {
case .share: true
case .main, .nse: false
}
}
}
// MARK: -
public final class AppContextObjCBridge: NSObject {
@objc
@available(swift, obsoleted: 1)
@@ -116,6 +139,8 @@ public final class AppContextObjCBridge: NSObject {
}
}
// MARK: -
// These are fired whenever the corresponding "main app" or "app extension"
// notification is fired.
//
@@ -130,6 +155,8 @@ public extension Notification.Name {
static let OWSApplicationDidBecomeActive = Notification.Name("OWSApplicationDidBecomeActiveNotification")
}
// MARK: -
private var currentAppContext: (any AppContext)?
public func CurrentAppContext() -> any AppContext {
@@ -143,13 +170,3 @@ public func SetCurrentAppContext(_ appContext: any AppContext, isRunningTests: B
owsPrecondition(currentAppContext == nil || isRunningTests)
currentAppContext = appContext
}
extension AppContext {
public var isMainApp: Bool {
type == .main
}
public var isNSE: Bool {
type == .nse
}
}

View File

@@ -9,11 +9,6 @@ public extension Notification.Name {
static let themeDidChange = Notification.Name("ThemeDidChangeNotification")
}
@objc
public extension NSNotification {
static var ThemeDidChange: NSString { Notification.Name.themeDidChange.rawValue as NSString }
}
final public class Theme {
private static var shared = Theme(themeDataStore: ThemeDataStore())
@@ -108,12 +103,16 @@ final public class Theme {
private var cachedIsDarkThemeEnabled: Bool?
private var cachedCurrentMode: ThemeDataStore.Appearance?
public static var shareExtensionThemeOverride: UIUserInterfaceStyle = .unspecified {
public static var shareExtensionInterfaceStyleOverride: UIUserInterfaceStyle = .unspecified {
didSet {
guard !CurrentAppContext().isMainApp else {
return owsFailDebug("Should only be set in share extension")
owsPrecondition(
CurrentAppContext().isShareExtension,
"Must only be set in the share extension!"
)
if oldValue != shareExtensionInterfaceStyleOverride {
shared.themeDidChange()
}
shared.themeDidChange()
}
}
@@ -131,7 +130,7 @@ final public class Theme {
// Always respect the system theme in extensions.
guard CurrentAppContext().isMainApp else {
return switch Self.shareExtensionThemeOverride {
return switch Self.shareExtensionInterfaceStyleOverride {
case .dark:
true
case .light:

View File

@@ -207,13 +207,11 @@ open class ConversationPickerViewController: OWSTableViewController2 {
object: nil
)
DispatchQueue.main.async {
if
!CurrentAppContext().isMainApp,
self.traitCollection.userInterfaceStyle != UITraitCollection.current.userInterfaceStyle
{
Theme.shareExtensionThemeOverride = self.traitCollection.userInterfaceStyle
}
// Works around a mysterious issue in which UITraitCollection.current's
// userInterfaceStyle doesn't match that of this view controller, which
// results in wonky colors.
DispatchQueue.main.async { [self] in
configureThemeForShareExtension()
}
}
@@ -224,19 +222,24 @@ open class ConversationPickerViewController: OWSTableViewController2 {
presentationTime = presentationTime ?? Date()
}
// MARK: -
open override func themeDidChange() {
super.themeDidChange()
updateTableContents(shouldReload: false)
}
/// Manually observe `UITraitCollection` changes to manage `Theme`. This is
/// typically done by `OWSWindow`, but in the Share Extension we don't have
/// an `OWSWindow` to do this for us.
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
configureThemeForShareExtension()
}
let userInterfaceStyleDidChange = previousTraitCollection?.userInterfaceStyle != traitCollection.userInterfaceStyle
if !CurrentAppContext().isMainApp, userInterfaceStyleDidChange {
Theme.shareExtensionThemeOverride = traitCollection.userInterfaceStyle
}
private func configureThemeForShareExtension() {
guard CurrentAppContext().isShareExtension else { return }
Theme.shareExtensionInterfaceStyleOverride = traitCollection.userInterfaceStyle
}
// MARK: - ConversationCollection