aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ui/adapter/PresencesArrayAdapter.java
blob: 96faee917a70df0087f24436950ddd5959e41c29 (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
package de.thedevstack.conversationsplus.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Map;

import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.utils.UIHelper;

/**
 * Created by tzur on 27.09.2015.
 */
public class PresencesArrayAdapter extends ArrayAdapter<Presence> {
    private final Context context;
    private final Presence[] values;

    public PresencesArrayAdapter(Context context, Presences presences) {
        super(context, R.layout.dialog_resources_status);
        this.context = context;
        this.values = getPresenceArray(presences);
        addAll(this.values);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.dialog_resources_status, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.dlg_res_stat_resource_name);
        textView.setText(this.values[position].resource);
        textView.setTextColor(Color.parseColor(UIHelper.getStatusColor(this.values[position].status)));

        return rowView;
    }

    private static Presence[] getPresenceArray(Presences presences) {
        ArrayList<Presence> presenceArrayList = new ArrayList<>();
        if (null != presences && null != presences.getPresences() && !presences.getPresences().isEmpty()) {
            for (Map.Entry<String, Integer> entry : presences.getPresences().entrySet()) {
                Presence p = new Presence();
                p.resource = entry.getKey();
                p.status = entry.getValue();
                presenceArrayList.add(p);
            }
            presenceArrayList.trimToSize();
        }
        return presenceArrayList.toArray(new Presence[0]);
    }
}

class Presence {
    String resource;
    int status;
}