Commit 2b2f7499 authored by Tiago Cunha's avatar Tiago Cunha

LoginActivity

parent 2334c8a6
......@@ -8,20 +8,17 @@ import android.support.v4.app.Fragment;
import chat.rocket.android.R;
import chat.rocket.android.fragment.server_config.LoginFragment;
import chat.rocket.android.fragment.server_config.RetryLoginFragment;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.persistence.realm.models.internal.RealmSession;
import chat.rocket.persistence.realm.RealmObjectObserver;
import chat.rocket.persistence.realm.RealmStore;
import chat.rocket.core.interactors.SessionInteractor;
import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.persistence.realm.repositories.RealmSessionRepository;
/**
* Activity for Login, Sign-up, and Retry connecting...
*/
public class LoginActivity extends AbstractFragmentActivity {
public class LoginActivity extends AbstractFragmentActivity implements LoginContract.View {
public static final String KEY_HOSTNAME = "hostname";
private String hostname;
private RealmObjectObserver<RealmSession> sessionObserver;
private LoginContract.Presenter presenter;
@Override
protected int getLayoutContainerForFragment() {
......@@ -32,69 +29,38 @@ public class LoginActivity extends AbstractFragmentActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String hostname = null;
Intent intent = getIntent();
if (intent == null || intent.getExtras() == null) {
finish();
return;
}
if (intent != null && intent.getExtras() != null) {
hostname = intent.getStringExtra(KEY_HOSTNAME);
if (TextUtils.isEmpty(hostname)) {
finish();
return;
}
sessionObserver = RealmStore.get(hostname)
.createObjectObserver(RealmSession::queryDefaultSession)
.setOnUpdateListener(this::onRenderServerConfigSession);
setContentView(R.layout.simple_screen);
showFragment(new LoginFragment());
presenter = new LoginPresenter(
hostname,
new SessionInteractor(new RealmSessionRepository(hostname)),
ConnectivityManager.getInstance(getApplicationContext())
);
}
@Override
protected void onResume() {
super.onResume();
ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer();
sessionObserver.sub();
presenter.bindView(this);
}
@Override
protected void onDestroy() {
sessionObserver.unsub();
presenter.release();
super.onDestroy();
}
private void onRenderServerConfigSession(RealmSession session) {
if (session == null) {
return;
}
final String token = session.getToken();
if (!TextUtils.isEmpty(token)) {
if (TextUtils.isEmpty(session.getError())) {
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
} else {
showFragment(new RetryLoginFragment());
}
return;
}
}
@Override
protected void showFragment(Fragment fragment) {
injectHostnameArgTo(fragment);
private void showFragment(Fragment fragment, String hostname) {
setContentView(R.layout.simple_screen);
injectHostnameArgTo(fragment, hostname);
super.showFragment(fragment);
}
@Override
protected void showFragmentWithBackStack(Fragment fragment) {
injectHostnameArgTo(fragment);
super.showFragmentWithBackStack(fragment);
}
private void injectHostnameArgTo(Fragment fragment) {
private void injectHostnameArgTo(Fragment fragment, String hostname) {
Bundle args = fragment.getArguments();
if (args == null) {
args = new Bundle();
......@@ -107,4 +73,20 @@ public class LoginActivity extends AbstractFragmentActivity {
protected void onBackPressedNotHandled() {
moveTaskToBack(true);
}
@Override
public void showLogin(String hostname) {
showFragment(new LoginFragment(), hostname);
}
@Override
public void showRetryLogin(String hostname) {
showFragment(new RetryLoginFragment(), hostname);
}
@Override
public void close() {
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}
package chat.rocket.android.activity;
import chat.rocket.android.shared.BaseContract;
public interface LoginContract {
interface View extends BaseContract.View {
void showLogin(String hostname);
void showRetryLogin(String hostname);
void close();
}
interface Presenter extends BaseContract.Presenter<View> {
}
}
package chat.rocket.android.activity;
import android.support.annotation.NonNull;
import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.service.ConnectivityManagerApi;
import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.SessionInteractor;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
public class LoginPresenter extends BasePresenter<LoginContract.View>
implements LoginContract.Presenter {
private final String hostname;
private final SessionInteractor sessionInteractor;
private final ConnectivityManagerApi connectivityManagerApi;
public LoginPresenter(String hostname,
SessionInteractor sessionInteractor,
ConnectivityManagerApi connectivityManagerApi) {
this.hostname = hostname;
this.sessionInteractor = sessionInteractor;
this.connectivityManagerApi = connectivityManagerApi;
}
@Override
public void bindView(@NonNull LoginContract.View view) {
super.bindView(view);
connectivityManagerApi.keepAliveServer();
if (hostname == null || hostname.length() == 0) {
view.close();
return;
}
loadDefaultSession();
}
private void loadDefaultSession() {
final Subscription subscription = sessionInteractor.getSessionState()
.distinctUntilChanged()
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(state -> {
switch (state) {
case UNAVAILABLE:
view.showLogin(hostname);
break;
case INVALID:
view.showRetryLogin(hostname);
break;
case VALID:
view.close();
}
});
addSubscription(subscription);
}
}
......@@ -28,21 +28,19 @@ public class RealmSessionRepository extends RealmRepository implements SessionRe
return Observable.just(null);
}
final RealmSession realmSession =
realm.where(RealmSession.class).equalTo(RealmSession.ID, RealmSession.DEFAULT_ID)
.findFirst();
if (realmSession == null) {
realm.close();
return Observable.just(null);
}
return realmSession
return realm.where(RealmSession.class)
.equalTo(RealmSession.ID, RealmSession.DEFAULT_ID)
.findAll()
.<RealmSession>asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(it -> it != null && it.isLoaded() && it.isValid())
.map(RealmSession::asSession);
.map(realmSessions -> {
if (realmSessions.size() == 0) {
return null;
}
return realmSessions.get(0).asSession();
});
});
}
}
package chat.rocket.core.interactors;
import chat.rocket.core.models.Session;
import chat.rocket.core.repositories.SessionRepository;
import rx.Observable;
public class SessionInteractor {
private final SessionRepository sessionRepository;
public SessionInteractor(SessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}
public Observable<Session.State> getSessionState() {
return sessionRepository.getDefault()
.map(this::getStateFrom);
}
private Session.State getStateFrom(Session session) {
if (session == null) {
return Session.State.UNAVAILABLE;
}
final String token = session.getToken();
if (token == null || token.length() == 0) {
return Session.State.UNAVAILABLE;
}
final String error = session.getError();
if (error == null || error.length() == 0) {
return Session.State.VALID;
}
return Session.State.INVALID;
}
}
......@@ -7,6 +7,8 @@ import javax.annotation.Nullable;
@AutoValue
public abstract class Session {
public enum State {UNAVAILABLE, INVALID, VALID}
public abstract int getSessionId();
@Nullable
......
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