aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt
blob: 1f9411e4a878502273d1f02a21b2c083ecaa7924 (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
package com.pitchedapps.frost.settings

import android.annotation.SuppressLint
import android.content.Intent
import android.media.RingtoneManager
import android.net.Uri
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.minuteToText
import ca.allanwang.kau.utils.string
import com.pitchedapps.frost.BuildConfig
import com.pitchedapps.frost.R
import com.pitchedapps.frost.activities.SettingsActivity
import com.pitchedapps.frost.dbflow.NotificationModel
import com.pitchedapps.frost.dbflow.loadFbCookiesAsync
import com.pitchedapps.frost.services.fetchNotifications
import com.pitchedapps.frost.services.scheduleNotifications
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.frostSnackbar
import com.pitchedapps.frost.utils.materialDialogThemed
import com.pitchedapps.frost.views.Keywords


/**
 * Created by Allan Wang on 2017-06-29.
 */
@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 = {
            materialDialogThemed {
                title(R.string.notification_frequency)
                items(texts)
                itemsCallbackSingleChoice(options.indexOf(item.pref)) { _, _, which, _ ->
                    item.pref = options[which]
                    scheduleNotifications(item.pref)
                    true
                }
            }
        }
        enabler = {
            val enabled = Prefs.notificationsGeneral || Prefs.notificationsInstantMessages
            if (!enabled)
                scheduleNotifications(-1)
            enabled
        }
        textGetter = { minuteToText(it) }
    }

    plainText(R.string.notification_keywords) {
        descRes = R.string.notification_keywords_desc
        onClick = {
            val keywordView = Keywords(this@getNotificationPrefs)
            materialDialogThemed {
                title(R.string.notification_keywords)
                customView(keywordView, false)
                dismissListener { keywordView.save() }
                positiveText(R.string.kau_done)
            }
        }
    }

    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, Uri.parse(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, Uri.parse(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 = {
                loadFbCookiesAsync { cookies ->
                    cookies.map { NotificationModel(it.id) }.forEach { it.save() }
                }
            }
        }
    }

    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)
        }
    }

}