Working api call to backend

This commit is contained in:
Nathan Anderson
2023-07-19 02:16:13 -06:00
parent 37c644ab0d
commit eba628bb4c
26 changed files with 1094 additions and 119 deletions
+96
View File
@@ -0,0 +1,96 @@
import 'package:dio/dio.dart';
import 'package:helpers/helpers/print.dart';
class Api {
static final Api _instance = Api._internal();
factory Api() {
if (_instance.initDone) return _instance;
_instance.initDone = true;
_instance.dio = Dio();
_instance.dio.options.baseUrl = "http://localhost:8081/";
_instance.dio.interceptors.add(
InterceptorsWrapper(
onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
// Do something before request is sent.
// If you want to resolve the request with custom data,
// you can resolve a `Response` using `handler.resolve(response)`.
// If you want to reject the request with a error message,
// you can reject with a `DioException` using `handler.reject(dioError)`.
return handler.next(options);
},
onResponse: (Response response, ResponseInterceptorHandler handler) {
if (response.statusCode != null &&
response.statusCode! < 500 &&
response.statusCode! >= 400) {
return handler.reject(DioException.badResponse(
requestOptions: RequestOptions(),
response: response,
statusCode: response.statusCode!));
}
// Do something with response data.
// If you want to reject the request with a error message,
// you can reject a `DioException` object using `handler.reject(dioError)`.
return handler.next(response);
},
onError: (DioException e, ErrorInterceptorHandler handler) {
printPink(e);
// Do something with response error.
// If you want to resolve the request with some custom data,
// you can resolve a `Response` object using `handler.resolve(response)`.
return handler.next(e);
},
),
);
return _instance;
}
Api._internal();
bool initDone = false;
late final Dio dio;
Future<Map<String, dynamic>?> get(String path) async {
try {
final res = await dio.get(path);
if (res.data != null) {
return res.data as Map<String, dynamic>;
}
return null;
} catch (err) {
printRed('Error in get: $err');
return null;
}
}
Future<Map<String, dynamic>?> put(
{required String path, Object? data}) async {
try {
final res = await dio.put(path, data: data);
if (res.data != null) {
return res.data as Map<String, dynamic>;
}
return null;
} catch (err) {
printRed('Error in put: $err');
return null;
}
}
Future<Map<String, dynamic>?> delete(
{required String path, Object? data}) async {
try {
final res = await dio.delete(path, data: data);
if (res.data != null) {
return res.data as Map<String, dynamic>;
}
return null;
} catch (err) {
printRed('Error in delete: $err');
return null;
}
}
}
+81
View File
@@ -0,0 +1,81 @@
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() {}
}
+21
View File
@@ -0,0 +1,21 @@
import 'dart:ui';
class Styles {
// Theme Colors
static const Color purpleNurple = Color(0xffA188A6);
static const Color sunflower = Color(0xffFFEE88);
static const Color electricBlue = Color(0xFF19647E);
static const Color blushingPink = Color(0xFFE78F8E);
static const Color seaweedGreen = Color(0xFF86BA90);
static const Color emptyBarGrey = Color(0xFFC8C8C8);
static const Color lavender = Color(0xFFB8B8FF);
static const Color sand = Color(0xFFD9D9D9);
// Income Colors
static const Color incomeBlue = Color(0xFFB8B8FF);
static const Color incomeGreen = Color(0xFF0FA102);
// Expenses Colors
static const Color expensesOrange = Color(0xFFFA824C);
static const Color expensesRed = Color(0xFF9E0000);
}
+17
View File
@@ -0,0 +1,17 @@
import 'dart:ui';
import 'package:intl/intl.dart';
String formatDate(DateTime time) {
return DateFormat('EEEE, dd MMMM yyyy').format(time);
}
DateTime dateFromJson(int value) => DateTime.fromMillisecondsSinceEpoch(value);
int dateToJson(DateTime? value) =>
value == null ? 0 : value.millisecondsSinceEpoch;
String colorToJson(Color color) =>
color.toString().split('(0x')[1].split(')')[0];
Color colorFromJson(String hex) => Color(int.parse(hex, radix: 16));