aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt
blob: 61be271f8017c2b424916f77c83df0ee98ddef43 (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
/*
 * 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.views

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import ca.allanwang.kau.utils.colorToForeground
import ca.allanwang.kau.utils.dpToPx
import ca.allanwang.kau.utils.gone
import ca.allanwang.kau.utils.toDrawable
import ca.allanwang.kau.utils.visible
import ca.allanwang.kau.utils.withAlpha
import com.mikepenz.iconics.typeface.IIcon
import com.pitchedapps.frost.databinding.ViewBadgedIconBinding
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.prefs.Prefs
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
 * Created by Allan Wang on 2017-06-19.
 */
@AndroidEntryPoint
class BadgedIcon @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

    @Inject
    lateinit var prefs: Prefs

    @Inject
    lateinit var themeProvider: ThemeProvider

    private val binding: ViewBadgedIconBinding =
        ViewBadgedIconBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        binding.init()
    }

    private fun ViewBadgedIconBinding.init() {
        val badgeColor =
            prefs.mainActivityLayout.backgroundColor(themeProvider).withAlpha(255)
                .colorToForeground(0.2f)
        val badgeBackground =
            GradientDrawable(
                GradientDrawable.Orientation.BOTTOM_TOP,
                intArrayOf(badgeColor, badgeColor)
            )
        badgeBackground.cornerRadius = 13.dpToPx.toFloat()
        badgeText.background = badgeBackground
        badgeText.setTextColor(prefs.mainActivityLayout.iconColor(themeProvider))
    }

    var iicon: IIcon? = null
        set(value) {
            field = value
            binding.badgeImage.setImageDrawable(
                value?.toDrawable(
                    context,
                    sizeDp = 20,
                    color = prefs.mainActivityLayout.iconColor(themeProvider)
                )
            )
        }

    fun setAllAlpha(alpha: Float) {
        // badgeTextView.setTextColor(themeProvider.textColor.withAlpha(alpha.toInt()))
        binding.badgeImage.drawable.alpha = alpha.toInt()
    }

    var badgeText: String?
        get() = binding.badgeText.text.toString()
        set(value) {
            with(binding) {
                if (badgeText.text == value) return
                badgeText.text = value
                if (value != null && value != "0") badgeText.visible()
                else badgeText.gone()
            }
        }
}