2021-07-19 11:12:45 +02:00
|
|
|
//
|
2022-04-13 14:43:16 +02:00
|
|
|
// ThemeService.swift
|
2021-07-19 11:12:45 +02:00
|
|
|
// Mastodon
|
|
|
|
//
|
2022-09-30 13:28:09 +02:00
|
|
|
// Created by MainasuK Cirno on 2021-7-5.
|
2021-07-19 11:12:45 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-09-30 13:28:09 +02:00
|
|
|
import Combine
|
2022-04-13 14:43:16 +02:00
|
|
|
import MastodonCommon
|
2022-09-30 13:28:09 +02:00
|
|
|
|
|
|
|
// ref: https://zamzam.io/protocol-oriented-themes-for-ios-apps/
|
|
|
|
public final class ThemeService {
|
|
|
|
|
|
|
|
public static let tintColor: UIColor = .label
|
|
|
|
|
|
|
|
// MARK: - Singleton
|
|
|
|
public static let shared = ThemeService()
|
|
|
|
|
|
|
|
public let currentTheme: CurrentValueSubject<Theme, Never>
|
|
|
|
|
|
|
|
private init() {
|
2023-03-16 09:05:41 +01:00
|
|
|
let theme = ThemeName.system.theme
|
2022-09-30 13:28:09 +02:00
|
|
|
currentTheme = CurrentValueSubject(theme)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ThemeName {
|
|
|
|
public var theme: Theme {
|
|
|
|
switch self {
|
2023-03-16 09:05:41 +01:00
|
|
|
case .system:
|
|
|
|
return SystemTheme()
|
2022-09-30 13:28:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 11:12:45 +02:00
|
|
|
|
|
|
|
extension ThemeService {
|
2022-09-30 13:28:09 +02:00
|
|
|
public func set(themeName: ThemeName) {
|
2021-07-19 11:12:45 +02:00
|
|
|
UserDefaults.shared.currentThemeNameRawValue = themeName.rawValue
|
|
|
|
|
|
|
|
let theme = themeName.theme
|
|
|
|
apply(theme: theme)
|
|
|
|
currentTheme.value = theme
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public func apply(theme: Theme) {
|
2021-07-19 11:12:45 +02:00
|
|
|
// set navigation bar appearance
|
|
|
|
let appearance = UINavigationBarAppearance()
|
|
|
|
appearance.configureWithDefaultBackground()
|
|
|
|
appearance.backgroundColor = theme.navigationBarBackgroundColor
|
|
|
|
UINavigationBar.appearance().standardAppearance = appearance
|
|
|
|
UINavigationBar.appearance().compactAppearance = appearance
|
|
|
|
UINavigationBar.appearance().scrollEdgeAppearance = appearance
|
2022-12-17 20:26:20 +01:00
|
|
|
UINavigationBar.appearance().compactScrollEdgeAppearance = appearance
|
2021-07-19 11:12:45 +02:00
|
|
|
|
|
|
|
// set tab bar appearance
|
|
|
|
let tabBarAppearance = UITabBarAppearance()
|
|
|
|
tabBarAppearance.configureWithDefaultBackground()
|
|
|
|
|
|
|
|
let tabBarItemAppearance = UITabBarItemAppearance()
|
2021-07-20 14:24:53 +02:00
|
|
|
tabBarItemAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor.clear]
|
|
|
|
tabBarItemAppearance.focused.titleTextAttributes = [.foregroundColor: UIColor.clear]
|
|
|
|
tabBarItemAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]
|
|
|
|
tabBarItemAppearance.disabled.titleTextAttributes = [.foregroundColor: UIColor.clear]
|
2021-07-19 11:12:45 +02:00
|
|
|
tabBarItemAppearance.selected.iconColor = theme.tabBarItemSelectedIconColor
|
|
|
|
tabBarItemAppearance.focused.iconColor = theme.tabBarItemFocusedIconColor
|
|
|
|
tabBarItemAppearance.normal.iconColor = theme.tabBarItemNormalIconColor
|
|
|
|
tabBarItemAppearance.disabled.iconColor = theme.tabBarItemDisabledIconColor
|
|
|
|
tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
|
|
|
|
tabBarAppearance.inlineLayoutAppearance = tabBarItemAppearance
|
|
|
|
tabBarAppearance.compactInlineLayoutAppearance = tabBarItemAppearance
|
|
|
|
|
|
|
|
tabBarAppearance.backgroundColor = theme.tabBarBackgroundColor
|
|
|
|
UITabBar.appearance().standardAppearance = tabBarAppearance
|
2022-12-17 20:26:20 +01:00
|
|
|
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
|
2021-07-19 11:12:45 +02:00
|
|
|
UITabBar.appearance().barTintColor = theme.tabBarBackgroundColor
|
|
|
|
|
|
|
|
// set table view cell appearance
|
|
|
|
UITableViewCell.appearance().backgroundColor = theme.tableViewCellBackgroundColor
|
2022-09-30 13:28:09 +02:00
|
|
|
// FIXME: refactor
|
|
|
|
// UITableViewCell.appearance(whenContainedInInstancesOf: [SettingsViewController.self]).backgroundColor = theme.secondarySystemGroupedBackgroundColor
|
|
|
|
// UITableViewCell.appearance().selectionColor = theme.tableViewCellSelectionBackgroundColor
|
2021-07-19 11:12:45 +02:00
|
|
|
|
|
|
|
// set search bar appearance
|
2021-10-29 08:58:09 +02:00
|
|
|
UISearchBar.appearance().tintColor = ThemeService.tintColor
|
2021-07-19 11:12:45 +02:00
|
|
|
UISearchBar.appearance().barTintColor = theme.navigationBarBackgroundColor
|
2021-10-29 08:58:09 +02:00
|
|
|
let cancelButtonAttributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor: ThemeService.tintColor]
|
2021-07-19 11:12:45 +02:00
|
|
|
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)
|
|
|
|
}
|
|
|
|
}
|