user_preferences.dart 4 KB
import '../user/user_models.dart';
import '../vip/vip_info.dart';

/// Local session snapshot.
class UserPreferences {
  const UserPreferences({
    this.meUserInfo,
    this.partnerUserInfo,
    this.accessToken = '',
    this.rongcloudToken = '',
    this.vipInfo,
    this.showRealtimeStress,
  });

  final UserInfoResponse? meUserInfo;
  final UserInfoResponse? partnerUserInfo;
  final String accessToken;
  final String rongcloudToken;
  final UserPreferencesVipInfo? vipInfo;

  /// `null` preserves the product default: enabled for members and disabled
  /// for non-members.
  final bool? showRealtimeStress;

  static const empty = UserPreferences();

  factory UserPreferences.fromJson(Map<String, dynamic> json) {
    return UserPreferences(
      meUserInfo: json['me_user_info'] == null
          ? null
          : UserInfoResponse.fromJson(
              json['me_user_info'] as Map<String, dynamic>),
      partnerUserInfo: json['partner_user_info'] == null
          ? null
          : UserInfoResponse.fromJson(
              json['partner_user_info'] as Map<String, dynamic>),
      accessToken: (json['access_token'] as String?) ?? '',
      rongcloudToken: (json['rongcloud_token'] as String?) ?? '',
      vipInfo: json['vip_info'] == null
          ? null
          : UserPreferencesVipInfo.fromJson(
              json['vip_info'] as Map<String, dynamic>),
      showRealtimeStress: json['show_realtime_stress'] as bool?,
    );
  }

  Map<String, dynamic> toJson() {
    final val = <String, dynamic>{};
    if (meUserInfo != null) val['me_user_info'] = meUserInfo!.toJson();
    if (partnerUserInfo != null) {
      val['partner_user_info'] = partnerUserInfo!.toJson();
    }
    val['access_token'] = accessToken;
    val['rongcloud_token'] = rongcloudToken;
    if (vipInfo != null) val['vip_info'] = vipInfo!.toJson();
    if (showRealtimeStress != null) {
      val['show_realtime_stress'] = showRealtimeStress;
    }
    return val;
  }

  UserPreferences copyWith({
    UserInfoResponse? meUserInfo,
    UserInfoResponse? partnerUserInfo,
    String? accessToken,
    String? rongcloudToken,
    UserPreferencesVipInfo? vipInfo,
    bool? showRealtimeStress,
    bool clearPartner = false,
  }) {
    return UserPreferences(
      meUserInfo: meUserInfo ?? this.meUserInfo,
      partnerUserInfo:
          clearPartner ? null : (partnerUserInfo ?? this.partnerUserInfo),
      accessToken: accessToken ?? this.accessToken,
      rongcloudToken: rongcloudToken ?? this.rongcloudToken,
      vipInfo: vipInfo ?? this.vipInfo,
      showRealtimeStress: showRealtimeStress ?? this.showRealtimeStress,
    );
  }
}

class UserPreferencesVipInfo {
  const UserPreferencesVipInfo({
    this.isVip = false,
    this.isForeverVip = false,
    this.isShare = false,
    this.vipStartDate = 0,
    this.vipEndDate = 0,
    this.vipType = 0,
  });

  final bool isVip;
  final bool isForeverVip;
  final bool isShare;
  final int vipStartDate;
  final int vipEndDate;
  final int vipType;

  factory UserPreferencesVipInfo.fromJson(Map<String, dynamic> json) {
    return UserPreferencesVipInfo(
      isVip: (json['is_vip'] as bool?) ?? false,
      isForeverVip: (json['is_forever_vip'] as bool?) ?? false,
      isShare: (json['is_share'] as bool?) ?? false,
      vipStartDate: (json['vip_start_date'] as num?)?.toInt() ?? 0,
      vipEndDate: (json['vip_end_date'] as num?)?.toInt() ?? 0,
      vipType: (json['vip_type'] as num?)?.toInt() ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'is_vip': isVip,
      'is_forever_vip': isForeverVip,
      'is_share': isShare,
      'vip_start_date': vipStartDate,
      'vip_end_date': vipEndDate,
      'vip_type': vipType,
    };
  }

  factory UserPreferencesVipInfo.fromVipInfo(VipInfo info) {
    return UserPreferencesVipInfo(
      isVip: info.isVip ?? false,
      isForeverVip: info.isForeverVip ?? false,
      isShare: info.isShare ?? false,
      vipStartDate: info.vipStartDate ?? 0,
      vipEndDate: info.vipEndDate ?? 0,
      vipType: info.vipType ?? 0,
    );
  }
}