From e2600b2cbb00c156d9b1178922a2cb9c46facece Mon Sep 17 00:00:00 2001 From: Ninjdai Date: Wed, 18 Dec 2024 16:34:28 +0100 Subject: [PATCH] feat: price input and author fetching --- .idea/vcs.xml | 6 ++ .../ui/scanner/ScannerFragment.java | 97 +++++++------------ .../ui/scanner/ScannerItemsAdapter.java | 3 +- 3 files changed, 45 insertions(+), 61 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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 0ad537b..b376c85 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 @@ -25,6 +25,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.journeyapps.barcodescanner.ScanContract; import com.journeyapps.barcodescanner.ScanOptions; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -89,68 +90,43 @@ public class ScannerFragment extends Fragment { String ean = result.getContents(); Toast.makeText(getActivity(), "Scanned: " + ean, Toast.LENGTH_LONG).show(); Thread thread = new Thread(() -> { + String bookURL="https://openlibrary.org/isbn/"+ean+".json"; + JSONObject res = getJSONFromUrl(bookURL); try { - String bookURL="https://openlibrary.org/isbn/"+ean+".json"; - URLConnection urlConn = null; - BufferedReader bufferedReader = null; - try - { - URL url = new URL(bookURL); - urlConn = url.openConnection(); - bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); - - StringBuffer stringBuffer = new StringBuffer(); - String line; - while ((line = bufferedReader.readLine()) != null) - { - stringBuffer.append(line); - } - - JSONObject res = new JSONObject(stringBuffer.toString()); + String title = res.getString("title"); + JSONArray authors = res.getJSONArray("authors"); + String author = "N/A"; + if (authors.length() > 0) { + String authorId = authors.getJSONObject(0).getString("key"); try { - String title = res.getString("title"); - Book book = new Book(title, "", ean, MainActivity.avril, 0); - getActivity().runOnUiThread(() -> { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle("Prix"); - - final EditText input = new EditText(getActivity()); - input.setInputType(InputType.TYPE_CLASS_NUMBER); - builder.setView(input); - - builder.setPositiveButton("Valider", (dialog, which) -> { - book.setPrice(Integer.parseInt(input.getText().toString())); - MainActivity.books.computeIfPresent(MainActivity.avril, ((owner, ownerBooks) -> { - ownerBooks.add(book); - return ownerBooks; - })); - updateItems(); - }); - builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel()); - builder.show(); - }); + JSONObject authorRes = getJSONFromUrl("https://openlibrary.org" + authorId + ".json"); + if (authorRes != null) author = authorRes.getString("name"); } catch (JSONException ex) { Log.e("App", "Failure", ex); } } - catch(Exception ex) - { - Log.e("App", "OpenLibraryFetchTask", ex); - } - finally - { - if(bufferedReader != null) - { - try { - bufferedReader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - //Your code goes here - } catch (Exception e) { - Log.e("App", e.getMessage()); + Book book = new Book(title, author, ean, MainActivity.avril, 0); + getActivity().runOnUiThread(() -> { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle("Prix"); + + final EditText input = new EditText(getActivity()); + input.setInputType(InputType.TYPE_CLASS_NUMBER); + builder.setView(input); + + builder.setPositiveButton("Valider", (dialog, which) -> { + book.setPrice(Integer.parseInt(input.getText().toString())); + MainActivity.books.computeIfPresent(MainActivity.avril, ((owner, ownerBooks) -> { + ownerBooks.add(book); + return ownerBooks; + })); + updateItems(); + }); + builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel()); + builder.show(); + }); + } catch (JSONException ex) { + Log.e("App", "Failure", ex); } }); thread.start(); @@ -169,7 +145,6 @@ public class ScannerFragment extends Fragment { JSONObject getJSONFromUrl(String fetchURL) { URLConnection urlConn = null; BufferedReader bufferedReader = null; - JSONObject object = null; try { URL url = new URL(fetchURL); urlConn = url.openConnection(); @@ -181,10 +156,12 @@ public class ScannerFragment extends Fragment { stringBuffer.append(line); } - object = new JSONObject(stringBuffer.toString()); + bufferedReader.close(); + return new JSONObject(stringBuffer.toString()); } catch (Exception e) { - Log.e("App", e.getMessage()); + Log.e("BALScanner", fetchURL); + Log.e("BALScanner", "getJsonFromUrl", e); } finally { @@ -197,7 +174,7 @@ public class ScannerFragment extends Fragment { } } } - return object; + return null; } @Override diff --git a/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerItemsAdapter.java b/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerItemsAdapter.java index 6abec39..18e72a0 100644 --- a/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerItemsAdapter.java +++ b/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerItemsAdapter.java @@ -39,7 +39,8 @@ public class ScannerItemsAdapter extends RecyclerView.Adapter