2024-06-30 20:25:36 +02:00
|
|
|
// SPDX-FileCopyrightText: (C) 2024 Matyáš Caras
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-09-08 11:50:21 +02:00
|
|
|
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 {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Represents a category in a user's wallet
|
|
|
|
WalletCategory({
|
|
|
|
required this.name,
|
|
|
|
required this.id,
|
|
|
|
required this.icon,
|
2024-01-08 15:38:31 +01:00
|
|
|
required this.color,
|
2023-12-29 21:39:54 +01:00
|
|
|
});
|
2023-09-08 11:50:21 +02:00
|
|
|
|
2024-07-17 16:44:02 +02:00
|
|
|
/// Generates a class instance from a Map
|
2023-09-08 11:50:21 +02:00
|
|
|
factory WalletCategory.fromJson(Map<String, dynamic> json) =>
|
|
|
|
_$WalletCategoryFromJson(json);
|
|
|
|
|
2024-07-17 17:37:41 +02:00
|
|
|
/// Default [WalletCategory] instance for json_serializable
|
|
|
|
factory WalletCategory.unknown() => WalletCategory(
|
|
|
|
name: "Unknown",
|
|
|
|
id: -1,
|
|
|
|
icon: Icons.question_mark,
|
|
|
|
color: Colors.green,
|
|
|
|
);
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// User-defined name
|
2024-07-17 17:37:41 +02:00
|
|
|
@JsonKey(defaultValue: "Unknown")
|
2023-12-29 21:39:54 +01:00
|
|
|
String name;
|
|
|
|
|
|
|
|
/// Unique identificator of the category
|
2024-07-17 17:37:41 +02:00
|
|
|
@JsonKey(required: true, disallowNullValue: true)
|
2023-12-29 21:39:54 +01:00
|
|
|
final int id;
|
|
|
|
|
|
|
|
/// Selected Icon for the category
|
2024-07-17 17:37:41 +02:00
|
|
|
@JsonKey(
|
|
|
|
fromJson: _iconDataFromJson,
|
|
|
|
toJson: _iconDataToJson,
|
|
|
|
defaultValue: _defaultIcon,
|
|
|
|
)
|
2023-12-29 21:39:54 +01:00
|
|
|
IconData icon;
|
|
|
|
|
2024-01-08 15:38:31 +01:00
|
|
|
/// The color that will be displayed with entry
|
2024-07-17 17:37:41 +02:00
|
|
|
@JsonKey(
|
|
|
|
fromJson: _colorFromJson,
|
|
|
|
toJson: _colorToJson,
|
|
|
|
defaultValue: _defaultColor,
|
|
|
|
)
|
2024-01-08 15:38:31 +01:00
|
|
|
Color color;
|
|
|
|
|
2024-07-17 16:44:02 +02:00
|
|
|
/// Converts the data in this instance into a Map
|
2023-09-08 11:50:21 +02:00
|
|
|
Map<String, dynamic> toJson() => _$WalletCategoryToJson(this);
|
2023-09-14 19:44:34 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return name;
|
|
|
|
}
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> _iconDataToJson(IconData icon) =>
|
|
|
|
{'codepoint': icon.codePoint, 'family': icon.fontFamily};
|
2023-12-29 21:39:54 +01:00
|
|
|
|
2023-09-08 11:50:21 +02:00
|
|
|
IconData _iconDataFromJson(Map<String, dynamic> data) =>
|
2023-12-29 21:39:54 +01:00
|
|
|
IconData(data['codepoint'] as int, fontFamily: data['family'] as String?);
|
2023-09-08 11:50:21 +02:00
|
|
|
|
2024-01-08 15:38:31 +01:00
|
|
|
int _colorToJson(Color color) => color.value;
|
|
|
|
Color _colorFromJson(int input) => Color(input);
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Type of entry, either expense or income
|
|
|
|
enum EntryType {
|
|
|
|
/// Expense
|
|
|
|
expense,
|
|
|
|
|
|
|
|
/// Income
|
|
|
|
income
|
|
|
|
}
|
2024-07-17 17:37:41 +02:00
|
|
|
|
|
|
|
IconData _defaultIcon() => Icons.question_mark;
|
|
|
|
Color _defaultColor() => Colors.green;
|