From 5fb481a0d5a0070430249fec0e2216fa86368735 Mon Sep 17 00:00:00 2001 From: leca Date: Tue, 8 Oct 2024 02:24:16 +0300 Subject: [PATCH] categories --- app/build.gradle.kts | 1 + app/src/main/AndroidManifest.xml | 3 ++ .../AbstractProduct.kt | 2 +- .../barcodescannerforemployees/Category.kt | 8 ++++ .../DBStorageController.kt | 14 +++--- .../activities/AddCategoryActivity.kt | 46 ++++++++++++++++++ .../activities/AddProductActivity.kt | 44 ++++++++++++++--- .../activities/FullscreenActivity.kt | 2 +- .../activities/MainActivity.kt | 25 ++++++++-- .../fragments/AddProductFragment.kt | 9 ---- .../fragments/CategoriesFragment.kt | 35 +++++++++++++- .../fragments/StorageFragment.kt | 30 ++++++------ .../views/AbstractProductView.kt | 3 +- .../views/CategoryView.kt | 47 +++++++++++++++++++ .../main/res/layout/activity_add_category.xml | 26 ++++++++++ app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/category_view.xml | 19 ++++++++ .../main/res/layout/fragment_add_product.xml | 25 +++++++--- .../main/res/layout/fragment_categories.xml | 8 ++-- app/src/main/res/values/strings.xml | 36 ++------------ gradle/libs.versions.toml | 2 + 21 files changed, 297 insertions(+), 90 deletions(-) create mode 100644 app/src/main/java/org/foxarmy/barcodescannerforemployees/Category.kt create mode 100644 app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddCategoryActivity.kt create mode 100644 app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt create mode 100644 app/src/main/res/layout/activity_add_category.xml create mode 100644 app/src/main/res/layout/category_view.xml diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 674a5be..6a97330 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -45,6 +45,7 @@ dependencies { implementation(libs.androidx.navigation.ui.ktx) implementation(libs.firebase.crashlytics.buildtools) implementation(libs.androidx.gridlayout) + implementation(libs.androidx.activity) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 698c541..e8b3101 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,6 +24,9 @@ android:supportsRtl="true" android:theme="@style/Theme.BarcodeScannerForEmployees" tools:targetApi="31"> + (R.id.saveButton).setOnClickListener { + val db = DBStorageController(this).writableDatabase + + if (categoryId == 0) { // Inserting new category + val values = ContentValues().apply { + put(CategoriesContract.CategoryEntry.CATEGORY_NAME, categoryNameTextEdit.text.toString()) + } + + db.insert(CategoriesContract.CategoryEntry.TABLE_NAME, null, values) + } else { // Updating existing category + val values = ContentValues().apply { + put(CategoriesContract.CategoryEntry.CATEGORY_NAME, categoryNameTextEdit.text.toString()) + } + db.update(CategoriesContract.CategoryEntry.TABLE_NAME, values, "${BaseColumns._ID} = ?", arrayOf(categoryId.toString())) + } + + finish() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt index 8be4fb7..83bda71 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt @@ -4,11 +4,9 @@ import android.Manifest import android.content.ContentValues import android.os.Build import android.os.Bundle +import android.provider.BaseColumns import android.util.Log -import android.widget.Button -import android.widget.ImageView -import android.widget.TextView -import android.widget.Toast +import android.widget.* import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity @@ -31,6 +29,8 @@ class AddProductActivity : AppCompatActivity() { private lateinit var productNameText: TextView private lateinit var netWeightText: TextView + private lateinit var categorySpinner: Spinner + private lateinit var pictureFile: File private lateinit var picturesPath: File private lateinit var binding: ActivityAddProductBinding @@ -43,7 +43,6 @@ class AddProductActivity : AppCompatActivity() { picturesPath = File(filesDir, "pictures") picturesPath.mkdirs() -// imageView = binding.includeContent.addProductLayout imageView = findViewById(R.id.imageView) saveButton = findViewById(R.id.saveButton) @@ -53,6 +52,10 @@ class AddProductActivity : AppCompatActivity() { productNameText = findViewById(R.id.productName) netWeightText = findViewById(R.id.netWeight) + categorySpinner = findViewById(R.id.categorySpinner) + + fillupCategorySpinner() + saveButton.setOnClickListener { val productName = productNameText.text.toString() val netWeight = netWeightText.text @@ -70,13 +73,15 @@ class AddProductActivity : AppCompatActivity() { } val db = DBStorageController(this).writableDatabase - Log.d("QWERTYUIOP", "Putting ${pictureFile.name}") val values = ContentValues().apply { put(ProductContract.ProductEntry.PRODUCT_NAME, productName) put(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, netWeight.toString()) put(ProductContract.ProductEntry.IMAGE_FILENAME, pictureFile.nameWithoutExtension) + put(ProductContract.ProductEntry.CATEGORY, categorySpinner.selectedItemPosition) } + Log.d("QWERTYUIOP", "SIP ${categorySpinner.selectedItemPosition}") + db.insert(ProductContract.ProductEntry.TABLE_NAME, null, values) finish() @@ -112,6 +117,29 @@ class AddProductActivity : AppCompatActivity() { // setContentView(binding.root) } + + fun fillupCategorySpinner() { + val db = DBStorageController(this).readableDatabase + + val categories = mutableListOf("") + + val projection = arrayOf( + CategoriesContract.CategoryEntry.CATEGORY_NAME + ) + + val cursor = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection, null, null, null, null, BaseColumns._ID+" ASC") + + with (cursor) { + while (moveToNext()) { + categories.add(getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME))) + } + } + + val arrayAdapter = ArrayAdapter(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, categories) + arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item) + categorySpinner.adapter = arrayAdapter + } + @RequiresApi(Build.VERSION_CODES.R) val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean -> if (success) { @@ -137,7 +165,9 @@ class AddProductActivity : AppCompatActivity() { //Saving picture to a temp file for further hash calculation and moving to a proper directory val imageFile = File(this.filesDir, "image.png") val imageUri = getImageUri(this, imageFile) - takePicture.launch(imageUri) + if (imageUri != null) { + takePicture.launch(imageUri) + } } @RequiresApi(Build.VERSION_CODES.R) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FullscreenActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FullscreenActivity.kt index 9813c47..600c2b3 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FullscreenActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FullscreenActivity.kt @@ -28,7 +28,7 @@ class FullscreenActivity : Activity() { fullscreenImageView = findViewById(R.id.fullscreenImageView) val extras = intent.extras - val imageHash = extras!!.get("imagehash") as String? //extras!!.getParcelable("imagehash") as String? + val imageHash = extras!!.get("imagehash") as String? val picturesDir = File(filesDir, "pictures") 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 e5d15db..8a87d8e 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -33,10 +33,26 @@ class MainActivity : AppCompatActivity() { // appBarConfiguration = AppBarConfiguration(navController.graph) // setupActionBarWithNavController(navController, appBarConfiguration) - binding.addProductFab.setOnClickListener { view -> - val addProductIntent = Intent(this, AddProductActivity::class.java) - val extras = Bundle() - ContextCompat.startActivity(this, addProductIntent, extras) + binding.newElementFab.setOnClickListener { view -> + val currentPosition = binding.tabTablayout.selectedTabPosition + val fragment = adapter.getItem(currentPosition) + + when (fragment::class.simpleName.toString()) { + "StorageFragment" -> { + val addProductIntent = Intent(this, AddProductActivity::class.java) + val extras = Bundle() + ContextCompat.startActivity(this, addProductIntent, extras) + } + "CategoriesFragment" -> { + val addCategoryIntent = Intent(this, AddCategoryActivity::class.java) + val extras = Bundle() + extras.putInt("categoryid", 0) + extras.putString("categoryname", "New category") + addCategoryIntent.putExtras(extras) + ContextCompat.startActivity(this, addCategoryIntent, extras) + } + } + } } @@ -69,7 +85,6 @@ class MainActivity : AppCompatActivity() { "StorageFragment" -> { val storageFragment = fragment as StorageFragment storageFragment.removeSelected() - } } true diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/AddProductFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/AddProductFragment.kt index 00a45ff..a4f327d 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/AddProductFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/AddProductFragment.kt @@ -1,29 +1,20 @@ package org.foxarmy.barcodescannerforemployees.fragments import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.databinding.FragmentAddProductBinding -import java.io.File - class AddProductFragment : Fragment() { - - private lateinit var binding: FragmentAddProductBinding override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - - - - Log.d("QWERTYUIOP", "хуета1") binding = FragmentAddProductBinding.inflate(layoutInflater) return inflater.inflate(R.layout.fragment_add_product, container, false) } 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 494b9a5..4b39971 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt @@ -1,11 +1,17 @@ package org.foxarmy.barcodescannerforemployees.fragments 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 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.views.CategoryView class CategoriesFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { @@ -23,9 +29,34 @@ class CategoriesFragment : Fragment() { override fun onResume() { super.onResume() -// updateContent() + updateContent() } + fun updateContent() { + val layout = view?.findViewById(R.id.categoriesLayout) + layout?.removeAllViews() + + val db = DBStorageController(requireContext()).readableDatabase + + val projection = arrayOf( + BaseColumns._ID, + CategoriesContract.CategoryEntry.CATEGORY_NAME + ) + + val cursor = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection, null, null, null, null, null) + + with (cursor) { + while(moveToNext()) { + val categoryId = getInt(getColumnIndexOrThrow(BaseColumns._ID)) + val categoryName = getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME)) + + val category = Category(categoryId, categoryName) + + val categoryView = CategoryView(requireActivity(), requireContext(), category) + layout?.addView(categoryView) + } + } + } // public fun updateContent() { // // val grv = getView()?.findViewById(R.id.contentGridLayout) @@ -65,6 +96,6 @@ class CategoriesFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) -// updateContent() + updateContent() } } \ No newline at end of file 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 6264160..1ce474e 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -13,24 +13,12 @@ import androidx.gridlayout.widget.GridLayout import org.foxarmy.barcodescannerforemployees.* import org.foxarmy.barcodescannerforemployees.views.AbstractProductView -/** - * A simple [Fragment] subclass. - * Use the [StorageFragment.newInstance] factory method to - * create an instance of this fragment. - */ class StorageFragment : Fragment() { - public var test_value = 1 - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - } - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_storage, container, false) } @@ -41,7 +29,7 @@ class StorageFragment : Fragment() { } fun removeSelected() { - val grv = getView()?.findViewById(R.id.contentGridLayout) + val grv = view?.findViewById(R.id.contentGridLayout) val db = DBStorageController(requireContext()) var deleted = false @@ -61,7 +49,7 @@ class StorageFragment : Fragment() { fun updateContent() { - val grv = getView()?.findViewById(R.id.contentGridLayout) + val grv = view?.findViewById(R.id.contentGridLayout) grv?.removeAllViews() val db = DBStorageController(requireContext()).readableDatabase @@ -79,8 +67,20 @@ class StorageFragment : Fragment() { val productName = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NAME)) val netWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT)) val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) + val category = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.CATEGORY)) + var categoryName = "" - val product = AbstractProduct(productId, productName, netWeight, productImageHash, 1) + val projection2 = arrayOf(CategoriesContract.CategoryEntry.CATEGORY_NAME) + val cursor2 = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection2, "${BaseColumns._ID} = ?", + arrayOf(category.toString()), null, null, BaseColumns._ID+" ASC") + + with (cursor2) { + while (moveToNext()) { + categoryName = getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME)) + } + } + + val product = AbstractProduct(productId, productName, netWeight, productImageHash, categoryName) generateThumbnailForImage(context!!, productImageHash) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt index cc56321..d3bf0ba 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt @@ -61,8 +61,9 @@ class AbstractProductView: LinearLayout { productNameField.text = product.name netWeightField.text = product.netWeight.toString() + categoryField.text = product.categoryName - //TODO: category and units + //TODO: units productLayout.setOnLongClickListener { isProductSelected = !isProductSelected diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt new file mode 100644 index 0000000..57e2d12 --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt @@ -0,0 +1,47 @@ +package org.foxarmy.barcodescannerforemployees.views + +import android.app.Activity +import android.content.Context +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.R +import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity + +class CategoryView : LinearLayout { + var category: Category + val categoryName: TextView + val updateButton: Button + val deleteButton: Button + + constructor(activity: Activity, context: Context, category: Category) : super(context) { + this.category = category + + val inflater: LayoutInflater = activity.layoutInflater + inflater.inflate(R.layout.category_view, this) + + categoryName = findViewById(R.id.categoryNameTextView) + 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) + } + + deleteButton.setOnClickListener { + TODO("implement delete button") + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_add_category.xml b/app/src/main/res/layout/activity_add_category.xml new file mode 100644 index 0000000..eed9139 --- /dev/null +++ b/app/src/main/res/layout/activity_add_category.xml @@ -0,0 +1,26 @@ + + + + +