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 package org.foxarmy.barcodescannerforemployees.activities
import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager import androidx.viewpager.widget.ViewPager
@ -38,6 +40,7 @@ class MainActivity : AppCompatActivity() {
val extras = Bundle() val extras = Bundle()
ContextCompat.startActivity(this, addProductIntent, extras) ContextCompat.startActivity(this, addProductIntent, extras)
} }
"CategoriesFragment" -> { "CategoriesFragment" -> {
val addCategoryIntent = Intent(this, AddCategoryActivity::class.java) val addCategoryIntent = Intent(this, AddCategoryActivity::class.java)
val extras = Bundle() val extras = Bundle()
@ -68,22 +71,54 @@ class MainActivity : AppCompatActivity() {
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
val currentPosition = binding.tabTablayout.selectedTabPosition
val fragment = adapter.getItem(currentPosition)
return when (item.itemId) { return when (item.itemId) {
R.id.action_settings -> { R.id.action_settings -> {
true true
} }
R.id.action_delete -> { R.id.action_delete -> {
val currentPosition = binding.tabTablayout.selectedTabPosition
val fragment = adapter.getItem(currentPosition)
when (fragment::class.simpleName.toString()) { when (fragment::class.simpleName.toString()) {
"StorageFragment" -> { "StorageFragment" -> {
val storageFragment = fragment as StorageFragment val storageFragment = fragment as StorageFragment
storageFragment.removeSelected() 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 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) else -> super.onOptionsItemSelected(item)
} }
} }

View File

@ -1,16 +1,21 @@
package org.foxarmy.barcodescannerforemployees.fragments package org.foxarmy.barcodescannerforemployees.fragments
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.provider.BaseColumns import android.provider.BaseColumns
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import org.foxarmy.barcodescannerforemployees.CategoriesContract import org.foxarmy.barcodescannerforemployees.CategoriesContract
import org.foxarmy.barcodescannerforemployees.Category import org.foxarmy.barcodescannerforemployees.Category
import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity
import org.foxarmy.barcodescannerforemployees.views.CategoryView import org.foxarmy.barcodescannerforemployees.views.CategoryView
class CategoriesFragment : Fragment() { class CategoriesFragment : Fragment() {
@ -27,6 +32,40 @@ class CategoriesFragment : Fragment() {
updateContent() 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() { fun updateContent() {
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout) val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
layout?.removeAllViews() layout?.removeAllViews()

View File

@ -47,6 +47,10 @@ class StorageFragment : Fragment() {
updateContent() updateContent()
} }
fun updateSelected() {
}
fun updateContent() { fun updateContent() {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout) val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)

View File

@ -1,26 +1,21 @@
package org.foxarmy.barcodescannerforemployees.views package org.foxarmy.barcodescannerforemployees.views
import android.app.Activity import android.app.Activity
import android.app.AlertDialog
import android.content.Context import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.Button
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import org.foxarmy.barcodescannerforemployees.Category import org.foxarmy.barcodescannerforemployees.Category
import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity
class CategoryView : LinearLayout { class CategoryView : LinearLayout {
var category: Category var category: Category
val categoryName: TextView val categoryName: TextView
val updateButton: Button val amountOfProducts: TextView
val deleteButton: Button var isCategorySelected = false
// val updateButton: Button
// val deleteButton: Button
constructor(activity: Activity, context: Context, category: Category) : super(context) { constructor(activity: Activity, context: Context, category: Category) : super(context) {
this.category = category this.category = category
@ -28,32 +23,41 @@ class CategoryView : LinearLayout {
val inflater: LayoutInflater = activity.layoutInflater val inflater: LayoutInflater = activity.layoutInflater
inflater.inflate(R.layout.category_view, this) 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) categoryName = findViewById(R.id.categoryNameTextView)
updateButton = findViewById(R.id.updateButton) amountOfProducts = findViewById(R.id.amountOfProducts)
deleteButton = findViewById(R.id.deleteButton) // updateButton = findViewById(R.id.updateButton)
// deleteButton = findViewById(R.id.deleteButton)
categoryName.text = category.name categoryName.text = category.name
updateButton.setOnClickListener { setOnLongClickListener {
val addCategoryIntent = Intent(context, AddCategoryActivity::class.java) isCategorySelected = !isCategorySelected
val extras = Bundle() this.background = ContextCompat.getDrawable(context, if (isCategorySelected) R.drawable.outline_selected else R.drawable.outline)
extras.putInt("categoryid", category.id) true
extras.putString("categoryname", category.name)
addCategoryIntent.putExtras(extras)
ContextCompat.startActivity(context, addCategoryIntent, extras)
} }
deleteButton.setOnClickListener { // updateButton.setOnClickListener {
val builder = AlertDialog.Builder(context) // val addCategoryIntent = Intent(context, AddCategoryActivity::class.java)
.setMessage("Deleting this category will also delete ALL the products, that belong to that category. Do you want to proceed?") // val extras = Bundle()
.setPositiveButton("Yes") { _: DialogInterface, _: Int -> // extras.putInt("categoryid", category.id)
val db = DBStorageController(context).writableDatabase // extras.putString("categoryname", category.name)
DBStorageController(context).eraseCategory(db, category.id, context) // addCategoryIntent.putExtras(extras)
// ContextCompat.startActivity(context, addCategoryIntent, extras)
} // }
.setNegativeButton("No") { _: DialogInterface, _: Int -> //
// deleteButton.setOnClickListener {
}.show() // 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 <TextView
android:text="@string/sample_category" android:text="@string/sample_category"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/categoryNameTextView" android:layout_weight="1"/> android:layout_height="wrap_content" android:id="@+id/categoryNameTextView" android:layout_weight="1"
<Button android:textSize="25sp"/>
android:layout_width="0dp" <TextView
android:layout_height="wrap_content" android:id="@+id/updateButton" android:layout_weight="1" android:text="0"
android:text="@string/update" android:lines="1"/> android:layout_width="wrap_content"
<Button android:layout_height="wrap_content" android:id="@+id/amountOfProducts" android:layout_weight="1"
android:text="@string/delete" android:textAlignment="viewEnd" android:textSize="25sp"/>
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/deleteButton" android:layout_weight="1"/>
</LinearLayout> </LinearLayout>

View File

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

View File

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