From 70e557575fe252711455ec2467d676ee09e444ee Mon Sep 17 00:00:00 2001 From: len Date: Mon, 7 Mar 2016 23:48:43 +0100 Subject: [PATCH] Preferences ported to support library --- app/build.gradle | 2 + .../ui/setting/SettingsAccountsFragment.kt | 61 +++++++++----- .../tachiyomi/ui/setting/SettingsActivity.kt | 53 ++++++------ .../ui/setting/SettingsAdvancedFragment.kt | 2 +- .../ui/setting/SettingsGeneralFragment.kt | 57 +++++++++++-- .../ui/setting/SettingsNestedFragment.kt | 7 +- .../widget/preference/IntListPreference.java | 35 -------- .../widget/preference/IntListPreference.kt | 25 ++++++ .../preference/LibraryColumnsDialog.java | 80 ------------------- .../widget/preference/LibraryColumnsDialog.kt | 56 +++++++++++++ .../preference/LoginDialogPreference.java | 78 ------------------ .../preference/LoginDialogPreference.kt | 70 ++++++++++++++++ .../preference/MangaSyncLoginDialog.java | 74 ----------------- .../widget/preference/MangaSyncLoginDialog.kt | 77 ++++++++++++++++++ .../preference/SimpleDialogPreference.kt | 11 +++ .../widget/preference/SourceLoginDialog.java | 74 ----------------- .../widget/preference/SourceLoginDialog.kt | 76 ++++++++++++++++++ app/src/main/res/values-v21/styles.xml | 14 ---- app/src/main/res/values/styles.xml | 4 +- app/src/main/res/xml/pref_about.xml | 7 +- app/src/main/res/xml/pref_accounts.xml | 15 +++- app/src/main/res/xml/pref_advanced.xml | 5 +- app/src/main/res/xml/pref_downloads.xml | 7 +- app/src/main/res/xml/pref_general.xml | 16 ++-- app/src/main/res/xml/pref_main.xml | 5 +- app/src/main/res/xml/pref_reader.xml | 17 ++-- 26 files changed, 488 insertions(+), 440 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/LibraryColumnsDialog.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/LibraryColumnsDialog.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/SimpleDialogPreference.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.kt delete mode 100644 app/src/main/res/values-v21/styles.xml diff --git a/app/build.gradle b/app/build.gradle index 35fec2f4fc..a400f7a678 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -118,6 +118,8 @@ dependencies { compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION" compile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION" compile "com.android.support:percent:$SUPPORT_LIBRARY_VERSION" + compile "com.android.support:preference-v7:$SUPPORT_LIBRARY_VERSION" + compile "com.android.support:preference-v14:$SUPPORT_LIBRARY_VERSION" compile "com.squareup.okhttp3:okhttp:$OKHTTP_VERSION" compile "com.squareup.okhttp3:okhttp-urlconnection:$OKHTTP_VERSION" compile 'com.squareup.okio:okio:1.6.0' diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAccountsFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAccountsFragment.kt index 95d69e1587..52e4a16b59 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAccountsFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAccountsFragment.kt @@ -1,8 +1,12 @@ package eu.kanade.tachiyomi.ui.setting +import android.content.Context import android.os.Bundle -import android.preference.PreferenceCategory +import android.support.v7.preference.DialogPreference +import android.support.v7.preference.Preference +import android.support.v7.preference.PreferenceCategory import android.view.View +import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.source.base.Source import eu.kanade.tachiyomi.widget.preference.MangaSyncLoginDialog import eu.kanade.tachiyomi.widget.preference.SourceLoginDialog @@ -18,29 +22,32 @@ class SettingsAccountsFragment : SettingsNestedFragment() { } } + val sourceCategory by lazy { findPreference("pref_category_source_accounts") as PreferenceCategory } + val syncCategory by lazy { findPreference("pref_category_manga_sync_accounts") as PreferenceCategory } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - PreferenceCategory(activity).apply { - preferenceScreen.addPreference(this) - title = "Sources" + val themedContext = preferenceManager.context - for (source in getSourcesWithLogin()) { - val dialog = SourceLoginDialog(activity, preferences, source) - dialog.title = source.name - - addPreference(dialog) + for (source in getSourcesWithLogin()) { + val pref = SourcePreference(themedContext).apply { + isPersistent = false + title = source.name + key = source.id.toString() + dialogLayoutResource = R.layout.pref_account_login } + + sourceCategory.addPreference(pref) } - PreferenceCategory(activity).apply { - preferenceScreen.addPreference(this) - title = "Sync" - - for (sync in settingsActivity.syncManager.services) { - val dialog = MangaSyncLoginDialog(activity, preferences, sync) - dialog.title = sync.name - - addPreference(dialog) + for (sync in settingsActivity.syncManager.services) { + val pref = SyncPreference(themedContext).apply { + isPersistent = false + title = sync.name + key = sync.id.toString() + dialogLayoutResource = R.layout.pref_account_login } + + syncCategory.addPreference(pref) } } @@ -48,4 +55,22 @@ class SettingsAccountsFragment : SettingsNestedFragment() { return settingsActivity.sourceManager.sources.filter { it.isLoginRequired } } + override fun onDisplayPreferenceDialog(preference: Preference) { + if (preference is SourcePreference) { + val fragment = SourceLoginDialog.newInstance(preference) + fragment.setTargetFragment(this, 0) + fragment.show(childFragmentManager, null) + } else if (preference is SyncPreference) { + val fragment = MangaSyncLoginDialog.newInstance(preference) + fragment.setTargetFragment(this, 0) + fragment.show(childFragmentManager, null) + } else { + super.onDisplayPreferenceDialog(preference) + } + } + + class SourcePreference(context: Context) : DialogPreference(context) {} + + class SyncPreference(context: Context) : DialogPreference(context) {} + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt index 6ced1912a1..2d97da924d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt @@ -1,7 +1,7 @@ package eu.kanade.tachiyomi.ui.setting import android.os.Bundle -import android.preference.PreferenceFragment +import android.support.v7.preference.PreferenceFragmentCompat import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.cache.ChapterCache import eu.kanade.tachiyomi.data.database.DatabaseHelper @@ -28,46 +28,46 @@ class SettingsActivity : BaseActivity() { setupToolbar(toolbar) if (savedState == null) { - fragmentManager.beginTransaction().replace(R.id.settings_content, - SettingsMainFragment()).commit() + supportFragmentManager.beginTransaction() + .replace(R.id.settings_content,SettingsMainFragment()) + .commit() } } override fun onBackPressed() { - if (!fragmentManager.popBackStackImmediate()) { + if (!supportFragmentManager.popBackStackImmediate()) { super.onBackPressed() } } - class SettingsMainFragment : PreferenceFragment() { + class SettingsMainFragment : PreferenceFragmentCompat() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) + override fun onCreatePreferences(savedState: Bundle?, s: String?) { addPreferencesFromResource(R.xml.pref_main) - registerSubpreference(R.string.pref_category_general_key, - SettingsGeneralFragment.newInstance( - R.xml.pref_general, R.string.pref_category_general)) + registerSubpreference(R.string.pref_category_general_key) { + SettingsGeneralFragment.newInstance(R.xml.pref_general, R.string.pref_category_general) + } - registerSubpreference(R.string.pref_category_reader_key, - SettingsNestedFragment.newInstance( - R.xml.pref_reader, R.string.pref_category_reader)) + registerSubpreference(R.string.pref_category_reader_key) { + SettingsNestedFragment.newInstance(R.xml.pref_reader, R.string.pref_category_reader) + } - registerSubpreference(R.string.pref_category_downloads_key, - SettingsDownloadsFragment.newInstance( - R.xml.pref_downloads, R.string.pref_category_downloads)) + registerSubpreference(R.string.pref_category_downloads_key) { + SettingsDownloadsFragment.newInstance(R.xml.pref_downloads, R.string.pref_category_downloads) + } - registerSubpreference(R.string.pref_category_accounts_key, - SettingsAccountsFragment.newInstance( - R.xml.pref_accounts, R.string.pref_category_accounts)) + registerSubpreference(R.string.pref_category_accounts_key) { + SettingsAccountsFragment.newInstance(R.xml.pref_accounts, R.string.pref_category_accounts) + } - registerSubpreference(R.string.pref_category_advanced_key, - SettingsAdvancedFragment.newInstance( - R.xml.pref_advanced, R.string.pref_category_advanced)) + registerSubpreference(R.string.pref_category_advanced_key) { + SettingsAdvancedFragment.newInstance(R.xml.pref_advanced, R.string.pref_category_advanced) + } - registerSubpreference(R.string.pref_category_about_key, - SettingsAboutFragment.newInstance( - R.xml.pref_about, R.string.pref_category_about)) + registerSubpreference(R.string.pref_category_about_key) { + SettingsAboutFragment.newInstance(R.xml.pref_about, R.string.pref_category_about) + } } override fun onResume() { @@ -75,8 +75,9 @@ class SettingsActivity : BaseActivity() { (activity as BaseActivity).setToolbarTitle(getString(R.string.label_settings)) } - private fun registerSubpreference(preferenceResource: Int, fragment: PreferenceFragment) { + private fun registerSubpreference(preferenceResource: Int, func: () -> PreferenceFragmentCompat) { findPreference(getString(preferenceResource)).setOnPreferenceClickListener { + val fragment = func() fragmentManager.beginTransaction() .replace(R.id.settings_content, fragment) .addToBackStack(fragment.javaClass.simpleName) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt index 88eb32f6f8..2ef8aa44b2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt @@ -1,7 +1,7 @@ package eu.kanade.tachiyomi.ui.setting import android.os.Bundle -import android.preference.Preference +import android.support.v7.preference.Preference import android.view.View import com.afollestad.materialdialogs.MaterialDialog import eu.kanade.tachiyomi.R diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt index 8a95f8d17d..7b6f58f172 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt @@ -1,11 +1,15 @@ package eu.kanade.tachiyomi.ui.setting import android.os.Bundle +import android.support.v7.preference.Preference import android.view.View import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.library.LibraryUpdateAlarm import eu.kanade.tachiyomi.widget.preference.IntListPreference import eu.kanade.tachiyomi.widget.preference.LibraryColumnsDialog +import eu.kanade.tachiyomi.widget.preference.SimpleDialogPreference +import rx.Observable +import rx.Subscription class SettingsGeneralFragment : SettingsNestedFragment() { @@ -17,19 +21,56 @@ class SettingsGeneralFragment : SettingsNestedFragment() { } } + val columnsPreference by lazy { + findPreference(getString(R.string.pref_library_columns_dialog_key)) as SimpleDialogPreference + } + + val updateInterval by lazy { + findPreference(getString(R.string.pref_library_update_interval_key)) as IntListPreference + } + + var columnsSubscription: Subscription? = null + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - val columnsDialog = findPreference( - getString(R.string.pref_library_columns_dialog_key)) as LibraryColumnsDialog - - columnsDialog.setPreferencesHelper(preferences) - - val updateInterval = findPreference( - getString(R.string.pref_library_update_interval_key)) as IntListPreference - updateInterval.setOnPreferenceChangeListener { preference, newValue -> LibraryUpdateAlarm.startAlarm(activity, (newValue as String).toInt()) true } } + override fun onResume() { + super.onResume() + columnsSubscription = Observable.combineLatest(preferences.portraitColumns().asObservable(), + preferences.landscapeColumns().asObservable(), + { portraitColumns, landscapeColumns -> Pair(portraitColumns, landscapeColumns) }) + .subscribe { updateColumnsSummary(it.first, it.second) } + } + + override fun onPause() { + columnsSubscription?.unsubscribe() + super.onPause() + } + + override fun onDisplayPreferenceDialog(preference: Preference) { + if (preference === columnsPreference) { + val fragment = LibraryColumnsDialog.newInstance(preference) + fragment.setTargetFragment(this, 0) + fragment.show(childFragmentManager, null) + } else { + super.onDisplayPreferenceDialog(preference) + } + } + + private fun updateColumnsSummary(portraitColumns: Int, landscapeColumns: Int) { + val portrait = getColumnValue(portraitColumns) + val landscape = getColumnValue(landscapeColumns) + val msg = "${getString(R.string.portrait)}: $portrait, ${getString(R.string.landscape)}: $landscape" + + columnsPreference.summary = msg + } + + private fun getColumnValue(value: Int): String { + return if (value == 0) getString(R.string.default_columns) else value.toString() + } + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsNestedFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsNestedFragment.kt index 0d3a47af12..0eb1b3fccf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsNestedFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsNestedFragment.kt @@ -1,10 +1,10 @@ package eu.kanade.tachiyomi.ui.setting import android.os.Bundle -import android.preference.PreferenceFragment +import android.support.v7.preference.PreferenceFragmentCompat import eu.kanade.tachiyomi.data.preference.PreferencesHelper -open class SettingsNestedFragment : PreferenceFragment() { +open class SettingsNestedFragment : PreferenceFragmentCompat() { companion object { @@ -19,8 +19,7 @@ open class SettingsNestedFragment : PreferenceFragment() { } - override fun onCreate(savedState: Bundle?) { - super.onCreate(savedState) + override fun onCreatePreferences(savedState: Bundle?, s: String?) { addPreferencesFromResource(arguments.getInt(RESOURCE_FILE)) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.java b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.java deleted file mode 100644 index 5dc3d90d80..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.java +++ /dev/null @@ -1,35 +0,0 @@ -package eu.kanade.tachiyomi.widget.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; - } - } -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.kt new file mode 100644 index 0000000000..5d65ab8f90 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.kt @@ -0,0 +1,25 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.content.Context +import android.support.v7.preference.ListPreference +import android.util.AttributeSet + +class IntListPreference : ListPreference { + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { + } + + constructor(context: Context) : super(context) { + } + + override fun persistString(value: String?): Boolean { + return value != null && persistInt(value.toInt()) + } + + override fun getPersistedString(defaultReturnValue: String?): String? { + if (sharedPreferences.contains(key)) { + return getPersistedInt(0).toString() + } else { + return defaultReturnValue + } + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LibraryColumnsDialog.java b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LibraryColumnsDialog.java deleted file mode 100644 index 79ae1c3052..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LibraryColumnsDialog.java +++ /dev/null @@ -1,80 +0,0 @@ -package eu.kanade.tachiyomi.widget.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.tachiyomi.R; -import eu.kanade.tachiyomi.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(); - } - -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LibraryColumnsDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LibraryColumnsDialog.kt new file mode 100644 index 0000000000..79f00efcf8 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LibraryColumnsDialog.kt @@ -0,0 +1,56 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.os.Bundle +import android.support.v7.preference.Preference +import android.support.v7.preference.PreferenceDialogFragmentCompat +import android.view.View +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault +import eu.kanade.tachiyomi.ui.setting.SettingsActivity +import kotlinx.android.synthetic.main.pref_library_columns.view.* + +class LibraryColumnsDialog : PreferenceDialogFragmentCompat() { + + companion object { + + fun newInstance(preference: Preference): LibraryColumnsDialog { + val fragment = LibraryColumnsDialog() + val bundle = Bundle(1) + bundle.putString("key", preference.key) + fragment.arguments = bundle + return fragment + } + } + + var portrait: Int = 0 + var landscape: Int = 0 + + val preferences: PreferencesHelper + get() = (activity as SettingsActivity).preferences + + override fun onBindDialogView(view: View) { + super.onBindDialogView(view) + + portrait = preferences.portraitColumns().getOrDefault() + landscape = preferences.landscapeColumns().getOrDefault() + + view.portrait_columns.value = portrait + view.landscape_columns.value = landscape + + view.portrait_columns.setOnValueChangedListener { picker, oldValue, newValue -> + portrait = newValue + } + + view.landscape_columns.setOnValueChangedListener { picker, oldValue, newValue -> + landscape = newValue + } + } + + override fun onDialogClosed(positiveResult: Boolean) { + if (positiveResult) { + preferences.portraitColumns().set(portrait) + preferences.landscapeColumns().set(landscape) + } + } + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.java b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.java deleted file mode 100644 index 0ff7116951..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.java +++ /dev/null @@ -1,78 +0,0 @@ -package eu.kanade.tachiyomi.widget.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.tachiyomi.R; -import eu.kanade.tachiyomi.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(); - -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.kt new file mode 100644 index 0000000000..faec547718 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.kt @@ -0,0 +1,70 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.support.v7.app.AlertDialog +import android.support.v7.preference.PreferenceDialogFragmentCompat +import android.text.Editable +import android.text.TextWatcher +import android.text.method.PasswordTransformationMethod +import android.view.View +import com.dd.processbutton.iml.ActionProcessButton +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.ui.setting.SettingsActivity +import kotlinx.android.synthetic.main.pref_account_login.view.* +import rx.Subscription + +abstract class LoginDialogPreference : PreferenceDialogFragmentCompat() { + + var v: View? = null + private set + + val preferences: PreferencesHelper + get() = (activity as SettingsActivity).preferences + + var requestSubscription: Subscription? = null + + override fun onPrepareDialogBuilder(builder: AlertDialog.Builder) { + // Hide positive button + builder.setPositiveButton("", this) + } + + override fun onBindDialogView(view: View) { + super.onBindDialogView(view) + v = view.apply { + show_password.setOnCheckedChangeListener { v, isChecked -> + if (isChecked) + password.transformationMethod = null + else + password.transformationMethod = PasswordTransformationMethod() + } + + login.setMode(ActionProcessButton.Mode.ENDLESS) + login.setOnClickListener { checkLogin() } + + setCredentialsOnView(this) + + show_password.isEnabled = password.text.isNullOrEmpty() + + password.addTextChangedListener(object : TextWatcher { + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + + override fun afterTextChanged(s: Editable) {} + + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { + if (s.length == 0) { + show_password.isEnabled = true + } + } + }) + } + + } + + override fun onDialogClosed(positiveResult: Boolean) { + requestSubscription?.unsubscribe() + } + + protected abstract fun checkLogin() + + protected abstract fun setCredentialsOnView(view: View) + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.java b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.java deleted file mode 100644 index 4407095789..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.java +++ /dev/null @@ -1,74 +0,0 @@ -package eu.kanade.tachiyomi.widget.preference; - -import android.content.Context; -import android.content.DialogInterface; -import android.view.View; - -import eu.kanade.tachiyomi.R; -import eu.kanade.tachiyomi.data.mangasync.base.MangaSyncService; -import eu.kanade.tachiyomi.data.preference.PreferencesHelper; -import eu.kanade.tachiyomi.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); - }); - - } - -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.kt new file mode 100644 index 0000000000..630c416785 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.kt @@ -0,0 +1,77 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.content.DialogInterface +import android.os.Bundle +import android.support.v7.preference.Preference +import android.view.View +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.mangasync.base.MangaSyncService +import eu.kanade.tachiyomi.ui.setting.SettingsActivity +import eu.kanade.tachiyomi.util.toast +import kotlinx.android.synthetic.main.pref_account_login.view.* +import rx.android.schedulers.AndroidSchedulers +import rx.schedulers.Schedulers + +class MangaSyncLoginDialog : LoginDialogPreference() { + + companion object { + + fun newInstance(preference: Preference): LoginDialogPreference { + val fragment = MangaSyncLoginDialog() + val bundle = Bundle(1) + bundle.putString("key", preference.key) + fragment.arguments = bundle + return fragment + } + } + + lateinit var sync: MangaSyncService + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + val syncId = Integer.parseInt(arguments.getString("key")) + sync = (activity as SettingsActivity).syncManager.getService(syncId) + } + + override fun setCredentialsOnView(view: View) { + view.accounts_login.text = getString(R.string.accounts_login_title, sync.name) + view.username.setText(preferences.getMangaSyncUsername(sync)) + view.password.setText(preferences.getMangaSyncPassword(sync)) + } + + override fun checkLogin() { + requestSubscription?.unsubscribe() + + v?.apply { + if (username.text.length == 0 || password.text.length == 0) + return + + login.progress = 1 + + requestSubscription = sync.login(username.text.toString(), password.text.toString()) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ logged -> + if (logged) { + preferences.setMangaSyncCredentials(sync, + username.text.toString(), + password.text.toString()) + + // Simulate a positive button click and dismiss the dialog + onClick(dialog, DialogInterface.BUTTON_POSITIVE) + dialog.dismiss() + context.toast(R.string.login_success) + } else { + preferences.setMangaSyncCredentials(sync, "", "") + login.progress = -1 + } + }, { error -> + login.progress = -1 + login.setText(R.string.unknown_error) + }) + + } + } + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SimpleDialogPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SimpleDialogPreference.kt new file mode 100644 index 0000000000..7b31db1cbc --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SimpleDialogPreference.kt @@ -0,0 +1,11 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.content.Context +import android.support.v7.preference.DialogPreference +import android.support.v7.preference.R.attr +import android.util.AttributeSet + +open class SimpleDialogPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = attr.dialogPreferenceStyle, defStyleRes: Int = 0) : + DialogPreference(context, attrs, defStyleAttr, defStyleRes) { + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.java b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.java deleted file mode 100644 index 1d39315cb9..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.java +++ /dev/null @@ -1,74 +0,0 @@ -package eu.kanade.tachiyomi.widget.preference; - -import android.content.Context; -import android.content.DialogInterface; -import android.view.View; - -import eu.kanade.tachiyomi.R; -import eu.kanade.tachiyomi.data.preference.PreferencesHelper; -import eu.kanade.tachiyomi.data.source.base.Source; -import eu.kanade.tachiyomi.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); - }); - - } - -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.kt new file mode 100644 index 0000000000..47bcc8e20e --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.kt @@ -0,0 +1,76 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.content.DialogInterface +import android.os.Bundle +import android.support.v7.preference.Preference +import android.view.View +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.source.base.Source +import eu.kanade.tachiyomi.ui.setting.SettingsActivity +import eu.kanade.tachiyomi.util.toast +import kotlinx.android.synthetic.main.pref_account_login.view.* +import rx.android.schedulers.AndroidSchedulers +import rx.schedulers.Schedulers + +class SourceLoginDialog : LoginDialogPreference() { + + companion object { + + fun newInstance(preference: Preference): LoginDialogPreference { + val fragment = SourceLoginDialog() + val bundle = Bundle(1) + bundle.putString("key", preference.key) + fragment.arguments = bundle + return fragment + } + } + + lateinit var source: Source + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + val sourceId = Integer.parseInt(arguments.getString("key")) + source = (activity as SettingsActivity).sourceManager.get(sourceId)!! + } + + override fun setCredentialsOnView(view: View) { + view.accounts_login.text = getString(R.string.accounts_login_title, source.name) + view.username.setText(preferences.getSourceUsername(source)) + view.password.setText(preferences.getSourcePassword(source)) + } + + override fun checkLogin() { + requestSubscription?.unsubscribe() + + v?.apply { + if (username.text.length == 0 || password.text.length == 0) + return + + login.progress = 1 + + requestSubscription = source.login(username.text.toString(), password.text.toString()) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ logged -> + if (logged) { + preferences.setSourceCredentials(source, + username.text.toString(), + password.text.toString()) + + // Simulate a positive button click and dismiss the dialog + onClick(dialog, DialogInterface.BUTTON_POSITIVE) + dialog.dismiss() + context.toast(R.string.login_success) + } else { + preferences.setSourceCredentials(source, "", "") + login.progress = -1 + } + }, { error -> + login.progress = -1 + login.setText(R.string.unknown_error) + }) + } + } + +} diff --git a/app/src/main/res/values-v21/styles.xml b/app/src/main/res/values-v21/styles.xml deleted file mode 100644 index cc0e9350b9..0000000000 --- a/app/src/main/res/values-v21/styles.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 6023805e51..854e178958 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,4 +1,4 @@ - +