From 985c5c09a75db9c4572e6b6a6f4b29242996e40b Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 11 Nov 2015 19:15:56 +0100 Subject: [PATCH] Automatically close dialog when login is successful and notify user by toast --- .../ui/dialog/LoginDialogPreference.java | 48 ++++++++----------- .../mangafeed/ui/viewer/base/BaseViewer.java | 2 +- .../eu/kanade/mangafeed/util/RxPager.java | 2 +- .../eu/kanade/mangafeed/util/ToastUtil.java | 24 ++++++++++ .../main/res/layout/pref_account_login.xml | 2 +- app/src/main/res/values/strings.xml | 3 +- 6 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/util/ToastUtil.java diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/dialog/LoginDialogPreference.java b/app/src/main/java/eu/kanade/mangafeed/ui/dialog/LoginDialogPreference.java index 819a17c1c..9b5f86bc7 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/dialog/LoginDialogPreference.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/dialog/LoginDialogPreference.java @@ -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); }); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java index 5a80dbab0..36887fb68 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java @@ -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, diff --git a/app/src/main/java/eu/kanade/mangafeed/util/RxPager.java b/app/src/main/java/eu/kanade/mangafeed/util/RxPager.java index 032c5898b..1de155868 100644 --- a/app/src/main/java/eu/kanade/mangafeed/util/RxPager.java +++ b/app/src/main/java/eu/kanade/mangafeed/util/RxPager.java @@ -24,7 +24,7 @@ public class RxPager { public Observable pages() { return requests .concatMap(targetPage -> targetPage <= requestedCount ? - Observable.never() : + Observable.empty() : Observable.range(requestedCount, targetPage - requestedCount)) .startWith(Observable.range(0, initialPageCount)) .doOnNext(it -> requestedCount = it + 1); diff --git a/app/src/main/java/eu/kanade/mangafeed/util/ToastUtil.java b/app/src/main/java/eu/kanade/mangafeed/util/ToastUtil.java new file mode 100644 index 000000000..54b15b68f --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/util/ToastUtil.java @@ -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(); + } + +} diff --git a/app/src/main/res/layout/pref_account_login.xml b/app/src/main/res/layout/pref_account_login.xml index db4677483..50e8baa59 100644 --- a/app/src/main/res/layout/pref_account_login.xml +++ b/app/src/main/res/layout/pref_account_login.xml @@ -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"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e2a08c581..6e7243ef3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -70,9 +70,10 @@ Show password Check credentials Login - Success + Login successful Login error Loading… + Unknown error Add to favorites