LCOV - code coverage report
Current view: top level - lib\data\local\dao\chatwoot_user_dao.dart - chatwoot_user_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:chatwoot_client_sdk/data/local/entity/chatwoot_user.dart';
       3             : import 'package:hive_flutter/hive_flutter.dart';
       4             : 
       5             : abstract class ChatwootUserDao{
       6             : 
       7             :   Future<void> saveUser(ChatwootUser user);
       8             :   ChatwootUser? getUser();
       9             :   Future<void> deleteUser();
      10             :   Future<void> onDispose();
      11             :   Future<void> clearAll();
      12             : }
      13             : 
      14             : 
      15             : //Only used when persistence is enabled
      16          18 : enum ChatwootUserBoxNames{
      17             :   USERS, CLIENT_INSTANCE_TO_USER
      18             : }
      19             : class PersistedChatwootUserDao extends ChatwootUserDao{
      20             :   //box containing chat users
      21             :   Box<ChatwootUser> _box;
      22             :   //box with one to one relation between generated client instance id and user identifier
      23             :   final Box<String> _clientInstanceIdToUserIdentifierBox;
      24             : 
      25             :   final String _clientInstanceKey;
      26             : 
      27           2 :   PersistedChatwootUserDao(
      28             :       this._box,
      29             :       this._clientInstanceIdToUserIdentifierBox,
      30             :       this._clientInstanceKey
      31             :   );
      32             : 
      33             :   @override
      34           1 :   Future<void> deleteUser() async{
      35           2 :     final userIdentifier = _clientInstanceIdToUserIdentifierBox.get(
      36           1 :         _clientInstanceKey
      37             :     );
      38           3 :     await _clientInstanceIdToUserIdentifierBox.delete(
      39           1 :         _clientInstanceKey
      40             :     );
      41           3 :     await _box.delete(userIdentifier);
      42             :   }
      43             : 
      44             :   @override
      45           1 :   Future<void> saveUser(ChatwootUser user) async{
      46           3 :     await _clientInstanceIdToUserIdentifierBox.put(
      47           1 :         _clientInstanceKey,
      48           2 :         user.identifier.toString()
      49             :     );
      50           4 :     await _box.put(user.identifier, user);
      51             :   }
      52             : 
      53           1 :   @override
      54             :   ChatwootUser? getUser(){
      55           4 :     if(_box.values.length==0){
      56             :       return null;
      57             :     }
      58           2 :     final userIdentifier = _clientInstanceIdToUserIdentifierBox.get(
      59           1 :         _clientInstanceKey
      60             :     );
      61             : 
      62           2 :     return _box.get(userIdentifier);
      63             :   }
      64             : 
      65             :   @override
      66           1 :   Future<void> onDispose() async{
      67           3 :     await _box.close();
      68             :   }
      69             : 
      70             :   @override
      71           1 :   Future<void> clearAll() async{
      72           3 :     await _box.clear();
      73           3 :     await _clientInstanceIdToUserIdentifierBox.clear();
      74             :   }
      75             : 
      76           2 :   static Future<void> openDB() async{
      77           8 :     await Hive.openBox<ChatwootUser>(ChatwootUserBoxNames.USERS.toString());
      78           8 :     await Hive.openBox<String>(ChatwootUserBoxNames.CLIENT_INSTANCE_TO_USER.toString());
      79             :   }
      80             : 
      81             : }
      82             : 
      83             : class NonPersistedChatwootUserDao extends ChatwootUserDao{
      84             :   ChatwootUser? _user;
      85             : 
      86             :   @override
      87           1 :   Future<void> deleteUser() async{
      88           1 :     _user = null;
      89             :   }
      90             : 
      91           1 :   @override
      92             :   ChatwootUser? getUser() {
      93           1 :     return _user;
      94             :   }
      95             : 
      96             :   @override
      97           1 :   Future<void> onDispose() async {
      98           1 :     _user = null;
      99             :   }
     100             : 
     101             :   @override
     102           1 :   Future<void> saveUser(ChatwootUser user) async{
     103           1 :     _user = user;
     104             :   }
     105             : 
     106             :   @override
     107           1 :   Future<void> clearAll() async{
     108           1 :     _user = null;
     109             :   }
     110             : 
     111             : }

Generated by: LCOV version 1.15.alpha0w