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

LoginActivity

parent 2334c8a6
...@@ -8,20 +8,17 @@ import android.support.v4.app.Fragment; ...@@ -8,20 +8,17 @@ import android.support.v4.app.Fragment;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.fragment.server_config.LoginFragment; import chat.rocket.android.fragment.server_config.LoginFragment;
import chat.rocket.android.fragment.server_config.RetryLoginFragment; import chat.rocket.android.fragment.server_config.RetryLoginFragment;
import chat.rocket.android.helper.TextUtils; import chat.rocket.core.interactors.SessionInteractor;
import chat.rocket.persistence.realm.models.internal.RealmSession;
import chat.rocket.persistence.realm.RealmObjectObserver;
import chat.rocket.persistence.realm.RealmStore;
import chat.rocket.android.service.ConnectivityManager; import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.persistence.realm.repositories.RealmSessionRepository;
/** /**
* Activity for Login, Sign-up, and Retry connecting... * 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"; public static final String KEY_HOSTNAME = "hostname";
private String hostname; private LoginContract.Presenter presenter;
private RealmObjectObserver<RealmSession> sessionObserver;
@Override @Override
protected int getLayoutContainerForFragment() { protected int getLayoutContainerForFragment() {
...@@ -32,69 +29,38 @@ public class LoginActivity extends AbstractFragmentActivity { ...@@ -32,69 +29,38 @@ public class LoginActivity extends AbstractFragmentActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
String hostname = null;
Intent intent = getIntent(); Intent intent = getIntent();
if (intent == null || intent.getExtras() == null) { if (intent != null && intent.getExtras() != null) {
finish(); hostname = intent.getStringExtra(KEY_HOSTNAME);
return;
} }
hostname = intent.getStringExtra(KEY_HOSTNAME); presenter = new LoginPresenter(
if (TextUtils.isEmpty(hostname)) { hostname,
finish(); new SessionInteractor(new RealmSessionRepository(hostname)),
return; ConnectivityManager.getInstance(getApplicationContext())
} );
sessionObserver = RealmStore.get(hostname)
.createObjectObserver(RealmSession::queryDefaultSession)
.setOnUpdateListener(this::onRenderServerConfigSession);
setContentView(R.layout.simple_screen);
showFragment(new LoginFragment());
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer(); presenter.bindView(this);
sessionObserver.sub();
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
sessionObserver.unsub(); presenter.release();
super.onDestroy(); super.onDestroy();
} }
private void onRenderServerConfigSession(RealmSession session) { private void showFragment(Fragment fragment, String hostname) {
if (session == null) { setContentView(R.layout.simple_screen);
return; injectHostnameArgTo(fragment, hostname);
}
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);
super.showFragment(fragment); super.showFragment(fragment);
} }
@Override private void injectHostnameArgTo(Fragment fragment, String hostname) {
protected void showFragmentWithBackStack(Fragment fragment) {
injectHostnameArgTo(fragment);
super.showFragmentWithBackStack(fragment);
}
private void injectHostnameArgTo(Fragment fragment) {
Bundle args = fragment.getArguments(); Bundle args = fragment.getArguments();
if (args == null) { if (args == null) {
args = new Bundle(); args = new Bundle();
...@@ -107,4 +73,20 @@ public class LoginActivity extends AbstractFragmentActivity { ...@@ -107,4 +73,20 @@ public class LoginActivity extends AbstractFragmentActivity {
protected void onBackPressedNotHandled() { protected void onBackPressedNotHandled() {
moveTaskToBack(true); 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 ...@@ -28,21 +28,19 @@ public class RealmSessionRepository extends RealmRepository implements SessionRe
return Observable.just(null); return Observable.just(null);
} }
final RealmSession realmSession = return realm.where(RealmSession.class)
realm.where(RealmSession.class).equalTo(RealmSession.ID, RealmSession.DEFAULT_ID) .equalTo(RealmSession.ID, RealmSession.DEFAULT_ID)
.findFirst(); .findAll()
if (realmSession == null) {
realm.close();
return Observable.just(null);
}
return realmSession
.<RealmSession>asObservable() .<RealmSession>asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper)) .unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper)) .doOnUnsubscribe(() -> close(realm, looper))
.filter(it -> it != null && it.isLoaded() && it.isValid()) .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; ...@@ -7,6 +7,8 @@ import javax.annotation.Nullable;
@AutoValue @AutoValue
public abstract class Session { public abstract class Session {
public enum State {UNAVAILABLE, INVALID, VALID}
public abstract int getSessionId(); public abstract int getSessionId();
@Nullable @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