aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/models/ReaderUser.java
blob: abd905df16533de78f7f3ef5863fc7daeb5c96d7 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package org.wordpress.android.models;

import android.text.TextUtils;

import org.json.JSONObject;
import org.wordpress.android.util.JSONUtils;
import org.wordpress.android.util.StringUtils;
import org.wordpress.android.util.UrlUtils;

public class ReaderUser {
    public long userId;
    public long blogId;
    private String userName;
    private String displayName;
    private String url;
    private String profileUrl;
    private String avatarUrl;

    public static ReaderUser fromJson(JSONObject json) {
        ReaderUser user = new ReaderUser();
        if (json==null)
            return user;

        user.userId = json.optLong("ID");
        user.blogId = json.optLong("site_ID");

        user.userName = JSONUtils.getString(json, "username");
        user.url = JSONUtils.getString(json, "URL"); // <-- this isn't necessarily a wp blog
        user.profileUrl = JSONUtils.getString(json, "profile_URL");
        user.avatarUrl = JSONUtils.getString(json, "avatar_URL");

        // "me" api call (current user) has "display_name", others have "name"
        if (json.has("display_name")) {
            user.displayName = JSONUtils.getStringDecoded(json, "display_name");
        } else {
            user.displayName = JSONUtils.getStringDecoded(json, "name");
        }

        return user;
    }

    public String getUserName() {
        return StringUtils.notNullStr(userName);
    }
    public void setUserName(String userName) {
        this.userName = StringUtils.notNullStr(userName);
    }

    public String getDisplayName() {
        return StringUtils.notNullStr(displayName);
    }
    public void setDisplayName(String displayName) {
        this.displayName = StringUtils.notNullStr(displayName);
    }

    public String getUrl() {
        return StringUtils.notNullStr(url);
    }
    public void setUrl(String url) {
        this.url = StringUtils.notNullStr(url);
    }

    public String getProfileUrl() {
        return StringUtils.notNullStr(profileUrl);
    }
    public void setProfileUrl(String profileUrl) {
        this.profileUrl = StringUtils.notNullStr(profileUrl);
    }

    public String getAvatarUrl() {
        return StringUtils.notNullStr(avatarUrl);
    }
    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = StringUtils.notNullStr(avatarUrl);
    }

    public boolean hasUrl() {
        return !TextUtils.isEmpty(url);
    }

    public boolean hasAvatarUrl() {
        return !TextUtils.isEmpty(avatarUrl);
    }

    public boolean hasBlogId() {
        return (blogId != 0);
    }

    /*
     * not stored - used by ReaderUserAdapter for performance
     */
    private transient String urlDomain;
    public String getUrlDomain() {
        if (urlDomain == null) {
            if (hasUrl()) {
                urlDomain = UrlUtils.getHost(getUrl());
            } else {
                urlDomain = "";
            }
        }
        return urlDomain;
    }

    public boolean isSameUser(ReaderUser user) {
        if (user == null)
            return false;
        if (this.userId != user.userId)
            return false;
        if (!this.getAvatarUrl().equals(user.getAvatarUrl()))
            return false;
        if (!this.getDisplayName().equals(user.getDisplayName()))
            return false;
        if (!this.getUserName().equals(user.getUserName()))
            return false;
        if (!this.getUrl().equals(user.getUrl()))
            return false;
        if (!this.getProfileUrl().equals(user.getProfileUrl()))
            return false;
        return true;
    }
}