diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt index b1dee2d..02069a2 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt @@ -5,7 +5,11 @@ import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody.Companion.asRequestBody import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct +import org.foxarmy.barcodescannerforemployees.dataclasses.Category +import org.foxarmy.barcodescannerforemployees.dataclasses.Product import java.io.File +import java.io.FileOutputStream +import java.io.IOException import kotlin.concurrent.thread class Net { @@ -383,4 +387,190 @@ class Net { return response } + + fun uploadCategory(groupId: Int, category: Category): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val body = FormBody.Builder() + .add("localId", category.id.toString()) + .add("categoryName", category.name) + .add("groupId", groupId.toString()) + .build() + + val request = Request.Builder() + .url("https://$server/api/category/create") + .post(body) + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } + + fun updateCategory(groupId: Int, category: Category): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val body = FormBody.Builder() + .add("localId", category.id.toString()) + .add("categoryName", category.name) + .add("groupId", groupId.toString()) + .build() + + val request = Request.Builder() + .url("https://$server/api/category/update") + .post(body) + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } + + fun uploadProduct(groupId: Int, product: Product): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val body = FormBody.Builder() + .add("localId", product.id.toString()) + .add("groupId", groupId.toString()) + .add("abstract_product_id", product.abstractProductId.toString()) + .add("amount", product.amount.toString()) + .add("date_of_production", product.dateOfProduction.toString()) + .add("expiry_date", product.dateOfExpiry.toString()) + .build() + + val request = Request.Builder() + .url("https://$server/api/product/create") + .post(body) + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } + + fun updateProduct(groupId: Int, product: Product): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val body = FormBody.Builder() + .add("localId", product.id.toString()) + .add("groupId", groupId.toString()) + .add("abstract_product_id", product.abstractProductId.toString()) + .add("amount", product.amount.toString()) + .add("date_of_production", product.dateOfProduction.toString()) + .add("expiry_date", product.dateOfExpiry.toString()) + .build() + + val request = Request.Builder() + .url("https://$server/api/product/update") + .post(body) + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } + + fun synchronize(groupId: Int): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val request = Request.Builder() + .url("https://$server/api/user/synchronize/$groupId") + .get() + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } + + fun getProduct(groupId: Int, localId: Int): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val request = Request.Builder() + .url("https://$server/api/product/$groupId/$localId") + .get() + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } + + fun getAbstractProduct(groupId: Int, localId: Int): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val request = Request.Builder() + .url("https://$server/api/abstractproduct/$groupId/$localId") + .get() + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } + + fun downloadImage(url: String, file: File) { + thread { + val client = OkHttpClient() + val request = Request.Builder() + .url(url) + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) throw IOException("Unexpected code $response") + + val fos = FileOutputStream(file) + + response.body?.byteStream()?.use { inputStream -> + fos.use { + inputStream.copyTo(fos) + } + } + } + }.join() + } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt index ad4c2e8..802c965 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt @@ -12,11 +12,19 @@ import androidx.annotation.RequiresApi import androidx.core.content.FileProvider import com.google.firebase.components.BuildConfig import java.io.File +import java.io.FileInputStream import java.io.FileOutputStream -import java.net.URLEncoder import java.security.MessageDigest +import java.text.SimpleDateFormat import java.util.* +fun convertToUnixEpochTimestamp(dateString: String): Long { + val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") + format.timeZone = TimeZone.getTimeZone("UTC") + val date = format.parse(dateString) + return date!!.time / 1000 +} + fun getImageUri(activity: Activity, imageFile: File): Uri? { return FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + "." + activity.localClassName + ".provider", imageFile) } @@ -60,8 +68,6 @@ fun removeSubstringsFromString(text: String, toRemove: Array): String { return result } -fun String.utf8(): String = URLEncoder.encode(this, "UTF-8") - fun getActivity(context: Context?): Activity? { if (context == null) { return null @@ -98,4 +104,29 @@ fun getUnitNameById (context: Context, id: Int): String { fun parseArray(input: String): IntArray { return input.trim('[', ']').split(",").map { it.trim().toInt() }.toIntArray() +} + +fun calculateMd5Hash(file: File): String { + val digest = MessageDigest.getInstance("MD5") + val fis = FileInputStream(file) + val buffer = ByteArray(1024) + var bytesRead = fis.read(buffer) + while (bytesRead != -1) { + digest.update(buffer, 0, bytesRead) + bytesRead = fis.read(buffer) + } + fis.close() + return bytesToHex(digest.digest()) +} + +fun bytesToHex(bytes: ByteArray): String { + val hexString = StringBuilder() + for (byte in bytes) { + val hex = Integer.toHexString(0xff and byte.toInt()) + if (hex.length == 1) { + hexString.append('0') + } + hexString.append(hex) + } + return hexString.toString() } \ No newline at end of file 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 6030b4d..fe526e2 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt @@ -154,7 +154,7 @@ class AddAbstractProductActivity : AppCompatActivity() { net.server = sharedPreferences.getString("server", "")!! net.language = sharedPreferences.getString("language", "en-US")!! - val currentGroup = sharedPreferences.getString("currentGroup", "")!! + val currentGroup = sharedPreferences.getString("currentGroup", "offline")!! lateinit var response: Response @@ -164,7 +164,7 @@ class AddAbstractProductActivity : AppCompatActivity() { productName, netWeight.toString().toDouble(), pictureFile.nameWithoutExtension, - categorySpinner.selectedItemPosition, + categorySpinner.selectedItemPosition + 1, unitTypeSpinner.selectedItemPosition ) val pictureFile = File(File(filesDir, "pictures"), "${abstractProduct!!.imageHash}.png") @@ -269,8 +269,9 @@ class AddAbstractProductActivity : AppCompatActivity() { if (success) { //Move picture to a proper directory according to its calculated hash val tempfile = File(filesDir, "image.png") - val imageContent = tempfile.inputStream().readBytes() - val imageHash = imageContent.toString(Charsets.UTF_8).md5() + val imageHash = calculateMd5Hash(tempfile) +// val imageContent = tempfile.inputStream().readBytes() +// val imageHash = imageContent.toString(Charsets.UTF_8).md5() pictureFile = File(picturesPath, "$imageHash.png") Files.move(tempfile.toPath(), pictureFile.toPath(), StandardCopyOption.REPLACE_EXISTING) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddCategoryActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddCategoryActivity.kt index caf954d..185c69c 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddCategoryActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddCategoryActivity.kt @@ -8,6 +8,7 @@ import android.widget.EditText import android.widget.Toast import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKeys +import org.foxarmy.barcodescannerforemployees.Net import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.database.CategoryDAO import org.foxarmy.barcodescannerforemployees.database.DBStorageController @@ -31,7 +32,7 @@ class AddCategoryActivity : Activity() { EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ) - val dbHelper = DBStorageController(this, sharedPreferences.getString("currentGroup", "database")!!) + val dbHelper = DBStorageController(this, sharedPreferences.getString("currentGroup", "offline")!!) DAO = CategoryDAO(dbHelper) val extras = intent.extras @@ -41,6 +42,14 @@ class AddCategoryActivity : Activity() { categoryNameTextEdit.setText(category!!.name) + val net = Net() + + net.token = sharedPreferences.getString("token", "")!! + net.server = sharedPreferences.getString("server", "")!! + net.language = sharedPreferences.getString("language", "")!! + + val currentGroup = sharedPreferences.getString("currentGroup", "offline")!!.toInt() + findViewById