From 1e5b39a35d0d3b72a4832655c97a16b351dcc70a Mon Sep 17 00:00:00 2001 From: fakeyatogod Date: Sat, 28 Oct 2023 23:23:05 +0530 Subject: [PATCH] Get sources from sourceSelector --- .../animescrapsources/SourceSelector.kt | 2 +- .../ui/fragments/SettingsFragment.kt | 30 +++++++++++++++++-- app/src/main/res/xml/root_preferences.xml | 17 ++++++----- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/animeSources/src/main/java/com/talent/animescrapsources/SourceSelector.kt b/animeSources/src/main/java/com/talent/animescrapsources/SourceSelector.kt index 408d7b1..816f44c 100644 --- a/animeSources/src/main/java/com/talent/animescrapsources/SourceSelector.kt +++ b/animeSources/src/main/java/com/talent/animescrapsources/SourceSelector.kt @@ -5,7 +5,7 @@ import com.talent.animescrap_common.source.AnimeSource import com.talent.animescrapsources.animesources.* class SourceSelector(context: Context) { - private val sourceMap: Map = hashMapOf( + val sourceMap: Map = mapOf( "yugen" to YugenSource(), "allanime" to AllAnimeSource(), "enime" to EnimeSource(), diff --git a/app/src/main/java/com/talent/animescrap/ui/fragments/SettingsFragment.kt b/app/src/main/java/com/talent/animescrap/ui/fragments/SettingsFragment.kt index 2e1f773..7ce115d 100644 --- a/app/src/main/java/com/talent/animescrap/ui/fragments/SettingsFragment.kt +++ b/app/src/main/java/com/talent/animescrap/ui/fragments/SettingsFragment.kt @@ -7,19 +7,24 @@ import android.os.Build import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity -import androidx.appcompat.app.AppCompatDelegate.* +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES +import androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode import androidx.lifecycle.ViewModelProvider import androidx.preference.ListPreference import androidx.preference.Preference +import androidx.preference.PreferenceCategory import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreferenceCompat import com.google.android.material.snackbar.Snackbar import com.talent.animescrap.R import com.talent.animescrap.ui.activities.MainActivity import com.talent.animescrap.ui.viewmodels.UpdateViewModel -import com.talent.animescrap_common.utils.Utils import com.talent.animescrap_common.utils.Utils.httpClient +import com.talent.animescrapsources.SourceSelector import dagger.hilt.android.AndroidEntryPoint +import java.util.Locale import kotlin.system.exitProcess @AndroidEntryPoint @@ -27,6 +32,27 @@ class SettingsFragment : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.root_preferences, rootKey) + + val sourceCategory = preferenceScreen.findPreference("source_pref_category") + + val sourcePreference = ListPreference(requireContext()) + sourcePreference.key = "source" + sourcePreference.title = getString(R.string.source_list_pref_title) + val sourceList = SourceSelector(requireContext()).sourceMap.keys + sourcePreference.entryValues = sourceList.toTypedArray() + sourcePreference.entries = sourceList.map { + it.replaceFirstChar { str -> if (str.isLowerCase()) str.titlecase(Locale.ROOT) else str.toString() } + .replace("_", " ") + }.toTypedArray() + sourcePreference.setDefaultValue("yugen") + sourcePreference.summaryProvider = Preference.SummaryProvider { preference -> + val newValue = preference.value + "Source set to ${newValue?.uppercase()?.replace("_", " ")}" + } +// preferenceScreen.addPreference(sourceCategory) + sourceCategory?.addPreference(sourcePreference) + + val dynamicColorsPref = findPreference("dynamic_colors") dynamicColorsPref?.setOnPreferenceChangeListener { _, newValue -> view?.let { diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 357bde3..29f089a 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -21,14 +21,15 @@ - - + + + + + + + +