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

101 lines
3.7 KiB
Kotlin
Raw Normal View History

2019-07-22 15:26:37 +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.
*/
package code.name.monkey.retromusic.preferences
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
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
2019-07-22 15:26:37 +00:00
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import code.name.monkey.appthemehelper.common.prefs.supportv7.ATEDialogPreference
2019-11-04 18:43:54 +00:00
import code.name.monkey.retromusic.R
2019-07-22 15:26:37 +00:00
import code.name.monkey.retromusic.adapter.CategoryInfoAdapter
2020-07-19 11:21:15 +00:00
import code.name.monkey.retromusic.extensions.colorButtons
2020-02-22 12:10:12 +00:00
import code.name.monkey.retromusic.extensions.colorControlNormal
2020-07-20 19:51:15 +00:00
import code.name.monkey.retromusic.extensions.materialDialog
2019-07-22 15:26:37 +00:00
import code.name.monkey.retromusic.model.CategoryInfo
2020-06-06 18:57:28 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
2020-05-23 13:53:10 +00:00
2019-07-22 15:26:37 +00:00
2020-02-22 12:10:12 +00:00
class LibraryPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
2020-05-23 13:53:10 +00:00
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : ATEDialogPreference(context, attrs, defStyleAttr, defStyleRes) {
2019-07-22 15:26:37 +00:00
init {
2020-05-23 13:53:10 +00:00
icon?.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
context.colorControlNormal(),
SRC_IN
)
2019-07-22 15:26:37 +00:00
}
}
2020-05-23 13:53:10 +00:00
class LibraryPreferenceDialog : DialogFragment() {
2019-07-22 15:26:37 +00:00
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val view = layoutInflater
.inflate(R.layout.preference_dialog_library_categories, null)
2019-07-22 15:26:37 +00:00
2020-05-23 13:53:10 +00:00
val categoryAdapter = CategoryInfoAdapter()
2020-06-06 18:57:28 +00:00
categoryAdapter.categoryInfos = PreferenceUtil.libraryCategory
2019-11-04 18:43:54 +00:00
val recyclerView = view.findViewById<RecyclerView>(R.id.recycler_view)
2019-07-22 15:26:37 +00:00
recyclerView.layoutManager = LinearLayoutManager(activity)
2020-05-23 13:53:10 +00:00
recyclerView.adapter = categoryAdapter
categoryAdapter.attachToRecyclerView(recyclerView)
2019-07-22 15:26:37 +00:00
2020-07-20 19:51:15 +00:00
return materialDialog(R.string.library_categories)
2020-05-23 13:53:10 +00:00
.setNeutralButton(
R.string.reset_action
) { _, _ ->
updateCategories(PreferenceUtil.defaultCategories)
2020-02-22 12:10:12 +00:00
}
2020-05-23 13:53:10 +00:00
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton( R.string.done) { _, _ -> updateCategories(categoryAdapter.categoryInfos) }
2020-05-23 13:53:10 +00:00
.setView(view)
.create()
2020-07-19 11:21:15 +00:00
.colorButtons()
2019-07-22 15:26:37 +00:00
}
private fun updateCategories(categories: List<CategoryInfo>) {
if (getSelected(categories) == 0) return
if (getSelected(categories) > 5) {
Toast.makeText(context, "Not more than 5 items", Toast.LENGTH_SHORT).show()
return
}
2020-06-06 18:57:28 +00:00
PreferenceUtil.libraryCategory = categories
2019-07-22 15:26:37 +00:00
}
private fun getSelected(categories: List<CategoryInfo>): Int {
var selected = 0
for (categoryInfo in categories) {
if (categoryInfo.visible)
selected++
}
return selected
}
companion object {
fun newInstance(): LibraryPreferenceDialog {
return LibraryPreferenceDialog()
2019-07-22 15:26:37 +00:00
}
}
}