local_storage.dart 3.04 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);
  }

  // ─── App Review Prompt Storage ──────────────────────────────────────────────

  DateTime? get appReviewPromptNextShowAt {
    final value = sharedPreferences.getInt(
      StorageConst.appReviewPromptNextShowAtKey,
    );
    if (value == null || value <= 0) return null;
    return DateTime.fromMillisecondsSinceEpoch(value);
  }

  Future<void> setAppReviewPromptNextShowAt(DateTime value) async {
    await sharedPreferences.setInt(
      StorageConst.appReviewPromptNextShowAtKey,
      value.millisecondsSinceEpoch,
    );
  }

  // ─── Home Membership Offer Prompt Storage ─────────────────────────────────

  int? get homeMembershipOfferPromptDate =>
      sharedPreferences.getInt(StorageConst.homeMembershipOfferPromptDateKey);

  int? get homeMembershipOfferPromptUserId =>
      sharedPreferences.getInt(StorageConst.homeMembershipOfferPromptUserIdKey);

  Future<void> setHomeMembershipOfferPromptState({
    required int date,
    required int userId,
  }) async {
    await sharedPreferences.setInt(
      StorageConst.homeMembershipOfferPromptDateKey,
      date,
    );
    await sharedPreferences.setInt(
      StorageConst.homeMembershipOfferPromptUserIdKey,
      userId,
    );
  }
}