Commit cf96cd2f authored by Leonardo Aramaki's avatar Leonardo Aramaki

Delete all offline data prior to calling the 'logout' method

parent a02a2f2e
...@@ -20,6 +20,12 @@ import chat.rocket.persistence.realm.RocketChatPersistenceRealm; ...@@ -20,6 +20,12 @@ import chat.rocket.persistence.realm.RocketChatPersistenceRealm;
*/ */
public class RocketChatApplication extends MultiDexApplication { public class RocketChatApplication extends MultiDexApplication {
private static RocketChatApplication instance;
public static RocketChatApplication getInstance() {
return instance;
}
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
...@@ -37,5 +43,7 @@ public class RocketChatApplication extends MultiDexApplication { ...@@ -37,5 +43,7 @@ public class RocketChatApplication extends MultiDexApplication {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
} }
instance = this;
} }
} }
\ No newline at end of file
...@@ -252,6 +252,12 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -252,6 +252,12 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
R.string.server_config_activity_authenticating, Snackbar.LENGTH_INDEFINITE)); R.string.server_config_activity_authenticating, Snackbar.LENGTH_INDEFINITE));
} }
public void showLogoutMessage() {
statusTicker.updateStatus(StatusTicker.STATUS_LOGGING_OUT,
Snackbar.make(findViewById(getLayoutContainerForFragment()),
"Logging Out...", Snackbar.LENGTH_INDEFINITE));
}
@Override @Override
public void showConnectionOk() { public void showConnectionOk() {
statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null); statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null);
...@@ -309,6 +315,11 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -309,6 +315,11 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
} }
} }
@DebugLog
public void hideLogoutMessage() {
statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null);
}
@DebugLog @DebugLog
public void onLogout() { public void onLogout() {
if (new RocketChatCache(getApplicationContext()).getSelectedServerHostname() == null) { if (new RocketChatCache(getApplicationContext()).getSelectedServerHostname() == null) {
...@@ -319,12 +330,8 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -319,12 +330,8 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
} }
@DebugLog @DebugLog
public void beforeLogoutCleanup() { public void cleanUpBeforeLogout() {
presenter.beforeLogout(); presenter.beforeLogoutCleanUp();
}
public void onFailedLogout() {
onHostnameUpdated();
} }
//TODO: consider this class to define in layouthelper for more complicated operation. //TODO: consider this class to define in layouthelper for more complicated operation.
...@@ -332,6 +339,7 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -332,6 +339,7 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
public static final int STATUS_DISMISS = 0; public static final int STATUS_DISMISS = 0;
public static final int STATUS_CONNECTION_ERROR = 1; public static final int STATUS_CONNECTION_ERROR = 1;
public static final int STATUS_TOKEN_LOGIN = 2; public static final int STATUS_TOKEN_LOGIN = 2;
public static final int STATUS_LOGGING_OUT = 3;
private int status; private int status;
private Snackbar snackbar; private Snackbar snackbar;
......
...@@ -38,6 +38,6 @@ public interface MainContract { ...@@ -38,6 +38,6 @@ public interface MainContract {
void loadSignedInServers(String hostname); void loadSignedInServers(String hostname);
void beforeLogout(); void beforeLogoutCleanUp();
} }
} }
...@@ -134,7 +134,7 @@ public class MainPresenter extends BasePresenter<MainContract.View> ...@@ -134,7 +134,7 @@ public class MainPresenter extends BasePresenter<MainContract.View>
} }
@Override @Override
public void beforeLogout() { public void beforeLogoutCleanUp() {
clearSubscriptions(); clearSubscriptions();
} }
......
...@@ -7,20 +7,16 @@ import org.json.JSONArray; ...@@ -7,20 +7,16 @@ 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;
import bolts.Task; import bolts.Task;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.helper.CheckSum; import chat.rocket.android.helper.CheckSum;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.service.ConnectivityManager; import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.android.service.ConnectivityManagerApi;
import chat.rocket.android.service.DDPClientRef; import chat.rocket.android.service.DDPClientRef;
import chat.rocket.android_ddp.DDPClientCallback; import chat.rocket.android_ddp.DDPClientCallback;
import chat.rocket.core.SyncState; import chat.rocket.core.SyncState;
import chat.rocket.core.models.ServerInfo;
import chat.rocket.persistence.realm.RealmHelper; import chat.rocket.persistence.realm.RealmHelper;
import chat.rocket.persistence.realm.RealmStore; import chat.rocket.persistence.realm.RealmStore;
import chat.rocket.persistence.realm.models.ddp.RealmMessage; import chat.rocket.persistence.realm.models.ddp.RealmMessage;
...@@ -265,24 +261,10 @@ public class MethodCallHelper { ...@@ -265,24 +261,10 @@ public class MethodCallHelper {
*/ */
public Task<Void> logout() { public Task<Void> logout() {
return call("logout", TIMEOUT_MS).onSuccessTask(task -> { return call("logout", TIMEOUT_MS).onSuccessTask(task -> {
Context appContext = context.getApplicationContext(); if (task.isFaulted()) {
RocketChatCache rocketChatCache = new RocketChatCache(appContext); return Task.forError(task.getError());
String currentHostname = rocketChatCache.getSelectedServerHostname(); }
RealmHelper currentRealmHelper = RealmStore.getOrCreate(currentHostname); return null;
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.removeSelectedRoomId(currentHostname);
rocketChatCache.setSelectedServerHostname(newHostname);
return null;
});
}); });
} }
......
...@@ -23,6 +23,8 @@ public interface SidebarMainContract { ...@@ -23,6 +23,8 @@ public interface SidebarMainContract {
void filterRoomSidebarList(CharSequence term); void filterRoomSidebarList(CharSequence term);
void show(User user); void show(User user);
void onLogoutCleanUp();
} }
interface Presenter extends BaseContract.Presenter<View> { interface Presenter extends BaseContract.Presenter<View> {
...@@ -44,5 +46,7 @@ public interface SidebarMainContract { ...@@ -44,5 +46,7 @@ public interface SidebarMainContract {
void onUserOffline(); void onUserOffline();
void onLogout(Continuation continuation); void onLogout(Continuation continuation);
void cleanUpBeforeLogout();
} }
} }
\ No newline at end of file
...@@ -321,26 +321,32 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -321,26 +321,32 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
adapter.setRoomListHeaders(roomListHeaders); adapter.setRoomListHeaders(roomListHeaders);
} }
private void setupLogoutButton() { @Override
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> { public void onLogoutCleanUp() {
closeUserActionContainer(); Activity activity = getActivity();
Activity mainActivity = getActivity(); if (activity != null && activity instanceof MainActivity) {
if (mainActivity != null && mainActivity instanceof MainActivity) { ((MainActivity) activity).hideLogoutMessage();
((MainActivity) mainActivity).beforeLogoutCleanup(); ((MainActivity) activity).onLogout();
}
presenter.onLogout(task -> { presenter.onLogout(task -> {
if (task.isFaulted()) { if (task.isFaulted()) {
if (mainActivity != null && mainActivity instanceof MainActivity) {
((MainActivity) mainActivity).onFailedLogout();
}
return Task.forError(task.getError()); return Task.forError(task.getError());
} }
if (mainActivity != null && mainActivity instanceof MainActivity) {
((MainActivity) mainActivity).onLogout();
}
return null; return null;
}); });
}
}
private void setupLogoutButton() {
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> {
closeUserActionContainer();
final Activity activity = getActivity();
if (activity != null && activity instanceof MainActivity) {
((MainActivity) activity).showLogoutMessage();
// Clear subscriptions.
((MainActivity) activity).cleanUpBeforeLogout();
}
// Clear relative data and set new hostname if any.
presenter.cleanUpBeforeLogout();
}); });
} }
......
...@@ -9,20 +9,26 @@ import java.util.List; ...@@ -9,20 +9,26 @@ import java.util.List;
import bolts.Continuation; import bolts.Continuation;
import bolts.Task; import bolts.Task;
import chat.rocket.android.BackgroundLooper; import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.RocketChatApplication;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.AbsoluteUrlHelper; import chat.rocket.android.helper.AbsoluteUrlHelper;
import chat.rocket.android.helper.LogIfError; import chat.rocket.android.helper.LogIfError;
import chat.rocket.android.helper.Logger; import chat.rocket.android.helper.Logger;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.android.service.ConnectivityManagerApi;
import chat.rocket.android.shared.BasePresenter; import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.RoomInteractor; import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.models.Room; import chat.rocket.core.models.Room;
import chat.rocket.core.models.RoomSidebar; import chat.rocket.core.models.RoomSidebar;
import chat.rocket.core.models.ServerInfo;
import chat.rocket.core.models.Spotlight; import chat.rocket.core.models.Spotlight;
import chat.rocket.core.models.User; import chat.rocket.core.models.User;
import chat.rocket.core.repositories.SpotlightRepository; import chat.rocket.core.repositories.SpotlightRepository;
import chat.rocket.core.repositories.UserRepository; import chat.rocket.core.repositories.UserRepository;
import chat.rocket.persistence.realm.RealmHelper;
import chat.rocket.persistence.realm.RealmStore;
import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository; import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
...@@ -143,6 +149,27 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -143,6 +149,27 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
}); });
} }
@Override
public void cleanUpBeforeLogout() {
String currentHostname = rocketChatCache.getSelectedServerHostname();
RealmHelper realmHelper = RealmStore.getOrCreate(currentHostname);
realmHelper.executeTransaction(realm -> {
realm.deleteAll();
ConnectivityManagerApi connectivityManagerApi = ConnectivityManager.getInstance(RocketChatApplication.getInstance());
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.removeSelectedRoomId(currentHostname);
rocketChatCache.setSelectedServerHostname(newHostname);
view.onLogoutCleanUp();
return null;
});
}
@Override @Override
public void disposeSubscriptions() { public void disposeSubscriptions() {
clearSubscriptions(); clearSubscriptions();
......
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