Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Extension/MastodonUI/ThemeService.swift

72 lines
3.4 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 on 2022-4-13.
2021-07-19 11:12:45 +02:00
//
import UIKit
import MastodonCommon
import MastodonUI
2021-07-19 11:12:45 +02:00
extension ThemeService {
func set(themeName: ThemeName) {
UserDefaults.shared.currentThemeNameRawValue = themeName.rawValue
let theme = themeName.theme
apply(theme: theme)
currentTheme.value = theme
}
func apply(theme: Theme) {
// 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
if #available(iOS 15.0, *) {
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
2021-10-29 08:58:09 +02:00
tabBarAppearance.selectionIndicatorTintColor = ThemeService.tintColor
2021-07-19 11:12:45 +02:00
UITabBar.appearance().standardAppearance = tabBarAppearance
if #available(iOS 15.0, *) {
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
} else {
// Fallback on earlier versions
}
2021-07-19 11:12:45 +02:00
UITabBar.appearance().barTintColor = theme.tabBarBackgroundColor
// set table view cell appearance
UITableViewCell.appearance().backgroundColor = theme.tableViewCellBackgroundColor
UITableViewCell.appearance(whenContainedInInstancesOf: [SettingsViewController.self]).backgroundColor = theme.secondarySystemGroupedBackgroundColor
UITableViewCell.appearance().selectionColor = theme.tableViewCellSelectionBackgroundColor
// 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)
}
}