feat: EAN checks and book code display

This commit is contained in:
Ninjdai 2024-12-21 12:30:09 +01:00
parent 27dac61aab
commit 2de0662663
2 changed files with 63 additions and 40 deletions

View File

@ -10,7 +10,12 @@ import androidx.appcompat.app.AlertDialog;
import java.util.function.BiConsumer;
import dev.ninjdai.balscanner.books.Book;
public class Util {
public static String getBookCode(Book book) {
return String.format("%s %s %s", book.getOwner().getId(), book.getEAN().substring(9, 13), book.getPrice()==0 ? "PL" : book.getPrice() + "");
}
@NonNull
public static EditText getEditText(Context context, int type) {
final EditText input = new EditText(context);

View File

@ -10,6 +10,7 @@ import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
@ -80,6 +81,12 @@ public class ScannerFragment extends Fragment {
return ownerBooks;
}));
updateItems();
final TextView codeText = new TextView(activity);
codeText.setText(Util.getBookCode(book));
new AlertDialog.Builder(activity)
.setTitle("Code du livre")
.setView(codeText)
.setPositiveButton("Ok", (dialogInterface, i) -> dialogInterface.dismiss()).show();
}).show();
input.requestFocus();
});
@ -95,9 +102,12 @@ public class ScannerFragment extends Fragment {
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new ScannerItemsAdapter(getContext(), MainActivity.books.get(MainActivity.current_owner));
adapter.setClickListener((v, position) -> Toast.makeText(getActivity(), "Book: " + adapter.getItem(position), Toast.LENGTH_LONG).show());
adapter.setClickListener((v, position) -> {
Book b = adapter.getItem(position);
Toast.makeText(getActivity(), String.format("(%s) %s", Util.getBookCode(b), b.getTitle()), Toast.LENGTH_LONG).show();
});
adapter.setLongClickListener((v, position) -> {
Toast.makeText(getActivity(), "Removed " + adapter.getItem(position), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "Supprimé " + adapter.getItem(position), Toast.LENGTH_LONG).show();
adapter.removeItem(position);
adapter.notifyItemRemoved(position);
return true;
@ -109,45 +119,53 @@ public class ScannerFragment extends Fragment {
FragmentActivity activity = getActivity();
if (activity==null) return;
if(result.getContents() == null) {
Toast.makeText(activity, "Cancelled", Toast.LENGTH_LONG).show();
} else {
String ean = result.getContents();
Toast.makeText(activity, "Scanned: " + ean, Toast.LENGTH_LONG).show();
Thread thread = new Thread(() -> {
String bookURL="https://openlibrary.org/isbn/"+ean+".json";
JSONObject res = getJSONFromUrl(bookURL);
if (res==null) {
activity.runOnUiThread(() -> {
noBookFoundToast();
final EditText input = Util.getEditText(activity, InputType.TYPE_CLASS_TEXT);
Util.getEditTextAlertDialogBuilder(activity, "Titre", input, (dialog, which) -> {
Book book = new Book(input.getText().toString().strip(), "Inconnu", ean, MainActivity.current_owner, 0);
addBook(book);
}).show();
input.requestFocus();
});
} else {
try {
String title = res.getString("title");
String author = "Inconnu";
if (res.has("authors")) {
JSONArray authors = res.getJSONArray("authors");
if (authors.length() > 0) {
String authorId = authors.getJSONObject(0).getString("key");
JSONObject authorRes = getJSONFromUrl("https://openlibrary.org" + authorId + ".json");
if (authorRes != null) author = authorRes.getString("name");
}
}
Book book = new Book(title, author, ean, MainActivity.current_owner, 0);
addBook(book);
} catch (JSONException ex) {
Log.e("App", "Failure", ex);
}
}
});
thread.start();
Toast.makeText(activity, "Cancelled", Toast.LENGTH_SHORT).show();
return;
}
String ean = result.getContents();
Toast.makeText(activity, "Scanned: " + ean, Toast.LENGTH_LONG).show();
if (ean.length() != 13) {
Toast.makeText(activity, "Erreur de détection de l'EAN, réessayez", Toast.LENGTH_LONG).show();
return;
}
if (!ean.startsWith("97")) {
Toast.makeText(activity, "L'EAN détecté ne correspond pas à un livre, réessayez", Toast.LENGTH_LONG).show();
return;
}
Thread thread = new Thread(() -> {
String bookURL="https://openlibrary.org/isbn/"+ean+".json";
JSONObject res = getJSONFromUrl(bookURL);
if (res==null) {
activity.runOnUiThread(() -> {
noBookFoundToast();
final EditText input = Util.getEditText(activity, InputType.TYPE_CLASS_TEXT);
Util.getEditTextAlertDialogBuilder(activity, "Titre", input, (dialog, which) -> {
Book book = new Book(input.getText().toString().strip(), "Inconnu", ean, MainActivity.current_owner, 0);
addBook(book);
}).show();
input.requestFocus();
});
} else {
try {
String title = res.getString("title");
String author = "Inconnu";
if (res.has("authors")) {
JSONArray authors = res.getJSONArray("authors");
if (authors.length() > 0) {
String authorId = authors.getJSONObject(0).getString("key");
JSONObject authorRes = getJSONFromUrl("https://openlibrary.org" + authorId + ".json");
if (authorRes != null) author = authorRes.getString("name");
}
}
Book book = new Book(title, author, ean, MainActivity.current_owner, 0);
addBook(book);
} catch (JSONException ex) {
Log.e("App", "Failure", ex);
}
}
});
thread.start();
});
Button scannerButton = view.findViewById(R.id.scanner_button);