Rewrite the chapter insertion method. Create a wakelock until the library updates. Move custom preferences to widget package.

This commit is contained in:
inorichi
2016-01-10 19:49:26 +01:00
parent fcb5bf4dd4
commit e702be1a8d
18 changed files with 84 additions and 107 deletions
@@ -1,6 +1,7 @@
package eu.kanade.mangafeed.ui.manga.chapter;
import android.os.Bundle;
import android.util.Pair;
import java.util.List;
@@ -20,7 +21,6 @@ import eu.kanade.mangafeed.event.DownloadChaptersEvent;
import eu.kanade.mangafeed.event.ReaderEvent;
import eu.kanade.mangafeed.ui.base.presenter.BasePresenter;
import eu.kanade.mangafeed.util.EventBusHook;
import eu.kanade.mangafeed.util.PostResult;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
@@ -119,7 +119,7 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
chaptersSubject.onNext(chapters);
}
private Observable<PostResult> getOnlineChaptersObs() {
private Observable<Pair<Integer, Integer>> getOnlineChaptersObs() {
return source.pullChaptersFromNetwork(manga.url)
.subscribeOn(Schedulers.io())
.flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters))
@@ -16,8 +16,8 @@ import eu.kanade.mangafeed.data.mangasync.base.MangaSyncService;
import eu.kanade.mangafeed.data.mangasync.MangaSyncManager;
import eu.kanade.mangafeed.data.source.SourceManager;
import eu.kanade.mangafeed.data.source.base.Source;
import eu.kanade.mangafeed.ui.setting.preference.MangaSyncLoginDialog;
import eu.kanade.mangafeed.ui.setting.preference.SourceLoginDialog;
import eu.kanade.mangafeed.widget.preference.MangaSyncLoginDialog;
import eu.kanade.mangafeed.widget.preference.SourceLoginDialog;
import rx.Observable;
public class SettingsAccountsFragment extends SettingsNestedFragment {
@@ -8,8 +8,8 @@ import android.view.ViewGroup;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.preference.PreferencesHelper;
import eu.kanade.mangafeed.data.sync.LibraryUpdateAlarm;
import eu.kanade.mangafeed.ui.setting.preference.IntListPreference;
import eu.kanade.mangafeed.ui.setting.preference.LibraryColumnsDialog;
import eu.kanade.mangafeed.widget.preference.IntListPreference;
import eu.kanade.mangafeed.widget.preference.LibraryColumnsDialog;
public class SettingsGeneralFragment extends SettingsNestedFragment {
@@ -1,35 +0,0 @@
package eu.kanade.mangafeed.ui.setting.preference;
import android.content.Context;
import android.preference.ListPreference;
import android.util.AttributeSet;
public class IntListPreference extends ListPreference
{
public IntListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IntListPreference(Context context) {
super(context);
}
@Override
protected boolean persistString(String value) {
if(value == null) {
return false;
} else {
return persistInt(Integer.valueOf(value));
}
}
@Override
protected String getPersistedString(String defaultReturnValue) {
if(getSharedPreferences().contains(getKey())) {
int intValue = getPersistedInt(0);
return String.valueOf(intValue);
} else {
return defaultReturnValue;
}
}
}
@@ -1,80 +0,0 @@
package eu.kanade.mangafeed.ui.setting.preference;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.NumberPicker;
import butterknife.Bind;
import butterknife.ButterKnife;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.preference.PreferencesHelper;
public class LibraryColumnsDialog extends DialogPreference {
private Context context;
private PreferencesHelper preferences;
@Bind(R.id.portrait_columns) NumberPicker portraitColumns;
@Bind(R.id.landscape_columns) NumberPicker landscapeColumns;
public LibraryColumnsDialog(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LibraryColumnsDialog(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
this.context = context;
setDialogLayoutResource(R.layout.pref_library_columns);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
ButterKnife.bind(this, view);
portraitColumns.setValue(preferences.portraitColumns().get());
landscapeColumns.setValue(preferences.landscapeColumns().get());
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
preferences.portraitColumns().set(portraitColumns.getValue());
preferences.landscapeColumns().set(landscapeColumns.getValue());
updateSummary();
}
}
private void updateSummary() {
setSummary(getColumnsSummary());
}
private String getColumnsSummary() {
return String.format("%s: %s, %s: %s",
context.getString(R.string.portrait),
getColumnValue(preferences.portraitColumns().get()),
context.getString(R.string.landscape),
getColumnValue(preferences.landscapeColumns().get()));
}
private String getColumnValue(int value) {
return value == 0 ? context.getString(R.string.default_columns) : value + "";
}
public void setPreferencesHelper(PreferencesHelper preferences) {
this.preferences = preferences;
// Set initial summary when the preferences helper is provided
updateSummary();
}
}
@@ -1,78 +0,0 @@
package eu.kanade.mangafeed.ui.setting.preference;
import android.app.AlertDialog;
import android.content.Context;
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.preference.PreferencesHelper;
import rx.Subscription;
public abstract 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;
protected PreferencesHelper preferences;
protected AlertDialog dialog;
protected Subscription requestSubscription;
protected Context context;
public LoginDialogPreference(Context context, PreferencesHelper preferences) {
super(context, null);
this.context = context;
this.preferences = preferences;
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) {
super.onBindDialogView(view);
ButterKnife.bind(this, view);
showPassword.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked)
password.setTransformationMethod(null);
else
password.setTransformationMethod(new PasswordTransformationMethod());
});
loginBtn.setMode(ActionProcessButton.Mode.ENDLESS);
loginBtn.setOnClickListener(click -> checkLogin());
}
@Override
public void showDialog(Bundle state) {
super.showDialog(state);
dialog = ((AlertDialog) getDialog());
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (requestSubscription != null)
requestSubscription.unsubscribe();
}
protected abstract void checkLogin();
}
@@ -1,74 +0,0 @@
package eu.kanade.mangafeed.ui.setting.preference;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.mangasync.base.MangaSyncService;
import eu.kanade.mangafeed.data.preference.PreferencesHelper;
import eu.kanade.mangafeed.util.ToastUtil;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class MangaSyncLoginDialog extends LoginDialogPreference {
private MangaSyncService sync;
public MangaSyncLoginDialog(Context context, PreferencesHelper preferences, MangaSyncService sync) {
super(context, preferences);
this.sync = sync;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
title.setText(getContext().getString(R.string.accounts_login_title, sync.getName()));
username.setText(preferences.getMangaSyncUsername(sync));
password.setText(preferences.getMangaSyncPassword(sync));
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
preferences.setMangaSyncCredentials(sync,
username.getText().toString(),
password.getText().toString());
}
}
protected void checkLogin() {
if (requestSubscription != null)
requestSubscription.unsubscribe();
if (username.getText().length() == 0 || password.getText().length() == 0)
return;
loginBtn.setProgress(1);
requestSubscription = sync
.login(username.getText().toString(), password.getText().toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(logged -> {
if (logged) {
// Simulate a positive button click and dismiss the dialog
onClick(dialog, DialogInterface.BUTTON_POSITIVE);
dialog.dismiss();
ToastUtil.showShort(context, R.string.login_success);
} else {
preferences.setMangaSyncCredentials(sync, "", "");
loginBtn.setProgress(-1);
}
}, error -> {
loginBtn.setProgress(-1);
loginBtn.setText(R.string.unknown_error);
});
}
}
@@ -1,74 +0,0 @@
package eu.kanade.mangafeed.ui.setting.preference;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.preference.PreferencesHelper;
import eu.kanade.mangafeed.data.source.base.Source;
import eu.kanade.mangafeed.util.ToastUtil;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class SourceLoginDialog extends LoginDialogPreference {
private Source source;
public SourceLoginDialog(Context context, PreferencesHelper preferences, Source source) {
super(context, preferences);
this.source = source;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
title.setText(getContext().getString(R.string.accounts_login_title, source.getName()));
username.setText(preferences.getSourceUsername(source));
password.setText(preferences.getSourcePassword(source));
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
preferences.setSourceCredentials(source,
username.getText().toString(),
password.getText().toString());
}
}
protected void checkLogin() {
if (requestSubscription != null)
requestSubscription.unsubscribe();
if (username.getText().length() == 0 || password.getText().length() == 0)
return;
loginBtn.setProgress(1);
requestSubscription = source
.login(username.getText().toString(), password.getText().toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(logged -> {
if (logged) {
// Simulate a positive button click and dismiss the dialog
onClick(dialog, DialogInterface.BUTTON_POSITIVE);
dialog.dismiss();
ToastUtil.showShort(context, R.string.login_success);
} else {
preferences.setSourceCredentials(source, "", "");
loginBtn.setProgress(-1);
}
}, error -> {
loginBtn.setProgress(-1);
loginBtn.setText(R.string.unknown_error);
});
}
}