Преглед на файлове

Fix: Remove synced codes on logout

Neeraj Gupta преди 2 години
родител
ревизия
abf4afe76e
променени са 3 файла, в които са добавени 14 реда и са изтрити 0 реда
  1. 3 0
      lib/models/authenticator/local_auth_entity.dart
  2. 4 0
      lib/services/authenticator_service.dart
  3. 7 0
      lib/store/authenticator_db.dart

+ 3 - 0
lib/models/authenticator/local_auth_entity.dart

@@ -5,15 +5,18 @@ import 'package:flutter/material.dart';
 @immutable
 @immutable
 class LocalAuthEntity {
 class LocalAuthEntity {
   final int generatedID;
   final int generatedID;
+
   // id can be null if a code has been scanned locally but it's yet to be
   // id can be null if a code has been scanned locally but it's yet to be
   // synced with the remote server.
   // synced with the remote server.
   final String? id;
   final String? id;
   final String encryptedData;
   final String encryptedData;
   final String header;
   final String header;
+
   // createdAt and updateAt will be equal to local time of creation or updation
   // createdAt and updateAt will be equal to local time of creation or updation
   // till remote sync is completed.
   // till remote sync is completed.
   final int createdAt;
   final int createdAt;
   final int updatedAt;
   final int updatedAt;
+
   // shouldSync indicates that the entry was locally created or updated. The
   // shouldSync indicates that the entry was locally created or updated. The
   // app should try to sync it to the server during next sync
   // app should try to sync it to the server during next sync
   final bool shouldSync;
   final bool shouldSync;

+ 4 - 0
lib/services/authenticator_service.dart

@@ -134,7 +134,11 @@ class AuthenticatorService {
       _logger.info("local push completed");
       _logger.info("local push completed");
       Bus.instance.fire(CodesUpdatedEvent());
       Bus.instance.fire(CodesUpdatedEvent());
     } on UnauthorizedError {
     } on UnauthorizedError {
+      if ((await _db.removeSyncedData()) > 0) {
+        Bus.instance.fire(CodesUpdatedEvent());
+      }
       debugPrint("Firing logout event");
       debugPrint("Firing logout event");
+
       Bus.instance.fire(TriggerLogoutEvent());
       Bus.instance.fire(TriggerLogoutEvent());
     } catch (e) {
     } catch (e) {
       _logger.severe("Failed to sync with remote", e);
       _logger.severe("Failed to sync with remote", e);

+ 7 - 0
lib/store/authenticator_db.dart

@@ -135,6 +135,13 @@ class AuthenticatorDB {
     return _convertRows(rows);
     return _convertRows(rows);
   }
   }
 
 
+  // removeSyncedData will remove all the data which is synced with the server
+  Future<int> removeSyncedData() async {
+    final db = await instance.database;
+    return await db
+        .delete(entityTable, where: 'shouldSync = ?', whereArgs: [0]);
+  }
+
 // deleteByID will prefer generated id if both ids are passed during deletion
 // deleteByID will prefer generated id if both ids are passed during deletion
   Future<void> deleteByIDs({List<int>? generatedIDs, List<String>? ids}) async {
   Future<void> deleteByIDs({List<int>? generatedIDs, List<String>? ids}) async {
     final db = await instance.database;
     final db = await instance.database;