@@ -8,6 +8,7 @@ import 'package:watermeter/model/xidian_ids/score.dart';
8
8
import 'package:watermeter/page/public_widget/public_widget.dart' ;
9
9
import 'package:watermeter/page/score/score_page.dart' ;
10
10
import 'package:watermeter/page/score/score_state.dart' ;
11
+ import 'package:watermeter/repository/network_session.dart' ;
11
12
import 'package:watermeter/repository/xidian_ids/ehall_score_session.dart' ;
12
13
13
14
class ScoreWindow extends StatefulWidget {
@@ -18,6 +19,7 @@ class ScoreWindow extends StatefulWidget {
18
19
}
19
20
20
21
class _ScoreWindowState extends State <ScoreWindow > {
22
+ static const scoreListCacheName = "scores.json" ;
21
23
late Future <List <Score >> scoreList;
22
24
23
25
Navigator _getNavigator (BuildContext context, Widget child) {
@@ -28,12 +30,55 @@ class _ScoreWindowState extends State<ScoreWindow> {
28
30
);
29
31
}
30
32
31
- void dataInit () => scoreList = ScoreSession ().getScore ();
33
+ Future <List <Score >> loadScoreListCache () async {
34
+ log.i (
35
+ "[ScoreWindow][loadScoreListCache] "
36
+ "Path at ${supportPath .path }/$scoreListCacheName ." ,
37
+ );
38
+ file = File ("${supportPath .path }/$scoreListCacheName " );
39
+ if (file.existsSync ()) {
40
+ final timeDiff = DateTime .now ().difference (
41
+ file.lastModifiedSync ()
42
+ ).inDays;
43
+ if (timeDiff < 1 ) {
44
+ log.i (
45
+ "[ScoreWindow][loadScoreListCache] "
46
+ "Load effective cache file." ,
47
+ );
48
+ return jsonDecode (file.readAsStringSync ()).map (
49
+ (s) => Score .fromJson (s)
50
+ ).toList ();
51
+ }
52
+ }
53
+ log.i (
54
+ "[ScoreWindow][loadScoreListCache] "
55
+ "Cache file non-existent or ineffective." ,
56
+ );
57
+ return [];
58
+ }
59
+
60
+ void dumpScoreListCache (List <Score > scores) {
61
+ file.writeAsStringSync (
62
+ jsonEncode (scores.map ((s) => s.toJson ()).toList ())
63
+ );
64
+ }
65
+
66
+ Future <void > dataInit async () {
67
+ final cache = await loadScoreListCache ();
68
+ if (scoreList.isEmpty) {
69
+ log.i (
70
+ "[ScoreWindow][loadScoreListCache] "
71
+ "Get scores via ScoreSession." ,
72
+ );
73
+ scoreList = ScoreSession ().getScore ();
74
+ } else {
75
+ scoreList = Future .value (cache);
76
+ }
77
+ }
32
78
33
79
@override
34
80
void initState () {
35
- dataInit ();
36
- super .initState ();
81
+ dataInit ().then (() => super .initState ())
37
82
}
38
83
39
84
@override
0 commit comments