mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 06:17:57 +01:00 
			
		
		
		
	Preferences ported to support library
This commit is contained in:
		| @@ -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) {} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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() | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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)) | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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 | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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(); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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) | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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(); | ||||
|  | ||||
| } | ||||
| @@ -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) | ||||
|  | ||||
| } | ||||
| @@ -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); | ||||
|                 }); | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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) | ||||
|                     }) | ||||
|  | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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) { | ||||
|  | ||||
| } | ||||
| @@ -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); | ||||
|                 }); | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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) | ||||
|                     }) | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user