minor improvements
This commit is contained in:
parent
2feb2c1b5e
commit
7c33fe526c
|
@ -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()
|
||||
|
|
|
@ -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!!);
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
|
@ -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"/>
|
||||
<EditText
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="50dp"
|
||||
|
@ -42,8 +42,8 @@
|
|||
android:ems="10"
|
||||
android:id="@+id/passwordTextEdit"
|
||||
android:hint="@string/password" app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="@+id/registerButton"
|
||||
android:layout_marginBottom="24dp"/>
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@+id/languageSpinner" android:layout_marginBottom="24dp"/>
|
||||
<EditText
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
|
@ -53,4 +53,10 @@
|
|||
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@+id/usernameTextEdit" android:layout_marginBottom="16dp"
|
||||
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>
|
|
@ -103,4 +103,8 @@
|
|||
<string name="nav_close">Navigation close</string>
|
||||
<string name="new_name">New name</string>
|
||||
<string name="new_password">New password</string>
|
||||
<string-array name="languages">
|
||||
<item>en-US</item>
|
||||
<item>ru-RU</item>
|
||||
</string-array>
|
||||
</resources>
|
|
@ -101,4 +101,8 @@
|
|||
<string name="nav_close">Navigation close</string>
|
||||
<string name="new_name">New name</string>
|
||||
<string name="new_password">New password</string>
|
||||
<string-array name="languages">
|
||||
<item>en-US</item>
|
||||
<item>ru-RU</item>
|
||||
</string-array>
|
||||
</resources>
|
Loading…
Reference in New Issue