watch_theme_models.dart 8.97 KB
import 'dart:ui';

import 'package:doublefeel_flutter/app/modules/watch_theme/widgets/watch_theme_colors.dart';

class WatchThemeItem {
  const WatchThemeItem({
    this.id,
    this.createTime,
    this.updateTime,
    this.userId,
    this.status,
    this.isCustomTheme = false,
    this.isDefaultCharacter = false,
    required this.title,
    required this.infoList,
  });

  final int? id;
  final int? createTime;
  final int? updateTime;
  final int? userId;
  final int? status;
  final bool isDefaultCharacter;
  final bool isCustomTheme;
  final String title;
  final List<WatchThemeItemInfo> infoList;

  factory WatchThemeItem.fromJson(Map<String, dynamic> json) {
    final isDefaultTheme = json['is_default_theme'] == true;
    final userId = json['user_id'] as int?;
    final positiveDescription = json['positive_description'] as String?;
    final normalDescription = json['normal_description'] as String?;
    final negativeDescription = json['negative_description'] as String?;
    final positiveImage = json['positive_image'] as String?;
    final normalImage = json['normal_image'] as String?;
    final negativeImage = json['negative_image'] as String?;

    return WatchThemeItem(
      id: json['id'] as int?,
      createTime: json['create_time'] as int?,
      updateTime: json['update_time'] as int?,
      userId: userId,
      status: json['status'] as int?,
      isDefaultCharacter: isDefaultTheme,
      isCustomTheme: userId != null,
      title: (json['theme_name'] as String?) ?? '自定义主题',
      infoList: [
        WatchThemeItemInfo(
          title: positiveDescription ?? '状态优秀',
          imgUrl: positiveImage,
        ),
        WatchThemeItemInfo(
          title: normalDescription ?? '状态正常',
          imgUrl: normalImage,
        ),
        WatchThemeItemInfo(
          title: negativeDescription ?? '注意压力',
          imgUrl: negativeImage,
        ),
        WatchThemeItemInfo(
          title: negativeDescription ?? '压力过载',
          imgUrl: negativeImage,
        ),
      ],
    );
  }

  Map<String, dynamic> toJson() {
    final excellent = infoList.elementAtOrNull(0);
    final normal = infoList.elementAtOrNull(1);
    final overload = infoList.elementAtOrNull(3) ?? infoList.elementAtOrNull(2);
    final json = <String, dynamic>{};
    if (id != null) json['id'] = id;
    if (createTime != null) json['create_time'] = createTime;
    if (updateTime != null) json['update_time'] = updateTime;
    if (userId != null) json['user_id'] = userId;
    if (status != null) json['status'] = status;
    json['theme_name'] = title;
    json['is_default_theme'] = isDefaultCharacter;
    json['positive_description'] = excellent?.title;
    json['positive_image'] = excellent?.image;
    json['normal_description'] = normal?.title;
    json['normal_image'] = normal?.image;
    json['negative_description'] = overload?.title;
    json['negative_image'] = overload?.image;
    return json;
  }

  static WatchThemeItem empty() {
    return WatchThemeItem(
      title: '自定义主题',
      infoList: [],
    );
  }
}

class WatchThemeItemInfo {
  final String title;
  final String? imgUrl;
  final String? assetPath;

  WatchThemeItemInfo({required this.title, this.imgUrl, this.assetPath});

  String? get image => imgUrl ?? assetPath;
}

class WatchThemeResponse {
  const WatchThemeResponse({this.themes = const []});

  final List<WatchThemeItem> themes;

  factory WatchThemeResponse.fromJson(Map<String, dynamic> json) {
    final rawThemes = json['themes'];
    return WatchThemeResponse(
      themes: rawThemes is List
          ? rawThemes
              .whereType<Map>()
              .map((theme) => WatchThemeItem.fromJson(
                    Map<String, dynamic>.from(theme),
                  ))
              .toList()
          : const [],
    );
  }
}

class WatchThemeUpsertRequest {
  const WatchThemeUpsertRequest({
    this.themeId,
    required this.name,
    required this.fullOfEnergyName,
    required this.normalName,
    required this.overpressureName,
    required this.fullOfEnergyImage,
    required this.normalImage,
    required this.overpressureImage,
  });

  final int? themeId;
  final String name;
  final String fullOfEnergyName;
  final String normalName;
  final String overpressureName;
  final String fullOfEnergyImage;
  final String normalImage;
  final String overpressureImage;

  Map<String, dynamic> toJson() {
    return {
      if (themeId != null) 'theme_id': themeId,
      'theme_name': name,
      'positive_description': fullOfEnergyName,
      'positive_image': fullOfEnergyImage,
      'normal_description': normalName,
      'normal_image': normalImage,
      'negative_description': overpressureName,
      'negative_image': overpressureImage,
    };
  }
}

final officialThemes = <WatchThemeItem>[
  WatchThemeItem(
    title: '默认主题',
    isDefaultCharacter: true,
    infoList: [
      WatchThemeItemInfo(
        title: '状态优秀',
        assetPath: 'assets/images/watch_theme/official_default_green.png',
      ),
      WatchThemeItemInfo(
        title: '状态正常',
        assetPath: 'assets/images/watch_theme/official_default_blue.png',
      ),
      WatchThemeItemInfo(
        title: '注意压力',
        assetPath: 'assets/images/watch_theme/official_default_orange.png',
      ),
      WatchThemeItemInfo(
        title: '压力过载',
        assetPath: 'assets/images/watch_theme/official_default_red.png',
      ),
    ],
  ),
  WatchThemeItem(
    title: '垂耳粉兔',
    infoList: [
      WatchThemeItemInfo(
        title: '状态优秀',
        assetPath: 'assets/images/watch_theme/official_rabbit_pink.png',
      ),
      WatchThemeItemInfo(
        title: '状态正常',
        assetPath: 'assets/images/watch_theme/official_rabbit_pink.png',
      ),
      WatchThemeItemInfo(
        title: '注意压力',
        assetPath: 'assets/images/watch_theme/official_rabbit_pink.png',
      ),
      WatchThemeItemInfo(
        title: '压力过载',
        assetPath: 'assets/images/watch_theme/official_rabbit_pink.png',
      ),
    ],
  ),
  WatchThemeItem(
    title: '蓝象哥哥',
    infoList: [
      WatchThemeItemInfo(
        title: '状态优秀',
        assetPath: 'assets/images/watch_theme/official_elephant_blue.png',
      ),
      WatchThemeItemInfo(
        title: '状态正常',
        assetPath: 'assets/images/watch_theme/official_elephant_blue.png',
      ),
      WatchThemeItemInfo(
        title: '注意压力',
        assetPath: 'assets/images/watch_theme/official_elephant_blue.png',
      ),
      WatchThemeItemInfo(
        title: '压力过载',
        assetPath: 'assets/images/watch_theme/official_elephant_blue.png',
      ),
    ],
  ),
  WatchThemeItem(
    title: '小狗白白',
    infoList: [
      WatchThemeItemInfo(
        title: '状态优秀',
        assetPath: 'assets/images/watch_theme/official_dog_white.png',
      ),
      WatchThemeItemInfo(
        title: '状态正常',
        assetPath: 'assets/images/watch_theme/official_dog_white.png',
      ),
      WatchThemeItemInfo(
        title: '注意压力',
        assetPath: 'assets/images/watch_theme/official_dog_white.png',
      ),
      WatchThemeItemInfo(
        title: '压力过载',
        assetPath: 'assets/images/watch_theme/official_dog_white.png',
      ),
    ],
  ),
  WatchThemeItem(
    title: '猫猫狗狗',
    infoList: [
      WatchThemeItemInfo(
        title: '状态优秀',
        assetPath: 'assets/images/watch_theme/custom_cat_dog.png',
      ),
      WatchThemeItemInfo(
        title: '状态正常',
        assetPath: 'assets/images/watch_theme/custom_cat_dog.png',
      ),
      WatchThemeItemInfo(
        title: '注意压力',
        assetPath: 'assets/images/watch_theme/custom_cat_dog.png',
      ),
      WatchThemeItemInfo(
        title: '压力过载',
        assetPath: 'assets/images/watch_theme/custom_cat_dog.png',
      ),
    ],
  ),
];

final customThemes = <WatchThemeItem>[
  WatchThemeItem(
    title: '猫猫狗狗-AAA',
    infoList: [
      WatchThemeItemInfo(
        title: '状态优秀',
        assetPath: 'assets/images/watch_theme/custom_cat_dog.png',
      ),
      WatchThemeItemInfo(
        title: '状态正常',
        assetPath: 'assets/images/watch_theme/custom_cat_dog.png',
      ),
      WatchThemeItemInfo(
        title: '注意压力',
        assetPath: 'assets/images/watch_theme/custom_cat_dog.png',
      ),
      WatchThemeItemInfo(
        title: '压力过载',
        assetPath: 'assets/images/watch_theme/custom_cat_dog.png',
      ),
    ],
  ),
];

final defaultThemeTemplates = [
  WatchThemeTemplateItem(title: '状态优秀', color: WatchThemeColors.excellent),
  WatchThemeTemplateItem(title: '状态正常', color: WatchThemeColors.normal),
  WatchThemeTemplateItem(title: '注意压力', color: WatchThemeColors.stress),
  WatchThemeTemplateItem(title: '压力过载', color: WatchThemeColors.overload),
];

class WatchThemeTemplateItem {
  final String title;
  final Color color;

  WatchThemeTemplateItem({required this.title, required this.color});
}