feat: Add color picking support for event creation

This commit is contained in:
Peter Vacho 2025-01-01 18:56:52 +01:00
parent 3df1faa17c
commit a70c14634f
Signed by: school
GPG key ID: 8CFC3837052871B4
6 changed files with 52 additions and 6 deletions

View file

@ -53,4 +53,5 @@ dependencies {
implementation("com.fatboyindustrial.gson-javatime-serialisers:gson-javatime-serialisers:1.1.2") implementation("com.fatboyindustrial.gson-javatime-serialisers:gson-javatime-serialisers:1.1.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.1") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.1")
implementation("com.jakewharton.threetenabp:threetenabp:1.4.4") implementation("com.jakewharton.threetenabp:threetenabp:1.4.4")
implementation("com.github.Dhaval2404:ColorPicker:2.3")
} }

View file

@ -3,6 +3,7 @@ package com.p_vacho.neat_calendar.activities
import android.app.DatePickerDialog import android.app.DatePickerDialog
import android.app.TimePickerDialog import android.app.TimePickerDialog
import android.content.Intent import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
@ -10,9 +11,11 @@ import android.widget.EditText
import android.widget.Toast import android.widget.Toast
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.dhaval2404.colorpicker.ColorPickerDialog
import com.google.android.material.button.MaterialButton import com.google.android.material.button.MaterialButton
import com.google.android.material.button.MaterialButtonToggleGroup import com.google.android.material.button.MaterialButtonToggleGroup
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
@ -27,6 +30,7 @@ import retrofit2.HttpException
import java.time.LocalDate import java.time.LocalDate
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.ZoneId import java.time.ZoneId
import kotlin.properties.Delegates
class CreateEventActivity : AppCompatActivity() { class CreateEventActivity : AppCompatActivity() {
private lateinit var etEventTitle: EditText private lateinit var etEventTitle: EditText
@ -37,10 +41,12 @@ class CreateEventActivity : AppCompatActivity() {
private lateinit var txtStartTime: TextInputEditText private lateinit var txtStartTime: TextInputEditText
private lateinit var txtEndTime: TextInputEditText private lateinit var txtEndTime: TextInputEditText
private lateinit var btnCreateEvent: MaterialButton private lateinit var btnCreateEvent: MaterialButton
private lateinit var btnColorPicker: MaterialButton
private lateinit var defaultDate: LocalDate private lateinit var defaultDate: LocalDate
private var instantEvent: Boolean = true private var instantEvent: Boolean = true
private var selectedColor by Delegates.notNull<Int>()
private var startDateTime: LocalDateTime? = null private var startDateTime: LocalDateTime? = null
private var endDateTime: LocalDateTime? = null private var endDateTime: LocalDateTime? = null
@ -54,11 +60,6 @@ class CreateEventActivity : AppCompatActivity() {
insets 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")!! val dateString = intent.getStringExtra("date")!!
defaultDate = LocalDate.parse(dateString) defaultDate = LocalDate.parse(dateString)
@ -75,6 +76,9 @@ class CreateEventActivity : AppCompatActivity() {
txtStartTime = findViewById(R.id.txtStartTime) txtStartTime = findViewById(R.id.txtStartTime)
txtEndTime = findViewById(R.id.txtEndTime) txtEndTime = findViewById(R.id.txtEndTime)
btnCreateEvent = findViewById(R.id.btnCreateEvent) btnCreateEvent = findViewById(R.id.btnCreateEvent)
btnColorPicker = findViewById(R.id.btnColorPicker)
selectedColor = ContextCompat.getColor(this, R.color.event_indicator_color)
} }
private fun setupListeners() { private fun setupListeners() {
@ -115,6 +119,7 @@ class CreateEventActivity : AppCompatActivity() {
} }
} }
btnColorPicker.setOnClickListener { openColorPickerDialog() }
btnCreateEvent.setOnClickListener { createEvent() } btnCreateEvent.setOnClickListener { createEvent() }
} }
@ -139,7 +144,7 @@ class CreateEventActivity : AppCompatActivity() {
start_time = startDateTime!!.atZone(ZoneId.systemDefault()).toOffsetDateTime(), start_time = startDateTime!!.atZone(ZoneId.systemDefault()).toOffsetDateTime(),
end_time = endDateTime?.atZone(ZoneId.systemDefault())?.toOffsetDateTime() end_time = endDateTime?.atZone(ZoneId.systemDefault())?.toOffsetDateTime()
?: startDateTime!!.atZone(ZoneId.systemDefault()).toOffsetDateTime(), ?: startDateTime!!.atZone(ZoneId.systemDefault()).toOffsetDateTime(),
color = Color.valueOf(0xFF33AABB.toInt()) // Placeholder color color = Color.valueOf(selectedColor)
) )
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
@ -189,6 +194,22 @@ class CreateEventActivity : AppCompatActivity() {
datePicker.show() 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 { private fun formatDateTime(dateTime: LocalDateTime?): String {
return dateTime?.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")) ?: "Select Time" return dateTime?.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")) ?: "Select Time"
} }

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#e8eaed"
android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2z" />
</vector>

View file

@ -116,6 +116,18 @@
tools:visibility="visible" /> tools:visibility="visible" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<!-- Color Picker -->
<com.google.android.material.button.MaterialButton
android:id="@+id/btnColorPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/select_color"
app:icon="@drawable/ic_circle"
app:iconTint="@color/event_indicator_color"
app:cornerRadius="8dp"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"/>
<!-- Create Button --> <!-- Create Button -->
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/btnCreateEvent" android:id="@+id/btnCreateEvent"

View file

@ -40,4 +40,5 @@
<string name="start_time">Start Time</string> <string name="start_time">Start Time</string>
<string name="end_time">End Time</string> <string name="end_time">End Time</string>
<string name="create_event">Create Event</string> <string name="create_event">Create Event</string>
<string name="select_color">Select Color</string>
</resources> </resources>

View file

@ -16,6 +16,7 @@ dependencyResolutionManagement {
repositories { repositories {
google() google()
mavenCentral() mavenCentral()
maven { url = uri("https://jitpack.io") }
} }
} }