/
HandsWashedReceiver.kt
84 lines (80 loc) · 3.35 KB
/
HandsWashedReceiver.kt
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
/*
* Copyright © 2020 - present | Handwashing reminder by Javinator9889
*
* 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 https://www.gnu.org/licenses/.
*
* Created by Javinator9889 on 2/07/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.jobs
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.core.app.NotificationManagerCompat
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.data.repositories.HandwashingRepository
import com.javinator9889.handwashingreminder.data.room.db.HandwashingDatabase
import com.javinator9889.handwashingreminder.data.room.entities.Handwashing
import com.javinator9889.handwashingreminder.emoji.EmojiLoader
import com.javinator9889.handwashingreminder.utils.calendar.CalendarUtils
import com.javinator9889.handwashingreminder.utils.goAsync
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
internal const val HANDS_WASHED_CODE = 128
internal const val HANDS_WASHED_SCHED_CODE = 129
internal const val HANDS_WASHED_ACTION =
"com.javinator9889.handwashingreminder.HANDSWASHED_EVENT"
internal const val NOTIFICATION_ID_KEY =
"com.javinator9889.handwashingreminder.NID"
class HandsWashedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val emojiLoader = EmojiLoader.loadAsync(context)
val repository =
with(HandwashingDatabase.getDatabase(context).handwashingDao()) {
HandwashingRepository(this)
}
intent.getIntExtra(NOTIFICATION_ID_KEY, -1).let {
if (it != -1)
with(NotificationManagerCompat.from(context)) {
cancel(it)
}
}
goAsync {
val createdItem = withContext(Dispatchers.IO) {
repository.get(CalendarUtils.today.time)
}
if (createdItem == null) {
withContext(Dispatchers.IO) {
repository.create(Handwashing(CalendarUtils.today.time, 0))
}
}
withContext(Dispatchers.IO) {
repository.increment(CalendarUtils.today.time)
}
val emojiCompat = emojiLoader.await()
val toastText = context.getText(R.string.hurray)
withContext(Dispatchers.Main) {
try {
Toast.makeText(
context,
emojiCompat.process(toastText),
Toast.LENGTH_LONG
).show()
} catch (_: IllegalStateException) {
Toast.makeText(context, toastText, Toast.LENGTH_LONG).show()
}
}
}
}
}