|
@@ -0,0 +1,139 @@
|
|
|
+import 'dart:io';
|
|
|
+
|
|
|
+import 'package:path_provider/path_provider.dart';
|
|
|
+import 'package:vitalapp/managers/interfaces/cache.dart';
|
|
|
+
|
|
|
+class CacheManager implements ICacheManager {
|
|
|
+
|
|
|
+ @override
|
|
|
+ void clearApplicationCache() async {
|
|
|
+ Directory docDirectory = await getApplicationDocumentsDirectory();
|
|
|
+ Directory tempDirectory = await getTemporaryDirectory();
|
|
|
+
|
|
|
+ if (docDirectory.existsSync()) {
|
|
|
+ await deleteDirectory(docDirectory);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (tempDirectory.existsSync()) {
|
|
|
+ await deleteDirectory(tempDirectory);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ void clearApplicationImageCache() async {
|
|
|
+ Directory tempDirectory = await getTemporaryDirectory();
|
|
|
+ if (tempDirectory.existsSync()) {
|
|
|
+ await deleteImageDirectory(tempDirectory);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @override
|
|
|
+ Future<void> deleteDirectory(FileSystemEntity file) async {
|
|
|
+ if (file is Directory) {
|
|
|
+ final List<FileSystemEntity> children = file.listSync();
|
|
|
+ for (final FileSystemEntity child in children) {
|
|
|
+ await deleteDirectory(child);
|
|
|
+ await child.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Future<void> deleteImageDirectory(FileSystemEntity file) async {
|
|
|
+ if (file is File) {
|
|
|
+ if (file.path.endsWith('.png') ||
|
|
|
+ file.path.endsWith('.jpg') ||
|
|
|
+ file.path.endsWith('.jpeg') ||
|
|
|
+ file.path.endsWith('.mp4') ||
|
|
|
+ file.path.endsWith('.avi') ||
|
|
|
+ file.path.endsWith('.mov')) {
|
|
|
+ await file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (file is Directory) {
|
|
|
+ final List<FileSystemEntity> children = file.listSync();
|
|
|
+ for (final FileSystemEntity child in children) {
|
|
|
+ await deleteImageDirectory(child);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @override
|
|
|
+ Future<double> getCacheSize() async {
|
|
|
+
|
|
|
+ Directory docDirectory = await getApplicationDocumentsDirectory();
|
|
|
+ Directory tempDirectory = await getTemporaryDirectory();
|
|
|
+
|
|
|
+ double size = 0;
|
|
|
+
|
|
|
+ if (docDirectory.existsSync()) {
|
|
|
+ size += await getTotalSizeOfFilesInDir(docDirectory);
|
|
|
+ }
|
|
|
+ if (tempDirectory.existsSync()) {
|
|
|
+ size += await getTotalSizeOfFilesInDir(tempDirectory);
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ Future<double> getImageCacheSize() async {
|
|
|
+
|
|
|
+ Directory docDirectory = await getApplicationDocumentsDirectory();
|
|
|
+ Directory tempDirectory = await getTemporaryDirectory();
|
|
|
+
|
|
|
+ double size = 0;
|
|
|
+
|
|
|
+ if (docDirectory.existsSync()) {
|
|
|
+ size += await getTotalSizeOfImageInDir(docDirectory);
|
|
|
+ }
|
|
|
+ if (tempDirectory.existsSync()) {
|
|
|
+ size += await getTotalSizeOfImageInDir(tempDirectory);
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+ }
|
|
|
+
|
|
|
+ static Future<double> getTotalSizeOfFilesInDir(
|
|
|
+ final FileSystemEntity file) async {
|
|
|
+ if (file is File) {
|
|
|
+ int length = await file.length();
|
|
|
+ return double.parse(length.toString());
|
|
|
+ }
|
|
|
+ if (file is Directory) {
|
|
|
+ final List<FileSystemEntity> children = file.listSync();
|
|
|
+ double total = 0;
|
|
|
+ for (final FileSystemEntity child in children) {
|
|
|
+ total += await getTotalSizeOfFilesInDir(child);
|
|
|
+ }
|
|
|
+ return total;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ static Future<double> getTotalSizeOfImageInDir(
|
|
|
+ final FileSystemEntity file) async {
|
|
|
+ if (file is File) {
|
|
|
+ if (file.path.endsWith('.png') ||
|
|
|
+ file.path.endsWith('.jpg') ||
|
|
|
+ file.path.endsWith('.jpeg') ||
|
|
|
+ file.path.endsWith('.mp4') ||
|
|
|
+ file.path.endsWith('.avi') ||
|
|
|
+ file.path.endsWith('.mov')) {
|
|
|
+ int length = await file.length();
|
|
|
+ return double.parse(length.toString());
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (file is Directory) {
|
|
|
+ final List<FileSystemEntity> children = file.listSync();
|
|
|
+ double total = 0;
|
|
|
+ for (final FileSystemEntity child in children) {
|
|
|
+ total += await getTotalSizeOfImageInDir(child);
|
|
|
+ }
|
|
|
+ return total;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+}
|