-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcached_json.dart
107 lines (85 loc) · 2.95 KB
/
cached_json.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
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
import 'dart:convert';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:path_provider/path_provider.dart';
class CacheJson {
static var tempDirectory;
///Loads json file from cache(temp directory) with with given name
static Future<String> saveFile({
@required
Map<String, dynamic> file,
@required
String cachedFileName
}) async{
File jsonFile;
var tempDir = await getApplicationDocumentsDirectory();
String _filePath = tempDir.path + '/$cachedFileName.json';
jsonFile = new File(_filePath);
jsonFile.writeAsStringSync(json.encode(file));
return "Done";
}
///loads json cached file with a name
static Future<Map<String, dynamic>> loadFile({@required String cachedFileName}) async {
File jsonFile;
Map<String, dynamic> _file;
var tempDir = await getApplicationDocumentsDirectory();
String _filePath = tempDir.path + '/$cachedFileName.json';
jsonFile = new File(_filePath);
if (jsonFile.existsSync()) {
_file = json.decode(jsonFile.readAsStringSync());
return _file;
}
}
/// deletes the created file
static Future<String> deleteFile({String cachedFileName}) async{
File jsonFile;
var tempDir = await getApplicationDocumentsDirectory();
String _filePath = tempDir.path + '/$cachedFileName.json';
jsonFile.writeAsStringSync(json.encode({}));
return "Deleted";
}
/// now that all basic functionality is done ..but we still need some optimization and we can see that
/// our static functions are async and due to that reason it would be hard to load data quickly while calling them
///Future method for App document local directory
Future setTempDirectory() async{
var tempDir = await getApplicationDocumentsDirectory();
tempDirectory = tempDir;
}
///Once instantiated, Than this return object can be used to quickly store and load data
static Future<CacheJson> getInstance() async{
CacheJson cacheJson = CacheJson();
await cacheJson.setTempDirectory();
return cacheJson;
}
///save json file in cache(temp directory) with with given name using instance member
bool setFile({
@required
Map<String, dynamic> file,
@required
String cachedFileName})
{
File jsonFile;
String _filePath = tempDirectory.path + '/$cachedFileName.json';
jsonFile = new File(_filePath);
jsonFile.writeAsStringSync(json.encode(file));
return true;
}
///loads json cached file with a name
Map<String, dynamic> getFile({@required String cachedFileName}){
File jsonFile;
Map<String, dynamic> _file;
String _filePath = tempDirectory.path + '/$cachedFileName.json';
jsonFile = new File(_filePath);
if (jsonFile.existsSync()) {
_file = json.decode(jsonFile.readAsStringSync());
return _file;
}
}
/// deletes the created file
bool removeFile({@required String cachedFileName}) {
File jsonFile;
String _filePath = tempDirectory.path + '/$cachedFileName.json';
jsonFile.writeAsStringSync(json.encode({}));
return true;
}
}