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

Commit 73fa384

Browse files
koziodigitalincniftynei
authored andcommitted
Deserialization for GetEvents is now working
merging Revert "Remove humbug references from variable names" This reverts commit a93948e. Fix end of input at character 0 in AsyncGetOldMessages Changes return value in ZulipAsyncPushTask.doInBackground to null on failed request instead of an empty string. AsyncGetOldMessages is coded to test for failure by checking for null value. Undid the humbug revert. Undid the humbug revert. Events are updating. Fixing up AsyncGetOldMessages
1 parent 93ac4ef commit 73fa384

File tree

9 files changed

+358
-259
lines changed

9 files changed

+358
-259
lines changed

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

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.gson.stream.JsonReader;
2121
import com.google.gson.stream.JsonWriter;
2222
import com.j256.ormlite.dao.Dao;
23+
import com.j256.ormlite.dao.ObjectCache;
24+
import com.j256.ormlite.dao.ReferenceObjectCache;
2325
import com.j256.ormlite.dao.RuntimeExceptionDao;
2426
import com.j256.ormlite.misc.TransactionManager;
2527
import com.zulip.android.activities.ZulipActivity;
@@ -33,13 +35,13 @@
3335
import com.zulip.android.networking.ZulipInterceptor;
3436
import com.zulip.android.networking.response.UserConfigurationResponse;
3537
import com.zulip.android.networking.response.events.EventsBranch;
36-
import com.zulip.android.networking.response.events.GetEventResponse;
3738
import com.zulip.android.service.ZulipServices;
3839
import com.zulip.android.util.ZLog;
3940

4041
import java.io.IOException;
4142
import java.lang.reflect.Type;
4243
import java.sql.SQLException;
44+
import java.util.Date;
4345
import java.util.HashSet;
4446
import java.util.List;
4547
import java.util.Map;
@@ -83,6 +85,7 @@ public class ZulipApp extends Application {
8385
private Set<String> mutedTopics;
8486
private static final String MUTED_TOPIC_KEY = "mutedTopics";
8587
private ZulipServices zulipServices;
88+
private ReferenceObjectCache objectCache;
8689

8790
/**
8891
* Handler to manage batching of unread messages
@@ -168,6 +171,13 @@ public int getAppVersion() {
168171
}
169172
}
170173

174+
public ObjectCache getObjectCache() {
175+
if(objectCache == null) {
176+
objectCache = new ReferenceObjectCache(true);
177+
}
178+
return objectCache;
179+
}
180+
171181
private void afterLogin() {
172182
String email = settings.getString(EMAIL, null);
173183
setEmail(email);
@@ -176,9 +186,7 @@ private void afterLogin() {
176186

177187
public ZulipServices getZulipServices() {
178188
if(zulipServices == null) {
179-
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
180-
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
181-
189+
HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
182190
zulipServices = new Retrofit.Builder()
183191
.client(new OkHttpClient.Builder().readTimeout(60, TimeUnit.SECONDS)
184192
.addInterceptor(new ZulipInterceptor())
@@ -192,19 +200,40 @@ public ZulipServices getZulipServices() {
192200
return zulipServices;
193201
}
194202

195-
private Gson buildGson() {
196-
final Gson gson = new Gson();
203+
public Gson buildGson() {
204+
final Gson naiveGson = new Gson();
205+
final Gson nestedGson = new GsonBuilder()
206+
.registerTypeAdapter(Message.class, new JsonDeserializer<Message>() {
207+
@Override
208+
public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
209+
Message msg;
210+
if("stream".equalsIgnoreCase(json.getAsJsonObject().get("type").getAsString())) {
211+
msg = naiveGson.fromJson(json, Message.ZulipStreamMessage.class);
212+
}
213+
msg = naiveGson.fromJson(json, Message.ZulipDirectMessage.class);
214+
215+
216+
return msg;
217+
}
218+
}).create();
219+
220+
197221
return new GsonBuilder()
222+
.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
223+
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
224+
return new Date(json.getAsJsonPrimitive().getAsLong());
225+
}
226+
})
198227
.registerTypeAdapter(UserConfigurationResponse.class, new TypeAdapter<UserConfigurationResponse>() {
199228

200229
@Override
201230
public void write(JsonWriter out, UserConfigurationResponse value) throws IOException {
202-
gson.toJson(gson.toJsonTree(value), out);
231+
nestedGson.toJson(nestedGson.toJsonTree(value), out);
203232
}
204233

205234
@Override
206235
public UserConfigurationResponse read(JsonReader in) throws IOException {
207-
UserConfigurationResponse res = gson.fromJson(in, UserConfigurationResponse.class);
236+
UserConfigurationResponse res = nestedGson.fromJson(in, UserConfigurationResponse.class);
208237

209238
RuntimeExceptionDao<Person, Object> personDao = ZulipApp.this.getDao(Person.class);
210239
for (int i = 0; i < res.getRealmUsers().size(); i++) {
@@ -220,16 +249,17 @@ public UserConfigurationResponse read(JsonReader in) throws IOException {
220249
e.printStackTrace();
221250
}
222251
}
252+
223253
return res;
224254
}
225255
})
226-
.registerTypeAdapter(GetEventResponse.class, new JsonDeserializer<EventsBranch>() {
256+
.registerTypeAdapter(EventsBranch.class, new JsonDeserializer<EventsBranch>() {
227257
@Override
228258
public EventsBranch deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
229-
EventsBranch invalid = gson.fromJson(json, EventsBranch.class);
259+
EventsBranch invalid = nestedGson.fromJson(json, EventsBranch.class);
230260
Class<? extends EventsBranch> t = EventsBranch.BranchType.fromRawType(invalid);
231261
if(t != null) {
232-
return gson.fromJson(json, t);
262+
return nestedGson.fromJson(json, t);
233263
}
234264
Log.w("GSON", "Attempted to deserialize and unregistered EventBranch... See EventBranch.BranchType");
235265
return invalid;
@@ -352,16 +382,24 @@ public DatabaseHelper getDatabaseHelper() {
352382
}
353383

354384
@SuppressWarnings("unchecked")
355-
public <C, T> RuntimeExceptionDao<C, T> getDao(Class<C> cls) {
385+
public <C, T> RuntimeExceptionDao<C, T> getDao(Class<C> cls, boolean useCache) {
356386
try {
357-
return new RuntimeExceptionDao<>(
387+
RuntimeExceptionDao<C, T> ret = new RuntimeExceptionDao<>(
358388
(Dao<C, T>) databaseHelper.getDao(cls));
389+
if(useCache) {
390+
ret.setObjectCache(objectCache);
391+
}
392+
return ret;
359393
} catch (SQLException e) {
360394
// Well that's sort of awkward.
361395
throw new RuntimeException(e);
362396
}
363397
}
364398

399+
public <C, T> RuntimeExceptionDao<C, T> getDao(Class<C> cls) {
400+
return getDao(cls, false);
401+
}
402+
365403
public void setContext(Context targetContext) {
366404
this.attachBaseContext(targetContext);
367405
}

app/src/main/java/com/zulip/android/activities/LoginActivity.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected void onStop() {
148148
}
149149

150150
private void checkForError() {
151-
String serverURL = serverIn.getText().toString();
151+
String serverURL = "https://zulip.tabbott.net";//serverIn.getText().toString();
152152
int errorMessage = R.string.invalid_server_domain;
153153
String httpScheme = (BuildConfig.DEBUG) ? "http" : "https";
154154

@@ -354,14 +354,15 @@ public void onClick(View v) {
354354
setupGoogleSignIn();
355355
break;
356356
case R.id.zulip_login:
357-
if (!isInputValid()) {
358-
return;
359-
}
357+
// if (!isInputValid()) {
358+
// return;
359+
// }
360360
connectionProgressDialog.show();
361-
String username = mUserName.getText().toString();
361+
String username = "@gmail.com ";//mUserName.getText().toString();
362+
String password = ""; //mPassword.getText().toString()
362363
getApp().setEmail(username);
363364
getServices()
364-
.login(username, mPassword.getText().toString())
365+
.login(username, password)
365366
.enqueue(new DefaultCallback<LoginResponse>() {
366367

367368
@Override

0 commit comments

Comments
 (0)