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 bdc31c1..7af5ee9 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt @@ -2,13 +2,21 @@ package org.foxarmy.barcodescannerforemployees.views import android.app.Activity import android.content.Context +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 import android.widget.TextView +import androidx.annotation.RequiresApi import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat +import androidx.core.graphics.blue +import androidx.core.graphics.green +import androidx.core.graphics.red import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct @@ -16,6 +24,9 @@ import org.foxarmy.barcodescannerforemployees.dataclasses.Product import org.foxarmy.barcodescannerforemployees.getActivity import org.foxarmy.barcodescannerforemployees.getImageUri import java.io.File +import java.time.LocalDate +import java.time.Period +import java.time.format.DateTimeFormatter class ProductView: LinearLayout { var product: Product = Product() @@ -35,6 +46,7 @@ class ProductView: LinearLayout { inflater.inflate(R.layout.product_view, this) } + @RequiresApi(Build.VERSION_CODES.O) constructor(activity: Activity, context: Context, product: Product) : super(context) { this.product = product this.activity = activity @@ -48,9 +60,15 @@ class ProductView: LinearLayout { productCategoryView = findViewById(R.id.categoryView) productLifeSpan = findViewById(R.id.dateSpan) + findViewById(R.id.productLayout).setOnLongClickListener { + updateStroke() + true + } + update() } + @RequiresApi(Build.VERSION_CODES.O) fun update () { val linkedAbstractProduct: AbstractProduct = DBStorageController(activity).findAbstractProductById(DBStorageController(activity).readableDatabase, product.abstractProductId)!! @@ -66,12 +84,66 @@ class ProductView: LinearLayout { productCategoryView.text = DBStorageController(activity).getCategoryNameById(DBStorageController(activity).readableDatabase, linkedAbstractProduct.category) productLifeSpan.text = "${product.dateOfProduction}-${product.dateOfExpiry}" - this.background = ContextCompat.getDrawable(context, if (isProductSelected) R.drawable.outline_selected else R.drawable.outline) + updateStroke() + } - findViewById(R.id.productLayout).setOnLongClickListener { - isProductSelected = !isProductSelected - this.background = ContextCompat.getDrawable(context, if (isProductSelected) R.drawable.outline_selected else R.drawable.outline) - true + @RequiresApi(Build.VERSION_CODES.O) + fun updateStroke() { + if (isProductSelected) { + this.background = ContextCompat.getDrawable(context, R.drawable.outline_selected) + } else { + this.background = ContextCompat.getDrawable(context, R.drawable.outline) + val color = evaluateColor() + (this.background.current as GradientDrawable).setStroke(3, color) } } + + @RequiresApi(Build.VERSION_CODES.O) + fun evaluateColor(): Int { + val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("d.M.yyyy") + + + val fresh = LocalDate.parse(product.dateOfProduction, dateFormatter) + val expired = LocalDate.parse(product.dateOfExpiry, dateFormatter) + + val shelfLife = Period.between(fresh, expired).days + + val today = LocalDate.parse(LocalDate.now().format(dateFormatter), dateFormatter) + Log.d("QWERTYUIOP", today.toString()) + + val daysBeforeExpiry = Period.between(today, expired).days + + val freshnessPercentage: Double = daysBeforeExpiry / shelfLife.toDouble() + Log.d("QWERTYUIOP", "$daysBeforeExpiry, $shelfLife, $freshnessPercentage") + + return calculateFreshnessGradient(freshnessPercentage) + } + + fun calculateFreshnessGradient(percentage: Double): Int { + val startColor = ContextCompat.getColor(context, if (percentage > 0.5) R.color.full_freshness else R.color.half_freshness) + val endColor = ContextCompat.getColor(context, if (percentage > 0.5) R.color.half_freshness else R.color.expired_freshness) + + val gradientPosition = 1 - if (percentage > 0.5) (percentage - 0.5) * 2 else percentage * 2 + + Log.d("QWERTYUIOP", "GP: $gradientPosition") + + + 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() + + var redHex = java.lang.Integer.toHexString(red) + if (redHex.length == 1) redHex = "0$redHex" + + var greenHex = java.lang.Integer.toHexString(green) + if (greenHex.length == 1) greenHex = "0$greenHex" + + var blueHex = java.lang.Integer.toHexString(blue) + if (blueHex.length == 1 ) blueHex = "0$blueHex" + + + val colorString = "#$redHex$greenHex$blueHex" + Log.d("QWERTYUIOP", "Color: $colorString\nRGB:$red:$green:$blue") + return Color.parseColor(colorString) + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/outline.xml b/app/src/main/res/drawable/outline.xml index 6334f0b..b00970f 100644 --- a/app/src/main/res/drawable/outline.xml +++ b/app/src/main/res/drawable/outline.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index db762c3..422d3aa 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -10,4 +10,8 @@ #FF40C4FF #FF00B0FF #66000000 + + #5AFF30 + #FFF200 + #ff0000 \ No newline at end of file