aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt
blob: a0fd2e3de19fd878ba4037af77e411748accf85c (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.settings

import android.annotation.SuppressLint
import android.content.Intent
import android.media.RingtoneManager
import android.os.Build
import android.provider.Settings
import ca.allanwang.kau.kpref.activity.KPrefAdapterBuilder
import ca.allanwang.kau.kpref.activity.items.KPrefText
import ca.allanwang.kau.utils.materialDialog
import ca.allanwang.kau.utils.minuteToText
import ca.allanwang.kau.utils.string
import com.afollestad.materialdialogs.callbacks.onDismiss
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import com.pitchedapps.frost.BuildConfig
import com.pitchedapps.frost.R
import com.pitchedapps.frost.activities.SettingsActivity
import com.pitchedapps.frost.db.deleteAll
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.services.fetchNotifications
import com.pitchedapps.frost.utils.REQUEST_NOTIFICATION
import com.pitchedapps.frost.utils.frostSnackbar
import com.pitchedapps.frost.utils.frostUri
import com.pitchedapps.frost.views.Keywords
import kotlinx.coroutines.launch

/**
 * Created by Allan Wang on 2017-06-29.
 */

val Prefs.hasNotifications: Boolean
    get() = notificationsGeneral || notificationsInstantMessages

@SuppressLint("InlinedApi")
fun SettingsActivity.getNotificationPrefs(): KPrefAdapterBuilder.() -> Unit = {

    text(
        R.string.notification_frequency,
        prefs::notificationFreq,
        { prefs.notificationFreq = it }
    ) {
        val options = longArrayOf(15, 30, 60, 120, 180, 300, 1440, 2880)
        val texts =
            options.map { if (it <= 0) string(R.string.no_notifications) else minuteToText(it) }
        onClick = {
            materialDialog {
                title(R.string.notification_frequency)
                listItemsSingleChoice(
                    items = texts,
                    initialSelection = options.indexOf(item.pref)
                ) { _, index, _ ->
                    item.pref = options[index]
                    setFrostResult(REQUEST_NOTIFICATION)
                }
            }
        }
        enabler = { prefs.hasNotifications }
        textGetter = { minuteToText(it) }
    }

    plainText(R.string.notification_keywords) {
        descRes = R.string.notification_keywords_desc
        onClick = {
            val keywordView = Keywords(this@getNotificationPrefs)
            materialDialog {
                title(R.string.notification_keywords)
                customView(view = keywordView)
                positiveButton(R.string.kau_done)
                onDismiss { keywordView.save() }
            }
        }
    }

    checkbox(
        R.string.notification_general, prefs::notificationsGeneral,
        {
            prefs.notificationsGeneral = it
            reloadByTitle(R.string.notification_general_all_accounts)
            if (!prefs.notificationsInstantMessages)
                reloadByTitle(R.string.notification_frequency)
        }
    ) {
        descRes = R.string.notification_general_desc
    }

    checkbox(
        R.string.notification_general_all_accounts, prefs::notificationAllAccounts,
        { prefs.notificationAllAccounts = it }
    ) {
        descRes = R.string.notification_general_all_accounts_desc
        enabler = { prefs.notificationsGeneral }
    }

    checkbox(
        R.string.notification_messages, prefs::notificationsInstantMessages,
        {
            prefs.notificationsInstantMessages = it
            reloadByTitle(R.string.notification_messages_all_accounts)
            if (!prefs.notificationsGeneral)
                reloadByTitle(R.string.notification_frequency)
        }
    ) {
        descRes = R.string.notification_messages_desc
    }

    checkbox(
        R.string.notification_messages_all_accounts, prefs::notificationsImAllAccounts,
        { prefs.notificationsImAllAccounts = it }
    ) {
        descRes = R.string.notification_messages_all_accounts_desc
        enabler = { prefs.notificationsInstantMessages }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        plainText(R.string.notification_channel) {
            descRes = R.string.notification_channel_desc
            onClick = {
                val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
                    .putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
                startActivity(intent)
            }
        }
    } else {
        checkbox(
            R.string.notification_sound, prefs::notificationSound,
            {
                prefs.notificationSound = it
                reloadByTitle(
                    R.string.notification_ringtone,
                    R.string.message_ringtone
                )
            }
        )

        fun KPrefText.KPrefTextContract<String>.ringtone(code: Int) {
            enabler = prefs::notificationSound
            textGetter = {
                if (it.isBlank()) string(R.string.kau_default)
                else RingtoneManager.getRingtone(this@getNotificationPrefs, frostUri(it))
                    ?.getTitle(this@getNotificationPrefs)
                    ?: "---" // todo figure out why this happens
            }
            onClick = {
                val intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER).apply {
                    putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, string(R.string.select_ringtone))
                    putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false)
                    putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true)
                    putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION)
                    if (item.pref.isNotBlank()) {
                        putExtra(
                            RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
                            frostUri(item.pref)
                        )
                    }
                }
                startActivityForResult(intent, code)
            }
        }

        text(
            R.string.notification_ringtone, prefs::notificationRingtone,
            { prefs.notificationRingtone = it }
        ) {
            ringtone(SettingsActivity.REQUEST_NOTIFICATION_RINGTONE)
        }

        text(
            R.string.message_ringtone, prefs::messageRingtone,
            { prefs.messageRingtone = it }
        ) {
            ringtone(SettingsActivity.REQUEST_MESSAGE_RINGTONE)
        }

        checkbox(
            R.string.notification_vibrate, prefs::notificationVibrate,
            { prefs.notificationVibrate = it }
        )

        checkbox(
            R.string.notification_lights, prefs::notificationLights,
            { prefs.notificationLights = it }
        )
    }

    if (BuildConfig.DEBUG) {
        plainText(R.string.reset_notif_epoch) {
            onClick = {
                launch {
                    notifDao.deleteAll()
                }
            }
        }
    }

    plainText(R.string.notification_fetch_now) {
        descRes = R.string.notification_fetch_now_desc
        onClick = {
            val text =
                if (fetchNotifications()) R.string.notification_fetch_success
                else R.string.notification_fetch_fail
            frostSnackbar(text, themeProvider)
        }
    }
}