Commit 2ae619c8 authored by Yusuke Iwaki's avatar Yusuke Iwaki

implement logout.

parent 9666e58b
......@@ -63,6 +63,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
private void onRenderServerConfigSession(Session session) {
if (session == null) {
showFragment(new LoginFragment());
return;
}
......
......@@ -59,6 +59,9 @@ public class MethodCallHelper {
}
String errMessage = new JSONObject(errMessageJson).getString("message");
return Task.forError(new Exception(errMessage));
} else if (exception instanceof DDPClientCallback.RPC.Error) {
String errMessage = ((DDPClientCallback.RPC.Error) exception).error.getString("message");
return Task.forError(new Exception(errMessage));
} else if (exception instanceof DDPClientCallback.RPC.Timeout) {
return Task.forError(new MethodCall.Timeout());
} else {
......@@ -158,14 +161,24 @@ public class MethodCallHelper {
.put("resume", token)
)).onSuccessTask(CONVERT_TO_JSON_OBJECT)
.onSuccessTask(task -> Task.forResult(task.getResult().getString("token")))
.onSuccessTask(this::saveToken);
.onSuccessTask(this::saveToken)
.continueWithTask(task -> {
if (task.isFaulted()) {
Session.logError(realmHelper, task.getError());
}
return task;
});
}
/**
* Logout.
*/
public Task<String> logout() {
return call("logout", TIMEOUT_MS);
public Task<Void> logout() {
return call("logout", TIMEOUT_MS).onSuccessTask(task ->
realmHelper.executeTransaction(realm -> {
realm.delete(Session.class);
return null;
}));
}
/**
......
......@@ -51,7 +51,6 @@ public class RetryLoginFragment extends AbstractServerConfigFragment {
if (task.isFaulted()) {
view.setEnabled(true);
waitingView.setVisibility(View.GONE);
Session.logError(RealmStore.get(serverConfigId), task.getError());
}
return null;
});
......
......@@ -31,6 +31,7 @@ public class SidebarMainFragment extends AbstractFragment {
private String hostname;
private RealmListObserver<RoomSubscription> roomsObserver;
private RealmObjectObserver<User> currentUserObserver;
private MethodCallHelper methodCallHelper;
public SidebarMainFragment() {
}
......@@ -68,6 +69,8 @@ public class SidebarMainFragment extends AbstractFragment {
currentUserObserver = realmHelper
.createObjectObserver(realm -> realm.where(User.class).isNotEmpty("emails"))
.setOnUpdateListener(this::onRenderCurrentUser);
methodCallHelper = new MethodCallHelper(serverConfigId);
}
}
}
......@@ -87,6 +90,7 @@ public class SidebarMainFragment extends AbstractFragment {
setupUserActionToggle();
setupUserStatusButtons();
setupLogoutButton();
roomListManager = new RoomListManager(
(LinearLayout) rootView.findViewById(R.id.channels_container),
......@@ -111,24 +115,19 @@ public class SidebarMainFragment extends AbstractFragment {
}
private void setupUserStatusButtons() {
rootView.findViewById(R.id.btn_status_online).setOnClickListener(view -> {
updateCurrentUserStatus(User.STATUS_ONLINE);
});
rootView.findViewById(R.id.btn_status_away).setOnClickListener(view -> {
updateCurrentUserStatus(User.STATUS_AWAY);
});
rootView.findViewById(R.id.btn_status_busy).setOnClickListener(view -> {
updateCurrentUserStatus(User.STATUS_BUSY);
});
rootView.findViewById(R.id.btn_status_invisible).setOnClickListener(view -> {
updateCurrentUserStatus(User.STATUS_OFFLINE);
});
rootView.findViewById(R.id.btn_status_online).setOnClickListener(view ->
updateCurrentUserStatus(User.STATUS_ONLINE));
rootView.findViewById(R.id.btn_status_away).setOnClickListener(view ->
updateCurrentUserStatus(User.STATUS_AWAY));
rootView.findViewById(R.id.btn_status_busy).setOnClickListener(view ->
updateCurrentUserStatus(User.STATUS_BUSY));
rootView.findViewById(R.id.btn_status_invisible).setOnClickListener(view ->
updateCurrentUserStatus(User.STATUS_OFFLINE));
}
private void updateCurrentUserStatus(String status) {
if (!TextUtils.isEmpty(serverConfigId)) {
new MethodCallHelper(serverConfigId).setUserStatus(status)
.continueWith(new LogcatIfError());
if (methodCallHelper != null) {
methodCallHelper.setUserStatus(status).continueWith(new LogcatIfError());
}
}
......@@ -141,6 +140,14 @@ public class SidebarMainFragment extends AbstractFragment {
}
}
private void setupLogoutButton() {
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> {
if (methodCallHelper != null) {
methodCallHelper.logout().continueWith(new LogcatIfError());
}
});
}
@Override public void onResume() {
super.onResume();
if (roomsObserver != null) {
......
......@@ -35,6 +35,9 @@ public class RoomListManager {
* update ViewGroups with room list.
*/
public void setRooms(List<RoomSubscription> roomSubscriptionList) {
removeDeletedItem(channelsContainer, roomSubscriptionList);
removeDeletedItem(dmContainer, roomSubscriptionList);
for (RoomSubscription roomSubscription : roomSubscriptionList) {
String name = roomSubscription.getName();
if (TextUtils.isEmpty(name)) {
......@@ -61,6 +64,25 @@ public class RoomListManager {
this.listener = listener;
}
private void removeDeletedItem(ViewGroup parent, List<RoomSubscription> roomSubscriptionList) {
for (int index = parent.getChildCount() - 1; index >= 0; index--) {
RoomListItemView roomListItemView = (RoomListItemView) parent.getChildAt(index);
final String targetRoomName = roomListItemView.getRoomName();
if (!TextUtils.isEmpty(targetRoomName)) {
boolean found = false;
for (RoomSubscription roomSubscription : roomSubscriptionList) {
if (targetRoomName.equals(roomSubscription.getName())) {
found = true;
break;
}
}
if (!found) {
parent.removeViewAt(index);
}
}
}
}
private void insertOrUpdateItem(ViewGroup parent, RoomSubscription roomSubscription) {
final String roomName = roomSubscription.getName();
......
package chat.rocket.android.model.internal;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.realm_helper.RealmHelper;
import hugo.weaving.DebugLog;
import io.realm.RealmObject;
......@@ -53,12 +54,19 @@ public class Session extends RealmObject {
* Log the server connection is lost due to soem exception.
*/
@DebugLog public static void logError(RealmHelper realmHelper, Exception exception) {
// TODO: should remove token if 403.
realmHelper.executeTransaction(
realm -> realm.createOrUpdateObjectFromJson(Session.class, new JSONObject()
.put("sessionId", Session.DEFAULT_ID)
.put("tokenVerified", false)
.put("error", exception.getMessage())))
.continueWith(new LogcatIfError());
String errString = exception.getMessage();
if (!TextUtils.isEmpty(errString) && errString.contains("[403]")) {
realmHelper.executeTransaction(realm -> {
realm.delete(Session.class);
return null;
}).continueWith(new LogcatIfError());
} else {
realmHelper.executeTransaction(
realm -> realm.createOrUpdateObjectFromJson(Session.class, new JSONObject()
.put("sessionId", Session.DEFAULT_ID)
.put("tokenVerified", false)
.put("error", errString)))
.continueWith(new LogcatIfError());
}
}
}
......@@ -4,6 +4,8 @@ import android.content.Context;
import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.internal.LoadMessageProcedure;
import chat.rocket.android.model.internal.MethodCall;
import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper;
import hugo.weaving.DebugLog;
......@@ -60,6 +62,11 @@ public class SessionObserver extends AbstractModelObserver<Session> {
}
@DebugLog private void onLogout() {
realmHelper.executeTransaction(realm -> {
// remove all tables. ONLY INTERNAL TABLES!.
realm.delete(MethodCall.class);
realm.delete(LoadMessageProcedure.class);
return null;
}).continueWith(new LogcatIfError());
}
}
......@@ -2,6 +2,7 @@ package chat.rocket.android.service.observer;
import android.content.Context;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.api.DDPClientWraper;
......@@ -32,12 +33,6 @@ public class TokenLoginObserver extends AbstractModelObserver<Session> {
}
Session session = results.get(0);
methodCall.loginWithToken(session.getToken())
.continueWith(task -> {
if (task.isFaulted()) {
Session.logError(realmHelper, task.getError());
}
return null;
});
methodCall.loginWithToken(session.getToken()).continueWith(new LogcatIfError());
}
}
......@@ -168,8 +168,4 @@ public class RealmHelper {
RealmObjectObserver.Query<T> query) {
return new RealmObjectObserver<T>(this, query);
}
public void delete() {
Realm.deleteRealm(realmConfiguration);
}
}
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