diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt index 9d7bb4d..c525e3e 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt @@ -27,6 +27,7 @@ class AddAbstractProductActivity : AppCompatActivity() { private lateinit var takePictureButton: Button private lateinit var scanButton: Button + private lateinit var barcodeText: EditText private lateinit var productNameText: TextView private lateinit var netWeightText: TextView @@ -35,7 +36,7 @@ class AddAbstractProductActivity : AppCompatActivity() { private var abstractProduct: AbstractProduct? = null private lateinit var pictureFile: File private lateinit var picturesPath: File - private lateinit var barcode: String + private var barcode: String = "" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -58,6 +59,7 @@ class AddAbstractProductActivity : AppCompatActivity() { takePictureButton = findViewById(R.id.takePictureButton) scanButton = findViewById(R.id.scan_button) + barcodeText = findViewById(R.id.barcodeTextEdit) productNameText = findViewById(R.id.productName) netWeightText = findViewById(R.id.netWeight) @@ -79,11 +81,15 @@ class AddAbstractProductActivity : AppCompatActivity() { val productName = productNameText.text.toString() val netWeight = netWeightText.text if (abstractProduct == null && (!this::pictureFile.isInitialized || !pictureFile.exists())) { - Toast.makeText(this, "Please, make a picture of a product!", Toast.LENGTH_SHORT).show() return@setOnClickListener } + if (barcode == "") { + Toast.makeText(this, "Please, scan barcode on 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 @@ -124,6 +130,7 @@ class AddAbstractProductActivity : AppCompatActivity() { scanner.startScan() .addOnSuccessListener { barcode -> this.barcode = barcode.rawValue.toString() + barcodeText.setText(this.barcode) val requester = Requester("https://ean-online.ru", "match.php") requester.request(this, barcode.rawValue!!.toString()) var abstractProduct: AbstractProduct 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 ed89b8f..4fa5f4c 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -20,7 +20,7 @@ import org.foxarmy.barcodescannerforemployees.fragments.StorageFragment class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding - public lateinit var adapter: ViewPagerAdapter + lateinit var adapter: ViewPagerAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -50,9 +50,6 @@ class MainActivity : AppCompatActivity() { "CategoriesFragment" -> { val addCategoryIntent = Intent(this, AddCategoryActivity::class.java) val extras = Bundle() - //TODO: Implement parcellable for Category class to simplify this -// extras.putInt("categoryid", 0) -// extras.putString("categoryname", "New category") extras.putParcelable("category", Category(0, "New category")) addCategoryIntent.putExtras(extras) ContextCompat.startActivity(this, addCategoryIntent, extras) 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 77e4b79..09d45a4 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -30,6 +30,19 @@ class StorageFragment : Fragment() { return inflater.inflate(R.layout.fragment_storage, container, false) } + override fun onResume() { + super.onResume() + + updateContent() + } + + override fun onStop() { + super.onStop() + + val grv = view?.findViewById(R.id.contentGridLayout) + grv?.removeAllViews() + } + fun removeSelected() { thread { val grv = view?.findViewById(R.id.contentGridLayout) @@ -71,7 +84,7 @@ class StorageFragment : Fragment() { fun updateContent() { thread { - val grv = view?.findViewById(R.id.contentGridLayout) + val grv:GridLayout? = view?.findViewById(R.id.contentGridLayout) activity!!.runOnUiThread{ grv?.removeAllViews() } @@ -106,12 +119,13 @@ class StorageFragment : Fragment() { requireContext(), product ) + activity!!.runOnUiThread{ grv?.addView(abstractProduct) } } } - } + }.join() } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt index c48a246..f9b0e3a 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt @@ -6,6 +6,7 @@ import android.graphics.Color import android.graphics.drawable.GradientDrawable import android.os.Build import android.util.AttributeSet +import android.util.Log import android.view.LayoutInflater import android.widget.ImageView import android.widget.LinearLayout @@ -16,6 +17,7 @@ import androidx.core.content.ContextCompat import androidx.core.graphics.blue import androidx.core.graphics.green import androidx.core.graphics.red +import androidx.core.math.MathUtils.clamp import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct @@ -26,6 +28,7 @@ import java.io.File import java.time.Duration import java.time.LocalDate import java.time.format.DateTimeFormatter +import kotlin.concurrent.thread class ProductView: LinearLayout { var product: Product = Product() @@ -38,6 +41,9 @@ class ProductView: LinearLayout { private lateinit var productAmountTextView: TextView private lateinit var productCategoryView: TextView private lateinit var productLifeSpan: TextView + private var backgroundColor: Int = 0xffffff + private var strokeColor: Int = 0x000000 + private lateinit var outline: GradientDrawable constructor(context: Context, a: AttributeSet) : super(context, a) { activity = getActivity(context)!! @@ -72,11 +78,12 @@ class ProductView: LinearLayout { fun update () { val linkedAbstractProduct: AbstractProduct = DBStorageController(activity).findAbstractProductById(DBStorageController(activity).readableDatabase, product.abstractProductId)!! - val picturesDir = File(activity.filesDir, "pictures") - picturesDir.mkdirs() - val pictureFile = File(picturesDir, "${linkedAbstractProduct.imageHash}.png") + val thumbnailsDir = File(activity.cacheDir, "thumbnails") + thumbnailsDir.mkdirs() + val pictureFile = File(thumbnailsDir, "${linkedAbstractProduct.imageHash}.webp") val imageUri = getImageUri(activity, pictureFile) productImageView.setImageURI(imageUri) + productImageView.rotation = 90f productNameTextView.text = linkedAbstractProduct.name productNetWeightTextView.text = linkedAbstractProduct.netWeight.toString() @@ -84,6 +91,7 @@ class ProductView: LinearLayout { productCategoryView.text = DBStorageController(activity).getCategoryNameById(DBStorageController(activity).readableDatabase, linkedAbstractProduct.category) productLifeSpan.text = "${product.dateOfProduction}-${product.dateOfExpiry}" + updateStroke() } @@ -93,10 +101,15 @@ class ProductView: LinearLayout { this.background = ContextCompat.getDrawable(context, R.drawable.outline_selected) } else { if (product.id != 0) { - val color = evaluateColor() - val outline = GradientDrawable() - this.background = outline - (this.background.current as GradientDrawable).setStroke(3, color) + thread { + this.outline = GradientDrawable() + backgroundColor = evaluateColor() + strokeColor = darkenColor(backgroundColor, 0.25) // (backgroundColor and 0xfefefe ) shr 1 + this.outline.setColor(backgroundColor) + this.outline.setStroke(4, strokeColor) + this.outline.alpha = 84 + this.background = outline + } } else { this.background = ContextCompat.getDrawable(context, R.drawable.outline) } @@ -125,21 +138,37 @@ class ProductView: LinearLayout { val gradientPosition = 1 - if (percentage > 0.5) (percentage - 0.5) * 2 else percentage * 2 - val red = (startColor.red + gradientPosition * (endColor.red - startColor.red )).toInt() - val green = (startColor.green + gradientPosition * (endColor.green - startColor.green)).toInt() - val blue = (startColor.blue + gradientPosition * (endColor.blue - startColor.blue)).toInt() + val red = clamp((startColor.red + gradientPosition * (endColor.red - startColor.red )).toInt(), 0, 255) + val green = clamp((startColor.green + gradientPosition * (endColor.green - startColor.green)).toInt(), 0, 255) + val blue = clamp((startColor.blue + gradientPosition * (endColor.blue - startColor.blue)).toInt(), 0, 255) - var redHex = java.lang.Integer.toHexString(red) + var redHex = Integer.toHexString(red) if (redHex.length == 1) redHex = "0$redHex" - var greenHex = java.lang.Integer.toHexString(green) + var greenHex = Integer.toHexString(green) if (greenHex.length == 1) greenHex = "0$greenHex" - var blueHex = java.lang.Integer.toHexString(blue) + var blueHex = Integer.toHexString(blue) if (blueHex.length == 1 ) blueHex = "0$blueHex" val colorString = "#$redHex$greenHex$blueHex" + Log.d("QWERTYUIOP", "$red:$green:$blue") + Log.d("QWERTYUIOP", "$redHex-$greenHex-$blueHex") + Log.d("QWERTYUIOP", colorString) return Color.parseColor(colorString) } + + @RequiresApi(Build.VERSION_CODES.O) + fun darkenColor(color: Int, darkPercent: Double) : Int { + val c = Color.valueOf(color) + + val red = c.red() * (1 - darkPercent) + val green = c.green() * (1 - darkPercent) + val blue = c.blue() * (1 - darkPercent) + Log.d("QWERTYUIOP", "....A:${c.red()}, ${c.green()}, ${c.blue()}") + Log.d("QWERTYUIOP", "....B:${red.toFloat()}, ${green.toFloat()}, ${blue.toFloat()}") + + return Color.rgb(red.toFloat(), green.toFloat(), blue.toFloat()) + } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_add_abstract_product.xml b/app/src/main/res/layout/fragment_add_abstract_product.xml index 7223239..fe3eae5 100644 --- a/app/src/main/res/layout/fragment_add_abstract_product.xml +++ b/app/src/main/res/layout/fragment_add_abstract_product.xml @@ -38,7 +38,7 @@ app:layout_constraintBottom_toTopOf="@+id/netWeight" app:layout_constraintHorizontal_bias="0.5" android:visibility="visible" android:hint="@string/product_name_label" android:textColorHint="#737373" - app:layout_constraintTop_toBottomOf="@+id/imageView"/> + android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/barcodeTextEdit"/> + + android:layout_marginTop="18dp"/>