PresentationTheme.swift 6.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2018 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Carola Nitz <caro # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

import Foundation

14 15 16
extension Notification.Name {
    static let VLCThemeDidChangeNotification = Notification.Name("themeDidChangeNotfication")
}
17

18
public class ColorPalette : NSObject {
19 20 21 22 23 24 25 26

    @objc public let isDark: Bool
    @objc public let name: String
    @objc public let background:UIColor
    @objc public let cellBackgroundA:UIColor
    @objc public let cellBackgroundB:UIColor
    @objc public let cellDetailTextColor:UIColor
    @objc public let cellTextColor:UIColor
27
    @objc public let lightTextColor:UIColor
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    @objc public let sectionHeaderTextColor:UIColor
    @objc public let sectionHeaderTintColor:UIColor
    @objc public let settingsBackground:UIColor
    @objc public let settingsCellBackground:UIColor
    @objc public let settingsSeparatorColor:UIColor
    @objc public let tabBarColor:UIColor
    @objc public let orangeUI:UIColor

    public init(isDark: Bool,
                name: String,
                background:UIColor,
                cellBackgroundA:UIColor,
                cellBackgroundB:UIColor,
                cellDetailTextColor:UIColor,
                cellTextColor:UIColor,
43
                lightTextColor:UIColor,
44 45 46 47 48 49 50 51 52 53 54 55 56 57
                sectionHeaderTextColor:UIColor,
                sectionHeaderTintColor:UIColor,
                settingsBackground:UIColor,
                settingsCellBackground:UIColor,
                settingsSeparatorColor:UIColor,
                tabBarColor:UIColor,
                orangeUI:UIColor) {
        self.isDark = isDark
        self.name = name
        self.background = background
        self.cellBackgroundA = cellBackgroundA
        self.cellBackgroundB = cellBackgroundB
        self.cellDetailTextColor = cellDetailTextColor
        self.cellTextColor = cellTextColor
58
        self.lightTextColor = lightTextColor
59 60 61 62 63 64 65 66 67 68 69 70 71 72
        self.sectionHeaderTextColor = sectionHeaderTextColor
        self.sectionHeaderTintColor = sectionHeaderTintColor
        self.settingsBackground = settingsBackground
        self.settingsCellBackground = settingsCellBackground
        self.settingsSeparatorColor = settingsSeparatorColor
        self.tabBarColor = tabBarColor
        self.orangeUI = orangeUI
    }
}

public class PresentationTheme : NSObject {

    @objc public static let whiteTheme = PresentationTheme(colors: whitePalette)
    @objc public static let darkTheme = PresentationTheme(colors: darkPalette)
73

74
    @objc static var current: PresentationTheme = {
75 76
        let isDarkTheme = UserDefaults.standard.bool(forKey: kVLCSettingAppTheme)
        return isDarkTheme ? PresentationTheme.darkTheme : PresentationTheme.whiteTheme
77 78 79
        }()
        {
        didSet {
80
            NotificationCenter.default.post(name: .VLCThemeDidChangeNotification, object: self)
81
            AppearanceManager.setupAppearance(theme: self.current)
82 83
        }
    }
84 85 86 87 88
    public init(colors: ColorPalette) {
        self.colors = colors
    }

    @objc public let colors: ColorPalette
89 90
}

91
@objc public extension UIColor {
92 93

    public convenience init(_ rgbValue:UInt32, _ alpha:CGFloat = 1.0) {
94 95 96 97 98 99
        let r = CGFloat((rgbValue & 0xFF0000) >> 16)/255.0
        let g = CGFloat((rgbValue & 0xFF00) >> 8)/255.0
        let b = CGFloat(rgbValue & 0xFF)/255.0
        self.init(red: r, green: g, blue: b, alpha: 1.0)
    }

100
    private func toHex(alpha: Bool = false) -> String? {
101
        guard let components = cgColor.components, components.count >= 3 else {
102
            assertionFailure()
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            return nil
        }
        let r = Float(components[0])
        let g = Float(components[1])
        let b = Float(components[2])
        var a = Float(1.0)

        if components.count == 4 {
            a = Float(components[3])
        }

        if alpha {
            return String(format: "#%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
        } else {
            return String(format: "#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
        }
    }

    var toHex: String? {
        return toHex()
123 124 125
    }
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
let whitePalette = ColorPalette(isDark: false,
                                name: "Default",
                                background: UIColor(0xf9f9f7),
                                cellBackgroundA: UIColor(0xf9f9f7),
                                cellBackgroundB: UIColor(0xe5e5e3),
                                cellDetailTextColor: .lightGray,
                                cellTextColor: UIColor(0x000000),
                                lightTextColor: UIColor(0x888888),
                                sectionHeaderTextColor: UIColor(0xf9f9f7),
                                sectionHeaderTintColor: UIColor(0xe5efe3),
                                settingsBackground:UIColor(0xdcdcdc),
                                settingsCellBackground:UIColor(0xf9f9f7),
                                settingsSeparatorColor:.lightGray,
                                tabBarColor: UIColor(0xffffff),
                                orangeUI: UIColor(0xff8800))

let darkPalette = ColorPalette(isDark: true,
                               name: "Dark",
                               background: UIColor(0x292b36),
                               cellBackgroundA: UIColor(0x292b36),
                               cellBackgroundB: UIColor(0x000000),
                               cellDetailTextColor: .lightGray,
                               cellTextColor:UIColor(0xffffff),
                               lightTextColor: UIColor(0xb8b8b8),
                               sectionHeaderTextColor: UIColor(0x828282),
                               sectionHeaderTintColor:UIColor(0x3c3c3c),
                               settingsBackground:UIColor(0x292b36),
                               settingsCellBackground:UIColor(0x3d3f40),
                               settingsSeparatorColor:.darkGray,
                               tabBarColor: UIColor(0xffffff),
                               orangeUI: UIColor(0xff8800))