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