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 java.util.function.BiConsumer;
import dev.ninjdai.balscanner.books.Book;
public class Util { 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 @NonNull
public static EditText getEditText(Context context, int type) { public static EditText getEditText(Context context, int type) {
final EditText input = new EditText(context); final EditText input = new EditText(context);

View File

@ -10,6 +10,7 @@ import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.ActivityResultLauncher;
@ -80,6 +81,12 @@ public class ScannerFragment extends Fragment {
return ownerBooks; return ownerBooks;
})); }));
updateItems(); 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(); }).show();
input.requestFocus(); input.requestFocus();
}); });
@ -95,9 +102,12 @@ public class ScannerFragment extends Fragment {
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new ScannerItemsAdapter(getContext(), MainActivity.books.get(MainActivity.current_owner)); 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) -> { 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.removeItem(position);
adapter.notifyItemRemoved(position); adapter.notifyItemRemoved(position);
return true; return true;
@ -109,10 +119,19 @@ public class ScannerFragment extends Fragment {
FragmentActivity activity = getActivity(); FragmentActivity activity = getActivity();
if (activity==null) return; if (activity==null) return;
if(result.getContents() == null) { if(result.getContents() == null) {
Toast.makeText(activity, "Cancelled", Toast.LENGTH_LONG).show(); Toast.makeText(activity, "Cancelled", Toast.LENGTH_SHORT).show();
} else { return;
}
String ean = result.getContents(); String ean = result.getContents();
Toast.makeText(activity, "Scanned: " + ean, Toast.LENGTH_LONG).show(); 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(() -> { Thread thread = new Thread(() -> {
String bookURL="https://openlibrary.org/isbn/"+ean+".json"; String bookURL="https://openlibrary.org/isbn/"+ean+".json";
JSONObject res = getJSONFromUrl(bookURL); JSONObject res = getJSONFromUrl(bookURL);
@ -147,7 +166,6 @@ public class ScannerFragment extends Fragment {
} }
}); });
thread.start(); thread.start();
}
}); });
Button scannerButton = view.findViewById(R.id.scanner_button); Button scannerButton = view.findViewById(R.id.scanner_button);