Skip to content

Commit 2f0a420

Browse files
committed
updating editor and add toJson method
1 parent 93ec298 commit 2f0a420

13 files changed

Lines changed: 107 additions & 3 deletions

File tree

lib/app_module.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'app_widget.dart';
1010
import 'courses/home.dart';
1111
import 'courses/module.dart';
1212
import 'editor/home.dart';
13+
import 'editor/module.dart';
1314
import 'home.dart';
1415

1516
class AppModule extends Module {
@@ -24,6 +25,7 @@ class AppModule extends Module {
2425
...HomeRoutes.values.map((e) =>
2526
ChildRoute(e.route, child: (_, __) => e.widget, transition: TransitionType.fadeIn)),
2627
]),
28+
ModuleRoute('/editor', module: EditorModule()),
2729
ModuleRoute('/backends', module: BackendsModule()),
2830
ModuleRoute('/settings', module: SettingsModule()),
2931
ModuleRoute('/courses', module: CourseModule()),

lib/editor/bloc/course.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ class CourseEditorBloc {
1010
CourseEditorBloc.fromJson(Map<String, dynamic> json)
1111
: course = Course.fromJson(json['course']),
1212
parts = (json['parts'] as List<dynamic>).map((e) => CoursePart.fromJson(e));
13+
Map<String, dynamic> toJson() =>
14+
{"course": course.toJson(), "parts": parts.map((e) => e.toJson())};
1315
}

lib/editor/bloc/server.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import 'package:dev_doctor/models/server.dart';
2+
import 'package:flutter/cupertino.dart';
23

34
import 'course.dart';
45

6+
@immutable
57
class ServerEditorBloc {
68
final CoursesServer server;
9+
final String description;
710
final List<CourseEditorBloc> courses;
811

9-
ServerEditorBloc(this.server, {this.courses = const []});
12+
ServerEditorBloc(this.server, {this.courses = const [], this.description});
1013
ServerEditorBloc.fromJson(Map<String, dynamic> json)
1114
: server = CoursesServer.fromJson(json['server']),
15+
description = json['description'],
1216
courses =
1317
(json['courses'] as List<dynamic>).map((e) => CourseEditorBloc.fromJson(e)).toList();
18+
Map<String, dynamic> toJson() => {
19+
"server": server.toJson(),
20+
"description": description,
21+
"courses": courses.map((e) => e.toJson())
22+
};
1423
}

lib/editor/create.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
1+
import 'package:dev_doctor/models/server.dart';
2+
import 'package:dev_doctor/widgets/appbar.dart';
13
import 'package:flutter/material.dart';
4+
import 'package:easy_localization/easy_localization.dart';
5+
6+
import 'bloc/server.dart';
27

38
class CreateServerPage extends StatefulWidget {
49
@override
510
_CreateServerPageState createState() => _CreateServerPageState();
611
}
712

813
class _CreateServerPageState extends State<CreateServerPage> {
14+
TextEditingController _nameController = TextEditingController();
15+
TextEditingController _descriptionController = TextEditingController();
916
@override
1017
Widget build(BuildContext context) {
11-
return Container();
18+
return Scaffold(
19+
appBar: MyAppBar(title: "editor.create.title".tr()),
20+
body: Scrollbar(
21+
child: Center(
22+
child: Container(
23+
constraints: BoxConstraints(maxWidth: 1000),
24+
child: ListView(children: [
25+
TextField(
26+
decoration: InputDecoration(labelText: "editor.create.name".tr()),
27+
),
28+
TextField(
29+
decoration: InputDecoration(labelText: "editor.create.description".tr()),
30+
)
31+
])))),
32+
floatingActionButton: FloatingActionButton(
33+
child: Icon(Icons.check_outlined),
34+
onPressed: () => Navigator.of(context).pop(ServerEditorBloc(
35+
CoursesServer(name: _nameController.text),
36+
description: _descriptionController.text))));
1237
}
1338
}

lib/editor/home.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:dev_doctor/editor/bloc/server.dart';
44
import 'package:dev_doctor/widgets/appbar.dart';
55
import 'package:flutter/material.dart';
66
import 'package:easy_localization/easy_localization.dart';
7+
import 'package:flutter_modular/flutter_modular.dart';
78
import 'package:hive/hive.dart';
89
import 'package:hive_flutter/hive_flutter.dart';
910

@@ -29,6 +30,8 @@ class _EditorPageState extends State<EditorPage> {
2930
return ListTile(title: Text(bloc.server.name));
3031
}))),
3132
floatingActionButton: FloatingActionButton.extended(
32-
onPressed: () {}, icon: Icon(Icons.add_outlined), label: Text("editor.create").tr()));
33+
onPressed: () => Modular.to.pushNamed("/editor/create"),
34+
icon: Icon(Icons.add_outlined),
35+
label: Text("editor.create.fab").tr()));
3336
}
3437
}

lib/editor/module.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'package:flutter_modular/flutter_modular.dart';
2+
3+
import 'create.dart';
4+
import 'home.dart';
5+
6+
class EditorModule extends Module {
7+
@override
8+
List<ModularRoute> get routes => [
9+
ChildRoute('/', child: (_, args) => EditorPage()),
10+
ChildRoute('/create', child: (_, args) => CreateServerPage())
11+
];
12+
}

lib/models/course.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ class Course {
5353
parts = List<String>.from(json['parts']),
5454
private = json['private'],
5555
supportUrl = json['support_url'];
56+
Map<String, dynamic> toJson() => {
57+
"server": server,
58+
"slug": slug,
59+
"name": name,
60+
"description": description,
61+
"icon": icon,
62+
"author": author,
63+
"author_url": authorUrl,
64+
"author_avatar": authorAvatar,
65+
"body": body,
66+
"index": index,
67+
"installed": installed,
68+
"lang": lang,
69+
"parts": parts,
70+
"private": private,
71+
"support_url": supportUrl
72+
};
5673

5774
get url => server.url + "/" + slug;
5875

lib/models/item.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ abstract class PartItem {
1111
: name = json['name'],
1212
description = json['description'],
1313
index = json['index'];
14+
Map<String, dynamic> toJson();
1415
}

lib/models/items/quiz.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class QuizPartItem extends PartItem {
1616
.map((question) => QuizQuestion.fromJson(question))
1717
.toList(),
1818
super.fromJson(json);
19+
20+
@override
21+
Map<String, dynamic> toJson() =>
22+
{"text": text, "time": time, "questions": questions.map((e) => e.toJson())};
1923
}
2024

2125
@immutable
@@ -33,6 +37,12 @@ class QuizQuestion {
3337
answers = (json['answers'] as List<dynamic>)
3438
.map((answer) => QuizAnswer.fromJson(answer))
3539
.toList();
40+
Map<String, dynamic> toJson() => {
41+
"title": title,
42+
"description": description,
43+
"evaluation": evaluation,
44+
"answers": answers.map((e) => e.toJson())
45+
};
3646
}
3747

3848
@immutable
@@ -48,4 +58,6 @@ class QuizAnswer {
4858
description = json['description'],
4959
correct = json['correct'] ?? false,
5060
points = json['points'];
61+
Map<String, dynamic> toJson() =>
62+
{"correct": correct, "name": name, "description": description, "points": points};
5163
}

lib/models/items/text.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ class TextPartItem extends PartItem {
99
TextPartItem.fromJson(Map<String, dynamic> json)
1010
: text = json['text'],
1111
super.fromJson(json);
12+
Map<String, dynamic> toJson() => {"text": text};
1213
}

0 commit comments

Comments
 (0)