recoverSession method

Future<void> recoverSession()

Implementation

Future<void> recoverSession() async {
  if (_recovered) {
    return;
  }
  if (widget.studentID == -1) {
    _recovered = true;

    return;
  }
  await Connection().students().then(
    (ret) async {
      for (final i in ret) {
        if (i["id"] == widget.studentID && i["session"] == widget.sessionID) {
          await Connection()
              .getResultsByStudentID(widget.studentID)
              .then((value) => value as List)
              .then(
            (List value) async {
              for (final element in value) {
                final String command =
                    await Connection().getCommandsByAlgorithmID(
                  element["algorithmID"]!,
                );
                final CATInterpreter interpreter = CATInterpreter.fromSchemes(
                  SchemasReader().schemes,
                  Shape.cross,
                );
                final Pair<Results, CatError> results = interpreter
                    .validateOnScheme(command, element["schemaID"]!);

                _allResults[element["schemaID"]!]!
                  ..time = element["time"]!
                  ..result = results.first.getStates.last
                  ..score = interpreter.getResults.completed
                      ? catScore(
                          commands: splitCommands(command),
                          visible: element["visualFeedback"]!,
                          interface: element["artefactDimension"]! - 1,
                        )
                      : 0
                  ..done = true
                  ..correct = results.first.completed
                  ..state = element["complete"]!;
              }
            },
          ).whenComplete(
            () {
              final Map<int, ResultsRecord> res = _allResults.filter(
                (MapEntry<int, ResultsRecord> entry) => !entry.value.done,
              );
              if (res.isNotEmpty) {
                final int nextIndex = res.keys.sorted().first;
                context.read<ReferenceNotifier>().toLocation(nextIndex);

                return;
              }
              Connection()
                  .checkIfSurwayComplete(widget.sessionID, widget.studentID)
                  .then((bool value) {
                if (value) {
                  Navigator.of(context).push(
                    CupertinoPageRoute<Widget>(
                      builder: (BuildContext context) => ResultsScreen(
                        sessionID: widget.sessionID,
                        studentID: widget.studentID,
                        results: _allResults,
                      ),
                    ),
                  );
                } else {
                  Navigator.of(context).push(
                    CupertinoPageRoute<Widget>(
                      builder: (BuildContext context) => Survey(
                        results: _allResults,
                        sessionID: widget.sessionID,
                        studentID: widget.studentID,
                      ),
                    ),
                  );
                }
              });
            },
          );
        }
      }
    },
  );
  _recovered = true;
}