2020import com .google .gson .stream .JsonReader ;
2121import com .google .gson .stream .JsonWriter ;
2222import com .j256 .ormlite .dao .Dao ;
23+ import com .j256 .ormlite .dao .ObjectCache ;
24+ import com .j256 .ormlite .dao .ReferenceObjectCache ;
2325import com .j256 .ormlite .dao .RuntimeExceptionDao ;
2426import com .j256 .ormlite .misc .TransactionManager ;
2527import com .zulip .android .activities .ZulipActivity ;
3335import com .zulip .android .networking .ZulipInterceptor ;
3436import com .zulip .android .networking .response .UserConfigurationResponse ;
3537import com .zulip .android .networking .response .events .EventsBranch ;
36- import com .zulip .android .networking .response .events .GetEventResponse ;
3738import com .zulip .android .service .ZulipServices ;
3839import com .zulip .android .util .ZLog ;
3940
4041import java .io .IOException ;
4142import java .lang .reflect .Type ;
4243import java .sql .SQLException ;
44+ import java .util .Date ;
4345import java .util .HashSet ;
4446import java .util .List ;
4547import 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 }
0 commit comments