feat: Add support for event deletion

This commit is contained in:
Peter Vacho 2024-12-31 14:50:47 +01:00
parent 5abe270b46
commit 7b73395c08
Signed by: school
GPG key ID: 8CFC3837052871B4
4 changed files with 50 additions and 3 deletions

View file

@ -167,4 +167,13 @@ class CalendarActivity : AppCompatActivity() {
startActivity(intent)
// Don't finish the current activity, we want to be able to go back here
}
override fun onResume() {
super.onResume()
// Re-fetch the events for the current month
// Events could've been modified or even deleted, so make sure we have a fresh copy
// it would probably be better to pass these around through extras, this extra lookup
// isn't a huge deal, it's a potential future optimization though
setCalendarForMonth(currentYearMonth)
}
}

View file

@ -29,6 +29,12 @@ class DayViewActivity : AppCompatActivity() {
@Suppress("DEPRECATION")
val calendarDay = intent.getParcelableExtra<CalendarDay>("calendarDay")!!
// It's possible that events will be removed by the event card adapter, use a
// mutable list to keep track of them from here. We don't currently propagate
// these changes anywhere, going back to calendar activity will just trigger
// an api refetch, which is sufficient for now, but in the future, we could.
val events = calendarDay.events.toMutableList()
// Setup the UI
val tvDate: TextView = findViewById(R.id.tvDate)
val rvEvents: RecyclerView = findViewById(R.id.rvEvents)
@ -36,6 +42,6 @@ class DayViewActivity : AppCompatActivity() {
tvDate.text = calendarDay.date.toString()
rvEvents.layoutManager = LinearLayoutManager(this)
rvEvents.adapter = EventCardAdapter(calendarDay.events, this)
rvEvents.adapter = EventCardAdapter(events, this)
}
}

View file

@ -4,6 +4,7 @@ import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
@ -21,7 +22,7 @@ import java.time.format.FormatStyle
import java.util.Locale
class EventCardAdapter(
private val events: List<EventResponse>,
private val events: MutableList<EventResponse>,
private val lifecycleOwner: LifecycleOwner
) :
RecyclerView.Adapter<EventCardAdapter.EventViewHolder>() {
@ -32,6 +33,8 @@ class EventCardAdapter(
val tvDescription: TextView = view.findViewById(R.id.tvDescription)
val rvCategories: RecyclerView = view.findViewById(R.id.categoryChipRecyclerView)
val eventColorIndicator: View = view.findViewById(R.id.eventColorIndicator)
val btnEdit: ImageButton = view.findViewById(R.id.btnEditEvent)
val btnDelete: ImageButton = view.findViewById(R.id.btnDeleteEvent)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EventViewHolder {
@ -55,6 +58,14 @@ class EventCardAdapter(
holder.tvDescription.text = event.description
}
// Handle Delete & Edit Button Clicks
holder.btnEdit.setOnClickListener {
editEvent(event)
}
holder.btnDelete.setOnClickListener {
deleteEvent(event, position)
}
// Initialize empty state for categories
holder.rvCategories.layoutManager =
LinearLayoutManager(holder.itemView.context, LinearLayoutManager.HORIZONTAL, false)
@ -103,4 +114,20 @@ class EventCardAdapter(
holder.rvCategories.adapter = CategoryChipAdapter(categories)
}
}
private fun deleteEvent(event: EventResponse, position: Int) {
lifecycleOwner.lifecycleScope.launch {
withContext(Dispatchers.IO) {
RetrofitClient.eventsService.deleteEvent(event.id)
}
// Remove the event from the list and notify the adapter
events.removeAt(position)
notifyItemRemoved(position)
}
}
private fun editEvent(event: EventResponse) {
TODO("Implement event editing")
}
}

View file

@ -1,6 +1,7 @@
package com.p_vacho.neat_calendar.api.services
import com.p_vacho.neat_calendar.api.models.EventResponse
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
@ -26,4 +27,8 @@ interface EventsService {
@Query("start_to") startTo: OffsetDateTime? = null,
@Query("end_from") endFrom: OffsetDateTime? = null,
@Query("end_to") endTo: OffsetDateTime? = null
): List<EventResponse>}
): List<EventResponse>
@DELETE("events/{event_id}")
suspend fun deleteEvent(@Path("event_id") eventId: String): Unit
}