1010import android .util .Log ;
1111
1212import com .crashlytics .android .Crashlytics ;
13+ import com .google .gson .Gson ;
14+ import com .google .gson .GsonBuilder ;
15+ import com .google .gson .JsonDeserializationContext ;
16+ import com .google .gson .JsonDeserializer ;
17+ import com .google .gson .JsonElement ;
18+ import com .google .gson .JsonParseException ;
19+ import com .google .gson .TypeAdapter ;
20+ import com .google .gson .stream .JsonReader ;
21+ import com .google .gson .stream .JsonWriter ;
1322import com .j256 .ormlite .dao .Dao ;
1423import com .j256 .ormlite .dao .RuntimeExceptionDao ;
1524import com .j256 .ormlite .misc .TransactionManager ;
2231import com .zulip .android .models .Stream ;
2332import com .zulip .android .networking .AsyncUnreadMessagesUpdate ;
2433import com .zulip .android .networking .ZulipInterceptor ;
34+ import com .zulip .android .networking .response .UserConfigurationResponse ;
35+ import com .zulip .android .networking .response .events .EventsBranch ;
36+ import com .zulip .android .networking .response .events .GetEventResponse ;
2537import com .zulip .android .service .ZulipServices ;
2638import com .zulip .android .util .ZLog ;
2739
28- import org .json .JSONArray ;
29- import org .json .JSONException ;
30-
3140import java .io .IOException ;
41+ import java .lang .reflect .Type ;
3242import java .sql .SQLException ;
3343import java .util .HashSet ;
44+ import java .util .List ;
3445import java .util .Map ;
3546import java .util .Queue ;
3647import java .util .Set ;
4152
4253import io .fabric .sdk .android .Fabric ;
4354import okhttp3 .OkHttpClient ;
44- import okhttp3 .Request ;
4555import okhttp3 .ResponseBody ;
4656import okhttp3 .logging .HttpLoggingInterceptor ;
4757import retrofit2 .Call ;
@@ -74,18 +84,6 @@ public class ZulipApp extends Application {
7484 private static final String MUTED_TOPIC_KEY = "mutedTopics" ;
7585 private ZulipServices zulipServices ;
7686
77- public Request goodRequest ;
78- public Request badRequest ;
79- private ZulipActivity zulipActivity ;
80-
81- public ZulipActivity getZulipActivity () {
82- return zulipActivity ;
83- }
84-
85- public void setZulipActivity (ZulipActivity zulipActivity ) {
86- this .zulipActivity = zulipActivity ;
87- }
88-
8987 /**
9088 * Handler to manage batching of unread messages
9189 */
@@ -111,6 +109,7 @@ public void setZulipActivity(ZulipActivity zulipActivity) {
111109 * every couple of seconds
112110 */
113111 public final Queue <Integer > unreadMessageQueue = new ConcurrentLinkedQueue <>();
112+ public String tester ;
114113
115114 public static ZulipApp get () {
116115 return instance ;
@@ -185,14 +184,60 @@ public ZulipServices getZulipServices() {
185184 .addInterceptor (new ZulipInterceptor ())
186185 .addInterceptor (logging )
187186 .build ())
188- .addConverterFactory (GsonConverterFactory .create ())
187+ .addConverterFactory (GsonConverterFactory .create (buildGson () ))
189188 .baseUrl (getServerURI ())
190189 .build ()
191190 .create (ZulipServices .class );
192191 }
193192 return zulipServices ;
194193 }
195194
195+ private Gson buildGson () {
196+ final Gson gson = new Gson ();
197+ return new GsonBuilder ()
198+ .registerTypeAdapter (UserConfigurationResponse .class , new TypeAdapter <UserConfigurationResponse >() {
199+
200+ @ Override
201+ public void write (JsonWriter out , UserConfigurationResponse value ) throws IOException {
202+ gson .toJson (gson .toJsonTree (value ), out );
203+ }
204+
205+ @ Override
206+ public UserConfigurationResponse read (JsonReader in ) throws IOException {
207+ UserConfigurationResponse res = gson .fromJson (in , UserConfigurationResponse .class );
208+
209+ RuntimeExceptionDao <Person , Object > personDao = ZulipApp .this .getDao (Person .class );
210+ for (int i = 0 ; i < res .getRealmUsers ().size (); i ++) {
211+
212+ Person currentPerson = res .getRealmUsers ().get (i );
213+ Person foundPerson = null ;
214+ try {
215+ foundPerson = personDao .queryBuilder ().where ().eq (Person .EMAIL_FIELD , currentPerson .getEmail ()).queryForFirst ();
216+ if (foundPerson != null ) {
217+ currentPerson .setId (foundPerson .getId ());
218+ }
219+ } catch (SQLException e ) {
220+ e .printStackTrace ();
221+ }
222+ }
223+ return res ;
224+ }
225+ })
226+ .registerTypeAdapter (GetEventResponse .class , new JsonDeserializer <EventsBranch >() {
227+ @ Override
228+ public EventsBranch deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
229+ EventsBranch invalid = gson .fromJson (json , EventsBranch .class );
230+ Class <? extends EventsBranch > t = EventsBranch .BranchType .fromRawType (invalid );
231+ if (t != null ) {
232+ return gson .fromJson (json , t );
233+ }
234+ Log .w ("GSON" , "Attempted to deserialize and unregistered EventBranch... See EventBranch.BranchType" );
235+ return invalid ;
236+ }
237+ })
238+ .create ();
239+ }
240+
196241 /**
197242 * Fills the Emoji Table with the existing emoticons saved in the assets folder.
198243 */
@@ -249,20 +294,18 @@ public String getUserAgent() {
249294 }
250295 }
251296
252- public void addToMutedTopics (JSONArray jsonArray ) {
297+ public void addToMutedTopics (List < List < String >> mutedTopics ) {
253298 Stream stream ;
254299
255- for (int i = 0 ; i < jsonArray .length (); i ++) {
256- try {
257- JSONArray mutedTopic = jsonArray .getJSONArray (i );
258- stream = Stream .getByName (this , mutedTopic .get (0 ).toString ());
259- mutedTopics .add (stream .getId () + mutedTopic .get (1 ).toString ());
260- } catch (JSONException e ) {
261- Log .e ("JSONException" , "JSON Is not correct" , e );
300+ if (mutedTopics != null ) {
301+ for (int i = 0 ; i < mutedTopics .size (); i ++) {
302+ List <String > mutedTopic = mutedTopics .get (i );
303+ stream = Stream .getByName (this , mutedTopic .get (0 ));
304+ this .mutedTopics .add (stream .getId () + mutedTopic .get (1 ));
262305 }
263306 }
264307 SharedPreferences .Editor editor = settings .edit ();
265- editor .putStringSet (MUTED_TOPIC_KEY , new HashSet <>(mutedTopics ));
308+ editor .putStringSet (MUTED_TOPIC_KEY , new HashSet <>(this . mutedTopics ));
266309 editor .apply ();
267310 }
268311
0 commit comments