replacing date to timestamp

This commit is contained in:
leca 2024-10-17 14:34:29 +03:00
parent 28da5408a1
commit 4e11a7b4b9
6 changed files with 37 additions and 37 deletions

View File

@ -52,8 +52,8 @@ const val SQL_CREATE_PRODUCTS_TABLE =
"${BaseColumns._ID} INTEGER PRIMARY KEY," +
"${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID} INTEGER," +
"${ProductContract.ProductEntry.AMOUNT} INTEGER," +
"${ProductContract.ProductEntry.DATE_OF_PRODUCTION} DATE," +
"${ProductContract.ProductEntry.EXPIRY_DATE} DATE)"
"${ProductContract.ProductEntry.DATE_OF_PRODUCTION} INTEGER," +
"${ProductContract.ProductEntry.EXPIRY_DATE} INTGER)"
const val SQL_CREATE_CATEGORIES_TABLE =
"CREATE TABLE ${CategoriesContract.CategoryEntry.TABLE_NAME} (" +

View File

@ -7,6 +7,7 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.FileProvider
import androidx.core.graphics.scale
@ -15,9 +16,7 @@ import java.io.File
import java.io.FileOutputStream
import java.net.URLEncoder
import java.security.MessageDigest
import java.time.Duration
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.*
fun getImageUri(activity: Activity, imageFile: File): Uri? {
return FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + "." + activity.localClassName + ".provider", imageFile)
@ -75,15 +74,12 @@ fun getActivity(context: Context?): Activity? {
}
@RequiresApi(Build.VERSION_CODES.O)
fun calculateProductFreshness(dateOfProduction: String, dateOfExpiry: String): Double {
val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("d.M.yyyy")
fun calculateProductFreshness(dateOfProduction: Long, dateOfExpiry: Long): Double {
val fresh = LocalDate.parse(dateOfProduction, dateFormatter)
val expired = LocalDate.parse(dateOfExpiry, dateFormatter)
val productLifeSpan = dateOfExpiry - dateOfProduction;
val lifeSpanLeft: Long = dateOfExpiry - Date().time / 1000
val shelfLife = Duration.between(fresh.atStartOfDay(), expired.atStartOfDay()).toDays()
val today = LocalDate.parse(LocalDate.now().format(dateFormatter), dateFormatter)
val daysBeforeExpiry = Duration.between(today.atStartOfDay(), expired.atStartOfDay()).toDays()
Log.d("QWERTYUIOP", "ProductLifeSpan: ${productLifeSpan}, Lifespan left: ${lifeSpanLeft}, percent: ${lifeSpanLeft / productLifeSpan.toDouble()}")
return daysBeforeExpiry / shelfLife.toDouble()
return lifeSpanLeft / productLifeSpan.toDouble()
}

View File

@ -15,6 +15,7 @@ import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import org.foxarmy.barcodescannerforemployees.dataclasses.Product
import org.foxarmy.barcodescannerforemployees.views.AbstractProductView
import java.text.SimpleDateFormat
import java.util.*
class AddProductActivity : AppCompatActivity() {
@ -36,8 +37,8 @@ class AddProductActivity : AppCompatActivity() {
private lateinit var saveProductButton: Button
private var expiryDateOverShelfLife: Boolean? = null
private var dateOfProduction: String = ""
private var dateOfExpiry: String = ""
private var dateOfProduction: Long = 0
private var dateOfExpiry: Long = 0
private var barcode: String = ""
@ -105,8 +106,8 @@ class AddProductActivity : AppCompatActivity() {
val day = c.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(this, { _, y, m, d ->
dateOfProduction = "$d.${m+1}.$y"
findViewById<TextView>(R.id.dateOfProductionTextView).text = "Date of production: $dateOfProduction"
dateOfProduction = SimpleDateFormat("dd.MM.yyyy").parse("$d.${m+1}.$y").time / 1000
findViewById<TextView>(R.id.dateOfProductionTextView).text = "Date of production: $d.${m+1}.$y"
}, year, month, day)
dpd.show()
@ -119,8 +120,8 @@ class AddProductActivity : AppCompatActivity() {
val day = c.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(this, { view, y, m, d ->
dateOfExpiry = "$d.${m+1}.$y"
expiryDateTextView.text = "Expiry date: $dateOfExpiry"
dateOfExpiry = SimpleDateFormat("dd.MM.yyyy").parse("$d.${m+1}.$y").time / 1000
expiryDateTextView.text = "Expiry date: $d.${m + 1}.$y"
}, year, month, day)
dpd.show()
@ -131,7 +132,7 @@ class AddProductActivity : AppCompatActivity() {
Toast.makeText(this, "Please, choose and fill in shelf life or expiry date.", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
if (dateOfProduction == "") {
if (dateOfProduction == 0.toLong()) {
Toast.makeText(this, "Please, choose date of production.", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
@ -141,7 +142,7 @@ class AddProductActivity : AppCompatActivity() {
evaluateDateOfExpiry(shelfLifeTextEdit.text.toString().toInt())
}
} else {
if (dateOfExpiry == "") {
if (dateOfExpiry == 0.toLong()) {
Toast.makeText(this, "Please, choose date of expiry.", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
@ -161,17 +162,19 @@ class AddProductActivity : AppCompatActivity() {
}
fun evaluateDateOfExpiry(shelfLifeMonths: Int) {
if (dateOfProduction == "" ) {
if (dateOfProduction == 0.toLong() ) {
return
}
val c = Calendar.getInstance()
val dateOfProductionSplit = dateOfProduction.split(".")
c.set(Calendar.YEAR, dateOfProductionSplit[0].toInt())
c.set(Calendar.MONTH, dateOfProductionSplit[1].toInt())
c.set(Calendar.DAY_OF_MONTH, dateOfProductionSplit[2].toInt())
// val dateOfProductionSplit = dateOfProduction.split(".")
c.set(Calendar.YEAR, Date(dateOfProduction).year)
c.set(Calendar.MONTH, Date(dateOfProduction).month)
c.set(Calendar.DAY_OF_MONTH, Date(dateOfProduction).day)
c.add(Calendar.MONTH, shelfLifeMonths)
dateOfExpiry = "${c.get(Calendar.YEAR)}.${c.get(Calendar.MONTH)}.${c.get(Calendar.DAY_OF_MONTH)}"
dateOfExpiry = SimpleDateFormat("dd.MM.yyyy").parse("${c.get(Calendar.YEAR)}.${c.get(Calendar.MONTH)}.${c.get(Calendar.DAY_OF_MONTH)}").time / 1000
}
fun findAndDisplayAbstractProductByBarcode(barcode: String) {

View File

@ -10,12 +10,12 @@ class Product() : Parcelable {
var id = 0
var abstractProductId = 0
var amount = 0
var dateOfProduction = "01.01.1970"
var dateOfExpiry = "01.01.1970"
var dateOfProduction: Long = 0
var dateOfExpiry: Long = 0
var freshness: Double = 0.0
@RequiresApi(Build.VERSION_CODES.O)
constructor(id: Int, abstractProductId: Int, amount: Int, dateOfProduction: String, dateOfExpiry: String) : this() {
constructor(id: Int, abstractProductId: Int, amount: Int, dateOfProduction: Long, dateOfExpiry: Long) : this() {
this.id = id
this.abstractProductId = abstractProductId
this.amount = amount
@ -28,8 +28,8 @@ class Product() : Parcelable {
id = parcel.readInt()
abstractProductId = parcel.readInt()
amount = parcel.readInt()
dateOfProduction = parcel.readString()!!
dateOfExpiry = parcel.readString()!!
dateOfProduction = parcel.readLong()
dateOfExpiry = parcel.readLong()
freshness = parcel.readDouble()
}
@ -37,8 +37,8 @@ class Product() : Parcelable {
parcel.writeInt(id)
parcel.writeInt(abstractProductId)
parcel.writeInt(amount)
parcel.writeString(dateOfProduction)
parcel.writeString(dateOfExpiry)
parcel.writeLong(dateOfProduction)
parcel.writeLong(dateOfExpiry)
parcel.writeDouble(freshness)
}

View File

@ -121,8 +121,8 @@ class ShelfFragment : Fragment() {
getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID))
val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT))
val dateOfProduction =
getString(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION))
val dateOfExpiry = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE))
getLong(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION))
val dateOfExpiry = getLong(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE))
val product = Product(productId, abstractProductId, amount, dateOfProduction, dateOfExpiry)

View File

@ -26,6 +26,7 @@ import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import org.foxarmy.barcodescannerforemployees.dataclasses.Product
import java.io.File
import java.text.DecimalFormat
import java.util.*
import kotlin.concurrent.thread
class ProductView: LinearLayout {
@ -99,7 +100,7 @@ class ProductView: LinearLayout {
productNetWeightTextView.text = linkedAbstractProduct.netWeight.toString()
productAmountTextView.text = product.amount.toString()
productCategoryView.text = DBStorageController(activity).getCategoryNameById(DBStorageController(activity).readableDatabase, linkedAbstractProduct.category)
productLifeSpan.text = "${product.dateOfProduction}-${product.dateOfExpiry}"
productLifeSpan.text = "${Date(product.dateOfProduction*1000)}-${Date(product.dateOfExpiry*1000)}"
productFreshnessTextView.text =
if (product.freshness == Double.NEGATIVE_INFINITY || product.freshness == Double.POSITIVE_INFINITY) {
"Expired"