11import 'dart:convert' ;
22import 'dart:io' as io;
33
4+ import 'package:gitmoji/nullable_string_extension.dart' ;
45import 'package:http/http.dart' as http;
56
67import 'gitmoji.dart' ;
78
89class GitmojiClient {
9- Future <List <Gitmoji >> fetch (
10- bool debug, {
10+ final bool debug;
11+
12+ GitmojiClient (this .debug);
13+
14+ Future <List <Gitmoji >> fetch ({
1115 String url =
1216 'https://raw.githubusercontent.com/carloscuesta/gitmoji/refs/heads'
1317 '/master/packages/gitmojis/src/gitmojis.json' ,
@@ -25,33 +29,25 @@ class GitmojiClient {
2529 if (debug) print ('Getting definition...' );
2630 jsonString = await _fetchFromWeb (debug, url);
2731
28- if (jsonString.isEmpty && cacheFile.existsSync ()) {
29- print ('Using old cache.' );
30- jsonString = cacheFile.readAsStringSync ();
32+ if (jsonString.isNotEmpty) {
33+ cacheFile.writeAsStringSync (jsonString);
34+ } else {
35+ if (cacheFile.existsSync ()) {
36+ if (debug) print ('Using old cache.' );
37+ jsonString = cacheFile.readAsStringSync ();
38+ }
3139 }
3240 }
3341
34- if (jsonString.trim ().isEmpty ) {
35- throw Exception ('GitMojis definition not found.' );
42+ if (jsonString.isNullOrBlank ) {
43+ throw Exception ('Gitmojis definition not found.' );
3644 }
3745
3846 final body = jsonDecode (jsonString);
3947
40- cacheFile.writeAsStringSync (jsonString);
41-
4248 final List <dynamic > list = body['gitmojis' ];
4349
44- return list.map ((item) {
45- final map = item as Map <String , dynamic >;
46- return Gitmoji (
47- emoji: map['emoji' ].toString (),
48- entity: map['entity' ].toString (),
49- code: map['code' ].toString (),
50- description: map['description' ].toString ().trim (),
51- name: map['name' ].toString (),
52- semver: map['semver' ]? .toString (),
53- );
54- }).toList ();
50+ return list.map ((item) => Gitmoji .fromJson (item)).toList ();
5551 }
5652
5753 Future <String > _fetchFromWeb (bool debug, String url) async {
0 commit comments