moved update and delete buttons of category view to a toolbar

This commit is contained in:
leca 2024-10-09 14:40:26 +03:00
parent 068cd7846b
commit a5a4c5db40
7 changed files with 125 additions and 42 deletions

View File

@ -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)
}
}

View File

@ -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<LinearLayout>(R.id.categoriesLayout)
val db = DBStorageController(requireContext())
var deleted = false
for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) {
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<LinearLayout>(R.id.categoriesLayout)
for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) {
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<LinearLayout>(R.id.categoriesLayout)
layout?.removeAllViews()

View File

@ -45,7 +45,11 @@ class StorageFragment : Fragment() {
Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show()
}
updateContent()
}
}
fun updateSelected() {
}
fun updateContent() {

View File

@ -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()
// }
}
}

View File

@ -7,13 +7,11 @@
<TextView
android:text="@string/sample_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/categoryNameTextView" android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/updateButton" android:layout_weight="1"
android:text="@string/update" android:lines="1"/>
<Button
android:text="@string/delete"
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/deleteButton" android:layout_weight="1"/>
android:layout_height="wrap_content" android:id="@+id/categoryNameTextView" android:layout_weight="1"
android:textSize="25sp"/>
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/amountOfProducts" android:layout_weight="1"
android:textAlignment="viewEnd" android:textSize="25sp"/>
</LinearLayout>

View File

@ -7,4 +7,6 @@
android:orderInCategory="100"
app:showAsAction="never"/>
<item android:id="@+id/action_delete" android:title="@string/delete_menu"/>
<item android:id="@+id/action_update" android:title="@string/update_menu"/>
</menu>

View File

@ -23,6 +23,7 @@
<string name="fullscreen_image">Fullscreen image</string>
<string name="next">Next</string>
<string name="delete_menu">Delete item(s)…</string>
<string name="update_menu">Update item</string>
<string name="update">update</string>
<string name="delete">delete</string>
<string name="category">Category</string>