Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ColorPickerDialogFragment.kt

114 lines
3.8 KiB
Kotlin
Raw Normal View History

2016-07-02 05:54:53 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program 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 program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.fragment
import android.app.Dialog
import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
2020-01-26 08:35:15 +01:00
import androidx.core.content.ContextCompat
import androidx.appcompat.app.AlertDialog
2016-07-02 05:54:53 +02:00
import me.uucky.colorpicker.ColorPickerDialog
import org.mariotaku.twidere.Constants.*
import org.mariotaku.twidere.R
2017-02-05 14:42:20 +01:00
import org.mariotaku.twidere.extension.applyTheme
2017-06-19 15:45:41 +02:00
import org.mariotaku.twidere.extension.onShow
2016-07-02 05:54:53 +02:00
import org.mariotaku.twidere.fragment.iface.IDialogFragmentCallback
class ColorPickerDialogFragment : BaseDialogFragment(), DialogInterface.OnClickListener {
2019-11-20 07:10:24 +01:00
private var controller: ColorPickerDialog.Controller? = null
2016-07-02 05:54:53 +02:00
2020-01-26 08:35:15 +01:00
override fun onCancel(dialog: DialogInterface) {
2016-07-02 05:54:53 +02:00
super.onCancel(dialog)
val a = activity
if (a is Callback) {
a.onCancelled()
}
}
override fun onClick(dialog: DialogInterface, which: Int) {
val a = activity
2019-11-20 07:10:24 +01:00
if (a !is Callback || controller == null) return
2016-07-02 05:54:53 +02:00
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
2019-11-20 07:10:24 +01:00
val color = controller!!.color
2016-07-02 05:54:53 +02:00
a.onColorSelected(color)
}
DialogInterface.BUTTON_NEUTRAL -> {
a.onColorCleared()
}
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val color: Int
val args = arguments
2020-06-09 01:09:31 +02:00
color =
savedInstanceState?.getInt(EXTRA_COLOR, Color.WHITE) ?: args!!.getInt(EXTRA_COLOR, Color.WHITE)
2016-07-02 05:54:53 +02:00
val activity = activity
val builder = AlertDialog.Builder(requireActivity())
2016-07-02 05:54:53 +02:00
builder.setView(me.uucky.colorpicker.R.layout.cp__dialog_color_picker)
builder.setPositiveButton(android.R.string.ok, this)
2020-01-26 08:35:15 +01:00
if (args!!.getBoolean(EXTRA_CLEAR_BUTTON, false)) {
2016-12-26 17:26:09 +01:00
builder.setNeutralButton(R.string.action_clear, this)
2016-07-02 05:54:53 +02:00
}
builder.setNegativeButton(android.R.string.cancel, this)
val dialog = builder.create()
2017-06-19 15:45:41 +02:00
dialog.onShow {
2017-02-05 14:42:20 +01:00
it.applyTheme()
2019-11-20 07:10:24 +01:00
controller = ColorPickerDialog.Controller(it.context, it.window!!.decorView)
2016-07-02 05:54:53 +02:00
val showAlphaSlider = args.getBoolean(EXTRA_ALPHA_SLIDER, true)
for (presetColor in PRESET_COLORS) {
controller!!.addColor(ContextCompat.getColor(requireContext(), presetColor))
2016-07-02 05:54:53 +02:00
}
2019-11-20 07:10:24 +01:00
controller!!.setAlphaEnabled(showAlphaSlider)
controller!!.setInitialColor(color)
2016-07-02 05:54:53 +02:00
}
return dialog
}
2020-01-26 08:35:15 +01:00
override fun onDismiss(dialog: DialogInterface) {
2016-07-02 05:54:53 +02:00
super.onDismiss(dialog)
val a = activity
if (a is Callback) {
a.onDismissed()
}
}
2020-01-26 08:35:15 +01:00
override fun onSaveInstanceState(outState: Bundle) {
2019-11-20 07:10:24 +01:00
if (controller != null) {
2020-05-31 08:44:38 +02:00
outState.putInt(EXTRA_COLOR, controller!!.color)
2016-07-02 05:54:53 +02:00
}
super.onSaveInstanceState(outState)
}
interface Callback : IDialogFragmentCallback {
fun onColorCleared()
fun onColorSelected(color: Int)
}
}