minor improvements

This commit is contained in:
leca 2024-10-31 08:31:47 +03:00
parent 2feb2c1b5e
commit 7c33fe526c
6 changed files with 47 additions and 23 deletions

View File

@ -9,6 +9,7 @@ import java.io.File
import kotlin.concurrent.thread import kotlin.concurrent.thread
class Net { class Net {
var language = "en-US"
fun requestProductFromOnlineDB(barcode: String): String { fun requestProductFromOnlineDB(barcode: String): String {
var response = "" var response = ""
@ -24,6 +25,7 @@ class Net {
.url(url) .url(url)
.post(body) .post(body)
.addHeader("referer", "https://ean-online.ru") .addHeader("referer", "https://ean-online.ru")
.addHeader("accept-language", language)
.build() .build()
response = client.newCall(request).execute().body!!.string() response = client.newCall(request).execute().body!!.string()
@ -51,22 +53,13 @@ class Net {
.url("https://$server/api/user/register") .url("https://$server/api/user/register")
.post(body) .post(body)
.addHeader("content-type", "application/x-www-form-urlencoded") .addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("accept-language", language)
.build() .build()
response = client.newCall(request).execute() response = client.newCall(request).execute()
}.join() }.join()
return when (response.code) { return response.body!!.string()
200 -> {
login(server, username, password)
}
400 -> {
"Such username exists"
}
else -> {
"Unknown error"
}
}
} }
fun login(server: String, username: String, password: String): String { fun login(server: String, username: String, password: String): String {
@ -83,6 +76,7 @@ class Net {
.url("https://$server/api/user/login") .url("https://$server/api/user/login")
.post(body) .post(body)
.addHeader("content-type", "application/x-www-form-urlencoded") .addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("accept-language", language)
.build() .build()
response = client.newCall(requestLogin).execute() response = client.newCall(requestLogin).execute()
}.join() }.join()
@ -104,7 +98,6 @@ class Net {
body.addFormDataPart("barcode", abstractProduct.barcode) body.addFormDataPart("barcode", abstractProduct.barcode)
body.addFormDataPart("name", abstractProduct.name) body.addFormDataPart("name", abstractProduct.name)
body.addFormDataPart("net_weight", abstractProduct.netWeight.toString()) body.addFormDataPart("net_weight", abstractProduct.netWeight.toString())
body.addFormDataPart("image_filename", abstractProduct.imageHash)
body.addFormDataPart("category", abstractProduct.category.toString()) body.addFormDataPart("category", abstractProduct.category.toString())
body.addFormDataPart("unit", abstractProduct.unit.toString()) body.addFormDataPart("unit", abstractProduct.unit.toString())
@ -114,6 +107,7 @@ class Net {
.url("https://$server/api/abstractproduct/create") .url("https://$server/api/abstractproduct/create")
.post(requestBody) .post(requestBody)
.addHeader("Authorization", "Bearer $token") .addHeader("Authorization", "Bearer $token")
.addHeader("accept-language", language)
.build() .build()
response = client.newCall(request).execute() response = client.newCall(request).execute()
@ -139,6 +133,7 @@ class Net {
val request = Request.Builder().url("https://$server/api/user/changeUsername") val request = Request.Builder().url("https://$server/api/user/changeUsername")
.post(requestBody) .post(requestBody)
.addHeader("Authorization", "Bearer $token") .addHeader("Authorization", "Bearer $token")
.addHeader("accept-language", language)
.build() .build()
response = client.newCall(request).execute() response = client.newCall(request).execute()
}.join() }.join()
@ -163,6 +158,7 @@ class Net {
val request = Request.Builder().url("https://$server/api/user/changePassword") val request = Request.Builder().url("https://$server/api/user/changePassword")
.post(requestBody) .post(requestBody)
.addHeader("Authorization", "Bearer $token") .addHeader("Authorization", "Bearer $token")
.addHeader("accept-language", language)
.build() .build()
response = client.newCall(request).execute() response = client.newCall(request).execute()
}.join() }.join()

View File

@ -161,6 +161,8 @@ class AddAbstractProductActivity : AppCompatActivity() {
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
) )
val language = sharedPreferences.getString("language", "en-US")
n.language = language!!
val token = sharedPreferences.getString("token", "") val token = sharedPreferences.getString("token", "")
val response = n.uploadAbstractProduct("bsfe.foxarmy.org", 1, abstractProduct, File(pictureFile.absolutePath), token!!); val response = n.uploadAbstractProduct("bsfe.foxarmy.org", 1, abstractProduct, File(pictureFile.absolutePath), token!!);

View File

@ -2,6 +2,7 @@ package org.foxarmy.barcodescannerforemployees.activities
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.EncryptedSharedPreferences
@ -20,6 +21,8 @@ class LoginActivity : AppCompatActivity() {
binding = ActivityLoginBinding.inflate(layoutInflater); binding = ActivityLoginBinding.inflate(layoutInflater);
setContentView(binding.root) setContentView(binding.root)
fillUpLanguagesSpinner()
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create( val sharedPreferences = EncryptedSharedPreferences.create(
@ -34,13 +37,15 @@ class LoginActivity : AppCompatActivity() {
val server = binding.serverTextEdit.text.toString() val server = binding.serverTextEdit.text.toString()
val username = binding.usernameTextEdit.text.toString() val username = binding.usernameTextEdit.text.toString()
val password = binding.passwordTextEdit.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() val n = Net()
n.language = language
val response = n.login(server, username, password) val response = n.login(server, username, password)
//TODO: handle it properly when server will support Accept-Language header if (response.length < 40) {
if (response == "Wrong password") { Toast.makeText(this, response, Toast.LENGTH_SHORT).show()
Toast.makeText(this, getString(R.string.wrong_password), Toast.LENGTH_SHORT).show()
} else { } else {
sharedPreferences.edit().putString("token", response).apply() sharedPreferences.edit().putString("token", response).apply()
val intent = Intent(this, MainActivity::class.java) val intent = Intent(this, MainActivity::class.java)
@ -53,13 +58,16 @@ class LoginActivity : AppCompatActivity() {
val server = binding.serverTextEdit.text.toString() val server = binding.serverTextEdit.text.toString()
val username = binding.usernameTextEdit.text.toString() val username = binding.usernameTextEdit.text.toString()
val password = binding.passwordTextEdit.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() val n = Net()
n.language = language
val response = n.registerAccount(server, username, password); val response = n.registerAccount(server, username, password);
//TODO: handle it properly when server will support Accept-Language header
if (response == "Such username exists") { if (response.length < 40) {
Toast.makeText(this, getString(R.string.username_already_exists), Toast.LENGTH_SHORT).show(); Toast.makeText(this, response, Toast.LENGTH_SHORT).show();
} else { } else {
sharedPreferences.edit().putString("token", response).apply() sharedPreferences.edit().putString("token", response).apply()
sharedPreferences.edit().putString("server", server).apply() sharedPreferences.edit().putString("server", server).apply()
@ -67,10 +75,14 @@ class LoginActivity : AppCompatActivity() {
startActivity(intent) startActivity(intent)
finish() 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
} }
} }

View File

@ -25,7 +25,7 @@
android:layout_height="wrap_content" android:id="@+id/offlineButton" android:layout_height="wrap_content" android:id="@+id/offlineButton"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="300dp"/> android:layout_marginBottom="250dp"/>
<EditText <EditText
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="50dp" android:layout_height="50dp"
@ -42,8 +42,8 @@
android:ems="10" android:ems="10"
android:id="@+id/passwordTextEdit" android:id="@+id/passwordTextEdit"
android:hint="@string/password" app:layout_constraintStart_toStartOf="parent" android:hint="@string/password" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="@+id/registerButton" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="24dp"/> app:layout_constraintBottom_toTopOf="@+id/languageSpinner" android:layout_marginBottom="24dp"/>
<EditText <EditText
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -53,4 +53,10 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/usernameTextEdit" android:layout_marginBottom="16dp" app:layout_constraintBottom_toTopOf="@+id/usernameTextEdit" android:layout_marginBottom="16dp"
android:hint="@string/server" android:text="bsfe.foxarmy.org"/> android:hint="@string/server" android:text="bsfe.foxarmy.org"/>
<Spinner
android:layout_width="409dp"
android:layout_height="32dp" android:id="@+id/languageSpinner"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@+id/registerButton" android:layout_marginBottom="24dp"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -103,4 +103,8 @@
<string name="nav_close">Navigation close</string> <string name="nav_close">Navigation close</string>
<string name="new_name">New name</string> <string name="new_name">New name</string>
<string name="new_password">New password</string> <string name="new_password">New password</string>
<string-array name="languages">
<item>en-US</item>
<item>ru-RU</item>
</string-array>
</resources> </resources>

View File

@ -101,4 +101,8 @@
<string name="nav_close">Navigation close</string> <string name="nav_close">Navigation close</string>
<string name="new_name">New name</string> <string name="new_name">New name</string>
<string name="new_password">New password</string> <string name="new_password">New password</string>
<string-array name="languages">
<item>en-US</item>
<item>ru-RU</item>
</string-array>
</resources> </resources>