New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More on Time Travel (undo/redo) #38
Comments
It is possible! Example:// imagine this function being called from a "Delete" button
void deleteItem() {
// delete code logic here ...
model.update(list: updatedList);
}
// imagine this function being called from an "Undo" button.
void undo() {
this.backward(); // very simple. or just "backward();" without "this."
}
// can even redo the undo.
void undo() {
this.forward();
} |
@DonnC I want to know how you are referencing |
Yes its from |
@DonnC I was able to reproduce undo/redo not working and I know the reason why. But before offering a possible solution. I want to know more about your
|
void addNewItem() {
var list = List<String>.from(model.list); // proper way to manipulate a list.
// var list = model.list; // wrong way to manipulate a list. undo/redo doesn't work properly.
// add item logic here ...
model.update(list: list);
}
void remove(int index) {
var list = List<String>.from(model.list); // proper way to manipulate a list.
// var list = model.list; // wrong way to manipulate a list. undo/redo doesn't work properly.
list.removeAt(index);
model.update(list: list);
} This is how I was able to produce the error. Please read the comments in the code. |
Yes the delete function was working perfectly fine and final List<Cart> cart; The whole import 'package:momentum/momentum.dart';
import 'package:restaurant_app/models/index.dart';
import 'index.dart';
class CartController extends MomentumController<CartModel> {
@override
CartModel init() {
return CartModel(
this,
cart: List<Cart>.empty(growable: true),
);
}
void addToCart(Cart cart) {
final _cart = model.cart;
_cart.add(cart);
model.update(cart: _cart);
}
double get totalPrice {
double _totalPrice = 0.00;
final _productsInCart = model.cart;
for (var cartProduct in _productsInCart) {
_totalPrice += cartProduct.product.price * cartProduct.quantity;
}
return _totalPrice;
}
void deleteCartProduct(Cart cart) {
final _cart = model.cart;
_cart.remove(cart);
model.update(cart: _cart);
}
void undoDelete() {
this.backward();
}
void redoDelete() {
this.forward();
}
} From your suggestions, i wasn't doing it properly, thanks i will try as per your solution and see if i will be able to solve it |
Just now on branch
Screenrecording_20210206_063940.mp4 |
Thank you so much, this is exactly what im looking for. Will check the example code and refactor my cart controllers too 😃 |
👌 after i refactored per your suggestion it worked like a charm, but before closing, can you explain more on |
Hello @xamantra i have looked at the
listify
example and the documentation, it looks liketime-travel
works best withTextFields
is there a way it can work for other modelsImagine a scenario:
I add my
todos
which are in a list just like listify example, but i want to doundo
andredo
on mytodos
when i delete a particulartodo
in my list of todos and not on text fields like what the example listify is doingIs this possible with momentum's time travel?
The text was updated successfully, but these errors were encountered: