import 'package:shared_preferences/shared_preferences.dart';

class StorageService {
  StorageService._internal();
  static final StorageService instance = StorageService._internal();

  late final SharedPreferences _prefs;

  /// 启动时调用一次，确保 prefs 已就绪
  Future<void> init() async {
    _prefs = await SharedPreferences.getInstance();
  }

  /// 存储 String
  Future<bool> setString(String key, String value) =>
      _prefs.setString(key, value);

  /// 读取 String
  String? getString(String key) => _prefs.getString(key);

  /// 存储 String 列表
  Future<bool> setStringList(String key, List<String> value) =>
      _prefs.setStringList(key, value);

  /// 读取 String 列表
  List<String>? getStringList(String key) => _prefs.getStringList(key);

  /// 存储 int
  Future<bool> setInt(String key, int value) => _prefs.setInt(key, value);

  /// 读取 int
  int? getInt(String key) => _prefs.getInt(key);

  /// 存储 bool
  Future<bool> setBool(String key, bool value) => _prefs.setBool(key, value);

  /// 读取 bool
  bool? getBool(String key) => _prefs.getBool(key);

  /// 存储 double
  Future<bool> setDouble(String key, double value) =>
      _prefs.setDouble(key, value);

  /// 读取 double
  double? getDouble(String key) => _prefs.getDouble(key);

  /// 删除单个 key
  Future<bool> remove(String key) => _prefs.remove(key);

  /// 清空所有
  Future<bool> clear() => _prefs.clear();
}
