This commit is contained in:
leca 2024-09-16 03:00:34 +03:00
parent ea102c87a2
commit 50da2955e8
13 changed files with 227 additions and 63 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-feature android:name="android.hardware.camera" android:required="false"/> <uses-feature android:name="android.hardware.camera" android:required="true"/>
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.CAMERA" />
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -1,11 +1,16 @@
package org.foxarmy.barcodescannerforemployees package org.foxarmy.barcodescannerforemployees
//import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions //import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore
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.Toast import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.google.mlkit.vision.barcode.common.Barcode import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
@ -15,7 +20,9 @@ import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBind
/** /**
* A simple [Fragment] subclass as the default destination in the navigation. * A simple [Fragment] subclass as the default destination in the navigation.
*/ */
class addProductFragment : Fragment() {
class AddProductFragment : Fragment() {
private var _binding: AddProductFragmentBinding? = null private var _binding: AddProductFragmentBinding? = null
@ -33,25 +40,56 @@ class addProductFragment : Fragment() {
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
200 -> {
if (resultCode == Activity.RESULT_OK && data != null) {
binding.imageView.setImageBitmap(data.extras?.get("data") as Bitmap)
}
}
}
}
val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, 200)
} else {
Toast.makeText(
requireContext(),
"I need permission for camera in order to take a picture!",
Toast.LENGTH_LONG
).show()
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
binding.takePictureButton.setOnClickListener {
requestPermissionLauncher.launch(android.Manifest.permission.CAMERA)
}
binding.scanButton.setOnClickListener { binding.scanButton.setOnClickListener {
// findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
// Toast.makeText(requireContext(), "Test", Toast.LENGTH_LONG).show()
val options = GmsBarcodeScannerOptions.Builder() val options = GmsBarcodeScannerOptions.Builder()
.setBarcodeFormats( .setBarcodeFormats(
Barcode.FORMAT_EAN_13) Barcode.FORMAT_EAN_13
)
.build() .build()
val scanner = GmsBarcodeScanning.getClient(requireContext()) val scanner = GmsBarcodeScanning.getClient(requireContext())
scanner.startScan() scanner.startScan()
.addOnSuccessListener { barcode -> .addOnSuccessListener { barcode ->
// Task completed successfully
//Toast.makeText(requireContext(), barcode.rawValue, Toast.LENGTH_LONG).show()
binding.productName.setText(barcode.rawValue) binding.productName.setText(barcode.rawValue)
} }
.addOnFailureListener { e -> .addOnFailureListener { e ->
Toast.makeText(requireContext(), "Failed to scan barcode. Please, try again", Toast.LENGTH_LONG).show() Toast.makeText(
requireContext(),
"Failed to scan barcode. Please, try again or enter data manually",
Toast.LENGTH_LONG
).show()
} }
} }
} }

View File

@ -0,0 +1,39 @@
package org.foxarmy.barcodescannerforemployees
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.provider.BaseColumns
object ProductContract {
object ProductEntry :BaseColumns {
const val TABLE_NAME = "products"
const val PRODUCT_NAME = "name"
const val PRODUCT_NET_WEIGHT = "net_weight"
const val IMAGE_FILENAME = "image_filename"
}
}
const val SQL_CREATE_ENTRIES =
"CREATE TABLE ${ProductContract.ProductEntry.TABLE_NAME} (" +
"${BaseColumns._ID} INTEGER PRIMARY KEY," +
"${ProductContract.ProductEntry.PRODUCT_NAME} TEXT," +
"${ProductContract.ProductEntry.PRODUCT_NET_WEIGHT} TEXT" +
"${ProductContract.ProductEntry.IMAGE_FILENAME} TEXT)"
class DBStorageController(context: Context) : SQLiteOpenHelper (context, DATABASE_NAME, null, DATABASE_VERSION) {
override fun onCreate(db: SQLiteDatabase) {
db.execSQL(SQL_CREATE_ENTRIES)
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
TODO("Not yet implemented")
}
companion object {
// If you change the database schema, you must increment the database version.
const val DATABASE_VERSION = 1
const val DATABASE_NAME = "FeedReader.db"
}
}

View File

@ -1,15 +1,14 @@
package org.foxarmy.barcodescannerforemployees package org.foxarmy.barcodescannerforemployees
import android.os.Bundle import android.os.Bundle
import com.google.android.material.snackbar.Snackbar import android.provider.BaseColumns
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupActionBarWithNavController
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import org.foxarmy.barcodescannerforemployees.databinding.ActivityMainBinding import org.foxarmy.barcodescannerforemployees.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@ -20,6 +19,34 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val dbHelper = DBStorageController(applicationContext)
// val db = dbHelper.writableDatabase
//
// val values = ContentValues().apply {
// put(ProductContract.ProductEntry.PRODUCT_NAME, "Имя продукта")
// put(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, "0г")
// put(ProductContract.ProductEntry.IMAGE_FILENAME, "image.png")
// }
//
// val netRowId = db?.insert(ProductContract.ProductEntry.TABLE_NAME, null, values)
val db2 = dbHelper.readableDatabase
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("Имя продукта")
val cursor = db2.query(
ProductContract.ProductEntry.TABLE_NAME,
null,
selection,
selectionArgs,
null,
null,
null
)
binding = ActivityMainBinding.inflate(layoutInflater) binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
@ -33,7 +60,8 @@ class MainActivity : AppCompatActivity() {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null) // .setAction("Action", null)
// .setAnchorView(R.id.add_product_fab).show() // .setAnchorView(R.id.add_product_fab).show()
Toast.makeText(applicationContext, "Test", Toast.LENGTH_SHORT) // Toast.makeText(applicationContext, "Test", Toast.LENGTH_SHORT).show()
navController.navigate(R.id.AddProductFragment)
} }
} }

View File

@ -11,7 +11,7 @@ import org.foxarmy.barcodescannerforemployees.databinding.FragmentSecondBinding
/** /**
* A simple [Fragment] subclass as the second destination in the navigation. * A simple [Fragment] subclass as the second destination in the navigation.
*/ */
class SecondFragment : Fragment() { class MainScreenFragment : Fragment() {
private var _binding: FragmentSecondBinding? = null private var _binding: FragmentSecondBinding? = null

View File

@ -41,4 +41,5 @@ fun main () {
val p = Parser("""\<table\s*class\=\"randomBarcodes\"\s*>""", """\<\/table\>""", """[ёЁ\u0401\u0451\u0410-\u044f\d\w\s]{16,}""") val p = Parser("""\<table\s*class\=\"randomBarcodes\"\s*>""", """\<\/table\>""", """[ёЁ\u0401\u0451\u0410-\u044f\d\w\s]{16,}""")
p.parse(Requester("https://barcode-list.ru", "barcode/RU/Поиск.htm?barcode=4680036915828", ).request("4680036915828")) p.parse(Requester("https://barcode-list.ru", "barcode/RU/Поиск.htm?barcode=4680036915828", ).request("4680036915828"))
// println(Requester("https://barcode-list.ru", "barcode/RU/Поиск.htm?barcode=4680036915828", ).request("4680036915828")) // println(Requester("https://barcode-list.ru", "barcode/RU/Поиск.htm?barcode=4680036915828", ).request("4680036915828"))
} }

View File

@ -1,28 +0,0 @@
package org.foxarmy.barcodescannerforemployees
import android.content.Context
import android.provider.BaseColumns
object ProductContract {
object ProductEntry :BaseColumns {
const val TABLE_NAME = "products"
const val PRODUCT_NAME = "name"
const val PRODUCT_NET_WEIGHT = "net_weight"
const val IMAGE_FILENAME = "image_filename"
}
}
class StorageController(context: Context) {
val context = context
fun initStorage() {
val productContract = ProductContract
val SQL_CREATE_ENTRIES =
"CREATE TABLE ${ProductContract.ProductEntry.TABLE_NAME} (" +
"${BaseColumns._ID} INTEGER PRIMARY KEY," +
"${ProductContract.ProductEntry.PRODUCT_NAME} TEXT," +
"${ProductContract.ProductEntry.PRODUCT_NET_WEIGHT} TEXT" +
"${ProductContract.ProductEntry.IMAGE_FILENAME} TEXT)"
}
}

View File

@ -0,0 +1,27 @@
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
/**
* A simple [Fragment] subclass.
* Use the [StorageFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class StorageFragment : Fragment() {
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)
}
}

View File

@ -5,29 +5,66 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".addProductFragment"> tools:context=".AddProductFragment">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:padding="16dp"> android:padding="16dp">
<Button <Button
android:id="@+id/scan_button" android:id="@+id/scan_button"
android:layout_width="wrap_content" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_height="50dp"
android:text="@string/scan_label" android:text="@string/scan_label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/> app:layout_constraintTop_toBottomOf="@+id/editTextText"
android:layout_marginTop="25dp"/>
<ImageView
android:src="@android:drawable/ic_menu_camera"
android:layout_width="356dp"
android:layout_height="303dp" android:id="@+id/imageView"
android:layout_marginBottom="25dp"
app:layout_constraintBottom_toTopOf="@+id/productName" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="15dp"/>
<EditText <EditText
android:layout_width="365dp" android:layout_width="350dp"
android:layout_height="42dp" android:layout_height="50dp"
android:inputType="text" android:inputType="text"
android:text="@string/product_name_label"
android:ems="10" android:ems="10"
android:id="@+id/productName" android:id="@+id/productName"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="162dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" android:layout_marginEnd="8dp"
android:layout_marginStart="11dp"/> app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/editTextText"
app:layout_constraintHorizontal_bias="0.5"
android:visibility="visible" android:hint="@string/product_name_label" android:textColorHint="#737373"
app:layout_constraintTop_toBottomOf="@+id/imageView"/>
<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:ems="10"
android:id="@+id/editTextText"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:visibility="visible" android:hint="@string/netWeight" android:textColorHint="#737373"
app:layout_constraintTop_toBottomOf="@+id/productName"/>
<Button
android:text="@string/saveButton"
android:layout_width="100dp"
android:layout_height="50dp" android:id="@+id/saveButton"
app:layout_constraintTop_toBottomOf="@+id/editTextText"
android:layout_marginTop="25dp" app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:text="@string/takePicture"
android:layout_width="100dp"
android:layout_height="55dp" android:id="@+id/takePictureButton"
app:layout_constraintTop_toBottomOf="@+id/editTextText"
android:layout_marginTop="24dp" app:layout_constraintStart_toEndOf="@+id/scan_button"
android:layout_marginStart="33dp" app:layout_constraintEnd_toStartOf="@+id/saveButton"
android:layout_marginEnd="6dp" app:layout_constraintHorizontal_bias="0.0"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView> </androidx.core.widget.NestedScrollView>

View File

@ -5,7 +5,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".SecondFragment"> tools:context=".MainScreenFragment">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StorageFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View File

@ -3,26 +3,31 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph" android:id="@+id/nav_graph"
app:startDestination="@id/FirstFragment"> app:startDestination="@id/storageFragment">
<fragment <fragment
android:id="@+id/FirstFragment" android:id="@+id/AddProductFragment"
android:name="org.foxarmy.barcodescannerforemployees.addProductFragment" android:name="org.foxarmy.barcodescannerforemployees.AddProductFragment"
android:label="@string/first_fragment_label" android:label="@string/first_fragment_label"
tools:layout="@layout/add_product_fragment"> tools:layout="@layout/add_product_fragment">
<action <action
android:id="@+id/action_FirstFragment_to_SecondFragment" android:id="@+id/action_FirstFragment_to_SecondFragment"
app:destination="@id/SecondFragment"/> app:destination="@id/MainScreenFragment"/>
</fragment> </fragment>
<fragment <fragment
android:id="@+id/SecondFragment" android:id="@+id/MainScreenFragment"
android:name="org.foxarmy.barcodescannerforemployees.SecondFragment" android:name="org.foxarmy.barcodescannerforemployees.MainScreenFragment"
android:label="@string/second_fragment_label" android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_second"> tools:layout="@layout/fragment_second">
<action <action
android:id="@+id/action_SecondFragment_to_FirstFragment" android:id="@+id/action_SecondFragment_to_FirstFragment"
app:destination="@id/FirstFragment"/> app:destination="@id/AddProductFragment"/>
</fragment>
<fragment android:id="@+id/storageFragment" android:name="org.foxarmy.barcodescannerforemployees.StorageFragment"
android:label="fragment_storage" tools:layout="@layout/fragment_storage">
<action android:id="@+id/action_storageFragment_to_AddProductFragment"
app:destination="@id/AddProductFragment"/>
</fragment> </fragment>
</navigation> </navigation>

View File

@ -7,4 +7,9 @@
<string name="scan_label">Scan</string> <string name="scan_label">Scan</string>
<string name="previous">Previous</string> <string name="previous">Previous</string>
<string name="product_name_label">Product name</string> <string name="product_name_label">Product name</string>
<string name="netWeight">Net weight</string>
<string name="saveButton">Save</string>
<string name="takePicture">Take picture</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources> </resources>