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
|
*.iml
|
||||||
.gradle
|
.gradle
|
||||||
/local.properties
|
/local.properties
|
||||||
|
@ -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,23 +42,48 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
@Override
|
||||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
@ -71,9 +92,7 @@ public class ScannerFragment extends Fragment {
|
|||||||
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
adapter = new ScannerItemsAdapter(getContext(), MainActivity.books.get(MainActivity.avril));
|
adapter = new ScannerItemsAdapter(getContext(), MainActivity.books.get(MainActivity.avril));
|
||||||
|
|
||||||
adapter.setClickListener((v, position) -> {
|
adapter.setClickListener((v, position) -> Toast.makeText(getActivity(), "Book: " + adapter.getItem(position), Toast.LENGTH_LONG).show());
|
||||||
Toast.makeText(getActivity(), "Book: " + adapter.getItem(position), Toast.LENGTH_LONG).show();
|
|
||||||
});
|
|
||||||
adapter.setLongClickListener((v, position) -> {
|
adapter.setLongClickListener((v, position) -> {
|
||||||
Toast.makeText(getActivity(), "Removed " + adapter.getItem(position), Toast.LENGTH_LONG).show();
|
Toast.makeText(getActivity(), "Removed " + adapter.getItem(position), Toast.LENGTH_LONG).show();
|
||||||
adapter.removeItem(position);
|
adapter.removeItem(position);
|
||||||
@ -84,49 +103,51 @@ public class ScannerFragment extends Fragment {
|
|||||||
|
|
||||||
ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
|
ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
|
||||||
result -> {
|
result -> {
|
||||||
|
FragmentActivity activity = getActivity();
|
||||||
|
if (activity==null) return;
|
||||||
if(result.getContents() == null) {
|
if(result.getContents() == null) {
|
||||||
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
|
Toast.makeText(activity, "Cancelled", Toast.LENGTH_LONG).show();
|
||||||
} else {
|
} else {
|
||||||
String ean = result.getContents();
|
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(() -> {
|
Thread thread = new Thread(() -> {
|
||||||
String bookURL="https://openlibrary.org/isbn/"+ean+".json";
|
String bookURL="https://openlibrary.org/isbn/"+ean+".json";
|
||||||
JSONObject res = getJSONFromUrl(bookURL);
|
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());
|
if (res==null) {
|
||||||
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
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.setView(input);
|
||||||
|
|
||||||
builder.setPositiveButton("Valider", (dialog, which) -> {
|
builder.setPositiveButton("Suivant", (dialog, which) -> {
|
||||||
book.setPrice(Integer.parseInt(input.getText().toString()));
|
Book book = new Book(input.getText().toString(), "Inconnu", ean, MainActivity.avril, 0);
|
||||||
MainActivity.books.computeIfPresent(MainActivity.avril, ((owner, ownerBooks) -> {
|
addBook(book);
|
||||||
ownerBooks.add(book);
|
|
||||||
return ownerBooks;
|
|
||||||
}));
|
|
||||||
updateItems();
|
|
||||||
});
|
});
|
||||||
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel());
|
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel());
|
||||||
builder.show();
|
builder.show();
|
||||||
});
|
});
|
||||||
} catch (JSONException ex) {
|
} else {
|
||||||
Log.e("App", "Failure", ex);
|
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();
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user