Commit 9e92d9a2 authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix code style.

parent 8035b4a7
......@@ -75,15 +75,16 @@ public class MainActivity extends AbstractAuthedActivity {
.subscribe(RxView.visibility(findViewById(R.id.user_action_container)));
}
private RealmListObserver<RoomSubscription> roomsObserver = new RealmListObserver<RoomSubscription>() {
@Override protected RealmResults<RoomSubscription> queryItems(Realm realm) {
return realm.where(RoomSubscription.class).findAll();
}
@Override protected void onCollectionChanged(List<RoomSubscription> list) {
roomListManager.setRooms(list);
}
};
private RealmListObserver<RoomSubscription> roomsObserver =
new RealmListObserver<RoomSubscription>() {
@Override protected RealmResults<RoomSubscription> queryItems(Realm realm) {
return realm.where(RoomSubscription.class).findAll();
}
@Override protected void onCollectionChanged(List<RoomSubscription> list) {
roomListManager.setRooms(list);
}
};
private void showRoomFragment(String roomId) {
showFragment(RoomFragment.create(roomId));
......
......@@ -44,15 +44,16 @@ public class RoomFragment extends AbstractChatRoomFragment {
}
private RealmObjectObserver<RoomSubscription> roomObserver = new RealmObjectObserver<RoomSubscription>() {
@Override protected RealmQuery<RoomSubscription> query(Realm realm) {
return realm.where(RoomSubscription.class).equalTo("rid", roomId);
}
private RealmObjectObserver<RoomSubscription> roomObserver =
new RealmObjectObserver<RoomSubscription>() {
@Override protected RealmQuery<RoomSubscription> query(Realm realm) {
return realm.where(RoomSubscription.class).equalTo("rid", roomId);
}
@Override protected void onChange(RoomSubscription roomSubscription) {
onRenderRoom(roomSubscription);
}
};
@Override protected void onChange(RoomSubscription roomSubscription) {
onRenderRoom(roomSubscription);
}
};
private void onRenderRoom(RoomSubscription roomSubscription) {
activityToolbar.setTitle(roomSubscription.getName());
......
......@@ -154,41 +154,31 @@ public class MethodCallHelper {
}
private Task<Long> getRoomSubscriptionRecursive(long timestamp) {
return call("subscriptions/get", param -> param.put("$date", timestamp), task -> {
JSONObject result = task.getResult();
long nextTimestamp = 0;
try {
nextTimestamp = result.getJSONArray("remove")
.getJSONObject(0).getJSONObject("_deletedAt").getLong("$date");
} catch (JSONException exception) {
return getObjectRecursive("subscriptions", updatedRooms -> {
for (int i = 0; i < updatedRooms.length(); i++) {
updatedRooms.getJSONObject(i).put("serverConfigId", serverConfigId);
}
}, timestamp);
}
try {
JSONArray updatedRooms = result.getJSONArray("update");
for (int i = 0; i < updatedRooms.length(); i++) {
updatedRooms.getJSONObject(i).put("serverConfigId", serverConfigId);
}
Task<Void> saveToDB = RealmHelperBolts.executeTransaction(realm -> {
realm.createOrUpdateAllFromJson(RoomSubscription.class, result.getJSONArray("update"));
return null;
});
if (nextTimestamp > 0 && (timestamp == 0 || nextTimestamp < timestamp)) {
final long _next = nextTimestamp;
return saveToDB.onSuccessTask(_task -> getRoomSubscriptionRecursive(_next));
} else {
return saveToDB.onSuccessTask(_task -> Task.forResult(0L));
}
} catch (JSONException exception) {
return Task.forError(exception);
private Task<Long> getRoomRecursive(long timestamp) {
return getObjectRecursive("rooms", updatedRooms -> {
for (int i = 0; i < updatedRooms.length(); i++) {
JSONObject roomJson = updatedRooms.getJSONObject(i);
String rid = roomJson.getString("_id");
roomJson.put("rid", rid)
.put("serverConfigId", serverConfigId)
.remove("_id");
}
});
}, timestamp);
}
private Task<Long> getRoomRecursive(long timestamp) {
return call("rooms/get", param -> param.put("$date", timestamp), task -> {
private interface Customizer {
void customizeResult(JSONArray updatedRooms) throws JSONException;
}
private Task<Long> getObjectRecursive(String objName, Customizer customizer, long timestamp) {
return call(objName + "/get", param -> param.put("$date", timestamp), task -> {
JSONObject result = task.getResult();
long nextTimestamp = 0;
......@@ -196,17 +186,12 @@ public class MethodCallHelper {
nextTimestamp = result.getJSONArray("remove")
.getJSONObject(0).getJSONObject("_deletedAt").getLong("$date");
} catch (JSONException exception) {
// keep nextTimestamp = 0
}
try {
JSONArray updatedRooms = result.getJSONArray("update");
for (int i = 0; i < updatedRooms.length(); i++) {
JSONObject roomJson = updatedRooms.getJSONObject(i);
String rid = roomJson.getString("_id");
roomJson.put("rid", rid)
.put("serverConfigId", serverConfigId)
.remove("_id");
}
customizer.customizeResult(updatedRooms);
Task<Void> saveToDB = RealmHelperBolts.executeTransaction(realm -> {
realm.createOrUpdateAllFromJson(RoomSubscription.class, result.getJSONArray("update"));
......@@ -215,7 +200,7 @@ public class MethodCallHelper {
if (nextTimestamp > 0 && (timestamp == 0 || nextTimestamp < timestamp)) {
final long _next = nextTimestamp;
return saveToDB.onSuccessTask(_task -> getRoomRecursive(_next));
return saveToDB.onSuccessTask(_task -> getObjectRecursive(objName, customizer, _next));
} else {
return saveToDB.onSuccessTask(_task -> Task.forResult(0L));
}
......
......@@ -43,7 +43,8 @@ public class RoomListManager {
String type = roomSubscription.getT();
if (RoomSubscription.TYPE_CHANNEL.equals(type) || RoomSubscription.TYPE_PRIVATE.equals(type)) {
if (RoomSubscription.TYPE_CHANNEL.equals(type)
|| RoomSubscription.TYPE_PRIVATE.equals(type)) {
insertOrUpdateItem(channelsContainer, roomSubscription);
removeItemIfExists(dmContainer, name);
} else if (RoomSubscription.TYPE_DIRECT_MESSAGE.equals(type)) {
......@@ -85,7 +86,8 @@ public class RoomListManager {
}
}
private void updateRoomItemView(RoomListItemView roomListItemView, RoomSubscription roomSubscription) {
private void updateRoomItemView(RoomListItemView roomListItemView,
RoomSubscription roomSubscription) {
roomListItemView
.setRoomId(roomSubscription.getRid())
.setRoomName(roomSubscription.getName())
......
......@@ -4,6 +4,7 @@ import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
/**
* Login-User's email
*/
public class Email extends RealmObject {
@PrimaryKey private String address;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment