LCOV - code coverage report
Current view: top level - lib\data\local\dao\chatwoot_contact_dao.dart - chatwoot_contact_dao.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 36 36 100.0 %
Date: Tue Jul 13 13:39:38 2021 Functions: 0 0 -

          Line data    Source code
       1             : 
       2             : import 'package:hive_flutter/hive_flutter.dart';
       3             : 
       4             : import '../entity/chatwoot_contact.dart';
       5             : 
       6             : abstract class ChatwootContactDao{
       7             :   Future<void> saveContact(ChatwootContact contact);
       8             :   ChatwootContact? getContact();
       9             :   Future<void> deleteContact();
      10             :   Future<void> onDispose();
      11             :   Future<void> clearAll();
      12             : }
      13             : 
      14             : //Only used when persistence is enabled
      15          19 : enum ChatwootContactBoxNames{
      16             :   CONTACTS, CLIENT_INSTANCE_TO_CONTACTS
      17             : }
      18             : class PersistedChatwootContactDao extends ChatwootContactDao{
      19             :   
      20             :   //box containing all persisted contacts
      21             :   Box<ChatwootContact> _box;
      22             : 
      23             :   //_box with one to one relation between generated client instance id and conversation id
      24             :   final Box<String> _clientInstanceIdToContactIdentifierBox;
      25             : 
      26             :   final String _clientInstanceKey;
      27             : 
      28           2 :   PersistedChatwootContactDao(
      29             :     this._box,
      30             :     this._clientInstanceIdToContactIdentifierBox,
      31             :     this._clientInstanceKey
      32             :   );
      33             : 
      34             :   @override
      35           1 :   Future<void> deleteContact() async{
      36           2 :     final contactIdentifier = _clientInstanceIdToContactIdentifierBox.get(
      37           1 :         _clientInstanceKey
      38             :     );
      39           3 :     await _clientInstanceIdToContactIdentifierBox.delete(
      40           1 :         _clientInstanceKey
      41             :     );
      42           3 :     await _box.delete(contactIdentifier);
      43             :   }
      44             : 
      45             :   @override
      46           1 :   Future<void> saveContact(ChatwootContact contact) async{
      47           3 :     await _clientInstanceIdToContactIdentifierBox.put(
      48           1 :         _clientInstanceKey,
      49           1 :         contact.contactIdentifier!
      50             :     );
      51           4 :     await _box.put(contact.contactIdentifier, contact);
      52             :   }
      53             : 
      54           1 :   @override
      55             :   ChatwootContact? getContact(){
      56           4 :     if(_box.values.length==0){
      57             :       return null;
      58             :     }
      59             : 
      60           2 :     final contactIdentifier = _clientInstanceIdToContactIdentifierBox.get(
      61           1 :         _clientInstanceKey
      62             :     );
      63             : 
      64           2 :     return _box.get(contactIdentifier,defaultValue: null);
      65             :   }
      66             : 
      67             :   @override
      68           1 :   Future<void> onDispose() async{
      69           3 :     await _box.close();
      70             :   }
      71             : 
      72           1 :   Future<void> clearAll() async{
      73           3 :     await _box.clear();
      74           3 :     await _clientInstanceIdToContactIdentifierBox.clear();
      75             :   }
      76             : 
      77             : 
      78           3 :   static Future<void> openDB() async{
      79          12 :     await Hive.openBox<ChatwootContact>(ChatwootContactBoxNames.CONTACTS.toString());
      80          12 :     await Hive.openBox<String>(ChatwootContactBoxNames.CLIENT_INSTANCE_TO_CONTACTS.toString());
      81             :   }
      82             : 
      83             : }
      84             : 
      85             : class NonPersistedChatwootContactDao extends ChatwootContactDao{
      86             :   ChatwootContact? _contact;
      87             : 
      88             : 
      89             :   @override
      90           1 :   Future<void> deleteContact() async{
      91           1 :     _contact = null;
      92             :   }
      93             : 
      94           1 :   @override
      95             :   ChatwootContact? getContact() {
      96           1 :     return _contact;
      97             :   }
      98             : 
      99             :   @override
     100           1 :   Future<void> onDispose() async{
     101           1 :     _contact = null;
     102             :   }
     103             : 
     104             :   @override
     105           1 :   Future<void> saveContact(ChatwootContact contact) async{
     106           1 :     _contact = contact;
     107             :   }
     108             : 
     109             : 
     110           1 :   Future<void> clearAll() async{
     111           1 :     _contact = null;
     112             :   }
     113             : 
     114             : 
     115             : }

Generated by: LCOV version 1.15.alpha0w