feat: title input if book not found
This commit is contained in:
parent
e2600b2cbb
commit
22eaf7372f
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
/app/release/
|
||||
*.iml
|
||||
.gradle
|
||||
/local.properties
|
||||
|
@ -1,8 +1,5 @@
|
||||
package dev.ninjdai.balscanner.ui.scanner;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.text.InputType;
|
||||
import android.util.Log;
|
||||
@ -18,7 +15,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
@ -34,7 +31,6 @@ import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import dev.ninjdai.balscanner.MainActivity;
|
||||
import dev.ninjdai.balscanner.R;
|
||||
@ -46,23 +42,48 @@ public class ScannerFragment extends Fragment {
|
||||
private FragmentScannerBinding binding;
|
||||
private ScannerItemsAdapter adapter;
|
||||
|
||||
private ProgressDialog pd;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
ScanViewModel scanViewModel =
|
||||
new ViewModelProvider(this).get(ScanViewModel.class);
|
||||
//ScanViewModel scanViewModel = new ViewModelProvider(this).get(ScanViewModel.class);
|
||||
|
||||
binding = FragmentScannerBinding.inflate(inflater, container, false);
|
||||
View root = binding.getRoot();
|
||||
|
||||
return root;
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
public void updateItems() {
|
||||
adapter.notifyItemChanged(MainActivity.books.get(MainActivity.avril).size()-1);
|
||||
}
|
||||
|
||||
private void noBookFoundToast() {
|
||||
Toast.makeText(getActivity(), "Pas de livre correspondant trouvé", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
private void addBook(Book book) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity==null) return;
|
||||
activity.runOnUiThread(() -> {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
builder.setTitle("Prix");
|
||||
|
||||
final EditText input = new EditText(activity);
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
@ -71,9 +92,7 @@ public class ScannerFragment extends Fragment {
|
||||
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.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);
|
||||
@ -84,49 +103,51 @@ public class ScannerFragment extends Fragment {
|
||||
|
||||
ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
|
||||
result -> {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity==null) return;
|
||||
if(result.getContents() == null) {
|
||||
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(activity, "Cancelled", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
String ean = result.getContents();
|
||||
Toast.makeText(getActivity(), "Scanned: " + ean, Toast.LENGTH_LONG).show();
|
||||
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);
|
||||
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);
|
||||
getActivity().runOnUiThread(() -> {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
builder.setTitle("Prix");
|
||||
|
||||
final EditText input = new EditText(getActivity());
|
||||
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||||
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("Valider", (dialog, which) -> {
|
||||
book.setPrice(Integer.parseInt(input.getText().toString()));
|
||||
MainActivity.books.computeIfPresent(MainActivity.avril, ((owner, ownerBooks) -> {
|
||||
ownerBooks.add(book);
|
||||
return ownerBooks;
|
||||
}));
|
||||
updateItems();
|
||||
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();
|
||||
});
|
||||
} catch (JSONException ex) {
|
||||
Log.e("App", "Failure", ex);
|
||||
} 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) {
|
||||
Log.e("App", "Failure", ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
@ -143,7 +164,7 @@ public class ScannerFragment extends Fragment {
|
||||
}
|
||||
|
||||
JSONObject getJSONFromUrl(String fetchURL) {
|
||||
URLConnection urlConn = null;
|
||||
URLConnection urlConn;
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
URL url = new URL(fetchURL);
|
||||
@ -170,7 +191,7 @@ public class ScannerFragment extends Fragment {
|
||||
try {
|
||||
bufferedReader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.e("BALScanner", "getJsonFromUrl", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user