Refactored json storage

This commit is contained in:
2024-02-17 14:16:07 +01:00
parent 1a7f28703a
commit b2da8436e4
29 changed files with 389 additions and 235 deletions

View File

@@ -0,0 +1,31 @@
import 'dart:ui';
class Account {
String label;
String color;
bool saving;
Account({
this.label = '',
this.color = '',
this.saving = false,
});
factory Account.fromJson(Map<String, dynamic> json) {
return Account(
label: json['label'],
color: json['color'],
saving: bool.parse(json['saving']),
);
}
Map<String, String> toJson() => {
'label': label,
'color': color,
'saving': saving.toString(),
};
Color rgbToColor() {
return Color(int.parse(color.toUpperCase().replaceAll("#", ""), radix: 16));
}
}

View File

@@ -0,0 +1,21 @@
class Budget {
bool monthly;
double value;
Budget({
this.monthly = false,
this.value = 0.0,
});
factory Budget.fromJson(Map<String, dynamic> json) {
return Budget(
monthly: json['monthly'],
value: double.parse(json['value']),
);
}
Map<String, String> toJson() => {
'monthly': monthly.toString(),
'value': value.toString(),
};
}

View File

@@ -0,0 +1,35 @@
import 'dart:ui';
class Category {
String label;
String color;
bool essential;
bool saving;
Category({
this.label = '',
this.color = '',
this.essential = false,
this.saving = false,
});
factory Category.fromJson(Map<String, dynamic> json) {
return Category(
label: json['label'],
color: json['color'],
essential: bool.parse(json['essential']),
saving: bool.parse(json['saving']),
);
}
Map<String, String> toJson() => {
'label': label,
'color': color,
'essential': essential.toString(),
'saving': saving.toString(),
};
Color rgbToColor() {
return Color(int.parse(color.toUpperCase().replaceAll("#", ""), radix: 16));
}
}

View File

@@ -0,0 +1,39 @@
import 'package:tunas/repositories/metadata/models/budget.dart';
import 'package:tunas/repositories/metadata/models/category.dart';
import 'package:tunas/repositories/json/models/json.dart';
import 'package:tunas/repositories/metadata/models/account.dart';
class Metadata implements Json {
List<Budget> budgets;
List<Category> categories;
List<Account> accounts;
Metadata({
this.budgets = const [],
this.categories = const [],
this.accounts = const [],
});
@override
Map<String, dynamic> toJson() => {
'budgets': budgets.map((budget) => budget.toJson()).toList(),
'categories': categories.map((category) => category.toJson()).toList(),
'accounts': accounts.map((account) => account.toJson()).toList(),
};
@override
String getJsonFileName() {
return 'metadata.json';
}
}
class MetadataFactory implements JsonFactory<Metadata> {
@override
Metadata fromJson(Map<String, dynamic> json) {
return Metadata(
budgets: List<Budget>.from(json['budgets']?.map((budget) => Budget.fromJson(budget))),
categories: List<Category>.from(json['categories']?.map((category) => Category.fromJson(category))),
accounts: List<Account>.from(json['accounts']?.map((account) => Account.fromJson(account))),
);
}
}