Commit ab60e28a authored by Leonardo Aramaki's avatar Leonardo Aramaki

Logout method does not remove all servers anymore, but instead it removes...

Logout method does not remove all servers anymore, but instead it removes solely the current hostname which the client is connected
parent a8f5de0f
...@@ -2,11 +2,16 @@ package chat.rocket.android.api; ...@@ -2,11 +2,16 @@ package chat.rocket.android.api;
import android.content.Context; import android.content.Context;
import android.util.Patterns; import android.util.Patterns;
import chat.rocket.android.RocketChatApplication;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.service.ConnectivityManagerApi;
import chat.rocket.persistence.realm.models.ddp.RealmSpotlight; import chat.rocket.persistence.realm.models.ddp.RealmSpotlight;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import bolts.Continuation; import bolts.Continuation;
...@@ -260,18 +265,25 @@ public class MethodCallHelper { ...@@ -260,18 +265,25 @@ public class MethodCallHelper {
* Logout. * Logout.
*/ */
public Task<Void> logout() { public Task<Void> logout() {
return call("logout", TIMEOUT_MS).onSuccessTask(task -> return call("logout", TIMEOUT_MS).onSuccessTask(task -> {
realmHelper.executeTransaction(realm -> { Context appContext = context.getApplicationContext();
realm.delete(RealmSession.class); RocketChatCache rocketChatCache = new RocketChatCache(appContext);
//check whether the server list is empty String currentHostname = rocketChatCache.getSelectedServerHostname();
if (!ConnectivityManager.getInstance(context).getServerList().isEmpty()){ RealmHelper currentRealmHelper = RealmStore.getOrCreate(currentHostname);
//for each server in serverList -> remove the server return currentRealmHelper.executeTransaction(realm -> {
for (ServerInfo server: ConnectivityManager.getInstance(context).getServerList()) { realm.deleteAll();
ConnectivityManager.getInstance(context.getApplicationContext()).removeServer(server.getHostname()); ConnectivityManagerApi connectivityManagerApi = ConnectivityManager.getInstance(appContext);
} connectivityManagerApi.removeServer(currentHostname);
} List<ServerInfo> serverList = connectivityManagerApi.getServerList();
return null; String newHostname = null;
})); if (serverList != null && serverList.size() > 0) {
newHostname = serverList.get(0).getHostname();
}
rocketChatCache.removeHostname(currentHostname);
rocketChatCache.setSelectedServerHostname(newHostname);
return null;
});
});
} }
/** /**
......
...@@ -2,6 +2,7 @@ package chat.rocket.android.fragment.sidebar; ...@@ -2,6 +2,7 @@ package chat.rocket.android.fragment.sidebar;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import bolts.Continuation;
import chat.rocket.core.models.RoomSidebar; import chat.rocket.core.models.RoomSidebar;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import java.util.List; import java.util.List;
...@@ -42,6 +43,6 @@ public interface SidebarMainContract { ...@@ -42,6 +43,6 @@ public interface SidebarMainContract {
void onUserOffline(); void onUserOffline();
void onLogout(); void onLogout(Continuation continuation);
} }
} }
\ No newline at end of file
...@@ -12,6 +12,7 @@ import android.view.View; ...@@ -12,6 +12,7 @@ import android.view.View;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.TextView; import android.widget.TextView;
import bolts.Task;
import chat.rocket.android.BuildConfig; import chat.rocket.android.BuildConfig;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
...@@ -302,10 +303,15 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -302,10 +303,15 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
private void setupLogoutButton() { private void setupLogoutButton() {
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> { rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> {
presenter.onLogout(); presenter.onLogout(task -> {
if (task.isFaulted()) {
return Task.forError(task.getError());
}
// destroy Activity on logout to be able to recreate most of the environment
this.getActivity().recreate();
return null;
});
closeUserActionContainer(); closeUserActionContainer();
// destroy Activity on logout to be able to recreate most of the environment
this.getActivity().finish();
}); });
} }
......
...@@ -6,6 +6,8 @@ import android.support.v4.util.Pair; ...@@ -6,6 +6,8 @@ import android.support.v4.util.Pair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import bolts.Continuation;
import bolts.Task;
import chat.rocket.android.BackgroundLooper; import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
...@@ -131,8 +133,14 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -131,8 +133,14 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
} }
@Override @Override
public void onLogout() { public void onLogout(Continuation continuation) {
methodCallHelper.logout().continueWith(new LogIfError()); methodCallHelper.logout().continueWith(task -> {
if (task.isFaulted()) {
Logger.report(task.getError());
return Task.forError(task.getError());
}
return task.continueWith(continuation);
});
} }
@Override @Override
......
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