user_preferences.dart 2.47 KB
import 'package:json_annotation/json_annotation.dart';

import '../user/user_models.dart';
import '../vip/vip_info.dart';

part 'user_preferences.g.dart';

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

  @JsonKey(name: 'me_user_info')
  final UserInfoResponse? meUserInfo;
  @JsonKey(name: 'partner_user_info')
  final UserInfoResponse? partnerUserInfo;
  @JsonKey(name: 'access_token')
  final String accessToken;
  @JsonKey(name: 'rongcloud_token')
  final String rongcloudToken;
  @JsonKey(name: 'vip_info')
  final UserPreferencesVipInfo? vipInfo;

  static const empty = UserPreferences();

  factory UserPreferences.fromJson(Map<String, dynamic> json) =>
      _$UserPreferencesFromJson(json);
  Map<String, dynamic> toJson() => _$UserPreferencesToJson(this);

  UserPreferences copyWith({
    UserInfoResponse? meUserInfo,
    UserInfoResponse? partnerUserInfo,
    String? accessToken,
    String? rongcloudToken,
    UserPreferencesVipInfo? vipInfo,
    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,
    );
  }
}

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

  @JsonKey(name: 'is_vip')
  final bool isVip;
  @JsonKey(name: 'is_forever_vip')
  final bool isForeverVip;
  @JsonKey(name: 'is_share')
  final bool isShare;
  @JsonKey(name: 'vip_start_date')
  final int vipStartDate;
  @JsonKey(name: 'vip_end_date')
  final int vipEndDate;

  factory UserPreferencesVipInfo.fromJson(Map<String, dynamic> json) =>
      _$UserPreferencesVipInfoFromJson(json);
  Map<String, dynamic> toJson() => _$UserPreferencesVipInfoToJson(this);

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