1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-15 10:24:32 +01:00
mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonCore/Service/Theme/ThemeService.swift

93 lines
3.8 KiB
Swift
Raw Normal View History

2021-07-19 11:12:45 +02:00
//
// ThemeService.swift
2021-07-19 11:12:45 +02:00
// Mastodon
//
// Created by MainasuK Cirno on 2021-7-5.
2021-07-19 11:12:45 +02:00
//
import UIKit
import Combine
import MastodonCommon
// 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
currentTheme = CurrentValueSubject(theme)
}
}
extension ThemeName {
public var theme: Theme {
switch self {
2023-03-16 09:05:41 +01:00
case .system:
return SystemTheme()
}
}
}
2021-07-19 11:12:45 +02:00
extension ThemeService {
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
}
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
UINavigationBar.appearance().compactScrollEdgeAppearance = appearance
2021-07-19 11:12:45 +02:00
// set tab bar appearance
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.configureWithDefaultBackground()
let tabBarItemAppearance = UITabBarItemAppearance()
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
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
2021-07-19 11:12:45 +02:00
UITabBar.appearance().barTintColor = theme.tabBarBackgroundColor
// set table view cell appearance
UITableView.appearance().backgroundColor = theme.tableViewBackgroundColor
2021-07-19 11:12:45 +02:00
UITableViewCell.appearance().backgroundColor = theme.tableViewCellBackgroundColor
// 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)
}
}