diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 80dceec..ec11dbb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,7 +25,11 @@ android:theme="@style/Theme.BarcodeScannerForEmployees" tools:targetApi="31"> + + diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainScreenFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainScreenFragment.kt deleted file mode 100644 index 86e590c..0000000 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainScreenFragment.kt +++ /dev/null @@ -1,42 +0,0 @@ -package org.foxarmy.barcodescannerforemployees - -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.fragment.app.Fragment -import org.foxarmy.barcodescannerforemployees.databinding.FragmentSecondBinding - -/** - * A simple [Fragment] subclass as the second destination in the navigation. - */ -class MainScreenFragment : Fragment() { - - private var _binding: FragmentSecondBinding? = null - - // This property is only valid between onCreateView and - // onDestroyView. - private val binding get() = _binding!! - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = FragmentSecondBinding.inflate(inflater, container, false) - return binding.root - - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - -// binding..setOnClickListener { -// findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment) -// } - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt similarity index 54% rename from app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt rename to app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt index a2d4d32..8be4fb7 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt @@ -1,59 +1,131 @@ -package org.foxarmy.barcodescannerforemployees +package org.foxarmy.barcodescannerforemployees.activities +import android.Manifest import android.content.ContentValues import android.os.Build import android.os.Bundle import android.util.Log -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.widget.Button +import android.widget.ImageView +import android.widget.TextView import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi -import androidx.fragment.app.Fragment -import androidx.navigation.fragment.findNavController +import androidx.appcompat.app.AppCompatActivity import com.google.mlkit.vision.barcode.common.Barcode import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions import com.google.mlkit.vision.codescanner.GmsBarcodeScanning -import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBinding +import org.foxarmy.barcodescannerforemployees.* +import org.foxarmy.barcodescannerforemployees.databinding.ActivityAddProductBinding import java.io.File import java.nio.file.Files import java.nio.file.StandardCopyOption +class AddProductActivity : AppCompatActivity() { + private lateinit var imageView: ImageView -class AddProductFragment : Fragment() { + private lateinit var saveButton: Button + private lateinit var takePictureButton: Button + private lateinit var scanButton: Button + + private lateinit var productNameText: TextView + private lateinit var netWeightText: TextView - private var _binding: AddProductFragmentBinding? = null private lateinit var pictureFile: File - private val binding get() = _binding!! private lateinit var picturesPath: File + private lateinit var binding: ActivityAddProductBinding - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - picturesPath = File(requireContext().filesDir, "pictures") + setContentView(R.layout.fragment_add_product) + + + picturesPath = File(filesDir, "pictures") picturesPath.mkdirs() +// imageView = binding.includeContent.addProductLayout + imageView = findViewById(R.id.imageView) + + saveButton = findViewById(R.id.saveButton) + takePictureButton = findViewById(R.id.takePictureButton) + scanButton = findViewById(R.id.scan_button) + + productNameText = findViewById(R.id.productName) + netWeightText = findViewById(R.id.netWeight) + + saveButton.setOnClickListener { + val productName = productNameText.text.toString() + val netWeight = netWeightText.text + if (!this::pictureFile.isInitialized || !pictureFile.exists()) { + Toast.makeText(this, "Please, make a picture of a product!", Toast.LENGTH_SHORT).show() + return@setOnClickListener + } + + if (productName == "") { + Toast.makeText(this, "Please, write a name of a product!", Toast.LENGTH_SHORT).show() + return@setOnClickListener + } + if (netWeight.toString() == "" || netWeight.toString().toDoubleOrNull() == null) { + Toast.makeText(this, "Please, write a valid net weight of a product!", Toast.LENGTH_SHORT).show() + } + + 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) + } + + db.insert(ProductContract.ProductEntry.TABLE_NAME, null, values) + + finish() + } + + takePictureButton.setOnClickListener { + Log.d("QWERTYUIOP", "test") + requestPermissionLauncher.launch(Manifest.permission.CAMERA) + } + + scanButton.setOnClickListener { + val options = GmsBarcodeScannerOptions.Builder() + .setBarcodeFormats( + Barcode.FORMAT_EAN_13 + ) + .build() + val scanner = GmsBarcodeScanning.getClient(this) + scanner.startScan() + .addOnSuccessListener { barcode -> + productNameText.setText(barcode.rawValue) + } + .addOnFailureListener { e -> + Toast.makeText( + this, + "Failed to scan barcode. Please, try again or enter data manually", + Toast.LENGTH_LONG + ).show() + } + } + +// binding = ActivityAddProductBinding.inflate(layoutInflater) + +// setContentView(binding.root) - _binding = AddProductFragmentBinding.inflate(inflater, container, false) - return binding.root } - @RequiresApi(Build.VERSION_CODES.R) val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean -> if (success) { //Move picture to a proper directory according to its calculated hash - val tempfile = File(requireContext().filesDir, "image.png") + val tempfile = File(filesDir, "image.png") val imageContent = tempfile.inputStream().readBytes() val imageHash = imageContent.toString(Charsets.UTF_8).md5() pictureFile = File(picturesPath, "$imageHash.png") Files.move(tempfile.toPath(), pictureFile.toPath(), StandardCopyOption.REPLACE_EXISTING) tempfile.delete() - generateThumbnailForImage(context!!, imageHash) + generateThumbnailForImage(this, imageHash) - binding.imageView.setImageURI(getImageUri(requireActivity(), pictureFile)) + imageView.setImageURI(getImageUri(this, pictureFile)) Log.i("QWERTYUIOP", "Picture saved") } else { Log.e("QWERTYUIOP", "Cannot save a picture") @@ -63,8 +135,8 @@ class AddProductFragment : Fragment() { @RequiresApi(Build.VERSION_CODES.R) fun getPicture () { //Saving picture to a temp file for further hash calculation and moving to a proper directory - val imageFile = File(requireContext().filesDir, "image.png") - val imageUri = getImageUri(requireActivity(), imageFile) + val imageFile = File(this.filesDir, "image.png") + val imageUri = getImageUri(this, imageFile) takePicture.launch(imageUri) } @@ -76,71 +148,7 @@ class AddProductFragment : Fragment() { if (isGranted) { getPicture() } else { - Toast.makeText(requireContext(), "I need permission in order to take a picture", Toast.LENGTH_LONG).show() + Toast.makeText(this, "I need permission in order to take a picture", Toast.LENGTH_LONG).show() } } - - @RequiresApi(Build.VERSION_CODES.R) - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - - binding.saveButton.setOnClickListener { - val productName = binding.productName.text.toString() - val netWeight = binding.netWeight.text - if (!this::pictureFile.isInitialized || !pictureFile.exists()) { - Toast.makeText(context, "Please, make a picture of a product!", Toast.LENGTH_SHORT).show() - return@setOnClickListener - } - - if (productName == "") { - Toast.makeText(context, "Please, write a name of a product!", Toast.LENGTH_SHORT).show() - return@setOnClickListener - } - if (netWeight.toString() == "" || netWeight.toString().toDoubleOrNull() == null) { - Toast.makeText(context, "Please, write a valid net weight of a product!", Toast.LENGTH_SHORT).show() - } - - val db = DBStorageController(requireContext()).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) - } - - db.insert(ProductContract.ProductEntry.TABLE_NAME, null, values) - - findNavController().navigate(R.id.storageFragment) - } - - binding.takePictureButton.setOnClickListener { - requestPermissionLauncher.launch(android.Manifest.permission.CAMERA) - } - - binding.scanButton.setOnClickListener { - val options = GmsBarcodeScannerOptions.Builder() - .setBarcodeFormats( - Barcode.FORMAT_EAN_13 - ) - .build() - val scanner = GmsBarcodeScanning.getClient(requireContext()) - scanner.startScan() - .addOnSuccessListener { barcode -> - binding.productName.setText(barcode.rawValue) - } - .addOnFailureListener { e -> - Toast.makeText( - requireContext(), - "Failed to scan barcode. Please, try again or enter data manually", - Toast.LENGTH_LONG - ).show() - } - } - - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/FullscreenActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FullscreenActivity.kt similarity index 89% rename from app/src/main/java/org/foxarmy/barcodescannerforemployees/FullscreenActivity.kt rename to app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FullscreenActivity.kt index f893817..9813c47 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/FullscreenActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FullscreenActivity.kt @@ -1,4 +1,4 @@ -package org.foxarmy.barcodescannerforemployees +package org.foxarmy.barcodescannerforemployees.activities import android.app.Activity import android.os.Bundle @@ -7,7 +7,9 @@ import android.os.Looper import android.util.Log import android.widget.ImageView import android.widget.LinearLayout +import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.databinding.ActivityFullscreenBinding +import org.foxarmy.barcodescannerforemployees.getImageUri import java.io.File diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/StorageActivity.kt similarity index 72% rename from app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt rename to app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/StorageActivity.kt index 6913fbd..add21f8 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/StorageActivity.kt @@ -1,34 +1,39 @@ -package org.foxarmy.barcodescannerforemployees +package org.foxarmy.barcodescannerforemployees.activities +import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import androidx.navigation.findNavController import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.navigateUp import androidx.navigation.ui.setupActionBarWithNavController -import org.foxarmy.barcodescannerforemployees.databinding.ActivityMainBinding +import org.foxarmy.barcodescannerforemployees.R +import org.foxarmy.barcodescannerforemployees.databinding.ActivityStorageBinding -class MainActivity : AppCompatActivity() { +class StorageActivity : AppCompatActivity() { private lateinit var appBarConfiguration: AppBarConfiguration - private lateinit var binding: ActivityMainBinding + private lateinit var binding: ActivityStorageBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - binding = ActivityMainBinding.inflate(layoutInflater) + binding = ActivityStorageBinding.inflate(layoutInflater) setContentView(binding.root) setSupportActionBar(binding.toolbar) - val navController = findNavController(R.id.nav_host_fragment_content_main) + val navController = findNavController(R.id.nav_host_fragment_content_storage) appBarConfiguration = AppBarConfiguration(navController.graph) setupActionBarWithNavController(navController, appBarConfiguration) binding.addProductFab.setOnClickListener { view -> - navController.navigate(R.id.AddProductFragment) + val addProductIntent = Intent(this, AddProductActivity::class.java) + val extras = Bundle() + ContextCompat.startActivity(this, addProductIntent, extras) } } @@ -47,7 +52,7 @@ class MainActivity : AppCompatActivity() { } override fun onSupportNavigateUp(): Boolean { - val navController = findNavController(R.id.nav_host_fragment_content_main) + val navController = findNavController(R.id.nav_host_fragment_content_storage) return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp() } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/AddProductFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/AddProductFragment.kt new file mode 100644 index 0000000..00a45ff --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/AddProductFragment.kt @@ -0,0 +1,30 @@ +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) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt similarity index 74% rename from app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt rename to app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt index bfade28..4e28dc1 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -1,4 +1,4 @@ -package org.foxarmy.barcodescannerforemployees +package org.foxarmy.barcodescannerforemployees.fragments import android.os.Bundle import android.provider.BaseColumns @@ -8,6 +8,8 @@ import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.gridlayout.widget.GridLayout +import org.foxarmy.barcodescannerforemployees.* +import org.foxarmy.barcodescannerforemployees.views.AbstractProductView /** * A simple [Fragment] subclass. @@ -28,11 +30,23 @@ class StorageFragment : Fragment() { return inflater.inflate(R.layout.fragment_storage, container, false) } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) + override fun onResume() { + super.onResume() + + refillWithContent() + } + + public fun refillWithContent() { + + val grv = getView()?.findViewById(R.id.contentGridLayout) + grv?.removeAllViews() val db = DBStorageController(requireContext()).readableDatabase - val projection = arrayOf(BaseColumns._ID, ProductContract.ProductEntry.PRODUCT_NAME, ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, ProductContract.ProductEntry.IMAGE_FILENAME) + val projection = arrayOf(BaseColumns._ID, + ProductContract.ProductEntry.PRODUCT_NAME, + ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, + ProductContract.ProductEntry.IMAGE_FILENAME + ) val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, null, null, null, null, null) @@ -52,7 +66,7 @@ class StorageFragment : Fragment() { netWeight, 1 ) - getView()?.findViewById(R.id.contentGridLayout)?.addView(abstractProduct) + grv?.addView(abstractProduct) abstractProduct.setOnClickListener { Log.d("QWERTYUIOP", "Clicked view") @@ -61,4 +75,10 @@ class StorageFragment : Fragment() { } } } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + refillWithContent() + } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt similarity index 91% rename from app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt rename to app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt index ab04687..4885c17 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt @@ -1,4 +1,4 @@ -package org.foxarmy.barcodescannerforemployees +package org.foxarmy.barcodescannerforemployees.views import android.app.Activity import android.content.Context @@ -12,6 +12,9 @@ import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat.startActivity +import org.foxarmy.barcodescannerforemployees.R +import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity +import org.foxarmy.barcodescannerforemployees.getImageUri import java.io.File class AbstractProductView: LinearLayout { diff --git a/app/src/main/res/layout/fragment_second.xml b/app/src/main/res/layout/activity_add_product.xml similarity index 56% rename from app/src/main/res/layout/fragment_second.xml rename to app/src/main/res/layout/activity_add_product.xml index cd2be64..b500a44 100644 --- a/app/src/main/res/layout/fragment_second.xml +++ b/app/src/main/res/layout/activity_add_product.xml @@ -5,9 +5,6 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainScreenFragment"> - + tools:context=".fragments.AddProductFragment"> + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_fullscreen.xml b/app/src/main/res/layout/activity_fullscreen.xml index 7ded621..b5463ba 100644 --- a/app/src/main/res/layout/activity_fullscreen.xml +++ b/app/src/main/res/layout/activity_fullscreen.xml @@ -5,7 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" - tools:context=".FullscreenActivity"> + tools:context=".activities.FullscreenActivity">