Added stroke that depends on product freshness
This commit is contained in:
parent
8ef72db4e4
commit
6c4f11bcb0
|
@ -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<ConstraintLayout>(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)
|
||||
|
||||
findViewById<ConstraintLayout>(R.id.productLayout).setOnLongClickListener {
|
||||
isProductSelected = !isProductSelected
|
||||
this.background = ContextCompat.getDrawable(context, if (isProductSelected) R.drawable.outline_selected else R.drawable.outline)
|
||||
true
|
||||
updateStroke()
|
||||
}
|
||||
|
||||
@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)
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
|
||||
<solid android:color="@android:color/transparent" />
|
||||
<stroke android:width="1dip" android:color="#28283f"/>
|
||||
<stroke android:width="1dip" android:color="#28283f" android:id="@+id/test"/>
|
||||
<padding android:bottom="1dp" android:top="1dp" android:left="1dp" android:right="1dp" />
|
||||
</shape>
|
|
@ -10,4 +10,8 @@
|
|||
<color name="light_blue_A200">#FF40C4FF</color>
|
||||
<color name="light_blue_A400">#FF00B0FF</color>
|
||||
<color name="black_overlay">#66000000</color>
|
||||
|
||||
<color name="full_freshness">#5AFF30</color>
|
||||
<color name="half_freshness">#FFF200</color>
|
||||
<color name="expired_freshness">#ff0000</color>
|
||||
</resources>
|
Loading…
Reference in New Issue