Skip to content

Instantly share code, notes, and snippets.

@arx8x
Created June 22, 2020 14:57
Show Gist options
  • Save arx8x/231a0e86e06c6fe9827bff66dc163e84 to your computer and use it in GitHub Desktop.
Save arx8x/231a0e86e06c6fe9827bff66dc163e84 to your computer and use it in GitHub Desktop.
Theme, ThemeManager, Themeable
//
// Theme.swift
// SHSH Host
//
// Created by ninja on 31/12/19.
// Copyright © 2019 arx8x.net. All rights reserved.
//
import UIKit
enum ThemeAppearanceType
{
case dark
case light
case neutral
}
class Theme
{
var backgroundColor : UIColor
let appearanceType : ThemeAppearanceType
var cellColorMain : UIColor
var cellColorAlt : UIColor
var cellTextColor : UIColor
var cellHighlightColor : UIColor
var cellHighlightColorAlt : UIColor
var textColor : UIColor
var detailTextColor : UIColor
var tabBarColor : UIColor?
let blurAppearance : UIBlurEffect.Style
var tintColor : UIColor
var statusBarStyle : UIStatusBarStyle
var statusBarColor : UIColor
var statusBarAccentColor : UIColor
var navBarColor : UIColor?
var navBarTextColor : UIColor
var navbarTitleTextAttributes : [NSAttributedString.Key : Any]
var tabBarGlyphClor : UIColor
var activityIndicatorStyle : UIActivityIndicatorView.Style
var activityIndicatorColor : UIColor
var keyboardAppearance : UIKeyboardAppearance
var barStyle : UIBarStyle
init(appearanceType type: ThemeAppearanceType, tintColor : UIColor = .systemTintBlue)
{
self.appearanceType = type
self.tintColor = tintColor
cellHighlightColor = self.tintColor
cellHighlightColorAlt = self.tintColor
tabBarGlyphClor = self.tintColor
if appearanceType == .dark
{
backgroundColor = .black
cellColorMain = .black
cellColorAlt = cellColorMain.ligterColor(byFacor: 0.1)
cellTextColor = .white
textColor = .white
detailTextColor = cellColorMain.darkerColor(byFacor: 0.35)
if #available(iOS 13.0, *)
{
blurAppearance = .systemMaterialDark
}
else
{
blurAppearance = .dark
}
statusBarColor = .black
statusBarStyle = .lightContent
statusBarAccentColor = .white
navBarTextColor = .white
navbarTitleTextAttributes = [.foregroundColor: UIColor.white]
activityIndicatorStyle = .white
activityIndicatorColor = .white
keyboardAppearance = .dark
barStyle = .black
}
else
{
backgroundColor = .white
cellColorMain = .white
cellColorAlt = cellColorMain.darkerColor(byFacor: 0.10)
cellTextColor = .notSoBlack
textColor = .notSoBlack
detailTextColor = .darkGray
if #available(iOS 13.0, *)
{
blurAppearance = .systemMaterialLight
}
else
{
blurAppearance = .light
}
statusBarColor = .white
statusBarStyle = .default
statusBarAccentColor = .black
navBarTextColor = .black
navbarTitleTextAttributes = [.foregroundColor: UIColor.black]
activityIndicatorStyle = .gray
activityIndicatorColor = .gray
keyboardAppearance = .light
barStyle = .default
}
}
static let amber : Theme =
{
() -> Theme in
let theme = Theme.init(appearanceType: .light)
theme.cellColorMain = UIColor(red:0.95, green:0.87, blue:0.74, alpha:1.0)
theme.cellColorAlt = UIColor(red:0.98, green:0.95, blue:0.83, alpha:1.0)
theme.cellTextColor = .brown
theme.navBarColor = theme.cellColorAlt
theme.tabBarColor = theme.navBarColor
theme.navbarTitleTextAttributes = [.foregroundColor: UIColor.brown]
theme.tintColor = .brown
theme.backgroundColor = theme.cellColorMain
theme.activityIndicatorColor = .brown
return theme
}()
}
//
// ThemeManager.swift
// SHSH Host
//
// Created by ninja on 31/12/19.
// Copyright © 2019 arx8x.net. All rights reserved.
//
import Foundation
class ThemeManager
{
static let shared = ThemeManager()
private(set) var theme = PreferenceManager.shared.themeForAppearanceType
func applyTheme(theme: Theme)
{
self.theme = theme
PreferenceManager.shared.saveCurrentAppearanceSelection()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "shsh.host.themeapplied"), object: nil, userInfo: ["theme":self.theme])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment