-
Notifications
You must be signed in to change notification settings - Fork 1
/
ActivityMultiSelectList.kt
84 lines (77 loc) · 2.92 KB
/
ActivityMultiSelectList.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 16/04/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.activities.views.fragments.settings
import android.content.Context
import android.util.AttributeSet
import androidx.preference.MultiSelectListPreference
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.application.HandwashingApplication
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.ionicons.Ionicons
import com.mikepenz.iconics.utils.sizeDp
import java.util.*
class ActivityMultiSelectList : MultiSelectListPreference {
private var isFirstCall = true
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
super(context, attrs, defStyleAttr)
constructor(
context: Context, attrs: AttributeSet?, defStyleAttr: Int,
defStyleRes: Int
) : super(context, attrs, defStyleAttr, defStyleRes)
init {
icon = IconicsDrawable(context, Ionicons.Icon.ion_android_alert).apply {
sizeDp = 20
}
}
override fun notifyChanged() {
super.notifyChanged()
loadSummary()
if (!isFirstCall)
reloadActivityHandler()
else
isFirstCall = false
}
private fun loadSummary() {
val builder = StringBuilder()
var isFirstItem = true
var allDisabled = true
selectedItems.forEachIndexed { i, enabled ->
if (enabled) {
allDisabled = false
if (!isFirstItem)
builder.append(", ")
else
isFirstItem = false
builder.append(entries[i].toString().toLowerCase(Locale.ROOT))
}
}
builder.append('.')
summary = if (allDisabled)
context.getText(R.string.activities_disabled)
else
"${context.getString(R.string.activities_info)} $builder"
}
private fun reloadActivityHandler() {
with(HandwashingApplication.getInstance()) {
activityHandler.reload()
}
}
}