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;
import android.content.Context;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import java.util.UUID;
import bolts.Continuation;
......@@ -260,18 +265,25 @@ public class MethodCallHelper {
* Logout.
*/
public Task<Void> logout() {
return call("logout", TIMEOUT_MS).onSuccessTask(task ->
realmHelper.executeTransaction(realm -> {
realm.delete(RealmSession.class);
//check whether the server list is empty
if (!ConnectivityManager.getInstance(context).getServerList().isEmpty()){
//for each server in serverList -> remove the server
for (ServerInfo server: ConnectivityManager.getInstance(context).getServerList()) {
ConnectivityManager.getInstance(context.getApplicationContext()).removeServer(server.getHostname());
}
}
return null;
}));
return call("logout", TIMEOUT_MS).onSuccessTask(task -> {
Context appContext = context.getApplicationContext();
RocketChatCache rocketChatCache = new RocketChatCache(appContext);
String currentHostname = rocketChatCache.getSelectedServerHostname();
RealmHelper currentRealmHelper = RealmStore.getOrCreate(currentHostname);
return currentRealmHelper.executeTransaction(realm -> {
realm.deleteAll();
ConnectivityManagerApi connectivityManagerApi = ConnectivityManager.getInstance(appContext);
connectivityManagerApi.removeServer(currentHostname);
List<ServerInfo> serverList = connectivityManagerApi.getServerList();
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;
import android.support.annotation.NonNull;
import bolts.Continuation;
import chat.rocket.core.models.RoomSidebar;
import io.reactivex.Flowable;
import java.util.List;
......@@ -42,6 +43,6 @@ public interface SidebarMainContract {
void onUserOffline();
void onLogout();
void onLogout(Continuation continuation);
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import bolts.Task;
import chat.rocket.android.BuildConfig;
import chat.rocket.android.R;
import chat.rocket.android.RocketChatCache;
......@@ -302,10 +303,15 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
private void setupLogoutButton() {
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();
// 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;
import java.util.ArrayList;
import java.util.List;
import bolts.Continuation;
import bolts.Task;
import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.MethodCallHelper;
......@@ -131,8 +133,14 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
}
@Override
public void onLogout() {
methodCallHelper.logout().continueWith(new LogIfError());
public void onLogout(Continuation continuation) {
methodCallHelper.logout().continueWith(task -> {
if (task.isFaulted()) {
Logger.report(task.getError());
return Task.forError(task.getError());
}
return task.continueWith(continuation);
});
}
@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