aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt
blob: 733bc1510eacb382816a3e971220cd136f6c0e85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.pitchedapps.frost.injectors

import android.graphics.Color
import android.webkit.WebView
import ca.allanwang.kau.utils.*
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import java.io.FileNotFoundException
import java.util.*

/**
 * Created by Allan Wang on 2017-05-31.
 * Mapping of the available assets
 * The enum name must match the css file name
 */
enum class CssAssets(val folder: String = "themes") : InjectorContract {
    MATERIAL_LIGHT, MATERIAL_DARK, MATERIAL_AMOLED, MATERIAL_GLASS, CUSTOM, ROUND_ICONS("components")
    ;

    var file = "${name.toLowerCase(Locale.CANADA)}.compact.css"
    var injector: JsInjector? = null

    override fun inject(webView: WebView, callback: ((String) -> Unit)?) {
        if (injector == null) {
            try {
                var content = webView.context.assets.open("css/$folder/$file").bufferedReader().use { it.readText() }
                if (this == CUSTOM) {
                    var bbt = Prefs.bgColor
                    val bt: String
                    if (Color.alpha(bbt) == 255) {
                        bbt = bbt.adjustAlpha(0.2f).colorToForeground(0.35f)
                        bt = Prefs.bgColor.toRgbaString()
                    } else {
                        bbt = bbt.adjustAlpha(0.05f).colorToForeground(0.5f)
                        bt = "transparent"
                    }
                    content = content
                            .replace("\$T\$", Prefs.textColor.toRgbaString())
                            .replace("\$TT\$", Prefs.textColor.colorToBackground(0.05f).toRgbaString())
                            .replace("\$B\$", Prefs.bgColor.toRgbaString())
                            .replace("\$BT\$", bt)
                            .replace("\$BBT\$", bbt.toRgbaString())
                            .replace("\$O\$", Prefs.bgColor.withAlpha(255).toRgbaString())
                            .replace("\$OO\$", Prefs.bgColor.colorToForeground(0.35f).withAlpha(255).toRgbaString())
                            .replace("\$D\$", Prefs.textColor.adjustAlpha(0.3f).toRgbaString())
                }
                injector = JsBuilder().css(content).build()
            } catch (e: FileNotFoundException) {
                L.e(e, "CssAssets file not found")
                injector = JsInjector(JsActions.EMPTY.function)
            }
        }
        injector!!.inject(webView, callback)
    }

    fun reset() {
        injector = null
    }

}