diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt index 4961472..9fd369a 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt @@ -1,47 +1,58 @@ package org.foxarmy.barcodescannerforemployees +import android.app.Activity import android.content.Context -import android.graphics.Bitmap -import android.graphics.BitmapFactory +import android.os.Bundle import android.util.Log +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView -import android.widget.Toast +import androidx.core.net.toFile +import org.foxarmy.barcodescannerforemployees.databinding.AbstractProductViewBinding +import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBinding import java.io.File -/** - * TODO: document your custom view class. - */ class AbstractProductView: LinearLayout { + private var _binding: AbstractProductViewBinding? = null private val productImage = ImageView(context) private val productNameField = TextView(context) private val netWeightField = TextView(context) private val categoryField = TextView(context) + fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { - constructor(context: Context, productImageFile: String, productName: String, netWeight: Double, category: Int) : super(context) { - this.orientation = LinearLayout.VERTICAL + _binding = AbstractProductViewBinding.inflate(inflater, container, false) + } - val imageFile = File(File(context.filesDir, "pictures"), productImageFile) - val image: ByteArray = imageFile.readBytes() + constructor(activity: Activity, context: Context, productImageFile: String, productName: String, netWeight: Double, category: Int) : super(context) { + this.orientation = VERTICAL - if (image.isEmpty()) { - Toast.makeText(context, image.size.toString(), Toast.LENGTH_LONG).show() + val picturesDir = File(context.filesDir, "pictures") + picturesDir.mkdirs() + val imageUri = getImageUri(activity, File(picturesDir, productImageFile)) + if (imageUri != null) { + Log.d("QWERTYUIOP", "хуй: ${imageUri.path.toString()}") + try { + Log.d("QWERTYUIOP", "Does exist: ${imageUri.toFile().absolutePath}") + } catch (e:Exception) { + Log.d("QWERTYUIOP", e.message.toString()) + } } - try { - Log.d("org.foxarmy.barcodescannerforemployees", "[ХУЙ]" + imageFile.absolutePath) - val bitmap: Bitmap = BitmapFactory.decodeFile(imageFile.absolutePath) - } catch (e:Exception) { - Log.d("org.foxarmy.barcodescannerforemployees", "[ХУЙ] " + e) - } -// productImage.setImageBitmap(bitmap) - productNameField.setText(productName) - netWeightField.setText(netWeight.toString()) + productImage.setImageURI(imageUri) + productNameField.text = productName + netWeightField.text = netWeight.toString() addView(productNameField) addView(netWeightField) + + // if(findViewById(R.id.productLayout) == null) { // Toast.makeText(context, "AAAAAAAAAAA", Toast.LENGTH_LONG).show() // } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt index 1bb7962..aebf3e8 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt @@ -1,8 +1,6 @@ package org.foxarmy.barcodescannerforemployees -//import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions import android.content.ContentValues -import android.net.Uri import android.os.Build import android.os.Bundle import android.util.Log @@ -12,9 +10,7 @@ import android.view.ViewGroup import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi -import androidx.core.content.FileProvider import androidx.fragment.app.Fragment -import com.google.firebase.components.BuildConfig import com.google.mlkit.vision.barcode.common.Barcode import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions import com.google.mlkit.vision.codescanner.GmsBarcodeScanning @@ -22,16 +18,8 @@ import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBind import java.io.File import java.nio.file.Files import java.nio.file.StandardCopyOption -import java.security.MessageDigest -@OptIn(ExperimentalStdlibApi::class) -fun String.md5(): String { - val md = MessageDigest.getInstance("MD5") - val digest = md.digest(this.toByteArray()) - return digest.toHexString() -} - class AddProductFragment : Fragment() { private var _binding: AddProductFragmentBinding? = null @@ -51,10 +39,6 @@ class AddProductFragment : Fragment() { return binding.root } - fun getImageUri(imageFile: File): Uri? { - return FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID + "." + requireActivity().localClassName + ".provider", imageFile) - } - @RequiresApi(Build.VERSION_CODES.O) val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean -> if (success) { @@ -64,7 +48,7 @@ class AddProductFragment : Fragment() { pictureFile = File(picturesPath, "$imageHash.png") Files.move(tempfile.toPath(), pictureFile.toPath(), StandardCopyOption.REPLACE_EXISTING) tempfile.delete() - binding.imageView.setImageURI(getImageUri(pictureFile)) + binding.imageView.setImageURI(getImageUri(requireActivity(), pictureFile)) Log.i("QWERTYUIOP", "Picture saved") } else { Log.e("QWERTYUIOP", "Cannot save a picture") @@ -75,7 +59,10 @@ class AddProductFragment : Fragment() { 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(imageFile)//FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID + "." + requireActivity().localClassName + ".provider", imageFile) + val imageUri = getImageUri(requireActivity(), imageFile) + if (imageUri != null) { + Log.d("QWERTYUIOP", imageUri.path.toString()) + } takePicture.launch(imageUri) } @@ -113,7 +100,7 @@ class AddProductFragment : Fragment() { } 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()) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt index ed73c27..65e7fd2 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt @@ -18,7 +18,6 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val dbHelper = DBStorageController(applicationContext) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt index c4d33d2..caab862 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt @@ -34,7 +34,7 @@ class StorageFragment : Fragment() { val projection = arrayOf(BaseColumns._ID, ProductContract.ProductEntry.PRODUCT_NAME, ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, ProductContract.ProductEntry.IMAGE_FILENAME) val selection = "${ProductContract.ProductEntry.PRODUCT_NAME} = ?" - val selectionArgs = arrayOf("test") + val selectionArgs = arrayOf("обогреватель") val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) @@ -46,6 +46,7 @@ class StorageFragment : Fragment() { getView()?.findViewById(R.id.storageLayout)?.addView( AbstractProductView( + requireActivity(), requireContext(), productImageFile = pictureFilename, productName = productName, diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt new file mode 100644 index 0000000..fdbaa01 --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt @@ -0,0 +1,19 @@ +package org.foxarmy.barcodescannerforemployees + +import android.app.Activity +import android.net.Uri +import androidx.core.content.FileProvider +import com.google.firebase.components.BuildConfig +import java.io.File +import java.security.MessageDigest + +fun getImageUri(activity: Activity, imageFile: File): Uri? { + return FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + "." + activity.localClassName + ".provider", imageFile) +} + +@OptIn(ExperimentalStdlibApi::class) +fun String.md5(): String { + val md = MessageDigest.getInstance("MD5") + val digest = md.digest(this.toByteArray()) + return digest.toHexString() +} \ No newline at end of file diff --git a/app/src/main/res/layout/abstract_product_view.xml b/app/src/main/res/layout/abstract_product_view.xml new file mode 100644 index 0000000..8a51305 --- /dev/null +++ b/app/src/main/res/layout/abstract_product_view.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/sample_abstract_product_view.xml b/app/src/main/res/layout/sample_abstract_product_view.xml deleted file mode 100644 index b0fd7af..0000000 --- a/app/src/main/res/layout/sample_abstract_product_view.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3e0901f..72f9bf7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,4 +13,7 @@ Product name Product type Image of a product + Уззкое название товара + 1998 + g \ No newline at end of file