Skip to content

Commit 24412d3

Browse files
committed
Implement cache
1 parent f6b6360 commit 24412d3

File tree

9 files changed

+99
-13
lines changed

9 files changed

+99
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ build/
3838
/example/analysis_options.yaml
3939
/example/pubspec.lock
4040
/example/pubspec.yaml
41-
/example/README.md
41+
/example/README.md
42+
/example/.flutter-plugins*

lib/github_colour.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import 'package:flutter/widgets.dart' show Color;
99
import 'package:http/http.dart' as http show get;
1010
import 'package:meta/meta.dart' show sealed;
1111

12-
/// An [Error] thrown when response unsuccessfully.
12+
import 'src/cache/cache.dart';
13+
14+
/// An [Error] thrown when response unsuccessfully and no cache can be used.
1315
class GitHubColourLoadFailedError extends Error {
1416
/// HTTP response code when fetching colour data.
1517
final int responseCode;
@@ -48,17 +50,26 @@ class GitHubColour {
4850

4951
var resp = await http.get(ghc);
5052

53+
Map<String, Color> ghjson;
54+
5155
if (resp.statusCode != 200) {
52-
throw GitHubColourLoadFailedError._(resp.statusCode);
56+
try {
57+
ghjson = await getCache();
58+
} catch (e) {
59+
throw GitHubColourLoadFailedError._(resp.statusCode);
60+
}
61+
} else {
62+
ghjson = (jsonDecode(resp.body) as Map<String, dynamic>)
63+
.map<String, Color>((key, value) {
64+
String hex = value["color"] ?? defaultColourHex;
65+
hex = "FF${hex.substring(1).toUpperCase()}";
66+
67+
return MapEntry(key, Color(int.parse(hex, radix: 16)));
68+
});
69+
await saveCache(ghjson);
5370
}
5471

55-
_instance = GitHubColour._((jsonDecode(resp.body) as Map<String, dynamic>)
56-
.map<String, Color>((key, value) {
57-
String hex = value["color"] ?? defaultColourHex;
58-
hex = "FF${hex.substring(1).toUpperCase()}";
59-
60-
return MapEntry(key, Color(int.parse(hex, radix: 16)));
61-
}));
72+
_instance = GitHubColour._(ghjson);
6273
}
6374

6475
return _instance!;

lib/src/cache/cache.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export 'cache_generic.dart'
2+
if (dart.library.io) "cache_vm.dart"
3+
if (dart.library.html) "cache_web.dart";

lib/src/cache/cache_generic.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:flutter/widgets.dart' show Color;
2+
3+
Future<void> saveCache(Map<String, Color> githubColour) async {
4+
throw UnsupportedError("No implementation in this platform");
5+
}
6+
7+
Future<Map<String, Color>> getCache() async {
8+
throw UnsupportedError("No implementation in this platform");
9+
}

lib/src/cache/cache_vm.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import "dart:io";
2+
3+
import 'package:flutter/widgets.dart' show Color;
4+
import "package:path/path.dart" as path;
5+
import "package:path_provider/path_provider.dart" as path_provider;
6+
7+
import "../compression.dart";
8+
9+
Future<File> get _cacheFile async {
10+
Directory tmp = await path_provider.getTemporaryDirectory();
11+
12+
File tmpfile = File(path.join(tmp.path, "github_colour_cache.tmp"));
13+
14+
if (!await tmpfile.exists()) {
15+
tmpfile = await tmpfile.create(recursive: true);
16+
}
17+
18+
return tmpfile;
19+
}
20+
21+
Future<void> saveCache(Map<String, Color> githubColour) async {
22+
File tf = await _cacheFile;
23+
24+
tf = await tf.writeAsBytes(compressGHC(githubColour), flush: true);
25+
}
26+
27+
Future<Map<String, Color>> getCache() async {
28+
File tf = await _cacheFile;
29+
30+
return decompressGHC(await tf.readAsBytes());
31+
}

lib/src/cache/cache_web.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:flutter/widgets.dart' show Color;
2+
3+
Future<void> saveCache(Map<String, Color> githubColour) async {
4+
// TODO: impl web
5+
throw UnsupportedError("No implementation in this platform");
6+
}
7+
8+
Future<Map<String, Color>> getCache() async {
9+
// TODO: impl web
10+
throw UnsupportedError("No implementation in this platform");
11+
}

lib/src/compression.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'dart:convert';
2+
import 'dart:typed_data';
3+
4+
import 'package:flutter/widgets.dart' show Color;
5+
import 'package:lzma/lzma.dart';
6+
7+
import 'conversion.dart';
8+
9+
Uint8List compressGHC(Map<String, Color> ghc) => Uint8List.fromList(
10+
lzma.encode(utf8.encode(jsonEncode(convertColourToInt(ghc)))));
11+
12+
Map<String, Color> decompressGHC(Uint8List compressed) =>
13+
convertIntToColour(jsonDecode(utf8.decode(lzma.decode(compressed))));

lib/src/conversion.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:flutter/widgets.dart' show Color;
2+
3+
Map<String, int> convertColourToInt(Map<String, Color> colourMap) =>
4+
colourMap.map((key, value) => MapEntry(key, value.value));
5+
6+
Map<String, Color> convertIntToColour(Map<String, int> intMap) =>
7+
intMap.map((key, value) => MapEntry(key, Color(value)));

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ dependencies:
1010
sdk: flutter
1111
http: ^0.13.4
1212
meta: ^1.7.0
13-
dev_dependencies:
14-
flutter_test:
15-
sdk: flutter
13+
path: ^1.8.1
14+
path_provider: ^2.0.9
15+
lzma: ^1.0.1
1616
flutter:
1717
uses-material-design: true

0 commit comments

Comments
 (0)