rluv_client/lib/global/store.dart
2023-07-19 02:16:13 -06:00

82 lines
2.5 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:helpers/helpers/print.dart';
import 'package:rluv/global/api.dart';
import 'package:rluv/models/budget.dart';
import 'package:rluv/models/budget_category_model.dart';
import '../models/family_model.dart';
import '../models/transaction_model.dart';
import '../models/user.dart';
class Store {
static final Store _instance = Store._internal();
bool _initDone = false;
factory Store() {
if (_instance._initDone) {
return _instance;
}
_instance._initDone = true;
_instance.dashboardProvider = FutureProvider<Map<String, dynamic>?>(
(ref) async {
final family = await ref.watch(_instance.familyProvider.future);
return Api().get("dashboard/${family.id}");
},
);
_instance.budgetCategoriesProvider =
FutureProvider<List<BudgetCategory>>((ref) async {
final dash = await ref.watch(_instance.dashboardProvider.future);
printAmber(dash);
if (dash == null) return [];
final categories = dash['budget_categories'] as List<dynamic>;
return categories
.map(
(e) => BudgetCategory.fromJson(e as Map<String, dynamic>),
)
.toList();
});
_instance.transactionsProvider =
FutureProvider<List<Transaction>>((ref) async {
final dash = await ref.watch(_instance.dashboardProvider.future);
if (dash == null) return [];
final transactions = dash['transactions'] as List<dynamic>;
return transactions
.map(
(e) => Transaction.fromJson(e as Map<String, dynamic>),
)
.toList();
});
return _instance;
}
Store._internal();
final FutureProvider<User> userProvider = FutureProvider<User>(
(ref) {
return User(
id: 0,
budgetId: 1,
createdAt: DateTime.now(),
familyId: 1,
lastActivityAt: DateTime.now(),
name: 'TEMP',
updatedAt: DateTime.now(),
);
},
);
final FutureProvider<FamilyModel> familyProvider =
FutureProvider<FamilyModel>((ref) => FamilyModel(
id: 1,
budgetId: 1,
createdAt: DateTime.now(),
updatedAt: DateTime.now()));
late final FutureProvider<List<BudgetCategory>> budgetCategoriesProvider;
late final FutureProvider<Budget> budgetProvider;
late final FutureProvider<List<Transaction>> transactionsProvider;
late final FutureProvider<Map<String, dynamic>?> dashboardProvider;
void fetchDashboard() {}
}