Migrate away from helpers package
This commit is contained in:
+18
-27
@@ -3,7 +3,6 @@ import 'dart:convert';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:helpers/helpers/print.dart';
|
||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||
import 'package:rluv/global/store.dart';
|
||||
|
||||
@@ -11,7 +10,7 @@ import '../models/token.dart';
|
||||
|
||||
final tokenProvider = StateProvider<Token?>((ref) {
|
||||
final jwt = ref.watch(jwtProvider);
|
||||
printLime('Current token: $jwt');
|
||||
print('Current token: $jwt');
|
||||
if (jwt == null) return null;
|
||||
try {
|
||||
return Token.fromJson(JwtDecoder.decode(jwt));
|
||||
@@ -37,12 +36,12 @@ class _JwtNotifier extends StateNotifier<String?> {
|
||||
|
||||
void setToken(String jwt) {
|
||||
state = jwt;
|
||||
printCyan('Loaded jwt into client: $jwt');
|
||||
print('Loaded jwt into client: $jwt');
|
||||
ref.read(prefsProvider)?.setString('jwt', jwt);
|
||||
}
|
||||
|
||||
void revokeToken() {
|
||||
printCyan('jwt token revoked');
|
||||
print('jwt token revoked');
|
||||
state = null;
|
||||
ref.read(prefsProvider)?.remove('jwt');
|
||||
}
|
||||
@@ -58,8 +57,7 @@ class _ApiNotifier extends StateNotifier<Dio> {
|
||||
// dio.options.baseUrl = "http://localhost:8081/";
|
||||
dio.options.baseUrl = "https://rluv.fosscat.com/";
|
||||
dio.interceptors.addAll([
|
||||
InterceptorsWrapper(onRequest:
|
||||
(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
InterceptorsWrapper(onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
|
||||
final jwt = ref.read(jwtProvider);
|
||||
if (jwt != null) {
|
||||
options.headers['token'] = jwt;
|
||||
@@ -69,12 +67,10 @@ class _ApiNotifier extends StateNotifier<Dio> {
|
||||
if (response.statusCode != null) {
|
||||
if (response.statusCode == 200) {
|
||||
try {
|
||||
if ((response.data as Map<String, dynamic>)
|
||||
.containsKey('success')) {
|
||||
if ((response.data as Map<String, dynamic>).containsKey('success')) {
|
||||
if (!response.data['success']) return handler.next(response);
|
||||
}
|
||||
if ((response.data as Map<String, dynamic>)
|
||||
.containsKey('token')) {
|
||||
if ((response.data as Map<String, dynamic>).containsKey('token')) {
|
||||
final jwt = response.data['token'];
|
||||
if (jwt != null) {
|
||||
ref.read(jwtProvider.notifier).setToken(jwt);
|
||||
@@ -83,7 +79,7 @@ class _ApiNotifier extends StateNotifier<Dio> {
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
printRed('Error in interceptor for token: $err');
|
||||
print('Error in interceptor for token: $err');
|
||||
return handler.next(response);
|
||||
}
|
||||
}
|
||||
@@ -115,8 +111,7 @@ class _ApiNotifier extends StateNotifier<Dio> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> put(
|
||||
{required String path, Object? data}) async {
|
||||
Future<Map<String, dynamic>?> put({required String path, Object? data}) async {
|
||||
try {
|
||||
final res = await dio.put(path, data: data);
|
||||
|
||||
@@ -129,8 +124,7 @@ class _ApiNotifier extends StateNotifier<Dio> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> post(
|
||||
{required String path, Object? data}) async {
|
||||
Future<Map<String, dynamic>?> post({required String path, Object? data}) async {
|
||||
try {
|
||||
final res = await dio.post(path, data: data);
|
||||
|
||||
@@ -143,8 +137,7 @@ class _ApiNotifier extends StateNotifier<Dio> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> delete(
|
||||
{required String path, Object? data}) async {
|
||||
Future<Map<String, dynamic>?> delete({required String path, Object? data}) async {
|
||||
try {
|
||||
final res = await dio.delete(path, data: data);
|
||||
|
||||
@@ -162,8 +155,7 @@ class _LoggingInterceptor extends Interceptor {
|
||||
_LoggingInterceptor();
|
||||
|
||||
@override
|
||||
Future onRequest(
|
||||
RequestOptions options, RequestInterceptorHandler handler) async {
|
||||
Future onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
|
||||
logPrint('///*** REQUEST ***\\\\\\');
|
||||
printKV('URI', options.uri);
|
||||
printKV('METHOD', options.method);
|
||||
@@ -177,7 +169,7 @@ class _LoggingInterceptor extends Interceptor {
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) {
|
||||
printRed('///*** ERROR RESPONSE ***\\\\\\');
|
||||
print('///*** ERROR RESPONSE ***\\\\\\');
|
||||
logPrint('URI: ${err.requestOptions.uri}');
|
||||
if (err.response != null) {
|
||||
logPrint('STATUS CODE: ${err.response?.statusCode?.toString()}');
|
||||
@@ -192,8 +184,7 @@ class _LoggingInterceptor extends Interceptor {
|
||||
}
|
||||
|
||||
@override
|
||||
Future onResponse(
|
||||
Response response, ResponseInterceptorHandler handler) async {
|
||||
Future onResponse(Response response, ResponseInterceptorHandler handler) async {
|
||||
logPrint('///*** RESPONSE ***\\\\\\');
|
||||
printKV('URI', response.requestOptions.uri);
|
||||
printKV('STATUS CODE', response.statusCode ?? '');
|
||||
@@ -206,7 +197,7 @@ class _LoggingInterceptor extends Interceptor {
|
||||
|
||||
void printKV(String key, Object v) {
|
||||
if (kDebugMode) {
|
||||
printOrange('$key: $v');
|
||||
print('$key: $v');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,21 +206,21 @@ class _LoggingInterceptor extends Interceptor {
|
||||
final data = (s as Map<String, dynamic>?);
|
||||
if (kDebugMode) {
|
||||
if (data == null) {
|
||||
printAmber({});
|
||||
print({});
|
||||
return;
|
||||
}
|
||||
JsonEncoder encoder = const JsonEncoder.withIndent(' ');
|
||||
String prettyprint = encoder.convert(s);
|
||||
printAmber(prettyprint);
|
||||
print(prettyprint);
|
||||
}
|
||||
} catch (_) {
|
||||
printAmber(s);
|
||||
print(s);
|
||||
}
|
||||
}
|
||||
|
||||
void logPrint(String s) {
|
||||
if (kDebugMode) {
|
||||
printOrange(s);
|
||||
print(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
-25
@@ -2,7 +2,6 @@ import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.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';
|
||||
@@ -13,8 +12,7 @@ import '../models/shared_note.dart';
|
||||
import '../models/transaction_model.dart';
|
||||
import '../models/user.dart';
|
||||
|
||||
StateProvider<SharedPreferences?> prefsProvider =
|
||||
StateProvider<SharedPreferences?>((ref) => null);
|
||||
StateProvider<SharedPreferences?> prefsProvider = StateProvider<SharedPreferences?>((ref) => null);
|
||||
|
||||
// final StateProvider<Token?> tokenProvider = StateProvider<Token?>((ref) {
|
||||
// // final tokenStr = prefs.getString('jwt');
|
||||
@@ -42,8 +40,7 @@ final Provider<FamilyModel?> familyProvider = Provider<FamilyModel?>(
|
||||
},
|
||||
);
|
||||
|
||||
final Provider<List<BudgetCategory>> budgetCategoriesProvider =
|
||||
Provider<List<BudgetCategory>>((ref) {
|
||||
final Provider<List<BudgetCategory>> budgetCategoriesProvider = Provider<List<BudgetCategory>>((ref) {
|
||||
final dash = ref.watch(dashboardProvider);
|
||||
if (dash == null) return [];
|
||||
final categoriesData = dash['budget_categories'] as List<dynamic>;
|
||||
@@ -55,7 +52,7 @@ final Provider<List<BudgetCategory>> budgetCategoriesProvider =
|
||||
|
||||
final prefs = ref.read(prefsProvider);
|
||||
final budgetJson = jsonEncode({'budget_categories': categoriesData});
|
||||
printBlue('updated prefs stored categories');
|
||||
print('updated prefs stored categories');
|
||||
prefs?.setString('budget_categories', budgetJson);
|
||||
|
||||
return categories;
|
||||
@@ -71,8 +68,7 @@ final Provider<Budget?> budgetProvider = Provider<Budget?>(
|
||||
},
|
||||
);
|
||||
|
||||
final Provider<List<Transaction>> transactionsProvider =
|
||||
Provider<List<Transaction>>((ref) {
|
||||
final Provider<List<Transaction>> transactionsProvider = Provider<List<Transaction>>((ref) {
|
||||
final dash = ref.watch(dashboardProvider);
|
||||
if (dash == null) return [];
|
||||
final transactions = dash['transactions'] as List<dynamic>;
|
||||
@@ -83,8 +79,7 @@ final Provider<List<Transaction>> transactionsProvider =
|
||||
.toList();
|
||||
});
|
||||
|
||||
final Provider<List<SharedNote>> sharedNotesProvider =
|
||||
Provider<List<SharedNote>>(
|
||||
final Provider<List<SharedNote>> sharedNotesProvider = Provider<List<SharedNote>>(
|
||||
(ref) {
|
||||
final dash = ref.watch(dashboardProvider);
|
||||
if (dash == null) return [];
|
||||
@@ -97,8 +92,7 @@ final Provider<List<SharedNote>> sharedNotesProvider =
|
||||
},
|
||||
);
|
||||
|
||||
final dashboardProvider =
|
||||
StateNotifierProvider<DashBoardStateNotifier, Map<String, dynamic>?>(
|
||||
final dashboardProvider = StateNotifierProvider<DashBoardStateNotifier, Map<String, dynamic>?>(
|
||||
(ref) {
|
||||
return DashBoardStateNotifier(ref);
|
||||
},
|
||||
@@ -119,10 +113,10 @@ class DashBoardStateNotifier extends StateNotifier<Map<String, dynamic>?> {
|
||||
);
|
||||
final token = ref.read(tokenProvider);
|
||||
if (token?.familyId == null) {
|
||||
printPink('No token, cannot fetch dashboard');
|
||||
print('No token, cannot fetch dashboard');
|
||||
return;
|
||||
}
|
||||
printAmber('Fetching dashboard');
|
||||
print('Fetching dashboard');
|
||||
state = await ref.read(apiProvider.notifier).get("dashboard");
|
||||
WidgetsBinding.instance.addPostFrameCallback(
|
||||
(_) => ref.read(loadingStateProvider.notifier).state = false,
|
||||
@@ -131,7 +125,7 @@ class DashBoardStateNotifier extends StateNotifier<Map<String, dynamic>?> {
|
||||
|
||||
void update(Map<String, dynamic> data) {
|
||||
if (state == null) {
|
||||
printPink('Cant update data, state is null');
|
||||
print('Cant update data, state is null');
|
||||
return;
|
||||
}
|
||||
if (data.keys.length != 1 || data.values.length != 1) {
|
||||
@@ -149,16 +143,14 @@ class DashBoardStateNotifier extends StateNotifier<Map<String, dynamic>?> {
|
||||
)
|
||||
.toList();
|
||||
subStateListObj.removeWhere(
|
||||
(element) =>
|
||||
element['id'] ==
|
||||
(data.values.first as Map<String, dynamic>)['id'],
|
||||
(element) => element['id'] == (data.values.first as Map<String, dynamic>)['id'],
|
||||
);
|
||||
subStateListObj.add(data.values.first);
|
||||
|
||||
final newState = state;
|
||||
newState![key] = subStateListObj;
|
||||
state = {...newState};
|
||||
// printBlue(state);
|
||||
// print(state);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -167,7 +159,7 @@ class DashBoardStateNotifier extends StateNotifier<Map<String, dynamic>?> {
|
||||
|
||||
void add(Map<String, dynamic> data) {
|
||||
if (state == null) {
|
||||
printPink('Cant add data, state is null');
|
||||
print('Cant add data, state is null');
|
||||
return;
|
||||
}
|
||||
if (data.keys.length != 1 || data.values.length != 1) {
|
||||
@@ -183,7 +175,7 @@ class DashBoardStateNotifier extends StateNotifier<Map<String, dynamic>?> {
|
||||
subStateList.add(data.values.first);
|
||||
newState![key] = subStateList;
|
||||
state = {...newState};
|
||||
// printBlue(state);
|
||||
// print(state);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -192,7 +184,7 @@ class DashBoardStateNotifier extends StateNotifier<Map<String, dynamic>?> {
|
||||
|
||||
void removeWithId(String stateKey, int id) {
|
||||
if (state == null) {
|
||||
printPink('Cant remove data, state is null');
|
||||
print('Cant remove data, state is null');
|
||||
return;
|
||||
}
|
||||
switch (stateKey) {
|
||||
@@ -201,11 +193,10 @@ class DashBoardStateNotifier extends StateNotifier<Map<String, dynamic>?> {
|
||||
case 'shared_noted':
|
||||
final subStateList = state![stateKey] as List<dynamic>;
|
||||
final newState = state;
|
||||
subStateList
|
||||
.removeWhere((e) => (e as Map<String, dynamic>)['id'] == id);
|
||||
subStateList.removeWhere((e) => (e as Map<String, dynamic>)['id'] == id);
|
||||
newState![stateKey] = subStateList;
|
||||
state = {...newState};
|
||||
// printBlue(state);
|
||||
// print(state);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
+10
-19
@@ -3,7 +3,6 @@ import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:helpers/helpers/print.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:rluv/global/styles.dart';
|
||||
import 'package:rluv/main.dart';
|
||||
@@ -20,20 +19,16 @@ bool boolFromJson(int value) => value == 1;
|
||||
|
||||
int boolToJson(bool hide) => hide ? 1 : 0;
|
||||
|
||||
String colorToJson(Color color) =>
|
||||
color.toString().split('(0x')[1].split(')')[0];
|
||||
String colorToJson(Color color) => color.toString().split('(0x')[1].split(')')[0];
|
||||
|
||||
Color colorFromJson(String hex) => Color(int.parse(hex, radix: 16));
|
||||
|
||||
String? optionalColorToJson(Color? optionalColor) => optionalColor == null
|
||||
? null
|
||||
: optionalColor.toString().split('(0x')[1].split(')')[0];
|
||||
String? optionalColorToJson(Color? optionalColor) =>
|
||||
optionalColor == null ? null : optionalColor.toString().split('(0x')[1].split(')')[0];
|
||||
|
||||
Color? optionalColorFromJson(String? hex) =>
|
||||
hex == null ? null : Color(int.parse(hex, radix: 16));
|
||||
Color? optionalColorFromJson(String? hex) => hex == null ? null : Color(int.parse(hex, radix: 16));
|
||||
|
||||
Brightness getBrightness(Color color) =>
|
||||
ThemeData.estimateBrightnessForColor(color);
|
||||
Brightness getBrightness(Color color) => ThemeData.estimateBrightnessForColor(color);
|
||||
|
||||
List<Color> generateColorList() {
|
||||
List<Color> colors = [];
|
||||
@@ -60,7 +55,7 @@ extension MonetaryExtension on double {
|
||||
return "\$${pieces.first}.00";
|
||||
} else {
|
||||
if (pieces.length > 2) {
|
||||
printBlue(pieces);
|
||||
print(pieces);
|
||||
}
|
||||
return '\$${pieces[0]}.${pieces[1].padRight(2, "0")}';
|
||||
}
|
||||
@@ -83,7 +78,7 @@ void showSnack(
|
||||
Duration duration = const Duration(seconds: 2)}) {
|
||||
final messengerKey = ref.read(scaffoldMessengerKeyProvider);
|
||||
if (messengerKey.currentState == null) {
|
||||
printPink('Cannot show snackbar, state == null');
|
||||
print('Cannot show snackbar, state == null');
|
||||
return;
|
||||
}
|
||||
final color = type == SnackType.info
|
||||
@@ -99,8 +94,7 @@ void showSnack(
|
||||
elevation: 8,
|
||||
backgroundColor: Styles.deepPurpleNurple,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(20.0), topRight: Radius.circular(20.0)),
|
||||
borderRadius: BorderRadius.only(topLeft: Radius.circular(20.0), topRight: Radius.circular(20.0)),
|
||||
),
|
||||
content: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
@@ -108,9 +102,7 @@ void showSnack(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 14.0),
|
||||
child: Icon(
|
||||
type == SnackType.success ? Icons.check_circle : Icons.info,
|
||||
color: color),
|
||||
child: Icon(type == SnackType.success ? Icons.check_circle : Icons.info, color: color),
|
||||
),
|
||||
Text(text, style: textStyle),
|
||||
],
|
||||
@@ -120,8 +112,7 @@ void showSnack(
|
||||
}
|
||||
|
||||
bool isEmailValid(String email) {
|
||||
final RegExp regex =
|
||||
RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$");
|
||||
final RegExp regex = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$");
|
||||
return regex.hasMatch(email);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user