-
Notifications
You must be signed in to change notification settings - Fork 41
/
related.dart
55 lines (43 loc) · 1.28 KB
/
related.dart
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
// This source code is a part of Project Violet.
// Copyright (C) 2020-2024. violet-team. Licensed under the Apache-2.0 License.
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:flutter/services.dart';
class Related {
static late Map<int, List<int>> related;
static Future<void> init() async {
String data;
if (Platform.environment.containsKey('FLUTTER_TEST')) {
final file = File('/home/ubuntu/violet/assets/rank/related.json');
data = await file.readAsString();
} else {
data = await rootBundle.loadString('assets/rank/related.json');
}
Future<Map<int, List<int>>> decodeJsonData() async {
final related = <int, List<int>>{};
json.decode(
data,
reviver: (key, value) {
if (key == null) {
return value;
}
if (key is String) {
related[int.parse(key)] = (value as List<dynamic>).cast();
return null;
} else {
return value;
}
},
);
return related;
}
related = await Isolate.run(decodeJsonData);
}
static bool existsRelated(int articleId) {
return related.containsKey(articleId);
}
static List<int> getRelated(int articleId) {
return related[articleId]!;
}
}