/* * 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 import androidx.core.graphics.BlendModeColorFilterCompat import androidx.core.graphics.BlendModeCompat.SRC_IN import androidx.fragment.app.DialogFragment import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import code.name.monkey.appthemehelper.common.prefs.supportv7.ATEDialogPreference import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.CategoryInfoAdapter import code.name.monkey.retromusic.extensions.colorButtons import code.name.monkey.retromusic.extensions.colorControlNormal import code.name.monkey.retromusic.extensions.materialDialog import code.name.monkey.retromusic.model.CategoryInfo import code.name.monkey.retromusic.util.PreferenceUtil class LibraryPreference @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0 ) : ATEDialogPreference(context, attrs, defStyleAttr, defStyleRes) { init { icon?.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat( context.colorControlNormal(), SRC_IN ) } } class LibraryPreferenceDialog : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val view = layoutInflater .inflate(R.layout.preference_dialog_library_categories, null) val categoryAdapter = CategoryInfoAdapter() categoryAdapter.categoryInfos = PreferenceUtil.libraryCategory val recyclerView = view.findViewById(R.id.recycler_view) recyclerView.layoutManager = LinearLayoutManager(activity) recyclerView.adapter = categoryAdapter categoryAdapter.attachToRecyclerView(recyclerView) return materialDialog(R.string.library_categories) .setNeutralButton( R.string.reset_action ) { _, _ -> updateCategories(PreferenceUtil.defaultCategories) } .setNegativeButton(android.R.string.cancel, null) .setPositiveButton( R.string.done) { _, _ -> updateCategories(categoryAdapter.categoryInfos) } .setView(view) .create() .colorButtons() } private fun updateCategories(categories: List) { if (getSelected(categories) == 0) return if (getSelected(categories) > 5) { Toast.makeText(context, "Not more than 5 items", Toast.LENGTH_SHORT).show() return } PreferenceUtil.libraryCategory = categories } private fun getSelected(categories: List): Int { var selected = 0 for (categoryInfo in categories) { if (categoryInfo.visible) selected++ } return selected } companion object { fun newInstance(): LibraryPreferenceDialog { return LibraryPreferenceDialog() } } }