Skip to content

Commit

Permalink
Fix issue on turning screen
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed May 25, 2022
1 parent d51cde1 commit cddd45a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public class CartActivity extends BaseActivity<ActivityWpiTapCartBinding> 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;
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -223,59 +222,54 @@ 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();
}
}

@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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -36,5 +38,4 @@ interface CartInteraction {
void decrement(CartProduct product);
void remove(CartProduct product);
void add(Product product);
Cart getCart();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@
*/
public class CartViewModel extends RequestViewModel<Cart> {

private final MutableLiveData<NetworkState> networkState;
private final MutableLiveData<Event<Result<OrderResult>>> requestResult;

private final MutableLiveData<NetworkState> networkState = new MutableLiveData<>(NetworkState.IDLE);
private final MutableLiveData<Event<Result<OrderResult>>> requestResult = new MutableLiveData<>();
private final MutableLiveData<Cart> lastSeenCart = new MutableLiveData<>();

public CartViewModel(Application application) {
super(application);
networkState = new MutableLiveData<>(NetworkState.IDLE);
requestResult = new MutableLiveData<>();
}

public LiveData<NetworkState> getNetworkState() {
Expand All @@ -56,6 +55,10 @@ public LiveData<Event<Result<OrderResult>>> getRequestResult() {
return requestResult;
}

public LiveData<Cart> getLastSeenCart() {
return this.lastSeenCart;
}

@NonNull
@Override
protected Request<Cart> getRequest() {
Expand All @@ -64,7 +67,7 @@ protected Request<Cart> getRequest() {

/**
* Send the cart to the server.
*
*
* @param cart The cart to save.
*/
public void startRequest(Cart cart) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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<Product> {

private CartInteraction interactor;

@NonNull
Expand All @@ -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);
}

Expand All @@ -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
Expand Down
20 changes: 14 additions & 6 deletions app/src/main/res/layout/fragment_wpi_cart_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="@string/app_layout_manager_linear"
tools:listitem="@layout/item_product" />
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="@string/app_layout_manager_linear"
tools:listitem="@layout/item_product" />

<include layout="@layout/x_progress_bar" />
</FrameLayout>



</LinearLayout>

0 comments on commit cddd45a

Please sign in to comment.