LCOV - code coverage report
Current view: top level - core/services - local_storage_service.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 10 22 45.5 %
Date: 2026-03-02 18:37:46 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:convert';
       2             : 
       3             : import 'package:flutter/foundation.dart';
       4             : import 'package:shared_preferences/shared_preferences.dart';
       5             : 
       6             : abstract class JsonConvertible {
       7             :   Map<String, dynamic> toJson();
       8             : }
       9             : 
      10             : typedef FromJsonFactory<T> = T Function(Map<String, dynamic> json);
      11             : 
      12             : class LocalStorageService {
      13             :   static SharedPreferences? _preferences;
      14             : 
      15           1 :   static Future<void> init() async {
      16             :     if (_preferences != null) return;
      17           1 :     _preferences = await SharedPreferences.getInstance();
      18             :   }
      19             : 
      20           0 :   Future<bool> setString(String key, String value) async {
      21           0 :     return _preferences!.setString(key, value);
      22             :   }
      23             : 
      24           0 :   String? getString(String key) {
      25           0 :     return _preferences!.getString(key);
      26             :   }
      27             : 
      28           0 :   Future<bool> remove(String key) async {
      29           0 :     return _preferences!.remove(key);
      30             :   }
      31             : 
      32           0 :   Future<bool> setJsonList<T extends JsonConvertible>(
      33             :     String key,
      34             :     List<T> list,
      35             :   ) async {
      36           0 :     await init();
      37             :     final List<String> jsonStringList = list
      38           0 :         .map((item) => jsonEncode(item.toJson()))
      39           0 :         .toList();
      40           0 :     return _preferences!.setStringList(key, jsonStringList);
      41             :   }
      42             : 
      43           1 :   Future<List<T>> getJsonList<T extends JsonConvertible>(
      44             :     String key,
      45             :     FromJsonFactory<T> fromJson,
      46             :   ) async {
      47           1 :     await init();
      48           1 :     final List<String>? jsonStringList = _preferences!.getStringList(key);
      49             : 
      50             :     if (jsonStringList == null) {
      51           0 :       return [];
      52             :     }
      53             : 
      54           1 :     final List<dynamic> jsonDynamicList = await compute(
      55             :       _decodeJsonList,
      56             :       jsonStringList,
      57             :     );
      58             : 
      59             :     return jsonDynamicList
      60           3 :         .map((jsonMap) => fromJson(jsonMap as Map<String, dynamic>))
      61           1 :         .toList();
      62             :   }
      63             : 
      64           1 :   static List<dynamic> _decodeJsonList(List<String> jsonStringList) {
      65           4 :     return jsonStringList.map((jsonString) => jsonDecode(jsonString)).toList();
      66             :   }
      67             : }

Generated by: LCOV version 1.14