Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 5b1ef64

Browse files
Sam1301kunall17
authored andcommitted
Maintain list of all Streams in the database.
1 parent cddb14a commit 5b1ef64

File tree

7 files changed

+111
-10
lines changed

7 files changed

+111
-10
lines changed

app/src/main/java/com/zulip/android/ZulipApp.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.gson.JsonElement;
1818
import com.google.gson.JsonParseException;
1919
import com.google.gson.TypeAdapter;
20+
import com.google.gson.reflect.TypeToken;
2021
import com.google.gson.stream.JsonReader;
2122
import com.google.gson.stream.JsonWriter;
2223
import com.j256.ormlite.dao.Dao;
@@ -31,10 +32,12 @@
3132
import com.zulip.android.models.MessageType;
3233
import com.zulip.android.models.Person;
3334
import com.zulip.android.models.Presence;
35+
import com.zulip.android.models.Stream;
3436
import com.zulip.android.networking.AsyncUnreadMessagesUpdate;
3537
import com.zulip.android.networking.ZulipInterceptor;
3638
import com.zulip.android.networking.response.UserConfigurationResponse;
3739
import com.zulip.android.networking.response.events.EventsBranch;
40+
import com.zulip.android.networking.response.events.SubscriptionWrapper;
3841
import com.zulip.android.service.ZulipServices;
3942
import com.zulip.android.util.Constants;
4043
import com.zulip.android.util.GoogleAuthHelper;
@@ -288,6 +291,18 @@ public EventsBranch deserialize(JsonElement json, Type typeOfT, JsonDeserializat
288291
}
289292
Class<? extends EventsBranch> t = EventsBranch.BranchType.fromRawType(invalid);
290293
if (t != null) {
294+
if (t.getSimpleName().equalsIgnoreCase("SubscriptionWrapper")) {
295+
// check operation
296+
if (SubscriptionWrapper.OPERATION_ADD.equalsIgnoreCase(json.getAsJsonObject().get("op").getAsString()) ||
297+
SubscriptionWrapper.OPERATION_REMOVE.equalsIgnoreCase(json.getAsJsonObject().get("op").getAsString()) ||
298+
SubscriptionWrapper.OPERATION_UPDATE.equalsIgnoreCase(json.getAsJsonObject().get("op").getAsString())) {
299+
Type type = new TypeToken<SubscriptionWrapper<Stream>>(){}.getType();
300+
return nestedGson.fromJson(json, type);
301+
} else {
302+
Type type = new TypeToken<SubscriptionWrapper<String>>(){}.getType();
303+
return nestedGson.fromJson(json, type);
304+
}
305+
}
291306
return nestedGson.fromJson(json, t);
292307
}
293308
Log.w("GSON", "Attempted to deserialize and unregistered EventBranch... See EventBranch.BranchType");

app/src/main/java/com/zulip/android/models/Stream.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ public void setInHomeView(boolean inHomeView) {
220220
this.inHomeView = inHomeView;
221221
}
222222

223+
public void setDefaultColor() {
224+
this.parsedColor = DEFAULT_COLOR;
225+
}
226+
223227
/**
224228
* This function returns the last message read in {@param streamName} stream.
225229
*

app/src/main/java/com/zulip/android/networking/AsyncGetEvents.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.zulip.android.networking.response.events.GetEventResponse;
2626
import com.zulip.android.networking.response.events.MessageWrapper;
2727
import com.zulip.android.networking.response.events.MutedTopicsWrapper;
28+
import com.zulip.android.networking.response.events.StreamWrapper;
2829
import com.zulip.android.networking.response.events.SubscriptionWrapper;
2930
import com.zulip.android.networking.response.events.UpdateMessageWrapper;
3031
import com.zulip.android.util.MutedTopics;
@@ -170,6 +171,18 @@ public Void call() throws Exception {
170171
}
171172
}
172173

174+
// get rest of the streams in the realm
175+
List<Stream> streams = response.getStreams();
176+
for (Stream stream : streams) {
177+
try {
178+
// use default color for not subscribed streams
179+
stream.setDefaultColor();
180+
streamDao.createIfNotExists(stream);
181+
} catch (Exception e) {
182+
ZLog.logException(e);
183+
}
184+
}
185+
173186
//MUST UPDATE AFTER SUBSCRIPTIONS ARE STORED IN DB
174187
mMutedTopics.addToMutedTopics(response.getMutedTopics());
175188

@@ -353,13 +366,22 @@ public Message convert(MessageWrapper messageWrapper) {
353366
processUpdateMessages(updateMessageEvents);
354367
}
355368

356-
//get message time limit events
369+
// get message time limit events
357370
List<EventsBranch> messageTimeLimit = events.getEventsOfBranchType(EventsBranch.BranchType.EDIT_MESSAGE_TIME_LIMIT);
358371
if (!messageTimeLimit.isEmpty()) {
359372
Log.i("AsyncGetEvents", "Received " + messageTimeLimit.size()
360373
+ " realm event");
361374
processMessageEditParam(messageTimeLimit);
362375
}
376+
377+
// get stream events
378+
List<EventsBranch> streamEvents = events.getEventsOfBranchType(EventsBranch.BranchType.STREAM);
379+
if (!streamEvents.isEmpty()) {
380+
Log.i("AsyncGetEvents", "Received " + streamEvents.size()
381+
+ " stream event");
382+
processStreams(streamEvents);
383+
}
384+
363385
}
364386

365387
/**
@@ -514,4 +536,27 @@ public void run() {
514536
}
515537
});
516538
}
539+
540+
public void processStreams(List<EventsBranch> events) {
541+
for (EventsBranch event : events) {
542+
StreamWrapper streamEvent = (StreamWrapper) event;
543+
if (streamEvent.getOperation().equalsIgnoreCase(StreamWrapper.OP_OCCUPY)) {
544+
Dao<Stream, Integer> streamDao = app.getDao(Stream.class);
545+
List<Stream> streams = streamEvent.getStreams();
546+
for (Stream stream : streams) {
547+
try {
548+
// use default color for not subscribed streams
549+
stream.setDefaultColor();
550+
streamDao.createIfNotExists(stream);
551+
} catch (SQLException e) {
552+
ZLog.logException(e);
553+
}
554+
}
555+
} else {
556+
// TODO: handle other operations of stream event
557+
Log.d("AsyncEvents", "unhandled operation for stream type event");
558+
return;
559+
}
560+
}
561+
}
517562
}

app/src/main/java/com/zulip/android/networking/response/events/EventsBranch.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public enum BranchType {
2727
SUBSCRIPTIONS(SubscriptionWrapper.class, "subscription"),
2828
MUTED_TOPICS(MutedTopicsWrapper.class, "muted_topics"),
2929
EDIT_MESSAGE_TIME_LIMIT(EditMessageWrapper.class, "realm"),
30-
UPDATE_MESSAGE(UpdateMessageWrapper.class, "update_message");
30+
UPDATE_MESSAGE(UpdateMessageWrapper.class, "update_message"),
31+
STREAM(StreamWrapper.class, "stream");
3132

3233

3334
private final Class<? extends EventsBranch> klazz;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.zulip.android.networking.response.events;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import com.zulip.android.models.Stream;
5+
6+
import java.util.List;
7+
8+
/**
9+
* This class is used to deserialize the stream type events {@link EventsBranch.BranchType#STREAM}.
10+
* example: {"streams":[{"stream_id":20,"description":"","name":"Good Morning","invite_only":false}],
11+
* "type":"stream","id":22,"op":"occupy"}
12+
*/
13+
14+
public class StreamWrapper extends EventsBranch {
15+
16+
public static final String OP_OCCUPY = "occupy";
17+
18+
@SerializedName("streams")
19+
private List<Stream> mStreams;
20+
21+
@SerializedName("op")
22+
private String mOperation;
23+
24+
public List<Stream> getStreams() {
25+
return this.mStreams;
26+
}
27+
28+
public String getOperation() {
29+
return this.mOperation;
30+
}
31+
}

app/src/main/java/com/zulip/android/networking/response/events/SubscriptionWrapper.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
* holds the updated value of this property.
3232
*/
3333

34-
public class SubscriptionWrapper extends EventsBranch {
34+
public class SubscriptionWrapper<T> extends EventsBranch {
3535

3636
public static final String OPERATION_ADD = "add";
3737
public static final String OPERATION_REMOVE = "remove";
3838
public static final String OPERATION_UPDATE = "update";
3939

4040
@SerializedName("subscriptions")
41-
private List<Stream> streams;
41+
private List<T> streams;
4242

4343
@SerializedName("op")
4444
private String operation;
@@ -55,12 +55,13 @@ public class SubscriptionWrapper extends EventsBranch {
5555
@SerializedName("value")
5656
private Object value;
5757

58-
public List<Stream> getStreams() {
59-
return this.streams;
60-
}
61-
62-
public void setStreams(List<Stream> streams) {
63-
this.streams = streams;
58+
public List<T> getStreams() {
59+
if (getOperation().equalsIgnoreCase(OPERATION_ADD) ||
60+
getOperation().equalsIgnoreCase(OPERATION_REMOVE) ||
61+
getOperation().equalsIgnoreCase(OPERATION_UPDATE)) {
62+
return this.streams;
63+
}
64+
return null;
6465
}
6566

6667
public String getOperation() {

app/src/main/java/com/zulip/android/networking/response/events/UpdateMessageWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,8 @@ public String getFormattedContent() {
6969
public void setFormattedContent(String formattedContent) {
7070
this.formattedContent = formattedContent;
7171
}
72+
73+
public String getOrigFormattedContent() {
74+
return this.origFormattedContent;
75+
}
7276
}

0 commit comments

Comments
 (0)