From 2de06626637de463d1d1b1798dc7fee1912a9f8d Mon Sep 17 00:00:00 2001 From: Ninjdai Date: Sat, 21 Dec 2024 12:30:09 +0100 Subject: [PATCH] feat: EAN checks and book code display --- .../java/dev/ninjdai/balscanner/Util.java | 5 + .../ui/scanner/ScannerFragment.java | 98 +++++++++++-------- 2 files changed, 63 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/dev/ninjdai/balscanner/Util.java b/app/src/main/java/dev/ninjdai/balscanner/Util.java index de13792..ec6e50a 100644 --- a/app/src/main/java/dev/ninjdai/balscanner/Util.java +++ b/app/src/main/java/dev/ninjdai/balscanner/Util.java @@ -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); diff --git a/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerFragment.java b/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerFragment.java index ec19aa7..56f2411 100644 --- a/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerFragment.java +++ b/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerFragment.java @@ -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);