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

85 lines
3.3 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
2018-12-06 08:52:57 +00:00
2019-09-29 09:33:41 +00:00
import android.content.SharedPreferences
2019-06-29 17:15:31 +00:00
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
2018-12-06 08:52:57 +00:00
import android.os.Bundle
2019-09-29 09:33:41 +00:00
import androidx.preference.Preference
2018-12-06 08:52:57 +00:00
import androidx.preference.TwoStatePreference
2019-08-05 16:13:44 +00:00
import code.name.monkey.retromusic.R
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
2019-06-29 17:15:31 +00:00
2018-12-06 08:52:57 +00:00
/**
* @author Hemanth S (h4h13).
*/
2019-09-29 09:33:41 +00:00
class NotificationSettingsFragment : AbsSettingsFragment(), SharedPreferences.OnSharedPreferenceChangeListener {
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == PreferenceUtil.CLASSIC_NOTIFICATION) {
if (VERSION.SDK_INT >= VERSION_CODES.O) {
findPreference<Preference>("colored_notification")?.isEnabled = sharedPreferences?.getBoolean(key, false)!!
}
}
}
2018-12-06 08:52:57 +00:00
override fun invalidateSettings() {
2019-06-29 17:15:31 +00:00
val classicNotification: TwoStatePreference? = findPreference("classic_notification")
if (VERSION.SDK_INT < VERSION_CODES.N) {
classicNotification?.isVisible = false
2018-12-06 08:52:57 +00:00
} else {
2019-06-29 17:15:31 +00:00
classicNotification?.apply {
isChecked = PreferenceUtil.getInstance(requireContext()).classicNotification()
2019-06-29 17:15:31 +00:00
setOnPreferenceChangeListener { _, newValue ->
// Save preference
PreferenceUtil.getInstance(requireContext()).setClassicNotification(newValue as Boolean)
2019-06-29 17:15:31 +00:00
invalidateSettings()
true
2018-12-06 08:52:57 +00:00
}
2019-06-29 17:15:31 +00:00
}
}
2018-12-06 08:52:57 +00:00
2019-06-29 17:15:31 +00:00
val coloredNotification: TwoStatePreference? = findPreference("colored_notification")
if (VERSION.SDK_INT >= VERSION_CODES.O) {
coloredNotification?.isEnabled = PreferenceUtil.getInstance(requireContext()).classicNotification()
2019-06-29 17:15:31 +00:00
} else {
coloredNotification?.apply {
isChecked = PreferenceUtil.getInstance(requireContext()).coloredNotification()
2019-06-29 17:15:31 +00:00
setOnPreferenceChangeListener { _, newValue ->
PreferenceUtil.getInstance(requireContext()).setColoredNotification(newValue as Boolean)
2019-06-29 17:15:31 +00:00
true
}
2018-12-06 08:52:57 +00:00
}
}
}
2019-09-29 09:33:41 +00:00
override fun onResume() {
super.onResume()
PreferenceUtil.getInstance(requireContext()).registerOnSharedPreferenceChangedListener(this)
}
override fun onDestroyView() {
super.onDestroyView()
PreferenceUtil.getInstance(requireContext()).unregisterOnSharedPreferenceChangedListener(this)
}
2018-12-06 10:23:03 +00:00
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
2019-08-05 16:13:44 +00:00
addPreferencesFromResource(R.xml.pref_notification)
2018-12-06 08:52:57 +00:00
}
}