Commit 2b2b6798 authored by Tiago Cunha's avatar Tiago Cunha Committed by GitHub

Merge pull request #128 from RocketChat/improve_login

Improve login screen
parents b228661c 4737dfbc
......@@ -4,7 +4,7 @@ import android.content.Context;
import android.content.Intent;
import chat.rocket.android.activity.AddServerActivity;
import chat.rocket.android.activity.ServerConfigActivity;
import chat.rocket.android.activity.LoginActivity;
/**
* utility class for launching Activity.
......@@ -23,8 +23,8 @@ public class LaunchUtil {
/**
* launch ServerConfigActivity with proper flags.
*/
public static void showServerConfigActivity(Context context, String serverCondigId) {
Intent intent = new Intent(context, ServerConfigActivity.class);
public static void showLoginActivity(Context context, String serverCondigId) {
Intent intent = new Intent(context, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("serverConfigId", serverCondigId);
context.startActivity(intent);
......
......@@ -18,9 +18,9 @@ import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.service.RocketChatService;
/**
* Activity for Login, Sign-up, and Connecting...
* Activity for Login, Sign-up, and Retry connecting...
*/
public class ServerConfigActivity extends AbstractFragmentActivity {
public class LoginActivity extends AbstractFragmentActivity {
private String serverConfigId;
private RealmObjectObserver<ServerConfig> serverConfigErrorObserver;
......@@ -91,17 +91,11 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
return;
}
if (session.isTokenVerified() && TextUtils.isEmpty(session.getError())) {
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
return;
}
final String token = session.getToken();
if (!TextUtils.isEmpty(token)) {
if (TextUtils.isEmpty(session.getError())) {
showFragment(
WaitingFragment.create(getString(R.string.server_config_activity_authenticating)));
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
} else {
showFragment(new RetryLoginFragment());
}
......
......@@ -2,6 +2,7 @@ package chat.rocket.android.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
......@@ -13,6 +14,8 @@ 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.LogcatIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper;
......@@ -27,6 +30,7 @@ public class MainActivity extends AbstractAuthedActivity {
private RealmObjectObserver<Session> sessionObserver;
private boolean isForeground;
private StatusTicker statusTicker;
@Override
protected int getLayoutContainerForFragment() {
......@@ -38,6 +42,7 @@ public class MainActivity extends AbstractAuthedActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
statusTicker = new StatusTicker();
setupSidebar();
if (roomId == null) {
showFragment(new HomeFragment());
......@@ -143,17 +148,42 @@ public class MainActivity extends AbstractAuthedActivity {
sessionObserver = realmHelper
.createObjectObserver(realm ->
Session.queryDefaultSession(realm)
.isNotNull(Session.TOKEN)
.equalTo(Session.TOKEN_VERIFIED, true)
.isNull(Session.ERROR))
.setOnUpdateListener(session -> {
if (session == null && isForeground) {
LaunchUtil.showServerConfigActivity(this, serverConfigId);
}
});
.isNotNull(Session.TOKEN))
.setOnUpdateListener(this::onSessionChanged);
sessionObserver.sub();
}
private void onSessionChanged(@Nullable Session session) {
if (session == null) {
if (isForeground) {
LaunchUtil.showLoginActivity(this, serverConfigId);
}
statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null);
} else if (!TextUtils.isEmpty(session.getError())) {
statusTicker.updateStatus(StatusTicker.STATUS_CONNECTION_ERROR,
Snackbar.make(findViewById(getLayoutContainerForFragment()),
R.string.fragment_retry_login_error_title, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.fragment_retry_login_retry_title, view ->
RealmStore.getDefault()
.executeTransaction(realm -> {
ServerConfig config = realm.where(ServerConfig.class)
.equalTo(ServerConfig.ID, serverConfigId).findFirst();
if (config != null
&& config.getState() == ServerConfig.STATE_CONNECTION_ERROR) {
config.setState(ServerConfig.STATE_READY);
}
return null;
}).continueWith(new LogcatIfError())));
} else if (!session.isTokenVerified()) {
statusTicker.updateStatus(StatusTicker.STATUS_TOKEN_LOGIN,
Snackbar.make(findViewById(getLayoutContainerForFragment()),
R.string.server_config_activity_authenticating, Snackbar.LENGTH_INDEFINITE));
} else {
statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null);
}
}
private void updateSidebarMainFragment() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(serverConfigId))
......@@ -185,4 +215,34 @@ public class MainActivity extends AbstractAuthedActivity {
protected boolean onBackPress() {
return closeSidebarIfNeeded() || super.onBackPress();
}
//TODO: consider this class to define in layouthelper for more complicated operation.
private static class StatusTicker {
public static final int STATUS_DISMISS = 0;
public static final int STATUS_CONNECTION_ERROR = 1;
public static final int STATUS_TOKEN_LOGIN = 2;
private int status;
private Snackbar snackbar;
public StatusTicker() {
status = STATUS_DISMISS;
}
public void updateStatus(int status, Snackbar snackbar) {
if (status == this.status) {
return;
}
this.status = status;
if (this.snackbar != null) {
this.snackbar.dismiss();
}
if (status != STATUS_DISMISS) {
this.snackbar = snackbar;
if (this.snackbar != null) {
this.snackbar.show();
}
}
}
}
}
......@@ -42,6 +42,7 @@ public class LoginFragment extends AbstractServerConfigFragment {
final View btnEmail = rootView.findViewById(R.id.btn_login_with_email);
final TextView txtUsername = (TextView) rootView.findViewById(R.id.editor_username);
final TextView txtPasswd = (TextView) rootView.findViewById(R.id.editor_passwd);
final View waitingView = rootView.findViewById(R.id.waiting);
btnEmail.setOnClickListener(view -> {
final CharSequence username = txtUsername.getText();
final CharSequence passwd = txtPasswd.getText();
......@@ -49,6 +50,7 @@ public class LoginFragment extends AbstractServerConfigFragment {
return;
}
view.setEnabled(false);
waitingView.setVisibility(View.VISIBLE);
new MethodCallHelper(getContext(), serverConfigId)
.loginWithEmail(username.toString(), passwd.toString())
......@@ -56,6 +58,7 @@ public class LoginFragment extends AbstractServerConfigFragment {
if (task.isFaulted()) {
showError(task.getError().getMessage());
view.setEnabled(true);
waitingView.setVisibility(View.GONE);
}
return null;
});
......
......@@ -120,5 +120,12 @@
app:fabSize="normal"
app:srcCompat="@drawable/ic_arrow_forward_white_24dp" />
</FrameLayout>
<chat.rocket.android.widget.WaitingView
android:id="@+id/waiting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"/>
</LinearLayout>
</FrameLayout>
\ No newline at end of file
......@@ -29,7 +29,7 @@
<string name="fragment_login_username_or_email">Username or email</string>
<string name="fragment_login_password">Password</string>
<string name="fragment_retry_login_retry_title">RETRY</string>
<string name="fragment_retry_login_error_title">Oops…</string>
<string name="fragment_retry_login_error_title">Connection Error</string>
<string name="add_server_activity_waiting_server">Connecting to server…</string>
<string name="server_config_activity_authenticating">Authenticating…</string>
<string name="home_fragment_title">Rocket.Chat - Home</string>
......
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