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.R;
import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import eu.kanade.mangafeed.data.helpers.PreferencesHelper;
import eu.kanade.mangafeed.sources.base.Source; import eu.kanade.mangafeed.sources.base.Source;
import eu.kanade.mangafeed.util.ToastUtil;
import rx.Subscription; import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
public class LoginDialogPreference extends DialogPreference { public class LoginDialogPreference extends DialogPreference {
@Bind(R.id.accounts_login) @Bind(R.id.accounts_login) TextView title;
TextView title; @Bind(R.id.username) EditText username;
@Bind(R.id.username)
EditText username;
@Bind(R.id.password) EditText password; @Bind(R.id.password) EditText password;
@Bind(R.id.show_password) @Bind(R.id.show_password) CheckBox showPassword;
CheckBox showPassword; @Bind(R.id.login) ActionProcessButton loginBtn;
@Bind(R.id.login)
ActionProcessButton loginBtn;
private PreferencesHelper preferences; private PreferencesHelper preferences;
private Source source; private Source source;
private AlertDialog dialog; private AlertDialog dialog;
private Subscription requestSubscription; private Subscription requestSubscription;
private Context context;
public LoginDialogPreference(Context context, PreferencesHelper preferences, Source source) { public LoginDialogPreference(Context context, PreferencesHelper preferences, Source source) {
super(context, null); super(context, null);
this.context = context;
this.preferences = preferences; this.preferences = preferences;
this.source = source; this.source = source;
setDialogLayoutResource(R.layout.pref_account_login); setDialogLayoutResource(R.layout.pref_account_login);
} }
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
// Hide positive button
builder.setPositiveButton("", this);
}
@Override @Override
protected void onBindDialogView(View view) { protected void onBindDialogView(View view) {
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
@ -63,7 +68,7 @@ public class LoginDialogPreference extends DialogPreference {
}); });
loginBtn.setMode(ActionProcessButton.Mode.ENDLESS); loginBtn.setMode(ActionProcessButton.Mode.ENDLESS);
loginBtn.setOnClickListener(v -> checkLogin()); loginBtn.setOnClickListener(click -> checkLogin());
super.onBindDialogView(view); super.onBindDialogView(view);
} }
@ -72,7 +77,6 @@ public class LoginDialogPreference extends DialogPreference {
public void showDialog(Bundle state) { public void showDialog(Bundle state) {
super.showDialog(state); super.showDialog(state);
dialog = ((AlertDialog) getDialog()); dialog = ((AlertDialog) getDialog());
setSubmitButtonEnabled(false);
} }
@Override @Override
@ -86,15 +90,6 @@ public class LoginDialogPreference extends DialogPreference {
preferences.setSourceCredentials(source, preferences.setSourceCredentials(source,
username.getText().toString(), username.getText().toString(),
password.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() { private void checkLogin() {
@ -106,23 +101,22 @@ public class LoginDialogPreference extends DialogPreference {
loginBtn.setProgress(1); loginBtn.setProgress(1);
requestSubscription = source.login(username.getText().toString(), requestSubscription = source
password.getText().toString()) .login(username.getText().toString(), password.getText().toString())
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(logged -> { .subscribe(logged -> {
if (logged) { if (logged) {
loginBtn.setProgress(100); // Simulate a positive button click and dismiss the dialog
loginBtn.setEnabled(false); onClick(dialog, DialogInterface.BUTTON_POSITIVE);
username.setEnabled(false); dialog.dismiss();
password.setEnabled(false); ToastUtil.showShort(context, R.string.login_success);
setSubmitButtonEnabled(true);
} else { } else {
loginBtn.setProgress(-1); loginBtn.setProgress(-1);
} }
}, throwable -> { }, throwable -> {
loginBtn.setProgress(-1); 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() { 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, // 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() { public Observable<Integer> pages() {
return requests return requests
.concatMap(targetPage -> targetPage <= requestedCount ? .concatMap(targetPage -> targetPage <= requestedCount ?
Observable.<Integer>never() : Observable.<Integer>empty() :
Observable.range(requestedCount, targetPage - requestedCount)) Observable.range(requestedCount, targetPage - requestedCount))
.startWith(Observable.range(0, initialPageCount)) .startWith(Observable.range(0, initialPageCount))
.doOnNext(it -> requestedCount = it + 1); .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:textColor="@android:color/white"
android:text="@string/login" android:text="@string/login"
android:id="@+id/login" android:id="@+id/login"
app:pb_textComplete="@string/success" app:pb_textComplete="@string/login_success"
app:pb_textProgress="@string/loading" app:pb_textProgress="@string/loading"
app:pb_textError="@string/invalid_login" app:pb_textError="@string/invalid_login"
android:layout_marginTop="20dp"/> android:layout_marginTop="20dp"/>

View file

@ -70,9 +70,10 @@
<string name="show_password">Show password</string> <string name="show_password">Show password</string>
<string name="check_credentials">Check credentials</string> <string name="check_credentials">Check credentials</string>
<string name="login">Login</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="invalid_login">Login error</string>
<string name="loading">Loading…</string> <string name="loading">Loading…</string>
<string name="unknown_error">Unknown error</string>
<string name="action_favorite">Add to favorites</string> <string name="action_favorite">Add to favorites</string>