feat: price input and author fetching

This commit is contained in:
Ninjdai 2024-12-18 16:34:28 +01:00
parent ce65b1b7e4
commit e2600b2cbb
3 changed files with 45 additions and 61 deletions

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -25,6 +25,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.journeyapps.barcodescanner.ScanContract; import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions; import com.journeyapps.barcodescanner.ScanOptions;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -89,68 +90,43 @@ public class ScannerFragment extends Fragment {
String ean = result.getContents(); String ean = result.getContents();
Toast.makeText(getActivity(), "Scanned: " + ean, Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), "Scanned: " + ean, Toast.LENGTH_LONG).show();
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
String bookURL="https://openlibrary.org/isbn/"+ean+".json";
JSONObject res = getJSONFromUrl(bookURL);
try { try {
String bookURL="https://openlibrary.org/isbn/"+ean+".json"; String title = res.getString("title");
URLConnection urlConn = null; JSONArray authors = res.getJSONArray("authors");
BufferedReader bufferedReader = null; String author = "N/A";
try if (authors.length() > 0) {
{ String authorId = authors.getJSONObject(0).getString("key");
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());
try { try {
String title = res.getString("title"); JSONObject authorRes = getJSONFromUrl("https://openlibrary.org" + authorId + ".json");
Book book = new Book(title, "", ean, MainActivity.avril, 0); if (authorRes != null) author = authorRes.getString("name");
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) { } catch (JSONException ex) {
Log.e("App", "Failure", ex); Log.e("App", "Failure", ex);
} }
} }
catch(Exception ex) Book book = new Book(title, author, ean, MainActivity.avril, 0);
{ getActivity().runOnUiThread(() -> {
Log.e("App", "OpenLibraryFetchTask", ex); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
} builder.setTitle("Prix");
finally
{ final EditText input = new EditText(getActivity());
if(bufferedReader != null) input.setInputType(InputType.TYPE_CLASS_NUMBER);
{ builder.setView(input);
try {
bufferedReader.close(); builder.setPositiveButton("Valider", (dialog, which) -> {
} catch (IOException e) { book.setPrice(Integer.parseInt(input.getText().toString()));
e.printStackTrace(); MainActivity.books.computeIfPresent(MainActivity.avril, ((owner, ownerBooks) -> {
} ownerBooks.add(book);
} return ownerBooks;
} }));
//Your code goes here updateItems();
} catch (Exception e) { });
Log.e("App", e.getMessage()); builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel());
builder.show();
});
} catch (JSONException ex) {
Log.e("App", "Failure", ex);
} }
}); });
thread.start(); thread.start();
@ -169,7 +145,6 @@ public class ScannerFragment extends Fragment {
JSONObject getJSONFromUrl(String fetchURL) { JSONObject getJSONFromUrl(String fetchURL) {
URLConnection urlConn = null; URLConnection urlConn = null;
BufferedReader bufferedReader = null; BufferedReader bufferedReader = null;
JSONObject object = null;
try { try {
URL url = new URL(fetchURL); URL url = new URL(fetchURL);
urlConn = url.openConnection(); urlConn = url.openConnection();
@ -181,10 +156,12 @@ public class ScannerFragment extends Fragment {
stringBuffer.append(line); stringBuffer.append(line);
} }
object = new JSONObject(stringBuffer.toString()); bufferedReader.close();
return new JSONObject(stringBuffer.toString());
} }
catch (Exception e) { catch (Exception e) {
Log.e("App", e.getMessage()); Log.e("BALScanner", fetchURL);
Log.e("BALScanner", "getJsonFromUrl", e);
} }
finally finally
{ {
@ -197,7 +174,7 @@ public class ScannerFragment extends Fragment {
} }
} }
} }
return object; return null;
} }
@Override @Override

View File

@ -39,7 +39,8 @@ public class ScannerItemsAdapter extends RecyclerView.Adapter<ScannerItemsAdapte
@Override @Override
public void onBindViewHolder(ViewHolder holder, int position) { public void onBindViewHolder(ViewHolder holder, int position) {
Book book = mData.get(position); Book book = mData.get(position);
holder.myTextView.setText(String.format("%s (%s) - %s", book.getTitle(), book.getPrice()!=0 ? book.getPrice()+"" : "PL", book.getEAN())); holder.myTextView.setText(String.format("%s (%s) - %s - %s",
book.getTitle(), book.getPrice()!=0 ? book.getPrice()+"" : "PL", book.getAuthor(), book.getEAN()));
} }
// total number of rows // total number of rows