local_storage.dart 1.69 KB
import 'package:shared_preferences/shared_preferences.dart';

import '../../core/config/app_environment.dart';
import '../../core/constants/storage_const.dart';

/// Opens local persistence accessors used during bootstrap.
class LocalStorage {
  LocalStorage._(this.sharedPreferences);

  final SharedPreferences sharedPreferences;

  static Future<LocalStorage> open() async {
    final prefs = await SharedPreferences.getInstance();
    return LocalStorage._(prefs);
  }

  // ─── Environment Configuration Storage ──────────────────────────────────────

  Future<AppEnvironment> readEnvironment() async {
    final ordinal = sharedPreferences.getInt(StorageConst.appEnvironmentKey);
    if (ordinal == null) {
      return AppEnvironment.release;
    }
    return AppEnvironment.fromOrdinal(ordinal);
  }

  Future<void> writeEnvironment(AppEnvironment environment) async {
    await sharedPreferences.setInt(
      StorageConst.appEnvironmentKey,
      environment.index,
    );
  }

  // ─── App Settings Storage ──────────────────────────────────────────────────

  bool get termsAgreed =>
      sharedPreferences.getBool(StorageConst.termsAgreedKey) ?? false;

  Future<void> setTermsAgreed(bool value) async {
    await sharedPreferences.setBool(StorageConst.termsAgreedKey, value);
  }

  String get lastLoginMethod =>
      sharedPreferences.getString(StorageConst.lastLoginMethodKey) ?? '';

  Future<void> setLastLoginMethod(String value) async {
    await sharedPreferences.setString(StorageConst.lastLoginMethodKey, value);
  }
}