Commit 492e3d0e authored by Leonardo Aramaki's avatar Leonardo Aramaki

Implement opened rooms caching

parent eefec932
......@@ -34,6 +34,7 @@ public class RocketChatCache {
private static final String KEY_SELECTED_ROOM_ID = "KEY_SELECTED_ROOM_ID";
private static final String KEY_PUSH_ID = "KEY_PUSH_ID";
private static final String KEY_HOSTNAME_LIST = "KEY_HOSTNAME_LIST";
private static final String KEY_OPENED_ROOMS = "KEY_OPENED_ROOMS";
private Context context;
......@@ -41,6 +42,38 @@ public class RocketChatCache {
this.context = context.getApplicationContext();
}
public void addOpenedRoom(@NonNull String roomId, long lastSeen) {
JSONObject openedRooms = getOpenedRooms();
try {
JSONObject room = new JSONObject().put("rid", roomId).put("ls", lastSeen);
openedRooms.put(roomId, room);
} catch (JSONException e) {
RCLog.e(e);
}
setString(KEY_OPENED_ROOMS, openedRooms.toString());
}
public void removeOpenedRoom(@NonNull String roomId) {
JSONObject openedRooms = getOpenedRooms();
if (openedRooms.has(roomId)) {
openedRooms.remove(roomId);
}
}
public @NonNull
JSONObject getOpenedRooms() {
String openedRooms = getString(KEY_OPENED_ROOMS, "");
if (openedRooms.isEmpty()) {
return new JSONObject();
}
try {
return new JSONObject(openedRooms);
} catch (JSONException e) {
RCLog.e(e);
}
return new JSONObject();
}
public String getSelectedServerHostname() {
return getString(KEY_SELECTED_SERVER_HOSTNAME, null);
}
......@@ -65,7 +98,8 @@ public class RocketChatCache {
}
}
public @NonNull String getHostSiteName(@NonNull String host) {
public @NonNull
String getHostSiteName(@NonNull String host) {
if (host.startsWith("http")) {
HttpUrl url = HttpUrl.parse(host);
if (url != null) {
......@@ -84,7 +118,8 @@ public class RocketChatCache {
return "";
}
private @Nullable String getHostSiteNamesJson() {
private @Nullable
String getHostSiteNamesJson() {
return getString(KEY_SELECTED_SITE_NAME, null);
}
......@@ -104,7 +139,8 @@ public class RocketChatCache {
}
}
public @Nullable String getSiteUrlFor(String hostname) {
public @Nullable
String getSiteUrlFor(String hostname) {
try {
String selectedServerHostname = getSelectedServerHostname();
if (getLoginHostnamesJson() == null || getLoginHostnamesJson().isEmpty()) {
......@@ -118,7 +154,8 @@ public class RocketChatCache {
return null;
}
private @Nullable String getLoginHostnamesJson() {
private @Nullable
String getLoginHostnamesJson() {
return getString(KEY_SELECTED_SITE_URL, null);
}
......@@ -150,7 +187,7 @@ public class RocketChatCache {
try {
JSONObject jsonObj = new JSONObject(json);
List<Pair<String, Pair<String, String>>> serverList = new ArrayList<>();
for (Iterator<String> iter = jsonObj.keys(); iter.hasNext();) {
for (Iterator<String> iter = jsonObj.keys(); iter.hasNext(); ) {
String hostname = iter.next();
JSONObject serverInfoJson = jsonObj.getJSONObject(hostname);
serverList.add(new Pair<>(hostname, new Pair<>(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment