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

127 lines
4.8 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.graphics.PorterDuff
import android.os.Bundle
import android.util.AttributeSet
import android.widget.Toast
import androidx.preference.PreferenceDialogFragmentCompat
2019-07-22 15:26:37 +00:00
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import code.name.monkey.appthemehelper.ThemeStore
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
import code.name.monkey.retromusic.model.CategoryInfo
import code.name.monkey.retromusic.util.PreferenceUtil
2019-09-17 19:36:13 +00:00
import com.afollestad.materialdialogs.LayoutMode
2019-07-22 15:26:37 +00:00
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.bottomsheets.BottomSheet
import com.afollestad.materialdialogs.customview.customView
import java.util.*
class LibraryPreference : ATEDialogPreference {
2019-07-31 06:48:55 +00:00
constructor(context: Context) : super(context)
2019-07-22 15:26:37 +00:00
2019-07-31 06:48:55 +00:00
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
2019-07-22 15:26:37 +00:00
2019-07-31 06:48:55 +00:00
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
2019-07-22 15:26:37 +00:00
2019-07-31 06:48:55 +00:00
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
2019-07-22 15:26:37 +00:00
init {
icon?.setColorFilter(ThemeStore.textColorSecondary(context), PorterDuff.Mode.SRC_IN)
2019-07-22 15:26:37 +00:00
}
}
class LibraryPreferenceDialog : PreferenceDialogFragmentCompat() {
override fun onDialogClosed(positiveResult: Boolean) {
}
2019-07-22 15:26:37 +00:00
lateinit var adapter: CategoryInfoAdapter
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
2019-11-04 18:43:54 +00:00
val view = requireActivity().layoutInflater.inflate(R.layout.preference_dialog_library_categories, null)
2019-07-22 15:26:37 +00:00
2019-12-08 15:18:08 +00:00
val categoryInfos: List<CategoryInfo> = if (savedInstanceState != null) {
savedInstanceState.getParcelableArrayList(PreferenceUtil.LIBRARY_CATEGORIES)!!
2019-07-22 15:26:37 +00:00
} else {
2019-12-08 15:18:08 +00:00
PreferenceUtil.getInstance(requireContext()).libraryCategoryInfos
2019-07-22 15:26:37 +00:00
}
adapter = CategoryInfoAdapter(categoryInfos)
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)
recyclerView.adapter = adapter
adapter.attachToRecyclerView(recyclerView)
2019-09-17 19:36:13 +00:00
return MaterialDialog(requireContext(), BottomSheet(LayoutMode.WRAP_CONTENT))
2019-11-04 18:43:54 +00:00
.title(R.string.library_categories)
2019-09-17 19:36:13 +00:00
.cornerRadius(PreferenceUtil.getInstance(requireContext()).dialogCorner)
2019-07-22 15:26:37 +00:00
.customView(view = view)
.positiveButton(android.R.string.ok) {
updateCategories(adapter.categoryInfos)
dismiss()
}
.negativeButton(android.R.string.cancel) {
dismiss()
}
2019-11-04 18:43:54 +00:00
.neutralButton(R.string.reset_action) {
adapter.categoryInfos = PreferenceUtil.getInstance(requireContext()).defaultLibraryCategoryInfos
2019-07-22 15:26:37 +00:00
}
.noAutoDismiss()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelableArrayList(PreferenceUtil.LIBRARY_CATEGORIES, ArrayList(adapter.categoryInfos))
}
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
}
PreferenceUtil.getInstance(requireContext()).libraryCategoryInfos = 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(key: String): LibraryPreferenceDialog {
val bundle = Bundle()
bundle.putString(ARG_KEY, key)
val fragment = LibraryPreferenceDialog()
fragment.arguments = bundle
return fragment
2019-07-22 15:26:37 +00:00
}
}
}