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

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import net.lacolaco.smileessence.util.UIHandler;
import net.lacolaco.smileessence.viewmodel.IViewModel;

import java.util.ArrayList;
import java.util.List;

public abstract class CustomListAdapter<T extends IViewModel> extends BaseAdapter {

    // ------------------------------ FIELDS ------------------------------

    protected boolean isNotifiable = true;
    private List<T> frozenList = new ArrayList<>();
    private Activity activity;

    // --------------------------- CONSTRUCTORS ---------------------------

    CustomListAdapter(Activity activity) {
        this.activity = activity;
    }

    // --------------------- GETTER / SETTER METHODS ---------------------

    public final void setNotifiable(boolean notifiable) {
        isNotifiable = notifiable;
    }

    // --------------------- Interface BaseAdapter ---------------------

    @Override
    public final int getCount() {
        return frozenList.size();
    }

    @Override
    public final T getItem(int position) {
        return frozenList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        return getItem(position).getView(activity, activity.getLayoutInflater(), convertView);
    }

    // ------------------------ OVERRIDE METHODS ------------------------

    @Override
    public final void notifyDataSetChanged() {
        frozenList = getFrozenList();
        super.notifyDataSetChanged();
    }

    // -------------------------- OTHER METHODS --------------------------

    protected abstract List<T> getFrozenList();

    public void update() {
        if (isNotifiable) {
            updateForce();
        }
    }

    public void updateForce() {
        new UIHandler().post(this::notifyDataSetChanged);
    }
}