feat: title input if book not found

This commit is contained in:
Ninjdai 2024-12-18 20:09:53 +01:00
parent e2600b2cbb
commit 22eaf7372f
2 changed files with 67 additions and 45 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/app/release/
*.iml *.iml
.gradle .gradle
/local.properties /local.properties

View File

@ -1,8 +1,5 @@
package dev.ninjdai.balscanner.ui.scanner; package dev.ninjdai.balscanner.ui.scanner;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.text.InputType; import android.text.InputType;
import android.util.Log; import android.util.Log;
@ -18,7 +15,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -34,7 +31,6 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList;
import dev.ninjdai.balscanner.MainActivity; import dev.ninjdai.balscanner.MainActivity;
import dev.ninjdai.balscanner.R; import dev.ninjdai.balscanner.R;
@ -46,71 +42,32 @@ public class ScannerFragment extends Fragment {
private FragmentScannerBinding binding; private FragmentScannerBinding binding;
private ScannerItemsAdapter adapter; private ScannerItemsAdapter adapter;
private ProgressDialog pd;
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
ScanViewModel scanViewModel = //ScanViewModel scanViewModel = new ViewModelProvider(this).get(ScanViewModel.class);
new ViewModelProvider(this).get(ScanViewModel.class);
binding = FragmentScannerBinding.inflate(inflater, container, false); binding = FragmentScannerBinding.inflate(inflater, container, false);
View root = binding.getRoot();
return root; return binding.getRoot();
} }
public void updateItems() { public void updateItems() {
adapter.notifyItemChanged(MainActivity.books.get(MainActivity.avril).size()-1); adapter.notifyItemChanged(MainActivity.books.get(MainActivity.avril).size()-1);
} }
@Override private void noBookFoundToast() {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { Toast.makeText(getActivity(), "Pas de livre correspondant trouvé", Toast.LENGTH_LONG).show();
super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = view.findViewById(R.id.scanner_items);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new ScannerItemsAdapter(getContext(), MainActivity.books.get(MainActivity.avril));
adapter.setClickListener((v, position) -> {
Toast.makeText(getActivity(), "Book: " + adapter.getItem(position), Toast.LENGTH_LONG).show();
});
adapter.setLongClickListener((v, position) -> {
Toast.makeText(getActivity(), "Removed " + adapter.getItem(position), Toast.LENGTH_LONG).show();
adapter.removeItem(position);
adapter.notifyItemRemoved(position);
return true;
});
recyclerView.setAdapter(adapter);
ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
} else {
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 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 {
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); private void addBook(Book book) {
getActivity().runOnUiThread(() -> { FragmentActivity activity = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); if (activity==null) return;
activity.runOnUiThread(() -> {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Prix"); builder.setTitle("Prix");
final EditText input = new EditText(getActivity()); final EditText input = new EditText(activity);
input.setInputType(InputType.TYPE_CLASS_NUMBER); input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input); builder.setView(input);
@ -125,9 +82,73 @@ public class ScannerFragment extends Fragment {
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel()); builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel());
builder.show(); builder.show();
}); });
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = view.findViewById(R.id.scanner_items);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new ScannerItemsAdapter(getContext(), MainActivity.books.get(MainActivity.avril));
adapter.setClickListener((v, position) -> Toast.makeText(getActivity(), "Book: " + adapter.getItem(position), Toast.LENGTH_LONG).show());
adapter.setLongClickListener((v, position) -> {
Toast.makeText(getActivity(), "Removed " + adapter.getItem(position), Toast.LENGTH_LONG).show();
adapter.removeItem(position);
adapter.notifyItemRemoved(position);
return true;
});
recyclerView.setAdapter(adapter);
ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
result -> {
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();
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Titre");
final EditText input = new EditText(activity);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Suivant", (dialog, which) -> {
Book book = new Book(input.getText().toString(), "Inconnu", ean, MainActivity.avril, 0);
addBook(book);
});
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel());
builder.show();
});
} 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.avril, 0);
addBook(book);
} catch (JSONException ex) { } catch (JSONException ex) {
Log.e("App", "Failure", ex); Log.e("App", "Failure", ex);
} }
}
}); });
thread.start(); thread.start();
} }
@ -143,7 +164,7 @@ public class ScannerFragment extends Fragment {
} }
JSONObject getJSONFromUrl(String fetchURL) { JSONObject getJSONFromUrl(String fetchURL) {
URLConnection urlConn = null; URLConnection urlConn;
BufferedReader bufferedReader = null; BufferedReader bufferedReader = null;
try { try {
URL url = new URL(fetchURL); URL url = new URL(fetchURL);
@ -170,7 +191,7 @@ public class ScannerFragment extends Fragment {
try { try {
bufferedReader.close(); bufferedReader.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.e("BALScanner", "getJsonFromUrl", e);
} }
} }
} }