diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/LoadingDialog.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/LoadingDialog.kt new file mode 100644 index 0000000..dce4389 --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/LoadingDialog.kt @@ -0,0 +1,14 @@ +package org.foxarmy.barcodescannerforemployees + +import android.app.Dialog +import android.os.Bundle +import androidx.fragment.app.DialogFragment + +class LoadingDialog : DialogFragment() { + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + + + + return super.onCreateDialog(savedInstanceState) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt index a0f96e1..71f9550 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt @@ -1,6 +1,9 @@ package org.foxarmy.barcodescannerforemployees import android.content.Context +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import okhttp3.* import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaTypeOrNull @@ -11,19 +14,18 @@ import org.foxarmy.barcodescannerforemployees.dataclasses.Product import java.io.File import java.io.FileOutputStream import java.io.IOException -import kotlin.concurrent.thread class Net { var language = "en-US" var server = "bsfe.foxarmy.org" var token = "" - fun serverIsAvailable(context: Context): Boolean { + fun serverIsAvailable(context: Context, callback: (Boolean) -> Unit) { if (!isInternetConnectionAvailable(context)) { - return false + callback(false) } - var flag = false - thread { + CoroutineScope(Dispatchers.IO).launch { + var flag = false val client = OkHttpClient(); val request = Request.Builder() .url("https://$server/status") @@ -32,17 +34,17 @@ class Net { try { val response = client.newCall(request).execute(); - flag = response.code == 200; + flag = response.code == 200; } catch (e: Exception) { flag = false; } - }.join() - return flag + callback(flag) + } } - fun requestProductFromOnlineDB(barcode: String): String { - var response = "" - thread { + fun requestProductFromOnlineDB(barcode: String, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response val url = "https://ean-online.ru/match.php" val client = OkHttpClient() @@ -58,19 +60,15 @@ class Net { .addHeader("accept-language", language) .build() - response = client.newCall(request).execute().body!!.string() - }.join() - - return if (response == "") { - "Not found 404" - } else { - response + response = client.newCall(request).execute() + callback(response) } } - fun registerAccount(username: String, password: String): Response { - lateinit var response: Response - thread { + fun registerAccount(username: String, password: String, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response + val client = OkHttpClient() val body = FormBody.Builder() @@ -86,14 +84,13 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun login(username: String, password: String): Response { - lateinit var response: Response - thread { + fun login(username: String, password: String, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response val client = OkHttpClient() val body = FormBody.Builder() @@ -107,15 +104,19 @@ class Net { .addHeader("accept-language", language) .build() response = client.newCall(requestLogin).execute() - }.join() - - return response + callback(response) + } } - fun uploadAbstractProduct(groupId: Int, abstractProduct: AbstractProduct, imageFile: File): Response { - lateinit var response: Response + fun uploadAbstractProduct( + groupId: Int, + abstractProduct: AbstractProduct, + imageFile: File, + callback: (Response) -> Unit, + ) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val body = MultipartBody.Builder() @@ -138,16 +139,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun changeUsername(newUsername: String): Response { + fun changeUsername(newUsername: String, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - lateinit var response: Response - - thread { val client = OkHttpClient() val body = FormBody.Builder() @@ -162,16 +161,14 @@ class Net { .addHeader("accept-language", language) .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun changePassword(newPassword: String): Response { + fun changePassword(newPassword: String, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - lateinit var response: Response - - thread { val client = OkHttpClient() val body = FormBody.Builder() @@ -186,15 +183,12 @@ class Net { .addHeader("accept-language", language) .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun createGroup(name: String, password: String): Response { - lateinit var response: Response - - thread { + fun createGroup(name: String, password: String, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { val client = OkHttpClient() val body = FormBody.Builder() @@ -207,19 +201,19 @@ class Net { .addHeader("Authorization", "Bearer $token") .addHeader("accept-language", language) .build() - response = client.newCall(request).execute() - }.join() + val response = client.newCall(request).execute() + changeGroupPassword(name, password, { responseFromPassword -> + callback(response) + }) - changeGroupPassword(name, password) - - return response + } } - fun joinGroup(id: Int, password: String): Response { - lateinit var response: Response + fun joinGroup(id: Int, password: String, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val body = FormBody.Builder() @@ -233,41 +227,38 @@ class Net { .addHeader("accept-language", language) .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun changeGroupPassword(name: String, password: String): Response { - lateinit var response: Response + fun changeGroupPassword(name: String, password: String, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { + getGroupId(name, { groupId -> + val client = OkHttpClient() - val groupId = getGroupId(name); + val body = FormBody.Builder() + body.add("password", password) - val client = OkHttpClient() + val requestBody = body.build() - val body = FormBody.Builder() - body.add("password", password) - - val requestBody = body.build() - - val request = Request.Builder() - .url("https://$server/api/group/password/$groupId") - .post(requestBody) - .addHeader("Authorization", "Bearer $token") - .addHeader("accept-language", language) - .build() - response = client.newCall(request).execute() - }.join() - - return response + val request = Request.Builder() + .url("https://$server/api/group/password/$groupId") + .post(requestBody) + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + response = client.newCall(request).execute() + callback(response) + }); + } } - fun getGroupId(name: String): String { - lateinit var response: Response + fun getGroupId(name: String, callback: (String) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -277,17 +268,14 @@ class Net { .addHeader("accept-language", language) .build() response = client.newCall(request).execute() - }.join() - - val responseText = response.body!!.string() - - return responseText + callback(response.body!!.string()) + } } - fun getGroupName(id: Int): String { - lateinit var response: Response + fun getGroupName(id: Int, callback: (String) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -297,17 +285,14 @@ class Net { .addHeader("accept-language", language) .build() response = client.newCall(request).execute() - }.join() - - val responseText = response.body!!.string() - - return responseText + callback(response.body!!.string()) + } } - fun getUsersInGroup(groupId: Int): Response { - lateinit var response: Response + fun getUsersInGroup(groupId: Int, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -318,15 +303,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun getMyGroups(): Response { - lateinit var response: Response + fun getMyGroups(callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -337,15 +321,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun getUsernameById(userId: Int): Response { - lateinit var response: Response + fun getUsernameById(userId: Int, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -356,15 +339,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun getGroupAdminId(groupId: Int): Response { - lateinit var response: Response + fun getGroupAdminId(groupId: Int, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -375,15 +357,19 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun updateAbstractProduct(groupId: Int, abstractProduct: AbstractProduct, imageFile: File): Response { - lateinit var response: Response + fun updateAbstractProduct( + groupId: Int, + abstractProduct: AbstractProduct, + imageFile: File, + callback: (Response) -> Unit, + ) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val body = MultipartBody.Builder() @@ -406,15 +392,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun uploadCategory(groupId: Int, category: Category): Response { - lateinit var response: Response + fun uploadCategory(groupId: Int, category: Category, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val body = FormBody.Builder() @@ -431,15 +416,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun updateCategory(groupId: Int, category: Category): Response { - lateinit var response: Response + fun updateCategory(groupId: Int, category: Category, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val body = FormBody.Builder() @@ -456,15 +440,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun uploadProduct(groupId: Int, product: Product): Response { - lateinit var response: Response + fun uploadProduct(groupId: Int, product: Product, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val body = FormBody.Builder() @@ -484,15 +467,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun updateProduct(groupId: Int, product: Product): Response { - lateinit var response: Response + fun updateProduct(groupId: Int, product: Product, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val body = FormBody.Builder() @@ -512,15 +494,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun synchronize(groupId: Int): Response { - lateinit var response: Response + fun synchronize(groupId: Int, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -531,51 +512,12 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(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 { + fun downloadImage(url: String, file: File, callback: () -> Unit) { + CoroutineScope(Dispatchers.IO).launch { val client = OkHttpClient() val request = Request.Builder() .url(url) @@ -583,24 +525,24 @@ class Net { .addHeader("accept-language", language) .build() - client.newCall(request).execute().use { response -> - if (!response.isSuccessful) throw IOException("Unexpected code $response") + val response = client.newCall(request).execute() + if (!response.isSuccessful) throw IOException("Unexpected code $response") - val fos = FileOutputStream(file) + val fos = FileOutputStream(file) - response.body?.byteStream()?.use { inputStream -> - fos.use { - inputStream.copyTo(fos) - } + response.body?.byteStream()?.use { inputStream -> + fos.use { + inputStream.copyTo(fos) } } - }.join() + callback() + } } - fun deleteCategory(groupId: Int, localId: Int): Response { - lateinit var response: Response + fun deleteCategory(groupId: Int, localId: Int, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -611,15 +553,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun deleteAbstractProduct(groupId: Int, localId: Int): Response { - lateinit var response: Response + fun deleteAbstractProduct(groupId: Int, localId: Int, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -630,15 +571,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun deleteProduct(groupId: Int, localId: Int): Response { - lateinit var response: Response + fun deleteProduct(groupId: Int, localId: Int, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -649,15 +589,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun renameGroup(groupId: Int, newName: String): Response { - lateinit var response: Response + fun renameGroup(groupId: Int, newName: String, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val body = FormBody.Builder() @@ -672,15 +611,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun transfer_ownership(groupId: Int, userId: Int): Response { - lateinit var response: Response + fun transfer_ownership(groupId: Int, userId: Int, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val body = FormBody.Builder() @@ -695,15 +633,14 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } - fun leaveGroup(groupId: Int): Response { - lateinit var response: Response + fun leaveGroup(groupId: Int, callback: (Response) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + lateinit var response: Response - thread { val client = OkHttpClient() val request = Request.Builder() @@ -714,8 +651,7 @@ class Net { .build() response = client.newCall(request).execute() - }.join() - - return response + callback(response) + } } } \ 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 8de2d95..cfabfe5 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt @@ -117,29 +117,17 @@ fun calculateProductFreshness(dateOfProduction: Long, dateOfExpiry: Long): Doubl fun getUnitNameById(context: Context, id: Int): String { return when (id) { - 0 -> { - context.getString(R.string.kilogram) - } + 0 -> { context.getString(R.string.kilogram) } - 1 -> { - context.getString(R.string.gram) - } + 1 -> { context.getString(R.string.gram) } - 2 -> { - context.getString(R.string.liter) - } + 2 -> { context.getString(R.string.liter) } - 3 -> { - context.getString(R.string.milliliter) - } + 3 -> { context.getString(R.string.milliliter) } - 4 -> { - context.getString(R.string.pieces) - } + 4 -> { context.getString(R.string.pieces) } - else -> { - "" - } + else -> { "" } } } @@ -200,6 +188,7 @@ fun noInternetConnectionAvailableNotification(context: Context) { (context as Activity).runOnUiThread { AlertDialog.Builder(context) .setMessage(context.getString(R.string.no_internet_connection)) + .setCancelable(false) .setPositiveButton(R.string.quit) { _, _ -> exitProcess(0) } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/WebSocketClient.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/WebSocketClient.kt index aafd814..fd543fc 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/WebSocketClient.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/WebSocketClient.kt @@ -20,8 +20,13 @@ import java.util.concurrent.TimeUnit class WebSocketClient(private val context: Context, private val server: String) { private lateinit var webSocket: WebSocket + private lateinit var token: String + private lateinit var currentGroup: String fun connect(token: String, currentGroup: String) { + this.token = token + this.currentGroup = currentGroup + val client = OkHttpClient.Builder() .connectTimeout(5, TimeUnit.SECONDS) .readTimeout(1, TimeUnit.MINUTES) @@ -70,9 +75,10 @@ class WebSocketClient(private val context: Context, private val server: String) val pictureFile = File(picturesDir, "${data["image_filename"]}.png") val url = "https://${net.server}/api/abstractproduct/getImage/${currentGroup}/${data["local_id"]}" - net.downloadImage(url, pictureFile) + net.downloadImage(url, pictureFile, { + abstractProductDAO.addAbstractProduct(newAbstractProduct) + }) - abstractProductDAO.addAbstractProduct(newAbstractProduct) } "product" -> { @@ -102,9 +108,10 @@ class WebSocketClient(private val context: Context, private val server: String) val pictureFile = File(picturesDir, "${data["image_filename"]}.png") val url = "https://${net.server}/api/abstractproduct/getImage/${currentGroup}/${data["local_id"]}" - net.downloadImage(url, pictureFile) + net.downloadImage(url, pictureFile, { + abstractProductDAO.updateAbstractProduct(updatedAbstractProduct) + }) - abstractProductDAO.updateAbstractProduct(updatedAbstractProduct) } "product" -> { val updatedProduct = Product.createFromJSON(data) @@ -139,7 +146,8 @@ class WebSocketClient(private val context: Context, private val server: String) override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { Log.d("QWERTYUIOP", "Closing ws. Reason: $reason") - noInternetConnectionAvailableNotification(context) +// noInternetConnectionAvailableNotification(context) + this@WebSocketClient.connect(token, currentGroup) } override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AccountSettingsActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AccountSettingsActivity.kt index 18c0857..30ff6d8 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AccountSettingsActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AccountSettingsActivity.kt @@ -28,27 +28,39 @@ class AccountSettingsActivity : AppCompatActivity() { net.token = sharedPreferences.getString("token", "")!! binding.saveUsernameButton.setOnClickListener { - val response = net.changeUsername(binding.newUsernameTextEdit.text.toString()) - if (response.code == 200) { - Toast.makeText(this, getString(R.string.username_changed), Toast.LENGTH_LONG).show() - val intent = Intent(this, LoginActivity::class.java) - startActivity(intent) - finish() - } else { - Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show() - } + net.changeUsername(binding.newUsernameTextEdit.text.toString(), { response -> + if (response.code == 200) { + runOnUiThread { + Toast.makeText(this, getString(R.string.username_changed), Toast.LENGTH_LONG).show() + val intent = Intent(this, LoginActivity::class.java) + startActivity(intent) + finish() + } + } else { + runOnUiThread { + Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show() + } + } + }) + } binding.savePasswordButton.setOnClickListener { - val response = net.changePassword(binding.newPasswordTextEdit.text.toString()) - if (response.code == 200) { - Toast.makeText(this, getString(R.string.password_changed), Toast.LENGTH_LONG).show() - val intent = Intent(this, LoginActivity::class.java) - startActivity(intent) - finish() - } else { - Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show() - } + net.changePassword(binding.newPasswordTextEdit.text.toString(), { response -> + if (response.code == 200) { + runOnUiThread { + Toast.makeText(this, getString(R.string.password_changed), Toast.LENGTH_LONG).show() + val intent = Intent(this, LoginActivity::class.java) + startActivity(intent) + finish() + } + } else { + runOnUiThread { + Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show() + } + } + }) + } } } \ 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 4cd5b9a..41ae7a5 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt @@ -1,5 +1,6 @@ package org.foxarmy.barcodescannerforemployees.activities +import android.app.ProgressDialog import android.content.DialogInterface import android.content.Intent import android.content.SharedPreferences @@ -30,7 +31,6 @@ import java.io.File import java.io.FileOutputStream import java.nio.file.Files import java.nio.file.StandardCopyOption -import kotlin.concurrent.thread class AddAbstractProductActivity : AppCompatActivity() { private lateinit var imageView: ImageView @@ -58,11 +58,18 @@ class AddAbstractProductActivity : AppCompatActivity() { private lateinit var DAO: AbstractProductDAO private lateinit var sharedPreferences: SharedPreferences + private lateinit var loadingDialog: ProgressDialog + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.fragment_add_abstract_product) + loadingDialog = ProgressDialog(this) + loadingDialog.setMessage(getString(R.string.loading_please_wait)) + loadingDialog.setCancelable(false) + loadingDialog.setTitle(getString(R.string.loading)) + sharedPreferences = getPreferences(this) val dbHelper = DBStorageController(this, sharedPreferences.getString("currentGroup", "offline")!!) @@ -146,10 +153,12 @@ class AddAbstractProductActivity : AppCompatActivity() { Toast.makeText(this, getString(R.string.product_net_weight_request), Toast.LENGTH_SHORT).show() return@setOnClickListener } + + loadingDialog.show() + val currentGroup: Int val currentGroupString = sharedPreferences.getString("currentGroup", "offline")!! - lateinit var response: Response val net = Net() if (currentGroupString != "offline") { @@ -177,15 +186,12 @@ class AddAbstractProductActivity : AppCompatActivity() { if (action == "update") { DAO.updateAbstractProduct(abstractProduct!!) - if (currentGroup > 0) response = net.updateAbstractProduct(currentGroup, abstractProduct!!, pictureFile) + if (currentGroup > 0) net.updateAbstractProduct(currentGroup, abstractProduct!!, pictureFile, this::notifyUserAndExit) } else if (action == "new" || action == "new_from_barcode") { abstractProduct!!.id = DAO.addAbstractProduct(abstractProduct!!).toInt() - if (currentGroup > 0) response = - net.uploadAbstractProduct(currentGroup, abstractProduct!!, pictureFile) + if (currentGroup > 0) + net.uploadAbstractProduct(currentGroup, abstractProduct!!, pictureFile, this::notifyUserAndExit) } - if (currentGroup > 0) Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show() - - finish() } takePictureButton.setOnClickListener { @@ -198,10 +204,16 @@ class AddAbstractProductActivity : AppCompatActivity() { } } + fun notifyUserAndExit(response: Response) { + runOnUiThread { + Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show() + loadingDialog.dismiss() + finish() + } + } + fun performRequest(barcode: String) { barcodeText.setText(this.barcode) - val net = Net(); - val result = net.requestProductFromOnlineDB(barcode) var abstractProduct: AbstractProduct @@ -223,24 +235,25 @@ class AddAbstractProductActivity : AppCompatActivity() { }.show() } - thread { - if (result == "Not found 404") { + val net = Net(); + net.requestProductFromOnlineDB(barcode, {response -> + if (response.code == 404) { runOnUiThread { Toast.makeText(this, getString(R.string.no_product_in_online_database), Toast.LENGTH_LONG) .show() productNameText.setText("") netWeightText.setText("") } - return@thread + return@requestProductFromOnlineDB } - abstractProduct = Parser().parse(result) + abstractProduct = Parser().parse(response.body!!.string()) runOnUiThread { productNameText.text = abstractProduct.name netWeightText.text = abstractProduct.netWeight.toString() unitTypeSpinner.setSelection(abstractProduct.unit) } - } + }) } private fun fillupUnitsSpinner() { 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 fa2677e..0ec78f2 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddCategoryActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddCategoryActivity.kt @@ -1,11 +1,13 @@ package org.foxarmy.barcodescannerforemployees.activities import android.app.Activity +import android.app.ProgressDialog import android.content.SharedPreferences import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.Toast +import okhttp3.Response import org.foxarmy.barcodescannerforemployees.Net import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.database.CategoryDAO @@ -16,12 +18,18 @@ class AddCategoryActivity : Activity() { private lateinit var DAO: CategoryDAO private lateinit var sharedPreferences: SharedPreferences + private lateinit var loadingDialog: ProgressDialog override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_add_category) + loadingDialog = ProgressDialog(this) + loadingDialog.setMessage(getString(R.string.loading_please_wait)) + loadingDialog.setCancelable(false) + loadingDialog.setTitle(getString(R.string.loading)) + sharedPreferences = org.foxarmy.barcodescannerforemployees.getPreferences(this) val dbHelper = DBStorageController(this, sharedPreferences.getString("currentGroup", "offline")!!) @@ -40,25 +48,35 @@ class AddCategoryActivity : Activity() { net.server = sharedPreferences.getString("server", "")!! net.language = sharedPreferences.getString("language", "")!! - val currentGroup: Int = if (sharedPreferences.getString("currentGroup", "offline")!! == "offline") 0 else sharedPreferences.getString("currentGroup", "")!!.toInt() + val currentGroup: Int = if (sharedPreferences.getString( + "currentGroup", + "offline" + )!! == "offline" + ) 0 else sharedPreferences.getString("currentGroup", "")!!.toInt() findViewById