feat(createEvent): Implement category adding
This commit is contained in:
parent
95c4211e08
commit
908bf293b0
|
@ -25,6 +25,7 @@ import com.google.android.material.button.MaterialButton
|
|||
import com.google.android.material.button.MaterialButtonToggleGroup
|
||||
import com.google.android.material.textfield.TextInputEditText
|
||||
import com.google.gson.Gson
|
||||
import com.p_vacho.neat_calendar.MyApplication
|
||||
import com.p_vacho.neat_calendar.R
|
||||
import com.p_vacho.neat_calendar.adapters.CategoryChipAdapter
|
||||
import com.p_vacho.neat_calendar.api.RetrofitClient
|
||||
|
@ -78,13 +79,13 @@ class CreateEventActivity : AppCompatActivity() {
|
|||
val dateString = intent.getStringExtra("date")!!
|
||||
defaultDate = LocalDate.parse(dateString)
|
||||
|
||||
// TODO: Fetch the available categories from the API
|
||||
allCategories = emptyList()
|
||||
eventCategories = mutableListOf()
|
||||
eventCategories = mutableListOf() // start off empty
|
||||
|
||||
initializeViews()
|
||||
setupListeners()
|
||||
updateCategoryView()
|
||||
fetchCategories()
|
||||
}
|
||||
|
||||
private fun initializeViews() {
|
||||
|
@ -153,16 +154,57 @@ class CreateEventActivity : AppCompatActivity() {
|
|||
btnClose.setOnClickListener { finish() }
|
||||
}
|
||||
|
||||
private fun fetchCategories() {
|
||||
val userId = (application as MyApplication).tokenManager.userId
|
||||
|
||||
if (userId.isNullOrEmpty()) {
|
||||
Toast.makeText(this, "User not authenticated. Unable to fetch categories.", Toast.LENGTH_SHORT).show()
|
||||
finish() // Close the activity if no user is authenticated
|
||||
return
|
||||
}
|
||||
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
// Fetch categories from the API
|
||||
val fetchedCategories = RetrofitClient.categoryService.userCategories(userId)
|
||||
|
||||
withContext(Dispatchers.Main) { allCategories = fetchedCategories }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle user clicking on the add button for the event categories.
|
||||
*
|
||||
* This will open up a modal where the user will be able to choose from
|
||||
* the available categories (excluding the already picked ones). Once chosen,
|
||||
* the category will be added to the categoryRecyclerView.
|
||||
*/
|
||||
private fun addCategory() {
|
||||
// This should probably open some modal with a dropdown
|
||||
// showing all the available categories (without the already picked ones)
|
||||
// Create a list categories available to select (not already picked)
|
||||
val selectableCategories = allCategories.filter { category ->
|
||||
!eventCategories.contains(category)
|
||||
}
|
||||
|
||||
var newCategory = TODO("Implement category adding")
|
||||
if (newCategory == null) return
|
||||
if (selectableCategories.isEmpty()) {
|
||||
Toast.makeText(this, "No more categories to add", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
eventCategories.add(newCategory)
|
||||
categoryRecyclerView.adapter!!.notifyItemInserted(eventCategories.size - 1)
|
||||
updateCategoryView()
|
||||
val categoryNames = selectableCategories.map { it.name }.toTypedArray()
|
||||
|
||||
val builder = android.app.AlertDialog.Builder(this)
|
||||
.setTitle("Add Category")
|
||||
.setItems(categoryNames) { dialog, which ->
|
||||
val selectedCategory = selectableCategories[which]
|
||||
eventCategories.add(selectedCategory)
|
||||
categoryRecyclerView.adapter?.notifyItemInserted(eventCategories.size - 1)
|
||||
updateCategoryView()
|
||||
dialog.dismiss()
|
||||
}
|
||||
.setNegativeButton("Cancel") { dialog, _ ->
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
builder.create().show()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -170,8 +212,17 @@ class CreateEventActivity : AppCompatActivity() {
|
|||
* the category chip was clicked.
|
||||
*/
|
||||
private fun onCategoryRemoved(category: CategoryResponse, position: Int) {
|
||||
TODO("Implement category removing")
|
||||
// We can't use the passed position, as the adapter still uses the old positions
|
||||
// which don't necessarily match ours
|
||||
val actualPosition = eventCategories.indexOf(category)
|
||||
|
||||
if (actualPosition == -1) {
|
||||
Log.w("CreateEventActivity", "Attempted to remove a category that no longer exists.")
|
||||
return
|
||||
}
|
||||
|
||||
eventCategories.removeAt(actualPosition)
|
||||
categoryRecyclerView.adapter!!.notifyItemRemoved(actualPosition)
|
||||
updateCategoryView()
|
||||
}
|
||||
|
||||
|
@ -192,7 +243,7 @@ class CreateEventActivity : AppCompatActivity() {
|
|||
val eventRequest = EventRequest(
|
||||
title = title,
|
||||
description = description,
|
||||
category_ids = emptyList(), // Categories will be added later
|
||||
category_ids = eventCategories.map { it.id },
|
||||
start_time = startDateTime!!.atZone(ZoneId.systemDefault()).toOffsetDateTime(),
|
||||
end_time = endDateTime?.atZone(ZoneId.systemDefault())?.toOffsetDateTime()
|
||||
?: startDateTime!!.atZone(ZoneId.systemDefault()).toOffsetDateTime(),
|
||||
|
|
Loading…
Reference in a new issue