From a70c14634f9e632a26e9782469eaa8c5c1b294bb Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Wed, 1 Jan 2025 18:56:52 +0100 Subject: [PATCH] feat: Add color picking support for event creation --- app/build.gradle.kts | 1 + .../activities/CreateEventActivity.kt | 33 +++++++++++++++---- app/src/main/res/drawable/ic_circle.xml | 10 ++++++ .../main/res/layout/activity_create_event.xml | 12 +++++++ app/src/main/res/values/strings.xml | 1 + settings.gradle.kts | 1 + 6 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 app/src/main/res/drawable/ic_circle.xml diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 95de898..7cd651c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -53,4 +53,5 @@ dependencies { implementation("com.fatboyindustrial.gson-javatime-serialisers:gson-javatime-serialisers:1.1.2") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.1") implementation("com.jakewharton.threetenabp:threetenabp:1.4.4") + implementation("com.github.Dhaval2404:ColorPicker:2.3") } \ No newline at end of file diff --git a/app/src/main/java/com/p_vacho/neat_calendar/activities/CreateEventActivity.kt b/app/src/main/java/com/p_vacho/neat_calendar/activities/CreateEventActivity.kt index b78b839..d985a19 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/activities/CreateEventActivity.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/activities/CreateEventActivity.kt @@ -3,6 +3,7 @@ package com.p_vacho.neat_calendar.activities import android.app.DatePickerDialog import android.app.TimePickerDialog import android.content.Intent +import android.content.res.ColorStateList import android.graphics.Color import android.os.Bundle import android.view.View @@ -10,9 +11,11 @@ import android.widget.EditText import android.widget.Toast import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.lifecycle.lifecycleScope +import com.github.dhaval2404.colorpicker.ColorPickerDialog import com.google.android.material.button.MaterialButton import com.google.android.material.button.MaterialButtonToggleGroup import com.google.android.material.textfield.TextInputEditText @@ -27,6 +30,7 @@ import retrofit2.HttpException import java.time.LocalDate import java.time.LocalDateTime import java.time.ZoneId +import kotlin.properties.Delegates class CreateEventActivity : AppCompatActivity() { private lateinit var etEventTitle: EditText @@ -37,10 +41,12 @@ class CreateEventActivity : AppCompatActivity() { private lateinit var txtStartTime: TextInputEditText private lateinit var txtEndTime: TextInputEditText private lateinit var btnCreateEvent: MaterialButton + private lateinit var btnColorPicker: MaterialButton private lateinit var defaultDate: LocalDate private var instantEvent: Boolean = true + private var selectedColor by Delegates.notNull() private var startDateTime: LocalDateTime? = null private var endDateTime: LocalDateTime? = null @@ -54,11 +60,6 @@ class CreateEventActivity : AppCompatActivity() { insets } - - // The getParcelableExtra wants the class as a second argument to - // be more type-safe, but this is only supported since api 33, which is over - // our min api version, so we can ignore this deprecation for now. - @Suppress("DEPRECATION") val dateString = intent.getStringExtra("date")!! defaultDate = LocalDate.parse(dateString) @@ -75,6 +76,9 @@ class CreateEventActivity : AppCompatActivity() { txtStartTime = findViewById(R.id.txtStartTime) txtEndTime = findViewById(R.id.txtEndTime) btnCreateEvent = findViewById(R.id.btnCreateEvent) + btnColorPicker = findViewById(R.id.btnColorPicker) + + selectedColor = ContextCompat.getColor(this, R.color.event_indicator_color) } private fun setupListeners() { @@ -115,6 +119,7 @@ class CreateEventActivity : AppCompatActivity() { } } + btnColorPicker.setOnClickListener { openColorPickerDialog() } btnCreateEvent.setOnClickListener { createEvent() } } @@ -139,7 +144,7 @@ class CreateEventActivity : AppCompatActivity() { start_time = startDateTime!!.atZone(ZoneId.systemDefault()).toOffsetDateTime(), end_time = endDateTime?.atZone(ZoneId.systemDefault())?.toOffsetDateTime() ?: startDateTime!!.atZone(ZoneId.systemDefault()).toOffsetDateTime(), - color = Color.valueOf(0xFF33AABB.toInt()) // Placeholder color + color = Color.valueOf(selectedColor) ) lifecycleScope.launch(Dispatchers.IO) { @@ -189,6 +194,22 @@ class CreateEventActivity : AppCompatActivity() { datePicker.show() } + private fun openColorPickerDialog() { + ColorPickerDialog + .Builder(this) + .setTitle("Choose Event Color") // TODO: Localize + // setDefaultColor expects an int coming from a color resource + // we're instead passing the current color as Int, so it's not + // from a resource, which the linter doesn't like, so suppress it + // noinspection ResourceType + .setDefaultColor(selectedColor) + .setColorListener { color, colorHex -> + selectedColor = color + btnColorPicker.iconTint = ColorStateList.valueOf(selectedColor) + } + .show() + } + private fun formatDateTime(dateTime: LocalDateTime?): String { return dateTime?.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")) ?: "Select Time" } diff --git a/app/src/main/res/drawable/ic_circle.xml b/app/src/main/res/drawable/ic_circle.xml new file mode 100644 index 0000000..06163cc --- /dev/null +++ b/app/src/main/res/drawable/ic_circle.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/layout/activity_create_event.xml b/app/src/main/res/layout/activity_create_event.xml index 855de25..616b403 100644 --- a/app/src/main/res/layout/activity_create_event.xml +++ b/app/src/main/res/layout/activity_create_event.xml @@ -116,6 +116,18 @@ tools:visibility="visible" /> + + + Start Time End Time Create Event + Select Color \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 5a7332b..c1b5cc5 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,6 +16,7 @@ dependencyResolutionManagement { repositories { google() mavenCentral() + maven { url = uri("https://jitpack.io") } } }