update method

  1. @override
(CountdownModel, Cmd?) update(
  1. Msg msg
)
override

Updates the component state in response to a message.

Returns the updated component (often this) and an optional command.

Implementation

@override
(CountdownModel, Cmd?) update(Msg msg) {
  if (msg is KeyMsg &&
      (msg.key.type == KeyType.escape ||
          (msg.key.ctrl &&
              msg.key.runes.isNotEmpty &&
              msg.key.runes.first == 0x63))) {
    return (this, Cmd.quit());
  }

  if (msg is TickMsg) {
    if (msg.id == _startTickId) {
      return (
        this,
        Cmd.tick(interval, (time) => TickMsg(time, id: _tickId)),
      );
    }

    if (msg.id == _tickId) {
      final newRemaining = _remaining - interval;
      if (newRemaining <= Duration.zero) {
        _remaining = Duration.zero;
        return (this, Cmd.quit());
      }
      _remaining = newRemaining;
      return (
        this,
        Cmd.tick(interval, (time) => TickMsg(time, id: _tickId)),
      );
    }
  }

  return (this, null);
}