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

Commit 93ac4ef

Browse files
koziodigitalincniftynei
authored andcommitted
Backend fetch and Login are working now.
Finished removal of async login Possible race condition occuring with ViewBinding Fixed mapping issue. Issue has been the key not being properly set Issues involving primary keys not staying consistent resolved. Moving into get events.
1 parent 1e194d5 commit 93ac4ef

26 files changed

+897
-468
lines changed

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

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
import android.util.Log;
1111

1212
import 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;
1322
import com.j256.ormlite.dao.Dao;
1423
import com.j256.ormlite.dao.RuntimeExceptionDao;
1524
import com.j256.ormlite.misc.TransactionManager;
@@ -22,15 +31,17 @@
2231
import com.zulip.android.models.Stream;
2332
import com.zulip.android.networking.AsyncUnreadMessagesUpdate;
2433
import 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;
2537
import com.zulip.android.service.ZulipServices;
2638
import com.zulip.android.util.ZLog;
2739

28-
import org.json.JSONArray;
29-
import org.json.JSONException;
30-
3140
import java.io.IOException;
41+
import java.lang.reflect.Type;
3242
import java.sql.SQLException;
3343
import java.util.HashSet;
44+
import java.util.List;
3445
import java.util.Map;
3546
import java.util.Queue;
3647
import java.util.Set;
@@ -41,7 +52,6 @@
4152

4253
import io.fabric.sdk.android.Fabric;
4354
import okhttp3.OkHttpClient;
44-
import okhttp3.Request;
4555
import okhttp3.ResponseBody;
4656
import okhttp3.logging.HttpLoggingInterceptor;
4757
import 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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.zulip.android.activities;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
5+
import com.zulip.android.ZulipApp;
6+
import com.zulip.android.service.ZulipServices;
7+
8+
/**
9+
* Created by patrykpoborca on 8/25/16.
10+
*/
11+
12+
public class BaseActivity extends AppCompatActivity {
13+
14+
protected ZulipApp getApp() {
15+
return ZulipApp.get();
16+
}
17+
18+
protected ZulipServices getServices() {
19+
return getApp().getZulipServices();
20+
}
21+
}

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.zulip.android.activities;
22

3-
import android.app.Activity;
43
import android.app.ProgressDialog;
54
import android.content.Intent;
65
import android.os.Bundle;
@@ -9,7 +8,8 @@
98

109
import com.zulip.android.R;
1110
import com.zulip.android.networking.AsyncDevGetEmails;
12-
import com.zulip.android.networking.AsyncLogin;
11+
import com.zulip.android.networking.response.LoginResponse;
12+
import com.zulip.android.networking.util.DefaultCallback;
1313
import com.zulip.android.util.AuthClickListener;
1414
import com.zulip.android.util.ZLog;
1515

@@ -20,10 +20,13 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23+
import retrofit2.Call;
24+
import retrofit2.Response;
25+
2326
/**
2427
* Activity where the Emails for the DevAuthBackend are displayed.
2528
*/
26-
public class DevAuthActivity extends Activity {
29+
public class DevAuthActivity extends BaseActivity {
2730
private RecyclerView recyclerView;
2831
private ProgressDialog connectionProgressDialog;
2932

@@ -58,9 +61,19 @@ protected void onCreate(Bundle savedInstanceState) {
5861
authEmailAdapter.setOnItemClickListener(new AuthClickListener() {
5962
@Override
6063
public void onItemClick(String email) {
61-
AsyncLogin asyncLogin = new AsyncLogin(DevAuthActivity.this, email, null, true);
62-
asyncLogin.execute();
63-
connectionProgressDialog.show();
64+
if(email.contains("@")) {
65+
getApp().setEmail(email);
66+
}
67+
getServices()
68+
.loginDEV(email)
69+
.enqueue(new DefaultCallback<LoginResponse>() {
70+
@Override
71+
public void onSuccess(Call<LoginResponse> call, Response<LoginResponse> response) {
72+
super.onSuccess(call, response);
73+
getApp().setLoggedInApiKey(response.body().getApiKey());
74+
openHome();
75+
}
76+
});
6477
}
6578
});
6679
recyclerView.setLayoutManager(new LinearLayoutManager(DevAuthActivity.this) {

0 commit comments

Comments
 (0)