Commit 1928d269 authored by Tiago Cunha's avatar Tiago Cunha

MainActivity cleaned up

parent 7abf6706
......@@ -14,11 +14,9 @@ import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.fragment.chatroom.HomeFragment;
import chat.rocket.android.fragment.chatroom.RoomFragment;
import chat.rocket.android.fragment.sidebar.SidebarMainFragment;
import chat.rocket.android.helper.LogIfError;
import chat.rocket.core.interactors.CanCreateRoomInteractor;
import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.interactors.SessionInteractor;
import chat.rocket.core.models.User;
import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.android.widget.RoomToolbar;
import chat.rocket.persistence.realm.repositories.RealmRoomRepository;
......@@ -47,29 +45,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
statusTicker = new StatusTicker();
setupSidebar();
if (roomId == null) {
showFragment(new HomeFragment());
}
if (shouldLaunchAddServerActivity()) {
LaunchUtil.showAddServerActivity(this);
}
}
@Override
protected void onStart() {
super.onStart();
setUserOnlineIfServerAvailable();
}
@Override
protected void onResume() {
super.onResume();
if (presenter != null) {
presenter.bindView(this);
}
}
@Override
......@@ -81,27 +56,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
super.onPause();
}
@Override
protected void onStop() {
setUserAwayIfServerAvailable();
super.onStop();
}
private void setUserOnlineIfServerAvailable() {
if (hostname != null) {
new MethodCallHelper(this, hostname).setUserPresence(User.STATUS_ONLINE)
.continueWith(new LogIfError());
}
}
private void setUserAwayIfServerAvailable() {
if (hostname != null) {
new MethodCallHelper(this, hostname).setUserPresence(User.STATUS_AWAY)
.continueWith(new LogIfError());
}
}
private void setupSidebar() {
SlidingPaneLayout pane = (SlidingPaneLayout) findViewById(R.id.sliding_pane);
if (pane == null) {
......@@ -184,10 +138,14 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
presenter = new MainPresenter(
roomInteractor,
createRoomInteractor,
sessionInteractor
sessionInteractor,
new MethodCallHelper(this, hostname),
ConnectivityManager.getInstance(getApplicationContext())
);
updateSidebarMainFragment();
presenter.bindView(this);
}
private void updateSidebarMainFragment() {
......@@ -226,6 +184,11 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
}
}
@Override
public void showAddServerScreen() {
LaunchUtil.showAddServerActivity(this);
}
@Override
public void showLoginScreen() {
LaunchUtil.showLoginActivity(this, hostname);
......
......@@ -12,6 +12,8 @@ public interface MainContract {
void showUnreadCount(long roomsCount, int mentionsCount);
void showAddServerScreen();
void showLoginScreen();
void showConnectionError();
......
......@@ -8,11 +8,15 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogIfError;
import chat.rocket.android.service.ConnectivityManagerApi;
import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.CanCreateRoomInteractor;
import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.interactors.SessionInteractor;
import chat.rocket.core.models.Session;
import chat.rocket.core.models.User;
public class MainPresenter extends BasePresenter<MainContract.View>
implements MainContract.Presenter {
......@@ -20,21 +24,42 @@ public class MainPresenter extends BasePresenter<MainContract.View>
private final CanCreateRoomInteractor canCreateRoomInteractor;
private final RoomInteractor roomInteractor;
private final SessionInteractor sessionInteractor;
private final MethodCallHelper methodCallHelper;
private final ConnectivityManagerApi connectivityManagerApi;
public MainPresenter(RoomInteractor roomInteractor,
CanCreateRoomInteractor canCreateRoomInteractor,
SessionInteractor sessionInteractor) {
SessionInteractor sessionInteractor,
MethodCallHelper methodCallHelper,
ConnectivityManagerApi connectivityManagerApi) {
this.roomInteractor = roomInteractor;
this.canCreateRoomInteractor = canCreateRoomInteractor;
this.sessionInteractor = sessionInteractor;
this.methodCallHelper = methodCallHelper;
this.connectivityManagerApi = connectivityManagerApi;
}
@Override
public void bindView(@NonNull MainContract.View view) {
super.bindView(view);
view.showHome();
if (shouldLaunchAddServerActivity()) {
view.showAddServerScreen();
return;
}
subscribeToUnreadCount();
subscribeToSession();
setUserOnline();
}
@Override
public void release() {
setUserAway();
super.release();
}
@Override
......@@ -101,4 +126,18 @@ public class MainPresenter extends BasePresenter<MainContract.View>
addSubscription(subscription);
}
private void setUserOnline() {
methodCallHelper.setUserPresence(User.STATUS_ONLINE)
.continueWith(new LogIfError());
}
private void setUserAway() {
methodCallHelper.setUserPresence(User.STATUS_AWAY)
.continueWith(new LogIfError());
}
private boolean shouldLaunchAddServerActivity() {
return connectivityManagerApi.getServerList().isEmpty();
}
}
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