new: controller command of newStack(int length) to allow for expansion and deletion of card stack data

This commit is contained in:
JohnE 2024-11-12 20:13:16 -08:00
parent 5fb6777279
commit d63bcd5c76
3 changed files with 57 additions and 9 deletions

View File

@ -15,16 +15,36 @@ class CardSwiperController {
_eventController.add(ControllerSwipeEvent(direction));
}
// Undo the last swipe
/// Undo the last swipe
void undo() {
_eventController.add(const ControllerUndoEvent());
}
// Change the top card to a specific index.
/// Change the top card to a specific index.
void moveTo(int index) {
_eventController.add(ControllerMoveEvent(index));
}
/// We have a new list of card data for this state machine to handle
/// 1. Set the length of the card list
/// 2. Move index to 0 (restart stack to first card)
void newStack(int length) {
_eventController.add(ControllerNewStackEvent(length));
}
/// We have a new added cards to the end
/// 0. Keep internal index the same (undo will work, can go backwards)
/// 1. Set the internal length of the card list
/// 2. Check if we need to redraw, if we are at end of card list
// void appendStack(int length) {
// _eventController.add(ControllerNewStackEvent(length));
// }
/// Set the length only, user can moveTo(0), or other actions
// void modifiedStack(int length) {
// _eventController.add(ControllerNewStackEvent(length));
// }
Future<void> dispose() async {
await _eventController.close();
}

View File

@ -17,3 +17,8 @@ class ControllerMoveEvent extends ControllerEvent {
final int index;
const ControllerMoveEvent(this.index);
}
class ControllerNewStackEvent extends ControllerEvent {
final int length;
const ControllerNewStackEvent(this.length);
}

View File

@ -22,10 +22,20 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
StreamSubscription<ControllerEvent>? controllerSubscription;
late NullableCardBuilder _cardBuilder;
late int _cardsCount;
@override
void initState() {
super.initState();
debugPrint('[WTF] _CardSwiperState.initState !!!');
debugPrint('[WTF] _CardSwiperState.initState !!!');
debugPrint('[WTF] _CardSwiperState.initState !!!');
_cardBuilder = widget.cardBuilder;
_cardsCount = widget.cardsCount;
_undoableIndex.state = widget.initialIndex;
controllerSubscription =
@ -67,6 +77,11 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
@override
void dispose() {
debugPrint('[WTF] _CardSwiperState.dispose !!!');
debugPrint('[WTF] _CardSwiperState.dispose !!!');
debugPrint('[WTF] _CardSwiperState.dispose !!!');
_animationController.dispose();
controllerSubscription?.cancel();
super.dispose();
@ -104,7 +119,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
angle: _cardAnimation.angle,
child: ConstrainedBox(
constraints: constraints,
child: widget.cardBuilder(
child: _cardBuilder(
context,
_currentIndex!,
(100 * _cardAnimation.left / widget.threshold).ceil(),
@ -155,7 +170,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
scale: _cardAnimation.scale - ((1 - widget.scale) * (index - 1)),
child: ConstrainedBox(
constraints: constraints,
child: widget.cardBuilder(context, getValidIndexOffset(index)!, 0, 0),
child: _cardBuilder(context, getValidIndexOffset(index)!, 0, 0),
),
),
);
@ -166,6 +181,8 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
ControllerSwipeEvent(:final direction) => _swipe(direction),
ControllerUndoEvent() => _undo(),
ControllerMoveEvent(:final index) => _moveTo(index),
// controller
ControllerNewStackEvent(:final length) => _newCardStack(length)
};
}
@ -189,7 +206,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
}
Future<void> _handleCompleteSwipe() async {
final isLastCard = _currentIndex! == widget.cardsCount - 1;
final isLastCard = _currentIndex! == _cardsCount - 1;
final shouldCancelSwipe = await widget.onSwipe
?.call(_currentIndex!, _nextIndex, _detectedDirection) ==
false;
@ -286,14 +303,20 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
}
void _moveTo(int index) {
debugPrint('[WTF] _moveTo() index: $index currentIndex: $_currentIndex cardsCount: $_cardsCount');
if (index == _currentIndex) return;
if (index < 0 || index >= widget.cardsCount) return;
if (index < 0 || index >= _cardsCount) return;
setState(() {
_undoableIndex.state = index;
});
}
void _newCardStack(int length) {
_cardsCount = length;
_moveTo(0); // move to first card, and force redraw if necessary
}
int numberOfCardsOnScreen() {
if (widget.isLoop) {
return widget.numberOfCardsDisplayed;
@ -304,7 +327,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
return math.min(
widget.numberOfCardsDisplayed,
widget.cardsCount - _currentIndex!,
_cardsCount - _currentIndex!,
);
}
@ -314,9 +337,9 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
}
final index = _currentIndex! + offset;
if (!widget.isLoop && !index.isBetween(0, widget.cardsCount - 1)) {
if (!widget.isLoop && !index.isBetween(0, _cardsCount - 1)) {
return null;
}
return index % widget.cardsCount;
return index % _cardsCount;
}
}