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

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

import de.thedevstack.conversationsplus.R;
import de.thedevstack.conversationsplus.entities.Presences;
import de.thedevstack.conversationsplus.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(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, de.thedevstack.conversationsplus.entities.Presence> entry : presences.getPresences().entrySet()) {
                Presence p = new Presence();
                p.resource = entry.getKey();
                p.status = entry.getValue().getStatus();
                presenceArrayList.add(p);
            }
            presenceArrayList.trimToSize();
        }
        return presenceArrayList.toArray(new Presence[0]);
    }
}

class Presence {
    String resource;
    de.thedevstack.conversationsplus.entities.Presence.Status status;
}