LCOV - code coverage report
Current view: top level - app\data\datasources\local\dts_user_pref.dart - dts_user_pref.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 12 12 100.0 %
Date: Wed Aug 6 23:43:55 2025 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:shared_preferences/shared_preferences.dart';
       2             : 
       3             : /// Singleton class for managing user preferences using `SharedPreferences`.
       4             : ///
       5             : /// The [UserPref] class provides convenient getters and setters to access and
       6             : /// modify user-specific settings, such as environment, favorites, and HTTP responses.
       7             : /// It uses `SharedPreferences` to persist data locally across app sessions.
       8             : ///
       9             : /// ### Singleton Pattern
      10             : /// - Only one instance of [UserPref] exists throughout the application.
      11             : /// - Use `UserPref()` to access the singleton instance.
      12             : ///
      13             : /// ### Initialization
      14             : /// - Call [initPrefs] before accessing any preferences to initialize the underlying storage.
      15             : ///
      16             : /// ### Getters & Setters
      17             : /// - [resHttp]: Gets or sets the list of HTTP responses as strings.
      18             : /// - [environment]: Gets or sets the current environment (default: 'dev').
      19             : /// - [favorites]: Gets or sets the list of favorite items as strings.
      20             : ///
      21             : /// ### Example
      22             : /// ```dart
      23             : /// void main() async {
      24             : ///   WidgetsFlutterBinding.ensureInitialized();
      25             : ///   final userPrefs = UserPref();
      26             : ///   await userPrefs.initPrefs();
      27             : ///
      28             : ///   userPrefs.environment = 'production';
      29             : ///   print(userPrefs.environment); // Output: production
      30             : ///
      31             : ///   userPrefs.favorites = ['item1', 'item2'];
      32             : ///   print(userPrefs.favorites); // Output: [item1, item2]
      33             : ///
      34             : ///   userPrefs.resHttp = ['response1', 'response2'];
      35             : ///   print(userPrefs.resHttp); // Output: [response1, response2]
      36             : /// }
      37             : /// ```
      38             : class UserPref {
      39             :   /// Private constructor to prevent instantiation from outside (singleton).
      40           5 :   UserPref._internal();
      41             : 
      42             :   /// Singleton instance of [UserPref].
      43          15 :   static final _instancia = UserPref._internal();
      44             : 
      45             :   /// Returns the singleton instance of [UserPref].
      46          10 :   factory UserPref() => _instancia;
      47             : 
      48             :   /// Underlying [SharedPreferences] instance.
      49             :   late SharedPreferences _userPref;
      50             : 
      51             :   /// Initializes the shared preferences instance.
      52             :   ///
      53             :   /// Must be called before accessing any preferences.
      54           4 :   Future<void> initPrefs() async {
      55           8 :     _userPref = await SharedPreferences.getInstance();
      56             :   }
      57             : 
      58             :   /// Gets the list of HTTP responses stored as strings.
      59           8 :   List<String> get resHttp => _userPref.getStringList('resHttp') ?? [];
      60             : 
      61             :   /// Sets the list of HTTP responses.
      62           6 :   set resHttp(List<String> value) => _userPref.setStringList('resHttp', value);
      63             : 
      64             :   /// Gets the current environment setting (default: 'dev').
      65           3 :   String get environment => _userPref.getString('environment') ?? 'dev';
      66             : 
      67             :   /// Sets the current environment.
      68           3 :   set environment(String value) => _userPref.setString('environment', value);
      69             : 
      70             :   /// Gets the list of favorite items stored as strings.
      71           7 :   List<String> get favorites => _userPref.getStringList('favorites') ?? [];
      72             : 
      73             :   /// Sets the list of favorite items.
      74           2 :   set favorites(List<String> value) =>
      75           4 :       _userPref.setStringList('favorites', value);
      76             : }

Generated by: LCOV version 1.15.alpha0w