local_storage.dart 4.34 KB
import 'package:doublefeel_flutter/core/config/app_environment_config.dart';
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,
    );
  }

  Future<AppRegion?> readRegion() async {
    final ordinal = sharedPreferences.getInt(StorageConst.appRegionKey);
    if (ordinal == null) {
      return null;
    }
    return AppRegion.values[ordinal];
  }

  Future<void> writeRegion(AppRegion region) async {
    await sharedPreferences.setInt(StorageConst.appRegionKey, region.index);
  }

  bool get hideChooseRegion =>
      sharedPreferences.getBool(StorageConst.hideChooseRegionKey) ?? false;

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

  // ─── 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? appReviewPromptNextShowAt(int userId) {
    final value = sharedPreferences.getInt(
      _appReviewPromptNextShowAtKey(userId),
    );
    if (value == null || value <= 0) return null;
    return DateTime.fromMillisecondsSinceEpoch(value);
  }

  Future<void> setAppReviewPromptNextShowAt(int userId, DateTime value) async {
    await sharedPreferences.setInt(
      _appReviewPromptNextShowAtKey(userId),
      value.millisecondsSinceEpoch,
    );
  }

  bool hasAppReviewPromptPending(int userId) {
    return sharedPreferences.getBool(_appReviewPromptPendingKey(userId)) ??
        false;
  }

  Future<void> setAppReviewPromptPending(int userId) async {
    await sharedPreferences.setBool(_appReviewPromptPendingKey(userId), true);
  }

  Future<void> clearAppReviewPromptPending(int userId) async {
    await sharedPreferences.remove(_appReviewPromptPendingKey(userId));
  }

  String _appReviewPromptNextShowAtKey(int userId) =>
      '${StorageConst.appReviewPromptNextShowAtKey}_$userId';

  String _appReviewPromptPendingKey(int userId) =>
      '${StorageConst.appReviewPromptPendingKey}_$userId';

  // ─── 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,
    );
  }
}