From e5772bcad3015d1b4a283147f10034661f239a46 Mon Sep 17 00:00:00 2001 From: leca Date: Thu, 14 Nov 2024 04:02:36 +0300 Subject: [PATCH] now handling unexpected ws close --- .../barcodescannerforemployees/Utils.kt | 40 ++++++++++--------- .../WebSocketClient.kt | 2 + .../layout/fragment_add_abstract_product.xml | 2 +- app/src/main/res/layout/fragment_shelf.xml | 2 +- app/src/main/res/values-ru/strings.xml | 8 ++-- app/src/main/res/values/strings.xml | 4 +- 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt index c292df9..a2e6cce 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt @@ -190,24 +190,26 @@ fun isInternetConnectionAvailable(context: Context): Boolean { } fun noInternetConnectionAvailableNotification(context: Context) { - AlertDialog.Builder(context) - .setMessage(context.getString(R.string.no_internet_connection)) - .setPositiveButton(R.string.quit) { _, _ -> - exitProcess(0) - } - .setNeutralButton(R.string.logout) { _, _ -> - val sharedPreferences = EncryptedSharedPreferences.create( - "sensitive", - MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), - context, - EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, - EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM - ) + (context as Activity).runOnUiThread { + AlertDialog.Builder(context) + .setMessage(context.getString(R.string.no_internet_connection)) + .setPositiveButton(R.string.quit) { _, _ -> + exitProcess(0) + } + .setNeutralButton(R.string.logout) { _, _ -> + val sharedPreferences = EncryptedSharedPreferences.create( + "sensitive", + MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), + context, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) - sharedPreferences.edit().putString("token", "").apply() - sharedPreferences.edit().putString("server", "").apply() - sharedPreferences.edit().putStringSet("groups", emptySet()).apply() - sharedPreferences.edit().putString("currentGroup", "").apply() - exitProcess(0) - }.show() + sharedPreferences.edit().putString("token", "").apply() + sharedPreferences.edit().putString("server", "").apply() + sharedPreferences.edit().putStringSet("groups", emptySet()).apply() + sharedPreferences.edit().putString("currentGroup", "").apply() + exitProcess(0) + }.show() + } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/WebSocketClient.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/WebSocketClient.kt index 8a7c42a..aafd814 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/WebSocketClient.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/WebSocketClient.kt @@ -139,10 +139,12 @@ 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) } override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { Log.d("QWERTYUIOP","Connection failed: ${t.message}") + noInternetConnectionAvailableNotification(context) } }) } diff --git a/app/src/main/res/layout/fragment_add_abstract_product.xml b/app/src/main/res/layout/fragment_add_abstract_product.xml index 81845ce..411f552 100644 --- a/app/src/main/res/layout/fragment_add_abstract_product.xml +++ b/app/src/main/res/layout/fragment_add_abstract_product.xml @@ -62,7 +62,7 @@ android:ems="10" android:id="@+id/barcodeTextEdit" app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintStart_toStartOf="parent" - android:layout_marginTop="8dp" android:hint="Barcode" android:textColorHint="#737373" + android:layout_marginTop="8dp" android:hint="@string/barcode" android:textColorHint="#737373" android:layout_marginStart="5dp"/> +> Степень сжатия изображения Текущая группа Отмена - No internet connection available. Please, connect to a network. - Ok - Log out - This feature is online only + Не могу установить соединение с сервером. Возможно, вы отключены от сети? + Ок + Выйти + Эта возможность доступна только с онлайн аккаунтом en-US ru-RU diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 73864d8..6c2df16 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -112,8 +112,8 @@ Image compression factor Current group Cancel - No internet connection available. Please, connect to a network. - Ok + Cannot reach the server. Maybe, you lost internet connection? + OK Log out This feature is online only