2016-03-29 19:38:50 +02:00
|
|
|
package com.github.dfa.diaspora_android.data;
|
2016-03-24 13:56:30 +01:00
|
|
|
|
2016-03-25 00:12:00 +01:00
|
|
|
import android.os.Handler;
|
2016-03-24 13:56:30 +01:00
|
|
|
import android.util.Log;
|
|
|
|
|
2016-03-29 19:38:50 +02:00
|
|
|
import com.github.dfa.diaspora_android.App;
|
|
|
|
import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener;
|
|
|
|
|
2016-03-24 13:56:30 +01:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
/**
|
2016-06-04 03:48:57 +02:00
|
|
|
* Created by gsantner on 24.03.16. Part of Diaspora for Android.
|
2016-03-24 13:56:30 +01:00
|
|
|
*/
|
2016-06-04 03:48:57 +02:00
|
|
|
public class PodUserProfile {
|
2016-03-25 00:12:00 +01:00
|
|
|
private static final int MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF = 5000;
|
|
|
|
|
2016-03-30 03:13:38 +02:00
|
|
|
private Handler callbackHandler;
|
2016-03-25 00:12:00 +01:00
|
|
|
private WebUserProfileChangedListener listener;
|
|
|
|
private App app;
|
|
|
|
private AppSettings appSettings;
|
|
|
|
private JSONObject json;
|
|
|
|
private long lastLoaded;
|
|
|
|
private boolean isWebUserProfileLoaded;
|
|
|
|
|
|
|
|
private String avatarUrl;
|
|
|
|
private String guid;
|
|
|
|
private String name;
|
|
|
|
private int notificationCount;
|
|
|
|
private int unreadMessagesCount;
|
|
|
|
|
2016-03-30 03:13:38 +02:00
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
public PodUserProfile(App app) {
|
2016-03-25 00:12:00 +01:00
|
|
|
this.app = app;
|
|
|
|
appSettings = app.getSettings();
|
|
|
|
|
|
|
|
avatarUrl = appSettings.getAvatarUrl();
|
|
|
|
guid = appSettings.getProfileId();
|
|
|
|
name = appSettings.getName();
|
2016-03-24 13:56:30 +01:00
|
|
|
}
|
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
public PodUserProfile(App app, Handler callbackHandler, WebUserProfileChangedListener listener) {
|
2016-03-30 03:13:38 +02:00
|
|
|
this(app);
|
|
|
|
this.listener = listener;
|
|
|
|
this.callbackHandler = callbackHandler;
|
|
|
|
}
|
|
|
|
|
2016-03-25 00:12:00 +01:00
|
|
|
public boolean isRefreshNeeded() {
|
2016-03-24 13:56:30 +01:00
|
|
|
return (System.currentTimeMillis() - lastLoaded) >= MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isWebUserProfileLoaded() {
|
|
|
|
return isWebUserProfileLoaded;
|
|
|
|
}
|
|
|
|
|
2016-03-25 00:12:00 +01:00
|
|
|
public boolean parseJson(String jsonStr) {
|
2016-03-24 13:56:30 +01:00
|
|
|
try {
|
2016-03-30 03:13:38 +02:00
|
|
|
json = new JSONObject(jsonStr);
|
2016-03-24 13:56:30 +01:00
|
|
|
lastLoaded = System.currentTimeMillis();
|
2016-03-25 00:12:00 +01:00
|
|
|
|
|
|
|
// Avatar
|
|
|
|
if (json.has("avatar")) {
|
|
|
|
JSONObject avatarJson = json.getJSONObject("avatar");
|
2016-03-30 03:13:38 +02:00
|
|
|
if (avatarJson.has("medium") && setAvatarUrl(avatarJson.getString("medium"))) {
|
2016-03-25 00:12:00 +01:00
|
|
|
app.getAvatarImageLoader().clearAvatarImage();
|
2016-03-30 03:13:38 +02:00
|
|
|
appSettings.setAvatarUrl(avatarUrl);
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GUID (User id)
|
2016-03-30 03:13:38 +02:00
|
|
|
if (json.has("guid") && loadGuid(json.getString("guid"))) {
|
2016-03-25 00:12:00 +01:00
|
|
|
appSettings.setProfileId(guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name
|
2016-03-30 03:13:38 +02:00
|
|
|
if (json.has("name") && loadName(json.getString("name"))) {
|
2016-03-25 00:12:00 +01:00
|
|
|
appSettings.setName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unread message count
|
2016-03-30 03:13:38 +02:00
|
|
|
if (json.has("notifications_count") && loadNotificationCount(json.getInt("notifications_count"))) {
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unread message count
|
2016-03-30 03:13:38 +02:00
|
|
|
if (json.has("unread_messages_count") && loadUnreadMessagesCount(json.getInt("unread_messages_count"))) {
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
2016-03-24 13:56:30 +01:00
|
|
|
isWebUserProfileLoaded = true;
|
|
|
|
} catch (JSONException e) {
|
2016-03-30 03:13:38 +02:00
|
|
|
Log.d(App.TAG, e.getMessage());
|
2016-03-24 13:56:30 +01:00
|
|
|
isWebUserProfileLoaded = false;
|
|
|
|
}
|
2016-03-25 00:12:00 +01:00
|
|
|
lastLoaded = System.currentTimeMillis();
|
2016-03-24 13:56:30 +01:00
|
|
|
return isWebUserProfileLoaded;
|
|
|
|
}
|
|
|
|
|
2016-03-25 00:12:00 +01:00
|
|
|
/*
|
|
|
|
// Getters & Setters
|
2016-03-24 13:56:30 +01:00
|
|
|
*/
|
|
|
|
|
2016-03-25 00:12:00 +01:00
|
|
|
public String getAvatarUrl() {
|
|
|
|
return avatarUrl;
|
2016-03-24 13:56:30 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:12:00 +01:00
|
|
|
public String getGuid() {
|
|
|
|
return guid;
|
2016-03-24 13:56:30 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:12:00 +01:00
|
|
|
public String getName() {
|
|
|
|
return name;
|
2016-03-24 13:56:30 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:12:00 +01:00
|
|
|
public int getNotificationCount() {
|
|
|
|
return notificationCount;
|
2016-03-24 13:56:30 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:12:00 +01:00
|
|
|
public int getUnreadMessagesCount() {
|
|
|
|
return unreadMessagesCount;
|
2016-03-24 13:56:30 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 03:13:38 +02:00
|
|
|
/*
|
|
|
|
* Private property setters
|
|
|
|
*/
|
|
|
|
private boolean setAvatarUrl(final String avatarUrl) {
|
|
|
|
if (!this.avatarUrl.equals(avatarUrl)) {
|
|
|
|
this.avatarUrl = avatarUrl;
|
|
|
|
if (listener != null && callbackHandler != null) {
|
|
|
|
callbackHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
listener.onUserProfileAvatarChanged(avatarUrl);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean loadGuid(final String guid) {
|
|
|
|
if (!this.guid.equals(guid)) {
|
|
|
|
this.guid = guid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean loadName(final String name) {
|
|
|
|
if (!this.name.equals(name)) {
|
|
|
|
this.name = name;
|
|
|
|
if (listener != null && callbackHandler != null) {
|
|
|
|
callbackHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
listener.onUserProfileNameChanged(name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean loadNotificationCount(final int notificationCount) {
|
|
|
|
if (this.notificationCount != notificationCount) {
|
|
|
|
this.notificationCount = notificationCount;
|
|
|
|
if (listener != null && callbackHandler != null) {
|
|
|
|
callbackHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
listener.onNotificationCountChanged(notificationCount);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean loadUnreadMessagesCount(final int unreadMessagesCount) {
|
|
|
|
if (this.unreadMessagesCount != unreadMessagesCount) {
|
|
|
|
this.unreadMessagesCount = unreadMessagesCount;
|
|
|
|
if (listener != null && callbackHandler != null) {
|
|
|
|
callbackHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
listener.onUnreadMessageCountChanged(unreadMessagesCount);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-24 13:56:30 +01:00
|
|
|
/*
|
|
|
|
* Not implemented / not needed yet:
|
2016-03-25 00:12:00 +01:00
|
|
|
* string "diasporaAddress"
|
|
|
|
* int "id"
|
2016-03-24 13:56:30 +01:00
|
|
|
* boolean "admin"
|
2016-03-25 00:12:00 +01:00
|
|
|
* int "following_count"
|
2016-03-24 13:56:30 +01:00
|
|
|
* boolean "moderator"
|
|
|
|
* array "aspects"
|
|
|
|
* int "id"
|
|
|
|
* string "name"
|
|
|
|
* boolean "selected"
|
|
|
|
*
|
|
|
|
* array "services"
|
|
|
|
* ? ?
|
|
|
|
* array "configured_services"
|
|
|
|
* ? ?
|
|
|
|
*/
|
|
|
|
}
|