Check credentials before saving

This commit is contained in:
inorichi 2015-10-26 18:50:11 +01:00
parent 1b45ff3b12
commit baa24fbf17
5 changed files with 146 additions and 77 deletions

View File

@ -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"

View File

@ -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");
});
}
}

View File

@ -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);
}
}
}

View File

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp">
@ -50,12 +52,15 @@
android:id="@+id/show_password"
android:layout_marginTop="10dp"/>
<Button
android:layout_width="wrap_content"
<com.dd.processbutton.iml.ActionProcessButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/check_credentials"
android:id="@+id/check_credentials" />
android:textColor="@android:color/white"
android:text="@string/login"
android:id="@+id/login"
app:pb_textComplete="@string/success"
app:pb_textProgress="@string/loading"
app:pb_textError="@string/invalid_login"
android:layout_marginTop="20dp"/>
</LinearLayout>

View File

@ -67,5 +67,9 @@
<string name="password">Password</string>
<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="invalid_login">Login error</string>
<string name="loading">Loading…</string>
</resources>