diff --git a/app/src/main/java/com/p_vacho/neat_calendar/activities/CategoriesActivity.kt b/app/src/main/java/com/p_vacho/neat_calendar/activities/CategoriesActivity.kt index 11b8433..dd88b2d 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/activities/CategoriesActivity.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/activities/CategoriesActivity.kt @@ -18,7 +18,6 @@ import com.p_vacho.neat_calendar.R import com.p_vacho.neat_calendar.adapters.CategoryAdapter import com.p_vacho.neat_calendar.api.RetrofitClient import com.p_vacho.neat_calendar.api.models.CategoryResponse -import com.p_vacho.neat_calendar.api.models.EventResponse import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -38,7 +37,12 @@ class CategoriesActivity : AppCompatActivity() { @Suppress("DEPRECATION") val newCategory: CategoryResponse? = result.data?.getParcelableExtra("newCategory") - newCategory?.let { categoryCreateReply(it) } + @Suppress("DEPRECATION") + val editedCategory: CategoryResponse? = result.data?.getParcelableExtra("editedCategory") + + if (newCategory != null && editedCategory != null) throw IllegalStateException("Got both edit & new response") + if (newCategory != null) categoryCreateReply(newCategory, CategoryMode.CREATE) + if (editedCategory != null) categoryCreateReply(editedCategory, CategoryMode.EDIT) } } @@ -59,14 +63,14 @@ class CategoriesActivity : AppCompatActivity() { tvEmptyState = findViewById(R.id.tvEmptyState) btnBack.setOnClickListener { finish() } - btnAddCategory.setOnClickListener { navigateToCreateCategory() } + btnAddCategory.setOnClickListener { handleCreateCategory() } rvCategories.layoutManager = LinearLayoutManager(this) lifecycleScope.launch { categories = fetchCategories().toMutableList() - val adapter = CategoryAdapter(categories, ::handleDeleteCategory) + val adapter = CategoryAdapter(categories, ::handleDeleteCategory, ::handleEditCategory) rvCategories.adapter = adapter updateEmptyState() @@ -105,20 +109,44 @@ class CategoriesActivity : AppCompatActivity() { } /** - * Navigates to the activity for adding a new category. + * Handle create category button getting pressed. + * + * Navigates to the create category activity in create mode. */ - private fun navigateToCreateCategory() { - val intent = Intent(this, CreateCategoryActivity::class.java) - startActivity(intent) + private fun handleCreateCategory() { + val intent = Intent(this, CreateCategoryActivity::class.java).apply { + putExtra("mode", CategoryMode.CREATE.name) + } + createActivityLauncher.launch(intent) + } + + /** + * Handle edit category button being pressed. + * + * Navigates to the create category activity in edit mode. + */ + private fun handleEditCategory(category: CategoryResponse, position: Int) { + val intent = Intent(this, CreateCategoryActivity::class.java).apply { + putExtra("mode", CategoryMode.EDIT.name) + putExtra("category", category) + } + createActivityLauncher.launch(intent) } /** * Used as a callback, triggered when the CreateCategory Activity returns a result. * - * The returned value (the new category data) is passed over as a parameter. + * The returned value (the new / edited category data) is passed over as a parameter. */ - private fun categoryCreateReply(category: CategoryResponse) { - (rvCategories.adapter as CategoryAdapter).addCategory(category) + private fun categoryCreateReply(category: CategoryResponse, mode: CategoryMode) { + val adapter = (rvCategories.adapter as CategoryAdapter) + when (mode) { + CategoryMode.CREATE -> { + adapter.addCategory(category) + updateEmptyState() + } + CategoryMode.EDIT -> { adapter.editCategory(category) } + } } /** diff --git a/app/src/main/java/com/p_vacho/neat_calendar/activities/CreateCategoryActivity.kt b/app/src/main/java/com/p_vacho/neat_calendar/activities/CreateCategoryActivity.kt index 9c55922..42a5765 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/activities/CreateCategoryActivity.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/activities/CreateCategoryActivity.kt @@ -7,6 +7,10 @@ import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import com.p_vacho.neat_calendar.R +enum class CategoryMode { + CREATE, EDIT +} + class CreateCategoryActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/com/p_vacho/neat_calendar/adapters/CategoryAdapter.kt b/app/src/main/java/com/p_vacho/neat_calendar/adapters/CategoryAdapter.kt index 4bd0a70..08b17a0 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/adapters/CategoryAdapter.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/adapters/CategoryAdapter.kt @@ -11,12 +11,14 @@ import com.p_vacho.neat_calendar.api.models.CategoryResponse class CategoryAdapter( private val categories: MutableList, - private val onDeleteClick: (CategoryResponse, Int) -> Unit + private val onDeleteClick: (CategoryResponse, Int) -> Unit, + private val onEditClick: (CategoryResponse, Int) -> Unit ) : RecyclerView.Adapter() { inner class CategoryViewHolder(view: View) : RecyclerView.ViewHolder(view) { val colorIndicator: View = view.findViewById(R.id.colorIndicator) val categoryName: TextView = view.findViewById(R.id.tvCategoryName) + val editButton: ImageButton = view.findViewById(R.id.btnEditCategory) val deleteButton: ImageButton = view.findViewById(R.id.btnDeleteCategory) } @@ -33,10 +35,9 @@ class CategoryAdapter( holder.colorIndicator.setBackgroundColor(category.color.toArgb()) holder.categoryName.text = category.name - // Set click listener for the delete button - holder.deleteButton.setOnClickListener { - onDeleteClick(category, position) - } + // Set click listeners for the buttons + holder.deleteButton.setOnClickListener { onDeleteClick(category, position) } + holder.editButton.setOnClickListener { onEditClick(category, position) } } override fun getItemCount(): Int = categories.size @@ -64,4 +65,15 @@ class CategoryAdapter( categories.add(category) notifyItemInserted(categories.size) } + + /** + * Edit an existing category, updating it in the UI. + * + * Call this after the onEditClick callback edits the category from the backend API. + */ + fun editCategory(category: CategoryResponse) { + val position = categories.indexOfFirst { it.id == category.id } + categories[position] = category + notifyItemChanged(position) + } } diff --git a/app/src/main/res/layout/item_category.xml b/app/src/main/res/layout/item_category.xml index 74896ae..00bdd84 100644 --- a/app/src/main/res/layout/item_category.xml +++ b/app/src/main/res/layout/item_category.xml @@ -40,20 +40,39 @@ app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/colorIndicator" - app:layout_constraintEnd_toStartOf="@id/btnDeleteCategory" /> + app:layout_constraintEnd_toStartOf="@id/buttonContainer" /> - - + + app:layout_constraintEnd_toEndOf="parent"> + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7cdf14c..ec6aa04 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -96,4 +96,5 @@ This event has no categories. Event owner: You can\'t see categories for invited events + Edit category \ No newline at end of file