20
20
import com .google .gson .stream .JsonReader ;
21
21
import com .google .gson .stream .JsonWriter ;
22
22
import com .j256 .ormlite .dao .Dao ;
23
+ import com .j256 .ormlite .dao .ObjectCache ;
24
+ import com .j256 .ormlite .dao .ReferenceObjectCache ;
23
25
import com .j256 .ormlite .dao .RuntimeExceptionDao ;
24
26
import com .j256 .ormlite .misc .TransactionManager ;
25
27
import com .zulip .android .activities .ZulipActivity ;
33
35
import com .zulip .android .networking .ZulipInterceptor ;
34
36
import com .zulip .android .networking .response .UserConfigurationResponse ;
35
37
import com .zulip .android .networking .response .events .EventsBranch ;
36
- import com .zulip .android .networking .response .events .GetEventResponse ;
37
38
import com .zulip .android .service .ZulipServices ;
38
39
import com .zulip .android .util .ZLog ;
39
40
40
41
import java .io .IOException ;
41
42
import java .lang .reflect .Type ;
42
43
import java .sql .SQLException ;
44
+ import java .util .Date ;
43
45
import java .util .HashSet ;
44
46
import java .util .List ;
45
47
import java .util .Map ;
@@ -83,6 +85,7 @@ public class ZulipApp extends Application {
83
85
private Set <String > mutedTopics ;
84
86
private static final String MUTED_TOPIC_KEY = "mutedTopics" ;
85
87
private ZulipServices zulipServices ;
88
+ private ReferenceObjectCache objectCache ;
86
89
87
90
/**
88
91
* Handler to manage batching of unread messages
@@ -168,6 +171,13 @@ public int getAppVersion() {
168
171
}
169
172
}
170
173
174
+ public ObjectCache getObjectCache () {
175
+ if (objectCache == null ) {
176
+ objectCache = new ReferenceObjectCache (true );
177
+ }
178
+ return objectCache ;
179
+ }
180
+
171
181
private void afterLogin () {
172
182
String email = settings .getString (EMAIL , null );
173
183
setEmail (email );
@@ -176,9 +186,7 @@ private void afterLogin() {
176
186
177
187
public ZulipServices getZulipServices () {
178
188
if (zulipServices == null ) {
179
- HttpLoggingInterceptor logging = new HttpLoggingInterceptor ();
180
- logging .setLevel (HttpLoggingInterceptor .Level .BASIC );
181
-
189
+ HttpLoggingInterceptor logging = new HttpLoggingInterceptor ().setLevel (HttpLoggingInterceptor .Level .BODY );
182
190
zulipServices = new Retrofit .Builder ()
183
191
.client (new OkHttpClient .Builder ().readTimeout (60 , TimeUnit .SECONDS )
184
192
.addInterceptor (new ZulipInterceptor ())
@@ -192,19 +200,40 @@ public ZulipServices getZulipServices() {
192
200
return zulipServices ;
193
201
}
194
202
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
+
197
221
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
+ })
198
227
.registerTypeAdapter (UserConfigurationResponse .class , new TypeAdapter <UserConfigurationResponse >() {
199
228
200
229
@ Override
201
230
public void write (JsonWriter out , UserConfigurationResponse value ) throws IOException {
202
- gson .toJson (gson .toJsonTree (value ), out );
231
+ nestedGson .toJson (nestedGson .toJsonTree (value ), out );
203
232
}
204
233
205
234
@ Override
206
235
public UserConfigurationResponse read (JsonReader in ) throws IOException {
207
- UserConfigurationResponse res = gson .fromJson (in , UserConfigurationResponse .class );
236
+ UserConfigurationResponse res = nestedGson .fromJson (in , UserConfigurationResponse .class );
208
237
209
238
RuntimeExceptionDao <Person , Object > personDao = ZulipApp .this .getDao (Person .class );
210
239
for (int i = 0 ; i < res .getRealmUsers ().size (); i ++) {
@@ -220,16 +249,17 @@ public UserConfigurationResponse read(JsonReader in) throws IOException {
220
249
e .printStackTrace ();
221
250
}
222
251
}
252
+
223
253
return res ;
224
254
}
225
255
})
226
- .registerTypeAdapter (GetEventResponse .class , new JsonDeserializer <EventsBranch >() {
256
+ .registerTypeAdapter (EventsBranch .class , new JsonDeserializer <EventsBranch >() {
227
257
@ Override
228
258
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 );
230
260
Class <? extends EventsBranch > t = EventsBranch .BranchType .fromRawType (invalid );
231
261
if (t != null ) {
232
- return gson .fromJson (json , t );
262
+ return nestedGson .fromJson (json , t );
233
263
}
234
264
Log .w ("GSON" , "Attempted to deserialize and unregistered EventBranch... See EventBranch.BranchType" );
235
265
return invalid ;
@@ -352,16 +382,24 @@ public DatabaseHelper getDatabaseHelper() {
352
382
}
353
383
354
384
@ 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 ) {
356
386
try {
357
- return new RuntimeExceptionDao <>(
387
+ RuntimeExceptionDao < C , T > ret = new RuntimeExceptionDao <>(
358
388
(Dao <C , T >) databaseHelper .getDao (cls ));
389
+ if (useCache ) {
390
+ ret .setObjectCache (objectCache );
391
+ }
392
+ return ret ;
359
393
} catch (SQLException e ) {
360
394
// Well that's sort of awkward.
361
395
throw new RuntimeException (e );
362
396
}
363
397
}
364
398
399
+ public <C , T > RuntimeExceptionDao <C , T > getDao (Class <C > cls ) {
400
+ return getDao (cls , false );
401
+ }
402
+
365
403
public void setContext (Context targetContext ) {
366
404
this .attachBaseContext (targetContext );
367
405
}
0 commit comments