import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// 共享(内置)存储服务 - (建议仅用于纯Web环境)
class SharedStorageService extends GetxService {
  SharedStorageService._();

  ///获取当前实例的静态属性
  static SharedStorageService get ins => Get.find();
  static late final SharedPreferences _prefs;

  ///共享存储服务器初始化
  static Future<SharedStorageService> init() async {
    _prefs = await SharedPreferences.getInstance();
    return SharedStorageService._();
  }

  ///保存字符串
  Future<bool> setString(String key, String value) async {
    return await _prefs.setString(key, value);
  }

  ///保存布尔值
  Future<bool> setBool(String key, bool value) async {
    return await _prefs.setBool(key, value);
  }

  ///保存集合
  Future<bool> setList(String key, List<String> value) async {
    return await _prefs.setStringList(key, value);
  }

  ///获取缓存的字符串
  String? getString(String key) {
    return _prefs.getString(key);
  }

  ///获取缓存的布尔值
  bool? getBool(String key) {
    return _prefs.getBool(key);
  }

  ///获取缓存的集合
  List<String> getList(String key) {
    return _prefs.getStringList(key) ?? [];
  }

  ///删除缓存
  Future<bool> remove(String key) async {
    return await _prefs.remove(key);
  }
}
