interaction_models.dart 5.45 KB
import 'package:flutter/foundation.dart';

/// Backend interaction scenes. Keep API values in one place rather than
/// duplicating integer literals across report cards.
enum FriendInteractionType {
  defaultInteraction(100),
  todayAverageHrv(101),
  todayRestingHeartRate(102),
  todaySleepStatus(103),
  todayFitnessStatus(104),
  weekStressTrend(105),
  monthStressTrend(106),
  yearStressTrend(107),
  dailyActivityBurn(108),
  weeklyActivityBurn(109),
  monthlyActivityBurn(110),
  dailySleepStatus(111),
  weeklySleepTime(112),
  monthlySleepTime(113),
  pkSteps(114),
  pkCalories(115),
  pkStandCount(116),
  pkMonthlyResult(117);

  const FriendInteractionType(this.value);
  final int value;
}

enum FriendInteractionActionType {
  poke(0),
  miss(1),
  punch(2);

  const FriendInteractionActionType(this.value);
  final int value;
}

/// All information required to render and send a friend interaction.
@immutable
class FriendInteractionContext {
  const FriendInteractionContext({
    required this.friendUserId,
    required this.type,
    this.avatarUrl,
  });

  final int friendUserId;
  final FriendInteractionType type;
  final String? avatarUrl;
}

class InteractionData {
  const InteractionData({
    required this.friendUserId,
    required this.interactionType,
    required this.actionType,
    this.action,
  });

  /// Target of every V2 action and records request.
  final int friendUserId;
  final int interactionType;
  final int actionType;
  final InteractionDataAction? action;

  factory InteractionData.fromJson(Map<String, dynamic> json) {
    return InteractionData(
      friendUserId: json['friend_user_id'] as int,
      interactionType: json['interaction_type'] as int,
      actionType: json['action_type'] as int,
      action: json['action_variables'] == null
          ? null
          : InteractionDataAction.fromJson(
              json['action_variables'] as Map<String, dynamic>,
            ),
    );
  }

  Map<String, dynamic> toJson() {
    final val = <String, dynamic>{
      'friend_user_id': friendUserId,
      'interaction_type': interactionType,
      'action_type': actionType,
    };
    if (action != null) val['action_variables'] = action!.toJson();
    return val;
  }
}

class InteractionRecordResponse {
  const InteractionRecordResponse({this.records});

  final List<InteractionRecord>? records;

  factory InteractionRecordResponse.fromJson(Map<String, dynamic> json) {
    return InteractionRecordResponse(
      records: (json['records'] as List<dynamic>?)
          ?.map((e) => InteractionRecord.fromJson(e as Map<String, dynamic>))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    final val = <String, dynamic>{};
    if (records != null) {
      val['records'] = records!.map((e) => e.toJson()).toList();
    }
    return val;
  }
}

class InteractionRecord {
  const InteractionRecord({
    this.id,
    this.createTime,
    this.userId,
    this.pairId,
    this.interactionType,
    this.actionType,
    this.action,
    this.text,
  });

  final int? id;
  final int? createTime;
  final int? userId;
  final int? pairId;
  final int? interactionType;
  final int? actionType;
  final InteractionDataAction? action;
  final String? text;

  factory InteractionRecord.fromJson(Map<String, dynamic> json) {
    return InteractionRecord(
      id: json['id'] as int?,
      createTime: json['create_time'] as int?,
      userId: json['user_id'] as int?,
      pairId: json['pair_id'] as int?,
      interactionType: json['interaction_type'] as int?,
      actionType: json['action_type'] as int?,
      action: json['action_variables'] == null
          ? null
          : InteractionDataAction.fromJson(
              json['action_variables'] as Map<String, dynamic>,
            ),
      text: json['text'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    final val = <String, dynamic>{};
    if (id != null) val['id'] = id;
    if (createTime != null) val['create_time'] = createTime;
    if (userId != null) val['user_id'] = userId;
    if (pairId != null) val['pair_id'] = pairId;
    if (interactionType != null) val['interaction_type'] = interactionType;
    if (actionType != null) val['action_type'] = actionType;
    if (action != null) val['action_variables'] = action!.toJson();
    if (text != null) val['text'] = text;
    return val;
  }
}

class InteractionDataAction {
  const InteractionDataAction({
    this.objectTarget,
    this.status,
    this.type,
    this.date,
    this.value,
    this.unit,
    this.action,
  });

  final int? objectTarget;
  final String? status;
  final String? type;
  final String? date;
  final String? value;
  final String? unit;
  final String? action;

  factory InteractionDataAction.fromJson(Map<String, dynamic> json) {
    return InteractionDataAction(
      objectTarget: json['object'] as int?,
      status: json['data_status'] as String?,
      type: json['data_type'] as String?,
      date: json['data_date'] as String?,
      value: json['data_value'] as String?,
      unit: json['data_unit'] as String?,
      action: json['action'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    final val = <String, dynamic>{};
    if (objectTarget != null) val['object'] = objectTarget;
    if (status != null) val['data_status'] = status;
    if (type != null) val['data_type'] = type;
    if (date != null) val['data_date'] = date;
    if (value != null) val['data_value'] = value;
    if (unit != null) val['data_unit'] = unit;
    if (action != null) val['action'] = action;
    return val;
  }
}