From 7c33fe526cb027ec4da9b7d643123054ab0c8ffd Mon Sep 17 00:00:00 2001 From: leca Date: Thu, 31 Oct 2024 08:31:47 +0300 Subject: [PATCH] minor improvements --- .../foxarmy/barcodescannerforemployees/Net.kt | 20 ++++++------- .../activities/AddAbstractProductActivity.kt | 2 ++ .../activities/LoginActivity.kt | 28 +++++++++++++------ app/src/main/res/layout/activity_login.xml | 12 ++++++-- app/src/main/res/values-ru/strings.xml | 4 +++ app/src/main/res/values/strings.xml | 4 +++ 6 files changed, 47 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt index ac1edc8..5136aa0 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt @@ -9,6 +9,7 @@ import java.io.File import kotlin.concurrent.thread class Net { + var language = "en-US" fun requestProductFromOnlineDB(barcode: String): String { var response = "" @@ -24,6 +25,7 @@ class Net { .url(url) .post(body) .addHeader("referer", "https://ean-online.ru") + .addHeader("accept-language", language) .build() response = client.newCall(request).execute().body!!.string() @@ -51,22 +53,13 @@ class Net { .url("https://$server/api/user/register") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") + .addHeader("accept-language", language) .build() response = client.newCall(request).execute() }.join() - return when (response.code) { - 200 -> { - login(server, username, password) - } - 400 -> { - "Such username exists" - } - else -> { - "Unknown error" - } - } + return response.body!!.string() } fun login(server: String, username: String, password: String): String { @@ -83,6 +76,7 @@ class Net { .url("https://$server/api/user/login") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") + .addHeader("accept-language", language) .build() response = client.newCall(requestLogin).execute() }.join() @@ -104,7 +98,6 @@ class Net { body.addFormDataPart("barcode", abstractProduct.barcode) body.addFormDataPart("name", abstractProduct.name) body.addFormDataPart("net_weight", abstractProduct.netWeight.toString()) - body.addFormDataPart("image_filename", abstractProduct.imageHash) body.addFormDataPart("category", abstractProduct.category.toString()) body.addFormDataPart("unit", abstractProduct.unit.toString()) @@ -114,6 +107,7 @@ class Net { .url("https://$server/api/abstractproduct/create") .post(requestBody) .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) .build() response = client.newCall(request).execute() @@ -139,6 +133,7 @@ class Net { val request = Request.Builder().url("https://$server/api/user/changeUsername") .post(requestBody) .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) .build() response = client.newCall(request).execute() }.join() @@ -163,6 +158,7 @@ class Net { val request = Request.Builder().url("https://$server/api/user/changePassword") .post(requestBody) .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) .build() response = client.newCall(request).execute() }.join() 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 4ae19f0..5acc7c1 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt @@ -161,6 +161,8 @@ class AddAbstractProductActivity : AppCompatActivity() { EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ) + val language = sharedPreferences.getString("language", "en-US") + n.language = language!! val token = sharedPreferences.getString("token", "") val response = n.uploadAbstractProduct("bsfe.foxarmy.org", 1, abstractProduct, File(pictureFile.absolutePath), token!!); diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt index 609da8a..f5ce131 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt @@ -2,6 +2,7 @@ package org.foxarmy.barcodescannerforemployees.activities import android.content.Intent import android.os.Bundle +import android.widget.ArrayAdapter import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.security.crypto.EncryptedSharedPreferences @@ -20,6 +21,8 @@ class LoginActivity : AppCompatActivity() { binding = ActivityLoginBinding.inflate(layoutInflater); setContentView(binding.root) + fillUpLanguagesSpinner() + val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) val sharedPreferences = EncryptedSharedPreferences.create( @@ -34,13 +37,15 @@ class LoginActivity : AppCompatActivity() { val server = binding.serverTextEdit.text.toString() val username = binding.usernameTextEdit.text.toString() val password = binding.passwordTextEdit.text.toString() + val language = resources.getStringArray(R.array.languages)[binding.languageSpinner.selectedItemPosition] + sharedPreferences.edit().putString("language", language).apply() val n = Net() + n.language = language val response = n.login(server, username, password) - //TODO: handle it properly when server will support Accept-Language header - if (response == "Wrong password") { - Toast.makeText(this, getString(R.string.wrong_password), Toast.LENGTH_SHORT).show() + if (response.length < 40) { + Toast.makeText(this, response, Toast.LENGTH_SHORT).show() } else { sharedPreferences.edit().putString("token", response).apply() val intent = Intent(this, MainActivity::class.java) @@ -53,13 +58,16 @@ class LoginActivity : AppCompatActivity() { val server = binding.serverTextEdit.text.toString() val username = binding.usernameTextEdit.text.toString() val password = binding.passwordTextEdit.text.toString() + val language = resources.getStringArray(R.array.languages)[binding.languageSpinner.selectedItemPosition] + sharedPreferences.edit().putString("language", language).apply() val n = Net() + n.language = language val response = n.registerAccount(server, username, password); - //TODO: handle it properly when server will support Accept-Language header - if (response == "Such username exists") { - Toast.makeText(this, getString(R.string.username_already_exists), Toast.LENGTH_SHORT).show(); + + if (response.length < 40) { + Toast.makeText(this, response, Toast.LENGTH_SHORT).show(); } else { sharedPreferences.edit().putString("token", response).apply() sharedPreferences.edit().putString("server", server).apply() @@ -67,10 +75,14 @@ class LoginActivity : AppCompatActivity() { startActivity(intent) finish() } - } + } - + private fun fillUpLanguagesSpinner() { + val languages = resources.getStringArray(R.array.languages) + val arrayAdapter = ArrayAdapter(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, languages) + arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item) + binding.languageSpinner.adapter = arrayAdapter } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 48f8829..b90992f 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -25,7 +25,7 @@ android:layout_height="wrap_content" android:id="@+id/offlineButton" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" - android:layout_marginBottom="300dp"/> + android:layout_marginBottom="250dp"/> + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintBottom_toTopOf="@+id/languageSpinner" android:layout_marginBottom="24dp"/> + \ No newline at end of file diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 780a430..968f097 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -103,4 +103,8 @@ Navigation close New name New password + + en-US + ru-RU + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fe69702..57e9dfc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -101,4 +101,8 @@ Navigation close New name New password + + en-US + ru-RU + \ No newline at end of file