Coder Social home page Coder Social logo

Comments (2)

felangel avatar felangel commented on June 1, 2024

Hi @simphotonics πŸ‘‹
Thanks for opening an issue!

Can you elaborate on why you’re creating a separate rewind event when you can just call undo on the bloc directly from the widget tree?

from bloc.

simphotonics avatar simphotonics commented on June 1, 2024

Hi @felangel. Thanks for taking the time to have a look.

In the concrete example I am working on the events are AwardPointTeam1, AwardPointTeam2, and ReplayLastPoint:

part of 'score_bloc.dart';

@immutable
sealed class ScoreEvent extends ReplayEvent {
  const ScoreEvent();
  @override
  String toString() => '$runtimeType';
}

@immutable
sealed class AwardPoint extends ScoreEvent {
  const AwardPoint();
}

@immutable
class AwardPointTeam1 extends AwardPoint {
  const AwardPointTeam1();
}

@immutable
class AwardPointTeam2 extends ScoreEvent {
  const AwardPointTeam2();
}

@immutable
class ReplayLastPoint extends ScoreEvent {
  const ReplayLastPoint();
}

and the bloc state represents a tennis score:

part of 'score_bloc.dart';

@immutable
final class ScoreState extends Equatable {
  const ScoreState({
    required this.server,
    this.points = const (0, 0),
    this.pointsWon = const (0, 0),
    this.games = const [(0, 0)],
    this.sets = const (0, 0),
    this.winner,
  });

  final (int, int) points;
  final (int, int) pointsWon;
  final List<(int, int)> games;
  final (int, int) sets;
  final TeamSelector server;
  final TeamSelector? winner;

  bool get hasWinner => winner == null ? false : true;

  @override
  List<Object?> get props => [points, pointsWon, games, sets, winner];
}

During a ReplayLastPoint event some logic is required to change the scoring method if necessary.

For example at games: 6:0, 6:7, 0:0, and points 0:0 the scoring method is that of a standard tennis game in the third set.
After the event ReplayLastPoint at score: 6:0, 6:6, and tiebreak points e.g. 8:9, the scoring method has to be changed to that of a set tiebreaker. I placed this logic into onTransition since it has access to the current and the next state.

@override
  void onTransition(covariant Transition<ReplayEvent, ScoreState> transition) {
    super.onTransition(transition);

    if (transition.event is AwardPoint) {
      return;
    }

    // Set the correct set configuration after an undo.
    if (transition.nextState.sets != decidingSetAtScore &&
        transition.currentState.sets == decidingSetAtScore) {
      _setConfig = matchConfig.setConfig.standard;
    }
    // Set the correct game configuration after an undo.
    _gameConfig =
        (transition.nextState.games.last == _setConfig.tiebreakAtScore)
            ? _setConfig.gameConfig.tiebreak
            : _setConfig.gameConfig.standard;
  }

from bloc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.