aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/models/ReaderUserList.java
blob: cd77a99d1df8ef0bf5e2e9f78233d478beaa5ff6 (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
package org.wordpress.android.models;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class ReaderUserList extends ArrayList<ReaderUser> {
    /*
     * returns all userIds in this list
     */
    public ReaderUserIdList getUserIds() {
        ReaderUserIdList ids = new ReaderUserIdList();
        for (ReaderUser user: this)
            ids.add(user.userId);
        return ids;
    }

    public int indexOfUserId(long userId) {
        for (int i = 0; i < this.size(); i++) {
            if (userId == this.get(i).userId) {
                return i;
            }
        }
        return -1;
    }

    /*
     * passed json is response from getting likes for a post
     */
    public static ReaderUserList fromJsonLikes(JSONObject json) {
        ReaderUserList users = new ReaderUserList();
        if (json==null)
            return users;

        JSONArray jsonLikes = json.optJSONArray("likes");
        if (jsonLikes!=null) {
            for (int i=0; i < jsonLikes.length(); i++)
                users.add(ReaderUser.fromJson(jsonLikes.optJSONObject(i)));
        }

        return users;
    }
}