Line data Source code
1 : import 'package:freezed_annotation/freezed_annotation.dart';
2 : import 'package:redfire/types.dart';
3 :
4 : import '../../adventures/models/adventure_model.dart';
5 : import '../../challenges/models/challenge_model.dart';
6 : import '../../steps/models/step_model.dart';
7 : import '../../tasks/models/task_model.dart';
8 :
9 : abstract class AdventureNode {
10 1 : static String nextName(AdventureNode? node) => (node == null)
11 : ? 'adventure'
12 1 : : (node.isAdventure())
13 : ? 'challenge'
14 1 : : (node.isChallenge())
15 : ? 'task'
16 0 : : (node.isTask())
17 : ? 'step'
18 : : '?';
19 :
20 : String get name;
21 : String get typeName;
22 : JsonMap toJson();
23 :
24 21 : bool isAdventure() => typeName == AdventureModel.className;
25 15 : bool isChallenge() => typeName == ChallengeModel.className;
26 6 : bool isTask() => typeName == TaskModel.className;
27 3 : bool isStep() => typeName == StepModel.className;
28 : }
29 :
30 : class AdventureNodeConverter implements JsonConverter<AdventureNode, JsonMap> {
31 15 : const AdventureNodeConverter();
32 :
33 0 : @override
34 : AdventureNode fromJson(JsonMap json) {
35 0 : if (json['type'] == AdventureModel.className) {
36 0 : return AdventureModel.fromJson(json);
37 : }
38 0 : if (json['type'] == ChallengeModel.className) {
39 0 : return ChallengeModel.fromJson(json);
40 : }
41 :
42 0 : throw Exception('No entry for \'type\' ${json['type']}');
43 : }
44 :
45 0 : @override
46 : JsonMap toJson(AdventureNode data) {
47 0 : final json = data.toJson();
48 0 : json['type'] = data.typeName;
49 : return json;
50 : }
51 : }
|