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,27 +90,22 @@ 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(() -> {
try {
String bookURL="https://openlibrary.org/isbn/"+ean+".json"; String bookURL="https://openlibrary.org/isbn/"+ean+".json";
URLConnection urlConn = null; JSONObject res = getJSONFromUrl(bookURL);
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());
try { try {
String title = res.getString("title"); String title = res.getString("title");
Book book = new Book(title, "", ean, MainActivity.avril, 0); JSONArray authors = res.getJSONArray("authors");
String author = "N/A";
if (authors.length() > 0) {
String authorId = authors.getJSONObject(0).getString("key");
try {
JSONObject authorRes = getJSONFromUrl("https://openlibrary.org" + authorId + ".json");
if (authorRes != null) author = authorRes.getString("name");
} catch (JSONException ex) {
Log.e("App", "Failure", ex);
}
}
Book book = new Book(title, author, ean, MainActivity.avril, 0);
getActivity().runOnUiThread(() -> { getActivity().runOnUiThread(() -> {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Prix"); builder.setTitle("Prix");
@ -132,26 +128,6 @@ public class ScannerFragment extends Fragment {
} catch (JSONException ex) { } catch (JSONException ex) {
Log.e("App", "Failure", 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());
}
}); });
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