From cddd45a9f816a86ffe61611d40c34fe3c0a96543 Mon Sep 17 00:00:00 2001 From: Niko Strijbol Date: Wed, 25 May 2022 17:34:15 +0000 Subject: [PATCH] Fix issue on turning screen --- .../zeus/hydra/wpi/tap/cart/CartActivity.java | 44 ++++++++----------- .../hydra/wpi/tap/cart/CartInteraction.java | 3 +- .../hydra/wpi/tap/cart/CartViewModel.java | 23 +++++++--- .../tap/cart/ProductPickerDialogFragment.java | 29 +++++++----- .../res/layout/fragment_wpi_cart_search.xml | 20 ++++++--- 5 files changed, 69 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartActivity.java b/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartActivity.java index a75868941..5a99dd51c 100644 --- a/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartActivity.java +++ b/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartActivity.java @@ -65,7 +65,6 @@ public class CartActivity extends BaseActivity implem * The latest instance of the cart we've found. * TODO: this can probably be nicer by moving this to the view holder. */ - private Cart lastCart; private CartViewModel viewModel; // Ugly hack to disable menus while submitting carts. private Boolean lastEnabledBoolean; @@ -81,21 +80,21 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { binding.scanAdd.setOnClickListener(v -> { BarcodeScanner scanner = Manager.getScanner(); scanner.getBarcode(CartActivity.this, s -> { - if (lastCart == null) { + if (viewModel.getLastCart() == null) { // There is no cart yet. Log.w(TAG, "onCreate: cart not ready yet..."); Snackbar.make(binding.getRoot(), "Product niet gevonden.", Snackbar.LENGTH_LONG) .show(); return; } - Product foundProduct = lastCart.getProductFor(s); + Product foundProduct = viewModel.getLastCart().getProductFor(s); if (foundProduct == null) { Log.w(TAG, "onCreate: barcode niet gevonden in map " + s); Snackbar.make(binding.getRoot(), "Product niet gevonden.", Snackbar.LENGTH_LONG) .show(); return; } - Cart newCart = lastCart.addProduct(foundProduct); + Cart newCart = viewModel.getLastCart().addProduct(foundProduct); saveCart(newCart, false); }, this::onError); }); @@ -132,7 +131,7 @@ public void onGlobalLayout() { protected void onSuccess(@NonNull Cart data) { Log.i(TAG, "onSuccess: received cart, with X items: " + data.getOrders().size()); adapter.submitData(data.getOrders()); - lastCart = data; + viewModel.registerLastCart(data); updateCartSummary(data); } }); @@ -175,15 +174,15 @@ protected void onSuccess(@NonNull Cart data) { }); binding.cartPay.setOnClickListener(v -> { - if (lastCart == null) { + if (viewModel.getLastCart() == null) { Toast.makeText(CartActivity.this, R.string.error_network, Toast.LENGTH_SHORT).show(); return; } - String formattedTotal = currencyFormatter.format(lastCart.getTotalPrice()); + String formattedTotal = currencyFormatter.format(viewModel.getLastCart().getTotalPrice()); String message = getString(R.string.wpi_tap_form_confirm, formattedTotal); new MaterialAlertDialogBuilder(CartActivity.this) .setMessage(message) - .setPositiveButton(android.R.string.ok, (dialog, which) -> viewModel.startRequest(lastCart)) + .setPositiveButton(android.R.string.ok, (dialog, which) -> viewModel.startRequest(viewModel.getLastCart())) .setNegativeButton(android.R.string.cancel, null) .show(); }); @@ -223,15 +222,15 @@ private void updateCartSummary(Cart cart) { @Override protected void onStop() { super.onStop(); - if (lastCart != null) { - saveCart(lastCart, true); + if (viewModel.getLastCart() != null) { + saveCart(viewModel.getLastCart(), true); } } private void saveCart(Cart toSave, boolean stopping) { StorageCart storage = toSave.forStorage(); ExistingCartRequest.saveCartStorage(this, storage); - this.lastCart = toSave; + viewModel.registerLastCart(toSave); if (!stopping) { viewModel.requestRefresh(); } @@ -239,43 +238,38 @@ private void saveCart(Cart toSave, boolean stopping) { @Override public void increment(CartProduct product) { - if (this.lastCart != null) { - Cart newCart = this.lastCart.increment(product); + if (this.viewModel.getLastCart() != null) { + Cart newCart = this.viewModel.getLastCart().increment(product); saveCart(newCart, false); } } @Override public void decrement(CartProduct product) { - if (this.lastCart != null) { - Cart newCart = this.lastCart.decrement(product); + if (this.viewModel.getLastCart() != null) { + Cart newCart = this.viewModel.getLastCart().decrement(product); saveCart(newCart, false); } } @Override public void remove(CartProduct product) { - if (this.lastCart != null) { - Cart newCart = this.lastCart.remove(product); + if (this.viewModel.getLastCart() != null) { + Cart newCart = this.viewModel.getLastCart().remove(product); saveCart(newCart, false); } } @Override public void add(Product product) { - if (this.lastCart != null) { - Cart newCart = this.lastCart.addProduct(product); + if (this.viewModel.getLastCart() != null) { + Cart newCart = this.viewModel.getLastCart().addProduct(product); saveCart(newCart, false); } } - - @Override - public Cart getCart() { - return lastCart; - } private void clearCart(boolean stopping) { - Cart newCart = this.lastCart.clear(); + Cart newCart = this.viewModel.getLastCart().clear(); saveCart(newCart, stopping); } diff --git a/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartInteraction.java b/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartInteraction.java index 1eb08abfa..7cf0ab1b1 100644 --- a/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartInteraction.java +++ b/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartInteraction.java @@ -22,6 +22,8 @@ package be.ugent.zeus.hydra.wpi.tap.cart; +import java.util.function.Consumer; + import be.ugent.zeus.hydra.wpi.tap.product.Product; /** @@ -36,5 +38,4 @@ interface CartInteraction { void decrement(CartProduct product); void remove(CartProduct product); void add(Product product); - Cart getCart(); } diff --git a/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartViewModel.java b/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartViewModel.java index a85cc12f7..5d9942c28 100644 --- a/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartViewModel.java +++ b/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/CartViewModel.java @@ -39,13 +39,12 @@ */ public class CartViewModel extends RequestViewModel { - private final MutableLiveData networkState; - private final MutableLiveData>> requestResult; - + private final MutableLiveData networkState = new MutableLiveData<>(NetworkState.IDLE); + private final MutableLiveData>> requestResult = new MutableLiveData<>(); + private final MutableLiveData lastSeenCart = new MutableLiveData<>(); + public CartViewModel(Application application) { super(application); - networkState = new MutableLiveData<>(NetworkState.IDLE); - requestResult = new MutableLiveData<>(); } public LiveData getNetworkState() { @@ -56,6 +55,10 @@ public LiveData>> getRequestResult() { return requestResult; } + public LiveData getLastSeenCart() { + return this.lastSeenCart; + } + @NonNull @Override protected Request getRequest() { @@ -64,7 +67,7 @@ protected Request getRequest() { /** * Send the cart to the server. - * + * * @param cart The cart to save. */ public void startRequest(Cart cart) { @@ -76,4 +79,12 @@ public void startRequest(Cart cart) { requestResult.postValue(new Event<>(result)); }); } + + public void registerLastCart(Cart lastSeenCart) { + this.lastSeenCart.setValue(lastSeenCart); + } + + public Cart getLastCart() { + return this.lastSeenCart.getValue(); + } } diff --git a/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/ProductPickerDialogFragment.java b/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/ProductPickerDialogFragment.java index 996c9ee20..8351e9398 100644 --- a/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/ProductPickerDialogFragment.java +++ b/app/src/main/java/be/ugent/zeus/hydra/wpi/tap/cart/ProductPickerDialogFragment.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2022 Niko Strijbol - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -32,6 +32,7 @@ import androidx.appcompat.widget.SearchView; import androidx.core.view.ViewCompat; import androidx.fragment.app.DialogFragment; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; @@ -44,11 +45,11 @@ /** * A dialog fragment allowing the user to search for and pick a product. - * + * * @author Niko Strijbol */ public class ProductPickerDialogFragment extends DialogFragment implements Consumer { - + private CartInteraction interactor; @NonNull @@ -59,7 +60,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { @Override public void onAttach(@NonNull Context context) { - interactor = (CartInteraction) context; + interactor = (CartInteraction) context; super.onAttach(context); } @@ -72,28 +73,32 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - + // Modify view :( Rect displayRectangle = new Rect(); Window window = requireActivity().getWindow(); window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle); requireDialog().getWindow().setLayout((int) (displayRectangle.width() * 0.9f), (int) (displayRectangle.width() * 0.9f)); - - // There must be a cart in the activity. - // TODO: this is actually not the case when the activity is recreated. - // Either fix it in the activity, or handle it here. - Cart cart = interactor.getCart(); + ProductAdapter adapter = new ProductAdapter(this); - adapter.submitData(new ArrayList<>(cart.getProductIdToProduct().values())); RecyclerView recyclerView = ViewCompat.requireViewById(view, R.id.recycler_view); recyclerView.setHasFixedSize(true); recyclerView.addItemDecoration(new SpanItemSpacingDecoration(requireContext())); recyclerView.setAdapter(adapter); - + SearchView searchView = ViewCompat.requireViewById(view, R.id.search_view); searchView.setOnQueryTextListener(adapter); searchView.setOnCloseListener(adapter); searchView.setOnSearchClickListener(v -> adapter.onOpen()); + + // There must be a cart in the activity. + // TODO: this is actually not the case when the activity is recreated. + // Either fix it in the activity, or handle it here. + CartViewModel viewModel = new ViewModelProvider(requireActivity()).get(CartViewModel.class); + viewModel.getLastSeenCart().observe(this, cart -> { + adapter.submitData(new ArrayList<>(cart.getProductIdToProduct().values())); + ViewCompat.requireViewById(view, R.id.progress_bar).setVisibility(View.GONE); + }); } @Override diff --git a/app/src/main/res/layout/fragment_wpi_cart_search.xml b/app/src/main/res/layout/fragment_wpi_cart_search.xml index 70ecdcbb9..375472332 100644 --- a/app/src/main/res/layout/fragment_wpi_cart_search.xml +++ b/app/src/main/res/layout/fragment_wpi_cart_search.xml @@ -34,12 +34,20 @@ android:id="@+id/search_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> - - + android:layout_height="match_parent"> + + + + + + \ No newline at end of file