PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/preferences/NowPlayingScreenPreferenceD...

163 lines
5.9 KiB
Kotlin
Raw Normal View History

2019-03-03 09:30:48 +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.
*/
2018-12-06 08:52:57 +00:00
package code.name.monkey.retromusic.preferences
import android.app.Dialog
import android.content.Context
import android.os.Bundle
2019-02-19 10:38:51 +00:00
import android.util.AttributeSet
2018-12-06 08:52:57 +00:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
2020-02-22 12:10:12 +00:00
import androidx.core.graphics.BlendModeColorFilterCompat
import androidx.core.graphics.BlendModeCompat.SRC_IN
2020-05-23 13:53:10 +00:00
import androidx.fragment.app.DialogFragment
2018-12-06 08:52:57 +00:00
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import code.name.monkey.appthemehelper.common.prefs.supportv7.ATEDialogPreference
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.extensions.*
2019-04-20 05:29:45 +00:00
import code.name.monkey.retromusic.fragments.NowPlayingScreen
import code.name.monkey.retromusic.fragments.NowPlayingScreen.*
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.util.NavigationUtil
2020-06-06 18:57:28 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.util.ViewUtil
import com.bumptech.glide.Glide
2020-02-22 12:10:12 +00:00
class NowPlayingScreenPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = -1,
defStyleRes: Int = -1
) : ATEDialogPreference(context, attrs, defStyleAttr, defStyleRes) {
2019-02-19 10:38:51 +00:00
private val mLayoutRes = R.layout.preference_dialog_now_playing_screen
override fun getDialogLayoutResource(): Int {
return mLayoutRes
2019-02-19 10:38:51 +00:00
}
2019-07-08 14:35:46 +00:00
init {
2020-02-22 12:10:12 +00:00
icon?.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
2020-05-20 20:28:38 +00:00
context.colorControlNormal(),
2020-02-22 12:10:12 +00:00
SRC_IN
)
2019-07-08 14:35:46 +00:00
}
2019-02-19 10:38:51 +00:00
}
2020-05-23 13:53:10 +00:00
class NowPlayingScreenPreferenceDialog : DialogFragment(), ViewPager.OnPageChangeListener {
2018-12-06 08:52:57 +00:00
private var viewPagerPosition: Int = 0
2019-02-19 10:38:51 +00:00
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
2018-12-06 08:52:57 +00:00
}
2019-02-19 10:38:51 +00:00
override fun onPageSelected(position: Int) {
this.viewPagerPosition = position
2018-12-06 08:52:57 +00:00
}
2019-02-19 10:38:51 +00:00
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val view = layoutInflater
.inflate(R.layout.preference_dialog_now_playing_screen, null)
2019-02-19 10:38:51 +00:00
val viewPager = view.findViewById<ViewPager>(R.id.now_playing_screen_view_pager)
?: throw IllegalStateException("Dialog view must contain a ViewPager with id 'now_playing_screen_view_pager'")
viewPager.adapter = NowPlayingScreenAdapter(requireContext())
2019-02-19 10:38:51 +00:00
viewPager.addOnPageChangeListener(this)
viewPager.pageMargin = ViewUtil.convertDpToPixel(32f, resources).toInt()
2020-06-06 18:57:28 +00:00
viewPager.currentItem = PreferenceUtil.nowPlayingScreen.ordinal
2019-02-19 10:38:51 +00:00
2020-07-20 19:51:15 +00:00
return materialDialog(R.string.pref_title_now_playing_screen_appearance)
2020-05-23 13:53:10 +00:00
.setCancelable(false)
.setPositiveButton(R.string.set) { _, _ ->
val nowPlayingScreen = values()[viewPagerPosition]
if (isNowPlayingThemes(nowPlayingScreen)) {
val result =
"${getString(nowPlayingScreen.titleRes)} theme is Pro version feature."
2020-05-23 13:53:10 +00:00
Toast.makeText(context, result, Toast.LENGTH_SHORT).show()
NavigationUtil.goToProVersion(requireContext())
} else {
2020-06-06 18:57:28 +00:00
PreferenceUtil.nowPlayingScreen = nowPlayingScreen
2020-05-23 13:53:10 +00:00
}
}
.setView(view)
.create()
2020-07-19 11:21:15 +00:00
.colorButtons()
2019-02-19 10:38:51 +00:00
}
2018-12-06 08:52:57 +00:00
2019-02-19 10:38:51 +00:00
companion object {
2020-05-23 13:53:10 +00:00
fun newInstance(): NowPlayingScreenPreferenceDialog {
return NowPlayingScreenPreferenceDialog()
2019-02-19 10:38:51 +00:00
}
2018-12-06 08:52:57 +00:00
}
2019-02-19 10:38:51 +00:00
}
2018-12-06 08:52:57 +00:00
private class NowPlayingScreenAdapter(private val context: Context) : PagerAdapter() {
2018-12-06 08:52:57 +00:00
2019-02-19 10:38:51 +00:00
override fun instantiateItem(collection: ViewGroup, position: Int): Any {
val nowPlayingScreen = values()[position]
2018-12-06 08:52:57 +00:00
2019-02-19 10:38:51 +00:00
val inflater = LayoutInflater.from(context)
val layout = inflater.inflate(
R.layout.preference_now_playing_screen_item,
collection,
false
) as ViewGroup
2019-02-19 10:38:51 +00:00
collection.addView(layout)
2018-12-06 08:52:57 +00:00
2019-02-19 10:38:51 +00:00
val image = layout.findViewById<ImageView>(R.id.image)
val title = layout.findViewById<TextView>(R.id.title)
val proText = layout.findViewById<TextView>(R.id.proText)
2019-02-19 10:38:51 +00:00
Glide.with(context).load(nowPlayingScreen.drawableResId).into(image)
title.setText(nowPlayingScreen.titleRes)
if (isNowPlayingThemes(nowPlayingScreen)) {
proText.show()
proText.setText(R.string.pro)
}else{
proText.hide()
}
2019-02-19 10:38:51 +00:00
return layout
}
2018-12-06 08:52:57 +00:00
override fun destroyItem(
collection: ViewGroup,
position: Int,
view: Any
) {
2019-02-19 10:38:51 +00:00
collection.removeView(view as View)
}
2018-12-06 08:52:57 +00:00
2019-02-19 10:38:51 +00:00
override fun getCount(): Int {
return values().size
2018-12-06 08:52:57 +00:00
}
override fun isViewFromObject(view: View, instance: Any): Boolean {
return view === instance
2019-02-19 10:38:51 +00:00
}
2018-12-06 08:52:57 +00:00
2019-02-19 10:38:51 +00:00
override fun getPageTitle(position: Int): CharSequence? {
return context.getString(values()[position].titleRes)
2018-12-06 08:52:57 +00:00
}
}
private fun isNowPlayingThemes(screen: NowPlayingScreen): Boolean {
return (screen == Full || screen == Card || screen == Plain || screen == Blur || screen == Color || screen == Simple || screen == BlurCard || screen == Circle || screen == Adaptive) && !App.isProVersion()
2019-02-19 10:38:51 +00:00
}