Advertisement
amonnoris

storage_helper.dart

Oct 5th, 2024
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.09 KB | Source Code | 0 0
  1. // lib/utils/storage_helper.dart
  2.  
  3. import 'dart:convert';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import '../models/v2ray_config.dart';
  6. import '../models/connection_log.dart';
  7.  
  8. class StorageHelper {
  9.   static const String configsKey = 'configs';
  10.   static const String logsKey = 'connection_logs'; // Key for connection logs
  11.  
  12.   // --- VPN Configuration Methods ---
  13.  
  14.   static Future<void> saveConfig(V2RayConfig config) async {
  15.     final prefs = await SharedPreferences.getInstance();
  16.     List<String> configs = prefs.getStringList(configsKey) ?? [];
  17.     configs.add(jsonEncode(config.toJson()));
  18.     await prefs.setStringList(configsKey, configs);
  19.   }
  20.  
  21.   static Future<List<V2RayConfig>> loadConfigs() async {
  22.     final prefs = await SharedPreferences.getInstance();
  23.     List<String> configs = prefs.getStringList(configsKey) ?? [];
  24.     return configs.map((e) => V2RayConfig.fromJson(jsonDecode(e))).toList();
  25.   }
  26.  
  27.   static Future<void> deleteConfig(String name) async {
  28.     final prefs = await SharedPreferences.getInstance();
  29.     List<String> configs = prefs.getStringList(configsKey) ?? [];
  30.     configs.removeWhere((e) {
  31.       final config = V2RayConfig.fromJson(jsonDecode(e));
  32.       return config.name == name;
  33.     });
  34.     await prefs.setStringList(configsKey, configs);
  35.   }
  36.  
  37.   // --- Connection Log Methods ---
  38.  
  39.   // Save a new connection log
  40.   static Future<void> saveLog(ConnectionLog log) async {
  41.     final prefs = await SharedPreferences.getInstance();
  42.     List<String> logs = prefs.getStringList(logsKey) ?? [];
  43.     logs.add(jsonEncode(log.toJson()));
  44.     await prefs.setStringList(logsKey, logs);
  45.   }
  46.  
  47.   // Load all connection logs
  48.   static Future<List<ConnectionLog>> loadLogs() async {
  49.     final prefs = await SharedPreferences.getInstance();
  50.     List<String> logs = prefs.getStringList(logsKey) ?? [];
  51.     return logs.map((e) => ConnectionLog.fromJson(jsonDecode(e))).toList();
  52.   }
  53.  
  54.   // Clear all connection logs
  55.   static Future<void> clearLogs() async {
  56.     final prefs = await SharedPreferences.getInstance();
  57.     await prefs.remove(logsKey);
  58.   }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement