From a5a4c5db40e739dd09df7d4f4970ceb42f0449d9 Mon Sep 17 00:00:00 2001 From: leca Date: Wed, 9 Oct 2024 14:40:26 +0300 Subject: [PATCH] moved update and delete buttons of category view to a toolbar --- .../activities/MainActivity.kt | 39 ++++++++++- .../fragments/CategoriesFragment.kt | 39 +++++++++++ .../fragments/StorageFragment.kt | 6 +- .../views/CategoryView.kt | 64 ++++++++++--------- app/src/main/res/layout/category_view.xml | 16 ++--- app/src/main/res/menu/menu_main.xml | 2 + app/src/main/res/values/strings.xml | 1 + 7 files changed, 125 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt index 0ac5ea9..596c1e2 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -1,9 +1,11 @@ package org.foxarmy.barcodescannerforemployees.activities +import android.content.DialogInterface import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem +import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.viewpager.widget.ViewPager @@ -38,6 +40,7 @@ class MainActivity : AppCompatActivity() { val extras = Bundle() ContextCompat.startActivity(this, addProductIntent, extras) } + "CategoriesFragment" -> { val addCategoryIntent = Intent(this, AddCategoryActivity::class.java) val extras = Bundle() @@ -68,22 +71,54 @@ class MainActivity : AppCompatActivity() { } override fun onOptionsItemSelected(item: MenuItem): Boolean { + val currentPosition = binding.tabTablayout.selectedTabPosition + val fragment = adapter.getItem(currentPosition) + return when (item.itemId) { + + R.id.action_settings -> { true } + R.id.action_delete -> { - val currentPosition = binding.tabTablayout.selectedTabPosition - val fragment = adapter.getItem(currentPosition) when (fragment::class.simpleName.toString()) { "StorageFragment" -> { val storageFragment = fragment as StorageFragment storageFragment.removeSelected() } + + "CategoriesFragment" -> { + val builder = AlertDialog.Builder(this) + .setMessage("Deleting a category will also delete ALL the products, that belong to that category. Do you want to proceed?") + .setPositiveButton("Yes") { _: DialogInterface, _: Int -> + val categoriesFragment = fragment as CategoriesFragment + categoriesFragment.removeSelected() + } + .setNegativeButton("No") { _: DialogInterface, _: Int -> + + }.show() + } } true } + + R.id.action_update -> { + when (fragment::class.simpleName.toString()) { + "StorageFragment" -> { + val storageFragment = fragment as StorageFragment + storageFragment.updateSelected() + } + + "CategoriesFragment" -> { + val categoriesFragment = fragment as CategoriesFragment + categoriesFragment.updateSelected() + } + } + true + } + else -> super.onOptionsItemSelected(item) } } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt index 5cee199..4a026b3 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt @@ -1,16 +1,21 @@ package org.foxarmy.barcodescannerforemployees.fragments +import android.content.Intent import android.os.Bundle import android.provider.BaseColumns import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout +import android.widget.Toast +import androidx.core.content.ContextCompat +import androidx.core.view.children import androidx.fragment.app.Fragment import org.foxarmy.barcodescannerforemployees.CategoriesContract import org.foxarmy.barcodescannerforemployees.Category import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R +import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity import org.foxarmy.barcodescannerforemployees.views.CategoryView class CategoriesFragment : Fragment() { @@ -27,6 +32,40 @@ class CategoriesFragment : Fragment() { updateContent() } + fun removeSelected() { + val layout = view?.findViewById(R.id.categoriesLayout) + + val db = DBStorageController(requireContext()) + var deleted = false + for (view: CategoryView in layout?.children!!.iterator() as Iterator) { + if (view.isCategorySelected) { + db.eraseCategory(db.writableDatabase, view.category.id, requireContext()) + deleted = true + } + } + + if (!deleted) { + Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show() + } + updateContent() + } + + fun updateSelected() { + val layout = view?.findViewById(R.id.categoriesLayout) + for (view: CategoryView in layout?.children!!.iterator() as Iterator) { + if (view.isCategorySelected) { + val addCategoryIntent = Intent(context, AddCategoryActivity::class.java) + val extras = Bundle() + extras.putInt("categoryid", view.category.id) + extras.putString("categoryname", view.category.name) + addCategoryIntent.putExtras(extras) + ContextCompat.startActivity(context!!, addCategoryIntent, extras) + } + } + + + } + fun updateContent() { val layout = view?.findViewById(R.id.categoriesLayout) layout?.removeAllViews() diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt index 124691f..2c4182b 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -45,7 +45,11 @@ class StorageFragment : Fragment() { Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show() } updateContent() -} + } + + fun updateSelected() { + + } fun updateContent() { diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt index 09e350c..aabdb57 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt @@ -1,26 +1,21 @@ package org.foxarmy.barcodescannerforemployees.views import android.app.Activity -import android.app.AlertDialog import android.content.Context -import android.content.DialogInterface -import android.content.Intent -import android.os.Bundle import android.view.LayoutInflater -import android.widget.Button import android.widget.LinearLayout import android.widget.TextView import androidx.core.content.ContextCompat import org.foxarmy.barcodescannerforemployees.Category -import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R -import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity class CategoryView : LinearLayout { var category: Category val categoryName: TextView - val updateButton: Button - val deleteButton: Button + val amountOfProducts: TextView + var isCategorySelected = false +// val updateButton: Button +// val deleteButton: Button constructor(activity: Activity, context: Context, category: Category) : super(context) { this.category = category @@ -28,32 +23,41 @@ class CategoryView : LinearLayout { val inflater: LayoutInflater = activity.layoutInflater inflater.inflate(R.layout.category_view, this) + this.background = ContextCompat.getDrawable(context, if (isCategorySelected) R.drawable.outline_selected else R.drawable.outline) + categoryName = findViewById(R.id.categoryNameTextView) - updateButton = findViewById(R.id.updateButton) - deleteButton = findViewById(R.id.deleteButton) + amountOfProducts = findViewById(R.id.amountOfProducts) +// updateButton = findViewById(R.id.updateButton) +// deleteButton = findViewById(R.id.deleteButton) categoryName.text = category.name - updateButton.setOnClickListener { - val addCategoryIntent = Intent(context, AddCategoryActivity::class.java) - val extras = Bundle() - extras.putInt("categoryid", category.id) - extras.putString("categoryname", category.name) - addCategoryIntent.putExtras(extras) - ContextCompat.startActivity(context, addCategoryIntent, extras) + setOnLongClickListener { + isCategorySelected = !isCategorySelected + this.background = ContextCompat.getDrawable(context, if (isCategorySelected) R.drawable.outline_selected else R.drawable.outline) + true } - deleteButton.setOnClickListener { - val builder = AlertDialog.Builder(context) - .setMessage("Deleting this category will also delete ALL the products, that belong to that category. Do you want to proceed?") - .setPositiveButton("Yes") { _: DialogInterface, _: Int -> - val db = DBStorageController(context).writableDatabase - DBStorageController(context).eraseCategory(db, category.id, context) - - } - .setNegativeButton("No") { _: DialogInterface, _: Int -> - - }.show() - } +// updateButton.setOnClickListener { +// val addCategoryIntent = Intent(context, AddCategoryActivity::class.java) +// val extras = Bundle() +// extras.putInt("categoryid", category.id) +// extras.putString("categoryname", category.name) +// addCategoryIntent.putExtras(extras) +// ContextCompat.startActivity(context, addCategoryIntent, extras) +// } +// +// deleteButton.setOnClickListener { +// val builder = AlertDialog.Builder(context) +// .setMessage("Deleting this category will also delete ALL the products, that belong to that category. Do you want to proceed?") +// .setPositiveButton("Yes") { _: DialogInterface, _: Int -> +// val db = DBStorageController(context).writableDatabase +// DBStorageController(context).eraseCategory(db, category.id, context) +// +// } +// .setNegativeButton("No") { _: DialogInterface, _: Int -> +// +// }.show() +// } } } \ No newline at end of file diff --git a/app/src/main/res/layout/category_view.xml b/app/src/main/res/layout/category_view.xml index 0cc430d..a40390b 100644 --- a/app/src/main/res/layout/category_view.xml +++ b/app/src/main/res/layout/category_view.xml @@ -7,13 +7,11 @@ -