aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.kt
blob: e68e3746c4556710fbdcf9e030a3b2ccb539648f (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
package net.lacolaco.smileessence.view.adapter

import android.widget.BaseAdapter
import net.lacolaco.smileessence.util.UIHandler

import java.util.ArrayList
import java.util.Collections

abstract class CustomListAdapter<out T> : BaseAdapter() {
    private var isNotifiable = true
    private var frozenList: List<T> = ArrayList()

    fun setNotifiable(notifiable: Boolean) {
        isNotifiable = notifiable
    }

    override fun getCount(): Int {
        return frozenList.size
    }

    override fun getItem(position: Int): T {
        return frozenList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun notifyDataSetChanged() {
        frozenList = Collections.unmodifiableList(list)
        super.notifyDataSetChanged()
    }

    protected abstract val list: List<T>

    fun update() {
        if (isNotifiable) {
            updateForce()
        }
    }

    fun updateForce() {
        UIHandler().post { this.notifyDataSetChanged() }
    }
}