PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/fragments/settings/AbsSettingsFragment.kt

94 lines
3.9 KiB
Kotlin
Raw Normal View History

2019-05-11 13:53:20 +00:00
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
2019-04-20 05:29:45 +00:00
package code.name.monkey.retromusic.fragments.settings
2019-02-19 10:38:51 +00:00
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.retromusic.preferences.*
import code.name.monkey.retromusic.util.NavigationUtil
/**
* @author Hemanth S (h4h13).
*/
abstract class AbsSettingsFragment : PreferenceFragmentCompat() {
2019-06-29 17:15:31 +00:00
2019-02-19 10:38:51 +00:00
internal fun showProToastAndNavigate(message: String) {
2019-08-05 16:13:44 +00:00
Toast.makeText(requireContext(), "$message is Pro version feature.", Toast.LENGTH_SHORT).show()
NavigationUtil.goToProVersion(requireActivity())
2019-02-19 10:38:51 +00:00
}
internal fun setSummary(preference: Preference, value: Any) {
val stringValue = value.toString()
if (preference is ListPreference) {
val index = preference.findIndexOfValue(stringValue)
preference.setSummary(if (index >= 0) preference.entries[index] else null)
} else {
preference.summary = stringValue
}
}
abstract fun invalidateSettings()
2019-06-29 17:15:31 +00:00
protected fun setSummary(preference: Preference) {
setSummary(preference, PreferenceManager
.getDefaultSharedPreferences(preference.context)
.getString(preference.key, "")!!)
}
2019-02-19 10:38:51 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setDivider(ColorDrawable(Color.TRANSPARENT))
2019-08-05 16:13:44 +00:00
listView.setBackgroundColor(ThemeStore.primaryColor(requireContext()))
2019-02-19 10:38:51 +00:00
listView.overScrollMode = View.OVER_SCROLL_NEVER
listView.setPadding(0, 0, 0, 0)
listView.setPaddingRelative(0, 0, 0, 0)
invalidateSettings()
}
override fun onDisplayPreferenceDialog(preference: Preference) {
2019-06-29 17:15:31 +00:00
var dialogFragment: DialogFragment? = null// Dialog creation could not be handled here. Try with the super method.
// The dialog was created (it was one of our custom Preferences), show the dialog for it
when (preference) {
is LibraryPreference -> dialogFragment = LibraryPreferenceDialog.newInstance(preference.key)
is NowPlayingScreenPreference -> dialogFragment = NowPlayingScreenPreferenceDialog.newInstance(preference.key)
is AlbumCoverStylePreference -> dialogFragment = AlbumCoverStylePreferenceDialog.newInstance(preference.key)
2019-06-29 17:15:31 +00:00
is MaterialListPreference -> {
preference.entries
dialogFragment = MaterialListPreferenceDialog.newInstance(preference)
}
is BlacklistPreference -> dialogFragment = BlacklistPreferenceDialog.newInstance()
2019-02-19 10:38:51 +00:00
}
2019-06-29 17:15:31 +00:00
2019-02-19 10:38:51 +00:00
if (dialogFragment != null) {
// The dialog was created (it was one of our custom Preferences), show the dialog for it
dialogFragment.setTargetFragment(this, 0);
2019-03-25 12:43:43 +00:00
dialogFragment.show(this.fragmentManager!!, "android.support.v7.preference.PreferenceFragment.DIALOG");
2019-02-19 10:38:51 +00:00
} else {
// Dialog creation could not be handled here. Try with the super method.
super.onDisplayPreferenceDialog(preference);
}
}
}