aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt
blob: 7cf90cc65c489bc95823e587941bb31596ce1d5d (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
package com.pitchedapps.frost

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import ca.allanwang.kau.email.sendEmail
import ca.allanwang.kau.kpref.KPrefActivity
import ca.allanwang.kau.kpref.KPrefAdapterBuilder
import ca.allanwang.kau.utils.*
import ca.allanwang.kau.views.RippleCanvas
import com.mikepenz.google_material_typeface_library.GoogleMaterial
import com.pitchedapps.frost.utils.*


/**
 * Created by Allan Wang on 2017-06-06.
 */
class SettingsActivity : KPrefActivity() {

    override fun onCreateKPrefs(savedInstanceState: android.os.Bundle?): KPrefAdapterBuilder.() -> Unit = {
        textColor = { Prefs.textColor }
        accentColor = { Prefs.textColor }
        header(R.string.settings)
        text(R.string.theme, { Prefs.theme }, { Prefs.theme = it }) {
            onClick = {
                _, _, item ->
                this@SettingsActivity.materialDialogThemed {
                    title(R.string.theme)
                    items(Theme.values()
                            .filter { it != Theme.CUSTOM || BuildConfig.DEBUG } //TODO actually add custom theme
                            .map { this@SettingsActivity.string(it.textRes) })
//                    itemsDisabledIndices(Theme.CUSTOM.ordinal)
                    itemsCallbackSingleChoice(item.pref, {
                        _, _, which, text ->
                        if (item.pref != which) {
                            item.pref = which
                            shouldRestartMain()
                            reload()
                            setFrostTheme(true)
                            themeExterior()
                            invalidateOptionsMenu()
                            frostAnswersCustom("Theme") { putCustomAttribute("Count", text.toString()) }
                        }
                        true
                    })
                }
                true
            }
            textGetter = { this@SettingsActivity.string(Theme(it).textRes) }
        }

        if (BuildConfig.DEBUG) {
            colorPicker(R.string.text_color, { Prefs.customTextColor }, { Prefs.customTextColor = it; reload() }) {
                enabler = { Prefs.isCustomTheme }
                onDisabledClick = { itemView, _, _ -> itemView.snackbar(R.string.requires_custom_theme); true }
                allowCustomAlpha = false
                allowCustom = true
            }

            colorPicker(R.string.background_color, { Prefs.customBackgroundColor },
                    { Prefs.customBackgroundColor = it; bgCanvas.ripple(it, duration = 500L) }) {
                enabler = { Prefs.isCustomTheme }
                onDisabledClick = { itemView, _, _ -> itemView.snackbar(R.string.requires_custom_theme); true }
                allowCustomAlpha = true
                allowCustom = true
            }

            colorPicker(R.string.header_color, { Prefs.customHeaderColor }, {
                Prefs.customHeaderColor = it
                val darkerColor = it.darken()
                this@SettingsActivity.navigationBarColor = darkerColor
                toolbarCanvas.ripple(darkerColor, RippleCanvas.MIDDLE, RippleCanvas.END, duration = 500L)
            }) {
                enabler = { Prefs.isCustomTheme }
                onDisabledClick = { itemView, _, _ -> itemView.snackbar(R.string.requires_custom_theme); true }
                allowCustomAlpha = true
                allowCustom = true
            }

            colorPicker(R.string.icon_color, { Prefs.customIconColor }, {
                Prefs.customIconColor = it
                invalidateOptionsMenu()
            }) {
                enabler = { Prefs.isCustomTheme }
                onDisabledClick = { itemView, _, _ -> itemView.snackbar(R.string.requires_custom_theme); true }
                allowCustom = true
            }
        }

        text(R.string.notifications, { Prefs.notificationFreq }, { Prefs.notificationFreq = it; reloadByTitle(R.string.notifications) }) {
            val options = longArrayOf(-1, 15, 30, 60, 120, 180, 300, 1440, 2880)
            val texts = options.map { this@SettingsActivity.minuteToText(it) }
            onClick = {
                _, _, item ->
                this@SettingsActivity.materialDialogThemed {
                    title(R.string.notifications)
                    items(texts)
                    itemsCallbackSingleChoice(options.indexOf(item.pref), {
                        _, _, which, text ->
                        item.pref = options[which]
                        this@SettingsActivity.scheduleNotifications(item.pref)
                        true
                    })
                }
                true
            }
            textGetter = { this@SettingsActivity.minuteToText(it) }
        }

    }

    fun shouldRestartMain() {
        setResult(MainActivity.REQUEST_RESTART)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        setFrostTheme(true)
        super.onCreate(savedInstanceState)
        themeExterior(false)
    }

    fun themeExterior(animate: Boolean = true) {
        if (animate) bgCanvas.fade(Prefs.bgColor)
        else bgCanvas.set(Prefs.bgColor)
        val darkAccent = Prefs.headerColor.darken()
        if (animate) toolbarCanvas.ripple(darkAccent, RippleCanvas.MIDDLE, RippleCanvas.END)
        else toolbarCanvas.set(darkAccent)
        this.navigationBarColor = darkAccent
    }

    override fun onBackPressed() {
        finishSlideOut()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_settings, menu)
        toolbar.tint(Prefs.iconColor)
        setMenuIcons(menu, Prefs.iconColor,
                R.id.action_email to GoogleMaterial.Icon.gmd_email,
                R.id.action_changelog to GoogleMaterial.Icon.gmd_info)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_email -> sendEmail(R.string.dev_email, R.string.frost_feedback) {
                addItem("Random Frost ID", "${Prefs.installDate}-${Prefs.identifier}")
            }
            R.id.action_changelog -> showChangelog(R.xml.changelog, { theme() })
            else -> return super.onOptionsItemSelected(item)
        }
        return true
    }
}