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," + "${BaseColumns._ID} INTEGER PRIMARY KEY," +
"${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID} INTEGER," + "${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID} INTEGER," +
"${ProductContract.ProductEntry.AMOUNT} INTEGER," + "${ProductContract.ProductEntry.AMOUNT} INTEGER," +
"${ProductContract.ProductEntry.DATE_OF_PRODUCTION} DATE," + "${ProductContract.ProductEntry.DATE_OF_PRODUCTION} INTEGER," +
"${ProductContract.ProductEntry.EXPIRY_DATE} DATE)" "${ProductContract.ProductEntry.EXPIRY_DATE} INTGER)"
const val SQL_CREATE_CATEGORIES_TABLE = const val SQL_CREATE_CATEGORIES_TABLE =
"CREATE TABLE ${CategoriesContract.CategoryEntry.TABLE_NAME} (" + "CREATE TABLE ${CategoriesContract.CategoryEntry.TABLE_NAME} (" +

View File

@ -7,6 +7,7 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.graphics.scale import androidx.core.graphics.scale
@ -15,9 +16,7 @@ import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.net.URLEncoder import java.net.URLEncoder
import java.security.MessageDigest import java.security.MessageDigest
import java.time.Duration import java.util.*
import java.time.LocalDate
import java.time.format.DateTimeFormatter
fun getImageUri(activity: Activity, imageFile: File): Uri? { fun getImageUri(activity: Activity, imageFile: File): Uri? {
return FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + "." + activity.localClassName + ".provider", imageFile) 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) @RequiresApi(Build.VERSION_CODES.O)
fun calculateProductFreshness(dateOfProduction: String, dateOfExpiry: String): Double { fun calculateProductFreshness(dateOfProduction: Long, dateOfExpiry: Long): Double {
val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("d.M.yyyy")
val fresh = LocalDate.parse(dateOfProduction, dateFormatter) val productLifeSpan = dateOfExpiry - dateOfProduction;
val expired = LocalDate.parse(dateOfExpiry, dateFormatter) val lifeSpanLeft: Long = dateOfExpiry - Date().time / 1000
val shelfLife = Duration.between(fresh.atStartOfDay(), expired.atStartOfDay()).toDays() Log.d("QWERTYUIOP", "ProductLifeSpan: ${productLifeSpan}, Lifespan left: ${lifeSpanLeft}, percent: ${lifeSpanLeft / productLifeSpan.toDouble()}")
val today = LocalDate.parse(LocalDate.now().format(dateFormatter), dateFormatter)
val daysBeforeExpiry = Duration.between(today.atStartOfDay(), expired.atStartOfDay()).toDays()
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.AbstractProduct
import org.foxarmy.barcodescannerforemployees.dataclasses.Product import org.foxarmy.barcodescannerforemployees.dataclasses.Product
import org.foxarmy.barcodescannerforemployees.views.AbstractProductView import org.foxarmy.barcodescannerforemployees.views.AbstractProductView
import java.text.SimpleDateFormat
import java.util.* import java.util.*
class AddProductActivity : AppCompatActivity() { class AddProductActivity : AppCompatActivity() {
@ -36,8 +37,8 @@ class AddProductActivity : AppCompatActivity() {
private lateinit var saveProductButton: Button private lateinit var saveProductButton: Button
private var expiryDateOverShelfLife: Boolean? = null private var expiryDateOverShelfLife: Boolean? = null
private var dateOfProduction: String = "" private var dateOfProduction: Long = 0
private var dateOfExpiry: String = "" private var dateOfExpiry: Long = 0
private var barcode: String = "" private var barcode: String = ""
@ -105,8 +106,8 @@ class AddProductActivity : AppCompatActivity() {
val day = c.get(Calendar.DAY_OF_MONTH) val day = c.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(this, { _, y, m, d -> val dpd = DatePickerDialog(this, { _, y, m, d ->
dateOfProduction = "$d.${m+1}.$y" dateOfProduction = SimpleDateFormat("dd.MM.yyyy").parse("$d.${m+1}.$y").time / 1000
findViewById<TextView>(R.id.dateOfProductionTextView).text = "Date of production: $dateOfProduction" findViewById<TextView>(R.id.dateOfProductionTextView).text = "Date of production: $d.${m+1}.$y"
}, year, month, day) }, year, month, day)
dpd.show() dpd.show()
@ -119,8 +120,8 @@ class AddProductActivity : AppCompatActivity() {
val day = c.get(Calendar.DAY_OF_MONTH) val day = c.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(this, { view, y, m, d -> val dpd = DatePickerDialog(this, { view, y, m, d ->
dateOfExpiry = "$d.${m+1}.$y" dateOfExpiry = SimpleDateFormat("dd.MM.yyyy").parse("$d.${m+1}.$y").time / 1000
expiryDateTextView.text = "Expiry date: $dateOfExpiry" expiryDateTextView.text = "Expiry date: $d.${m + 1}.$y"
}, year, month, day) }, year, month, day)
dpd.show() 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() Toast.makeText(this, "Please, choose and fill in shelf life or expiry date.", Toast.LENGTH_SHORT).show()
return@setOnClickListener return@setOnClickListener
} }
if (dateOfProduction == "") { if (dateOfProduction == 0.toLong()) {
Toast.makeText(this, "Please, choose date of production.", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Please, choose date of production.", Toast.LENGTH_SHORT).show()
return@setOnClickListener return@setOnClickListener
} }
@ -141,7 +142,7 @@ class AddProductActivity : AppCompatActivity() {
evaluateDateOfExpiry(shelfLifeTextEdit.text.toString().toInt()) evaluateDateOfExpiry(shelfLifeTextEdit.text.toString().toInt())
} }
} else { } else {
if (dateOfExpiry == "") { if (dateOfExpiry == 0.toLong()) {
Toast.makeText(this, "Please, choose date of expiry.", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Please, choose date of expiry.", Toast.LENGTH_SHORT).show()
return@setOnClickListener return@setOnClickListener
} }
@ -161,17 +162,19 @@ class AddProductActivity : AppCompatActivity() {
} }
fun evaluateDateOfExpiry(shelfLifeMonths: Int) { fun evaluateDateOfExpiry(shelfLifeMonths: Int) {
if (dateOfProduction == "" ) { if (dateOfProduction == 0.toLong() ) {
return return
} }
val c = Calendar.getInstance() val c = Calendar.getInstance()
val dateOfProductionSplit = dateOfProduction.split(".")
c.set(Calendar.YEAR, dateOfProductionSplit[0].toInt()) // val dateOfProductionSplit = dateOfProduction.split(".")
c.set(Calendar.MONTH, dateOfProductionSplit[1].toInt()) c.set(Calendar.YEAR, Date(dateOfProduction).year)
c.set(Calendar.DAY_OF_MONTH, dateOfProductionSplit[2].toInt()) c.set(Calendar.MONTH, Date(dateOfProduction).month)
c.set(Calendar.DAY_OF_MONTH, Date(dateOfProduction).day)
c.add(Calendar.MONTH, shelfLifeMonths) 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) { fun findAndDisplayAbstractProductByBarcode(barcode: String) {

View File

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

View File

@ -121,8 +121,8 @@ class ShelfFragment : Fragment() {
getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID)) getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID))
val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT)) val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT))
val dateOfProduction = val dateOfProduction =
getString(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION)) getLong(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION))
val dateOfExpiry = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE)) val dateOfExpiry = getLong(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE))
val product = Product(productId, abstractProductId, amount, dateOfProduction, dateOfExpiry) 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 org.foxarmy.barcodescannerforemployees.dataclasses.Product
import java.io.File import java.io.File
import java.text.DecimalFormat import java.text.DecimalFormat
import java.util.*
import kotlin.concurrent.thread import kotlin.concurrent.thread
class ProductView: LinearLayout { class ProductView: LinearLayout {
@ -99,7 +100,7 @@ class ProductView: LinearLayout {
productNetWeightTextView.text = linkedAbstractProduct.netWeight.toString() productNetWeightTextView.text = linkedAbstractProduct.netWeight.toString()
productAmountTextView.text = product.amount.toString() productAmountTextView.text = product.amount.toString()
productCategoryView.text = DBStorageController(activity).getCategoryNameById(DBStorageController(activity).readableDatabase, linkedAbstractProduct.category) 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 = productFreshnessTextView.text =
if (product.freshness == Double.NEGATIVE_INFINITY || product.freshness == Double.POSITIVE_INFINITY) { if (product.freshness == Double.NEGATIVE_INFINITY || product.freshness == Double.POSITIVE_INFINITY) {
"Expired" "Expired"