Automatically close dialog when login is successful and notify user by toast

This commit is contained in:
inorichi 2015-11-11 19:15:56 +01:00
parent 089b5d3326
commit 985c5c09a7
6 changed files with 50 additions and 31 deletions

View File

@ -18,35 +18,40 @@ import butterknife.ButterKnife;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.helpers.PreferencesHelper;
import eu.kanade.mangafeed.sources.base.Source;
import eu.kanade.mangafeed.util.ToastUtil;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class LoginDialogPreference extends DialogPreference {
@Bind(R.id.accounts_login)
TextView title;
@Bind(R.id.username)
EditText username;
@Bind(R.id.accounts_login) TextView title;
@Bind(R.id.username) EditText username;
@Bind(R.id.password) EditText password;
@Bind(R.id.show_password)
CheckBox showPassword;
@Bind(R.id.login)
ActionProcessButton loginBtn;
@Bind(R.id.show_password) CheckBox showPassword;
@Bind(R.id.login) ActionProcessButton loginBtn;
private PreferencesHelper preferences;
private Source source;
private AlertDialog dialog;
private Subscription requestSubscription;
private Context context;
public LoginDialogPreference(Context context, PreferencesHelper preferences, Source source) {
super(context, null);
this.context = context;
this.preferences = preferences;
this.source = source;
setDialogLayoutResource(R.layout.pref_account_login);
}
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
// Hide positive button
builder.setPositiveButton("", this);
}
@Override
protected void onBindDialogView(View view) {
ButterKnife.bind(this, view);
@ -63,7 +68,7 @@ public class LoginDialogPreference extends DialogPreference {
});
loginBtn.setMode(ActionProcessButton.Mode.ENDLESS);
loginBtn.setOnClickListener(v -> checkLogin());
loginBtn.setOnClickListener(click -> checkLogin());
super.onBindDialogView(view);
}
@ -72,7 +77,6 @@ public class LoginDialogPreference extends DialogPreference {
public void showDialog(Bundle state) {
super.showDialog(state);
dialog = ((AlertDialog) getDialog());
setSubmitButtonEnabled(false);
}
@Override
@ -86,15 +90,6 @@ public class LoginDialogPreference extends DialogPreference {
preferences.setSourceCredentials(source,
username.getText().toString(),
password.getText().toString());
super.onDialogClosed(true);
}
private void setSubmitButtonEnabled(boolean enabled) {
if (dialog != null) {
dialog.getButton(DialogInterface.BUTTON_POSITIVE)
.setEnabled(enabled);
}
}
private void checkLogin() {
@ -106,23 +101,22 @@ public class LoginDialogPreference extends DialogPreference {
loginBtn.setProgress(1);
requestSubscription = source.login(username.getText().toString(),
password.getText().toString())
requestSubscription = source
.login(username.getText().toString(), password.getText().toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(logged -> {
if (logged) {
loginBtn.setProgress(100);
loginBtn.setEnabled(false);
username.setEnabled(false);
password.setEnabled(false);
setSubmitButtonEnabled(true);
// Simulate a positive button click and dismiss the dialog
onClick(dialog, DialogInterface.BUTTON_POSITIVE);
dialog.dismiss();
ToastUtil.showShort(context, R.string.login_success);
} else {
loginBtn.setProgress(-1);
}
}, throwable -> {
loginBtn.setProgress(-1);
loginBtn.setText("Unknown error");
loginBtn.setText(R.string.unknown_error);
});
}

View File

@ -20,7 +20,7 @@ public abstract class BaseViewer {
}
public void updatePageNumber() {
activity.onPageChanged(getCurrentPageIndex(currentPosition), getTotalPages());
activity.onPageChanged(getCurrentPosition(), getTotalPages());
}
// Returns the page index given a position in the viewer. Useful por a right to left viewer,

View File

@ -24,7 +24,7 @@ public class RxPager {
public Observable<Integer> pages() {
return requests
.concatMap(targetPage -> targetPage <= requestedCount ?
Observable.<Integer>never() :
Observable.<Integer>empty() :
Observable.range(requestedCount, targetPage - requestedCount))
.startWith(Observable.range(0, initialPageCount))
.doOnNext(it -> requestedCount = it + 1);

View File

@ -0,0 +1,24 @@
package eu.kanade.mangafeed.util;
import android.content.Context;
import android.widget.Toast;
public class ToastUtil {
public static void showShort(Context context, int resourceId) {
Toast.makeText(context, resourceId, Toast.LENGTH_SHORT).show();
}
public static void showLong(Context context, int resourceId) {
Toast.makeText(context, resourceId, Toast.LENGTH_LONG).show();
}
public static void showShort(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
public static void showLong(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}

View File

@ -58,7 +58,7 @@
android:textColor="@android:color/white"
android:text="@string/login"
android:id="@+id/login"
app:pb_textComplete="@string/success"
app:pb_textComplete="@string/login_success"
app:pb_textProgress="@string/loading"
app:pb_textError="@string/invalid_login"
android:layout_marginTop="20dp"/>

View File

@ -70,9 +70,10 @@
<string name="show_password">Show password</string>
<string name="check_credentials">Check credentials</string>
<string name="login">Login</string>
<string name="success">Success</string>
<string name="login_success">Login successful</string>
<string name="invalid_login">Login error</string>
<string name="loading">Loading…</string>
<string name="unknown_error">Unknown error</string>
<string name="action_favorite">Add to favorites</string>