prasule/lib/api/category.dart

85 lines
2.1 KiB
Dart

// SPDX-FileCopyrightText: (C) 2024 Matyáš Caras
//
// SPDX-License-Identifier: AGPL-3.0-only
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
part 'category.g.dart';
@JsonSerializable()
/// Represents a category in a user's wallet
class WalletCategory {
/// Represents a category in a user's wallet
WalletCategory({
required this.name,
required this.id,
required this.icon,
required this.color,
});
/// Generates a class instance from a Map
factory WalletCategory.fromJson(Map<String, dynamic> json) =>
_$WalletCategoryFromJson(json);
/// Default [WalletCategory] instance for json_serializable
factory WalletCategory.unknown() => WalletCategory(
name: "Unknown",
id: -1,
icon: Icons.question_mark,
color: Colors.green,
);
/// User-defined name
@JsonKey(defaultValue: "Unknown")
String name;
/// Unique identificator of the category
@JsonKey(required: true, disallowNullValue: true)
final int id;
/// Selected Icon for the category
@JsonKey(
fromJson: _iconDataFromJson,
toJson: _iconDataToJson,
defaultValue: _defaultIcon,
)
IconData icon;
/// The color that will be displayed with entry
@JsonKey(
fromJson: _colorFromJson,
toJson: _colorToJson,
defaultValue: _defaultColor,
)
Color color;
/// Converts the data in this instance into a Map
Map<String, dynamic> toJson() => _$WalletCategoryToJson(this);
@override
String toString() {
return name;
}
}
Map<String, dynamic> _iconDataToJson(IconData icon) =>
{'codepoint': icon.codePoint, 'family': icon.fontFamily};
IconData _iconDataFromJson(Map<String, dynamic> data) =>
IconData(data['codepoint'] as int, fontFamily: data['family'] as String?);
int _colorToJson(Color color) => color.value;
Color _colorFromJson(int input) => Color(input);
/// Type of entry, either expense or income
enum EntryType {
/// Expense
expense,
/// Income
income
}
IconData _defaultIcon() => Icons.question_mark;
Color _defaultColor() => Colors.green;