Browse Source

Update delete API

Vishnu Mohandas 4 years ago
parent
commit
4dd4eed10c
3 changed files with 32 additions and 21 deletions
  1. 17 11
      lib/db/files_db.dart
  2. 6 6
      lib/services/sync_service.dart
  3. 9 4
      lib/utils/file_util.dart

+ 17 - 11
lib/db/files_db.dart

@@ -193,14 +193,20 @@ class FilesDB {
     return _convertToFiles(results);
   }
 
-  Future<List<File>> getAllDeleted() async {
+  Future<List<int>> getDeletedFileIDs() async {
     final db = await instance.database;
-    final results = await db.query(
+    final rows = await db.query(
       table,
+      columns: [columnUploadedFileID],
+      distinct: true,
       where: '$columnIsDeleted = 1',
       orderBy: '$columnCreationTime DESC',
     );
-    return _convertToFiles(results);
+    final result = List<int>();
+    for (final row in rows) {
+      result.add(row[columnUploadedFileID]);
+    }
+    return result;
   }
 
   Future<List<File>> getFilesToBeUploadedWithinFolders(
@@ -246,7 +252,7 @@ class FilesDB {
         $columnUpdationTime,
         MAX($columnCreationTime) as $columnCreationTime
       FROM $table
-      WHERE $columnCollectionID IN (${collectionIDs.join(', ')})
+      WHERE $columnCollectionID IN (${collectionIDs.join(', ')}) AND $columnIsDeleted = 0
       GROUP BY $columnCollectionID
       ORDER BY $columnCreationTime DESC;
     ''');
@@ -284,7 +290,7 @@ class FilesDB {
         $columnCreationTime,
         MAX($columnUpdationTime) AS $columnUpdationTime
       FROM $table
-      WHERE $columnCollectionID IN (${collectionIDs.join(', ')})
+      WHERE $columnCollectionID IN (${collectionIDs.join(', ')}) AND $columnIsDeleted = 0
       GROUP BY $columnCollectionID
       ORDER BY $columnUpdationTime DESC;
     ''');
@@ -344,24 +350,24 @@ class FilesDB {
     );
   }
 
-  Future<int> markForDeletion(File file) async {
+  Future<int> markForDeletion(int uploadedFileID) async {
     final db = await instance.database;
     final values = new Map<String, dynamic>();
     values[columnIsDeleted] = 1;
     return db.update(
       table,
       values,
-      where: '$columnGeneratedID =?',
-      whereArgs: [file.generatedID],
+      where: '$columnUploadedFileID =?',
+      whereArgs: [uploadedFileID],
     );
   }
 
-  Future<int> delete(File file) async {
+  Future<int> delete(int uploadedFileID) async {
     final db = await instance.database;
     return db.delete(
       table,
-      where: '$columnGeneratedID =?',
-      whereArgs: [file.generatedID],
+      where: '$columnUploadedFileID =?',
+      whereArgs: [uploadedFileID],
     );
   }
 

+ 6 - 6
lib/services/sync_service.dart

@@ -281,20 +281,20 @@ class SyncService {
   }
 
   Future<void> _deletePhotosOnServer() async {
-    return _db.getAllDeleted().then((deletedPhotos) async {
-      for (File deletedPhoto in deletedPhotos) {
-        await _deleteFileOnServer(deletedPhoto);
-        await _db.delete(deletedPhoto);
+    return _db.getDeletedFileIDs().then((ids) async {
+      for (int id in ids) {
+        await _deleteFileOnServer(id);
+        await _db.delete(id);
       }
     });
   }
 
-  Future<void> _deleteFileOnServer(File file) async {
+  Future<void> _deleteFileOnServer(int fileID) async {
     return _dio
         .delete(
           Configuration.instance.getHttpEndpoint() +
               "/files/" +
-              file.uploadedFileID.toString(),
+              fileID.toString(),
           options: Options(
               headers: {"X-Auth-Token": Configuration.instance.getToken()}),
         )

+ 9 - 4
lib/utils/file_util.dart

@@ -26,11 +26,16 @@ import 'crypto_util.dart';
 final logger = Logger("FileUtil");
 
 Future<void> deleteFiles(List<File> files) async {
-  await PhotoManager.editor
-      .deleteWithIds(files.map((file) => file.localID).toList());
-  for (File file in files) {
-    await FilesDB.instance.markForDeletion(file);
+  final localIDs = List<String>();
+  for (final file in files) {
+    if (file.localID != null) {
+      localIDs.add(file.localID);
+    }
+    if (file.uploadedFileID != null) {
+      await FilesDB.instance.markForDeletion(file.uploadedFileID);
+    }
   }
+  await PhotoManager.editor.deleteWithIds(localIDs);
   await FileRepository.instance.reloadFiles();
   SyncService.instance.sync();
 }