From baa24fbf17237bd627898433427779c5a360d38f Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 26 Oct 2015 18:50:11 +0100 Subject: [PATCH] Check credentials before saving --- app/build.gradle | 1 + .../ui/dialog/LoginDialogPreference.java | 127 ++++++++++++++++++ .../ui/fragment/SettingsAccountsFragment.java | 72 +--------- .../main/res/layout/pref_account_login.xml | 19 ++- app/src/main/res/values/strings.xml | 4 + 5 files changed, 146 insertions(+), 77 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/dialog/LoginDialogPreference.java diff --git a/app/build.gradle b/app/build.gradle index 2f6ce5b93..e48ff9f2a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -87,6 +87,7 @@ dependencies { compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.4.1' compile "frankiesardo:icepick:$ICEPICK_VERSION" provided "frankiesardo:icepick-processor:$ICEPICK_VERSION" + compile 'com.github.dmytrodanylyk.android-process-button:library:1.0.4' compile "com.google.dagger:dagger:$DAGGER_VERSION" apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION" 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 new file mode 100644 index 000000000..507bd4d17 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/dialog/LoginDialogPreference.java @@ -0,0 +1,127 @@ +package eu.kanade.mangafeed.ui.dialog; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.preference.DialogPreference; +import android.text.method.PasswordTransformationMethod; +import android.view.View; +import android.widget.CheckBox; +import android.widget.EditText; +import android.widget.TextView; + +import com.dd.processbutton.iml.ActionProcessButton; + +import butterknife.Bind; +import butterknife.ButterKnife; +import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.helpers.PreferencesHelper; +import eu.kanade.mangafeed.sources.base.Source; +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.password) EditText password; + @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; + + public LoginDialogPreference(Context context, PreferencesHelper preferences, Source source) { + super(context, null); + this.preferences = preferences; + this.source = source; + + setDialogLayoutResource(R.layout.pref_account_login); + } + + @Override + protected void onBindDialogView(View view) { + ButterKnife.bind(this, view); + + title.setText(getContext().getString(R.string.accounts_login_title, source.getName())); + + username.setText(preferences.getSourceUsername(source)); + password.setText(preferences.getSourcePassword(source)); + showPassword.setOnCheckedChangeListener((buttonView, isChecked) -> { + if (isChecked) + password.setTransformationMethod(null); + else + password.setTransformationMethod(new PasswordTransformationMethod()); + }); + + loginBtn.setMode(ActionProcessButton.Mode.ENDLESS); + loginBtn.setOnClickListener(v -> checkLogin()); + + super.onBindDialogView(view); + } + + @Override + public void showDialog(Bundle state) { + super.showDialog(state); + dialog = ((AlertDialog) getDialog()); + setSubmitButtonEnabled(false); + } + + @Override + protected void onDialogClosed(boolean positiveResult) { + if (requestSubscription != null) + requestSubscription.unsubscribe(); + + if(!positiveResult) + return; + + 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() { + if (requestSubscription != null) + requestSubscription.unsubscribe(); + + loginBtn.setProgress(1); + + 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); + } else { + loginBtn.setProgress(-1); + } + }, throwable -> { + loginBtn.setProgress(-1); + loginBtn.setText("Unknown error"); + }); + + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsAccountsFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsAccountsFragment.java index 80789207b..0b96c2052 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsAccountsFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsAccountsFragment.java @@ -1,33 +1,21 @@ package eu.kanade.mangafeed.ui.fragment; -import android.content.Context; import android.os.Bundle; -import android.preference.DialogPreference; import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; -import android.text.method.PasswordTransformationMethod; -import android.util.AttributeSet; -import android.view.View; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.TextView; import java.util.List; import javax.inject.Inject; -import butterknife.Bind; -import butterknife.ButterKnife; import eu.kanade.mangafeed.App; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import eu.kanade.mangafeed.data.helpers.SourceManager; import eu.kanade.mangafeed.sources.base.Source; import eu.kanade.mangafeed.ui.activity.base.BaseActivity; +import eu.kanade.mangafeed.ui.dialog.LoginDialogPreference; import rx.Observable; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; -import timber.log.Timber; public class SettingsAccountsFragment extends PreferenceFragment { @@ -50,7 +38,7 @@ public class SettingsAccountsFragment extends PreferenceFragment { for (Source source : sourceAccounts) { LoginDialogPreference dialog = new LoginDialogPreference( - screen.getContext(), null, source); + screen.getContext(), preferences, source); dialog.setTitle(source.getName()); screen.addPreference(dialog); @@ -72,60 +60,4 @@ public class SettingsAccountsFragment extends PreferenceFragment { .single(); } - public class LoginDialogPreference extends DialogPreference { - - @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; - - private Source source; - - public LoginDialogPreference(Context context, AttributeSet attrs, Source source) { - super(context, attrs); - this.source = source; - - setDialogLayoutResource(R.layout.pref_account_login); - } - - @Override - protected void onBindDialogView(View view) { - ButterKnife.bind(this, view); - - title.setText(getString(R.string.accounts_login_title, source.getName())); - - username.setText(preferences.getSourceUsername(source)); - password.setText(preferences.getSourcePassword(source)); - showPassword.setOnCheckedChangeListener((buttonView, isChecked) -> { - if (isChecked) - password.setTransformationMethod(null); - else - password.setTransformationMethod(new PasswordTransformationMethod()); - }); - - super.onBindDialogView(view); - } - - @Override - protected void onDialogClosed(boolean positiveResult) { - if(!positiveResult) - return; - - preferences.setSourceCredentials(source, - username.getText().toString(), - password.getText().toString()); - - source.login(username.getText().toString(), password.getText().toString()) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(result -> { - Timber.e("Result is " + result); - }); - - - super.onDialogClosed(true); - } - - } - } diff --git a/app/src/main/res/layout/pref_account_login.xml b/app/src/main/res/layout/pref_account_login.xml index 6dc7f8a70..db4677483 100644 --- a/app/src/main/res/layout/pref_account_login.xml +++ b/app/src/main/res/layout/pref_account_login.xml @@ -1,6 +1,8 @@ @@ -50,12 +52,15 @@ android:id="@+id/show_password" android:layout_marginTop="10dp"/> -