This repository was archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathserver.dart
More file actions
202 lines (181 loc) · 5.31 KB
/
Copy pathserver.dart
File metadata and controls
202 lines (181 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import 'package:dev_doctor/models/article.dart';
import 'package:dev_doctor/models/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import '../loader.dart';
import 'course.dart';
@immutable
class CoursesServer {
final String name;
final String url;
final String type;
final String icon;
final String supportUrl;
final int? index;
final BackendEntry? entry;
final List<String> courses;
final List<String> articles;
final String body;
static Box<String> get _box => Hive.box<String>('servers');
const CoursesServer(
{this.body = "",
this.icon = "",
this.index,
required this.name,
this.url = "",
this.courses = const [],
this.articles = const [],
this.type = "",
this.entry,
this.supportUrl = ""});
CoursesServer.fromJson(Map<String, dynamic> json)
: name = json['name'],
url = json['url'],
index = (json['index'] != -1) ? json['index'] : null,
type = json['type'] ?? '',
courses = List<String>.from(json['courses'] ?? []),
articles = List<String>.from(json['articles'] ?? []),
icon = json['icon'] ?? "",
entry = json['entry'],
body = json['body'] ?? "",
supportUrl = json['support_url'] ?? "";
Map<String, dynamic> toJson() => {
"name": name,
"url": url,
"index": index,
"courses": courses,
"icon": icon,
"entry": entry,
"body": body,
"support_url": supportUrl,
"articles": articles
};
bool get added =>
index != null || Hive.box<String>('servers').containsKey(url);
Future<CoursesServer> add() async => CoursesServer(
index: await _box.add(url),
courses: courses,
name: name,
type: type,
url: url,
icon: icon,
entry: entry,
supportUrl: supportUrl);
Future<CoursesServer> remove() async {
await _box.delete(index);
return CoursesServer(
index: null,
courses: courses,
name: name,
type: type,
url: url,
icon: icon,
entry: entry,
supportUrl: supportUrl);
}
Future<CoursesServer> toggle() => added ? remove() : add();
CoursesServer copyWith(
{String? name,
String? url,
String? type,
String? icon,
String? supportUrl,
List<String>? courses,
List<String>? articles,
String? body}) =>
CoursesServer(
name: name ?? this.name,
body: body ?? this.body,
articles: articles ?? this.articles,
courses: courses ?? this.courses,
entry: entry,
icon: icon ?? this.icon,
index: index,
supportUrl: supportUrl ?? this.supportUrl,
type: type ?? this.type,
url: url ?? this.url);
static Future<CoursesServer?> fetch(
{String? url, int? index, BackendEntry? entry}) async {
var data = <String, dynamic>{};
try {
if (index == null && url != null) {
var current = _box.values.toList().indexOf(url);
if (current != -1) index = _box.keyAt(current);
} else {
url ??= Hive.box<String>('servers').get(index);
}
var loadedData = await loadFile("$url/config");
if (loadedData == null) return null;
data = loadedData;
} catch (e) {
if (kDebugMode) {
print(e);
}
return null;
}
data['courses'] = data['courses'] ?? [];
data['entry'] = entry;
data['url'] = url;
data['index'] = index;
return CoursesServer.fromJson(data);
}
Future<List<Course>> fetchCourses() =>
Future.wait(courses.map((course) => fetchCourse(course)))
.then((value) async {
var list = <Course>[];
for (var element in value) {
if (element != null) list.add(element);
}
return list;
});
Future<Course?> fetchCourse(String course) async {
try {
var data = await loadFile("$url/$course/config");
if (data == null) return null;
data['server'] = this;
data['slug'] = course;
data['api-version'] = data['api-version'] ?? 0;
return Course.fromJson(data);
} catch (e) {
if (kDebugMode) {
print("Error $e");
}
return null;
}
}
Future<List<Article>> fetchArticles() =>
Future.wait(articles.map((article) => fetchArticle(article)))
.then((value) async {
var list = <Article>[];
for (var element in value) {
if (element != null) list.add(element);
}
return list;
});
Future<Article?> fetchArticle(String article) async {
try {
var data = await loadFile("$url/$article");
if (data == null) return null;
data['server'] = this;
data['slug'] = article;
data['api-version'] = data['api-version'] ?? 0;
return Article.fromJson(data);
} catch (e) {
if (kDebugMode) {
print("Error $e");
}
return null;
}
}
Uri get uri => Uri.parse(url);
}
class CoursesServerAdapter extends TypeAdapter<CoursesServer> {
@override
CoursesServer read(BinaryReader reader) =>
CoursesServer.fromJson(Map<String, dynamic>.from(reader.read()));
@override
final typeId = 0;
@override
void write(BinaryWriter writer, CoursesServer obj) =>
writer.write(obj.toJson());
}