This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('Cache Model In File', () async { | |
final model = ItemModel(id: 1345); | |
final directory = await getApplicationDocumentsDirectory(); | |
await Directory('${directory.path}').create(); | |
await Directory('${directory.path}/vb2').create(); | |
final path = '${directory.path}/vb2/${model.title}.jso'; | |
final file = File(path); | |
final fileWithData = await file.writeAsString(model.toString()); | |
expect(await fileWithData.exists(), true); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EncrypData implements IEncrypData { | |
late final Key key; | |
late final IV iv; | |
final String _privateKey = 'privateKey'; | |
final String _privateINV = 'privateINV'; | |
EncrypData() { | |
key = Key.fromUtf8(dotenv.env[_privateKey] ?? ''); | |
iv = IV.fromUtf8(utf8.decode((dotenv.env[_privateINV] ?? '').codeUnits)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('Cache Model & Encryption', () async { | |
final model = ItemModel(id: 1345); | |
final crypto = EncrypData(); | |
final directory = await getApplicationDocumentsDirectory(); | |
await Directory('${directory.path}').create(); | |
await Directory('${directory.path}/vb2').create(); | |
final path = '${directory.path}/vb2/${model.title}.json'; | |
final file = File(path); | |
final modelValuesWithCrypto = crypto.crypteFile(model.toString()); | |
await file.writeAsString(modelValuesWithCrypto); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: buildGridViewBody(), | |
); | |
} | |
GridView buildGridViewBody() { | |
return GridView.builder( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@override | |
void initState() { | |
super.initState(); | |
homeService = HomeService(EncryptNetworkManager().manager); | |
_fetchAndShow(); | |
} | |
Future<void> _fetchAndShow() async { | |
_changeLoading(); | |
items = await homeService.fetchItem(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<Uint8List?> fetchData() async { | |
final data = await homeService.downloadFile(model.document ?? ''); | |
return data; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
floatingActionButton: buildFloatingActionButtonDownlaod(), | |
appBar: AppBar(title: Text(model.title ?? '')), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class ItemCache { | |
final String _localPath = 'vb10'; | |
final ItemModel item; | |
ItemCache(this.item); | |
Future<void> cacheIt(); | |
Future<Directory> createDirectory(); | |
Future<File> createDirectoryWithPath(); | |
Future<ItemModel?> takeItemWithCache(int id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ItemModelCache extends ItemCache { | |
late final EncrypData encrypData; | |
Directory? _directory; | |
ItemModelCache(ItemModel item) : super(item) { | |
encrypData = EncrypData(); | |
} | |
factory ItemModelCache.dummy() { | |
return ItemModelCache(ItemModel.dummy()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@override | |
Future<void> cacheIt() async { | |
final file = await createDirectoryWithPath(); | |
item.itemValues = encrypData.crypteFile(item.itemValues ?? ''); | |
await file.writeAsString(jsonEncode(item)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@override | |
Future<void> clearAllDatasWithExpiry() async { | |
final directory = await createDirectory(); | |
final itemDirectory = await Directory('${directory.path}/$_localPath').create(); | |
final items = await itemDirectory.list().toSet(); | |
await Future.forEach<FileSystemEntity>(items, (element) async { | |
final file = File(element.path); | |
final jsonBody = json.decode(await file.readAsString()); | |
final feedItem = ItemModel.fromJson(jsonBody); |