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