Skip to content

Commit

Permalink
Abstract ColorPickerDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
massivemadness committed Nov 11, 2023
1 parent 11ac1c2 commit 7f06620
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 86 deletions.
1 change: 1 addition & 0 deletions common-ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ dependencies {
// UI
implementation(libs.androidx.appcompat)
implementation(libs.materialdesign)
implementation(libs.colorpicker)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 Squircle CE contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blacksquircle.ui.uikit

import android.app.Dialog
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.blacksquircle.ui.uikit.databinding.DialogColorPickerBinding
import com.skydoves.colorpickerview.flag.BubbleFlag
import com.skydoves.colorpickerview.flag.FlagMode

abstract class ColorPickerDialog : DialogFragment() {

abstract val titleRes: Int
abstract val positiveRes: Int
abstract val negativeRes: Int
abstract val initialColor: Int

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogColorPickerBinding.inflate(layoutInflater)
binding.colorPicker.setInitialColor(initialColor)
binding.colorPicker.attachAlphaSlider(binding.alphaSlideBar)
binding.colorPicker.attachBrightnessSlider(binding.brightnessSlideBar)
binding.colorPicker.flagView = BubbleFlag(requireContext()).apply {
flagMode = FlagMode.FADE
}
return AlertDialog.Builder(requireContext())
.setTitle(titleRes)
.setView(binding.root)
.setPositiveButton(positiveRes) { _, _ -> onColorSelected(binding.colorPicker.color) }
.setNegativeButton(negativeRes, null)
.create()
}

abstract fun onColorSelected(color: Int)
}
49 changes: 49 additions & 0 deletions common-ui/src/main/res/layout/dialog_color_picker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2023 Squircle CE contributors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.skydoves.colorpickerview.ColorPickerView
android:layout_width="236dp"
android:layout_height="236dp"
android:layout_gravity="center"
android:layout_marginTop="24dp"
android:id="@+id/colorPicker" />

<com.skydoves.colorpickerview.sliders.AlphaSlideBar
android:id="@+id/alphaSlideBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:selector_AlphaSlideBar="@drawable/colorpickerview_wheel" />

<com.skydoves.colorpickerview.sliders.BrightnessSlideBar
android:id="@+id/brightnessSlideBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:selector_BrightnessSlider="@drawable/colorpickerview_wheel" />

</LinearLayout>
1 change: 0 additions & 1 deletion feature-editor/impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.constraintlayout)
implementation(libs.materialdesign)
implementation(libs.colorpicker)

// AAC
implementation(libs.androidx.viewmodel)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023 Squircle CE contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blacksquircle.ui.feature.editor.ui.dialog

import android.graphics.Color
import androidx.fragment.app.activityViewModels
import com.blacksquircle.ui.feature.editor.R
import com.blacksquircle.ui.feature.editor.ui.mvi.EditorIntent
import com.blacksquircle.ui.feature.editor.ui.viewmodel.EditorViewModel
import com.blacksquircle.ui.uikit.ColorPickerDialog
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class InsertColorDialog : ColorPickerDialog() {

override val titleRes = R.string.dialog_title_color_picker
override val positiveRes = R.string.action_insert
override val negativeRes = android.R.string.cancel
override val initialColor = Color.WHITE

private val viewModel by activityViewModels<EditorViewModel>()

override fun onColorSelected(color: Int) {
viewModel.obtainEvent(EditorIntent.InsertColor(color))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ sealed class EditorScreen(route: String) : Screen<String>(route) {
)

data object GotoLine : EditorScreen("blacksquircle://editor/goto")
data object ColorPicker : EditorScreen("blacksquircle://editor/colorpicker")
data object InsertColor : EditorScreen("blacksquircle://editor/insertcolor")
}
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class EditorViewModel @Inject constructor(
viewModelScope.launch {
try {
if (selectedPosition > -1) {
_viewEvent.send(ViewEvent.Navigation(EditorScreen.ColorPicker))
_viewEvent.send(ViewEvent.Navigation(EditorScreen.InsertColor))
}
} catch (e: Exception) {
Timber.e(e, e.message)
Expand Down
8 changes: 4 additions & 4 deletions feature-editor/impl/src/main/res/navigation/editor_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
</dialog>

<dialog
android:id="@+id/colorPickerDialog"
android:name="com.blacksquircle.ui.feature.editor.ui.dialog.ColorPickerDialog"
android:label="ColorPickerDialog">
<deepLink app:uri="blacksquircle://editor/colorpicker" />
android:id="@+id/insertColorDialog"
android:name="com.blacksquircle.ui.feature.editor.ui.dialog.InsertColorDialog"
android:label="InsertColorDialog">
<deepLink app:uri="blacksquircle://editor/insertcolor" />
</dialog>

</navigation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023 Squircle CE contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blacksquircle.ui.feature.themes.ui.dialog

import androidx.core.graphics.toColorInt
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import androidx.navigation.fragment.navArgs
import com.blacksquircle.ui.core.extensions.toHexString
import com.blacksquircle.ui.feature.themes.R
import com.blacksquircle.ui.feature.themes.ui.mvi.ThemeIntent
import com.blacksquircle.ui.feature.themes.ui.viewmodel.ThemesViewModel
import com.blacksquircle.ui.uikit.ColorPickerDialog
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ChooseColorDialog : ColorPickerDialog() {

override val titleRes = R.string.dialog_title_color_picker
override val positiveRes = R.string.action_select
override val negativeRes = android.R.string.cancel
override val initialColor: Int
get() = navArgs.value.toColorInt()

private val viewModel by hiltNavGraphViewModels<ThemesViewModel>(R.id.themes_graph)
private val navArgs by navArgs<ChooseColorDialogArgs>()

override fun onColorSelected(color: Int) {
val event = ThemeIntent.ChangeColor(
key = navArgs.key,
value = color.toHexString()
)
viewModel.obtainEvent(event)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import androidx.core.graphics.toColorInt
import androidx.core.view.MenuProvider
import androidx.core.view.updatePadding
import androidx.core.widget.doAfterTextChanged
Expand All @@ -46,10 +45,6 @@ import com.blacksquircle.ui.feature.themes.ui.mvi.NewThemeViewState
import com.blacksquircle.ui.feature.themes.ui.mvi.ThemeIntent
import com.blacksquircle.ui.feature.themes.ui.viewmodel.ThemesViewModel
import com.blacksquircle.ui.filesystem.base.utils.isValidFileName
import com.skydoves.colorpickerview.ColorPickerDialog
import com.skydoves.colorpickerview.flag.BubbleFlag
import com.skydoves.colorpickerview.flag.FlagMode
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand Down Expand Up @@ -102,24 +97,8 @@ class NewThemeFragment : Fragment(R.layout.fragment_new_theme) {
binding.recyclerView.setHasFixedSize(false)
binding.recyclerView.adapter = PropertyAdapter(object : OnItemClickListener<PropertyItem> {
override fun onClick(item: PropertyItem) {
ColorPickerDialog.Builder(requireContext()).apply {
setTitle(R.string.dialog_title_color_picker)
setPositiveButton(R.string.action_select, ColorEnvelopeListener { envelope, _ ->
val event = ThemeIntent.ChangeProperty(
key = item.propertyKey,
value = envelope.color.toHexString(),
)
viewModel.obtainEvent(event)
})
setNegativeButton(android.R.string.cancel, null)
attachAlphaSlideBar(false)
attachBrightnessSlideBar(true)

colorPickerView.setInitialColor(item.propertyValue.toColorInt())
colorPickerView.flagView = BubbleFlag(requireContext()).apply {
flagMode = FlagMode.FADE
}
}.show()
val event = ThemeIntent.ChooseColor(item.propertyKey, item.propertyValue)
viewModel.obtainEvent(event)
}
}).also {
adapter = it
Expand Down Expand Up @@ -190,6 +169,7 @@ class NewThemeFragment : Fragment(R.layout.fragment_new_theme) {
.onEach { event ->
when (event) {
is ViewEvent.Toast -> context?.showToast(text = event.message)
is ViewEvent.Navigation -> navController.navigate(event.screen)
is ViewEvent.PopBackStack -> navController.popBackStack()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ sealed class ThemeIntent : ViewIntent() {

data class LoadProperties(val uuid: String?) : ThemeIntent()
data class CreateTheme(val meta: Meta, val properties: List<PropertyItem>) : ThemeIntent()
data class ChooseColor(val key: Property, val value: String) : ThemeIntent()

data class ChangeName(val value: String) : ThemeIntent()
data class ChangeAuthor(val value: String) : ThemeIntent()
data class ChangeDescription(val value: String) : ThemeIntent()
data class ChangeProperty(val key: Property, val value: String) : ThemeIntent()
data class ChangeColor(val key: String, val value: String) : ThemeIntent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@

package com.blacksquircle.ui.feature.themes.ui.navigation

import com.blacksquircle.ui.core.extensions.encodeUrl
import com.blacksquircle.ui.core.navigation.Screen

sealed class ThemesScreen(route: String) : Screen<String>(route) {

data object Create : ThemesScreen("blacksquircle://themes/create")

class Update(uuid: String?) : ThemesScreen("blacksquircle://themes/update?uuid=$uuid")

class ChooseColor(key: String, value: String) : ThemesScreen(
route = "blacksquircle://themes/choosecolor?key=$key&value=${value.encodeUrl()}"
)
}

0 comments on commit 7f06620

Please sign in to comment.