Removed helpers lib and updated to flutter 3.22

This commit is contained in:
2024-05-30 20:44:06 -06:00
parent 64baa04a47
commit 000f409052
19 changed files with 242 additions and 512 deletions
+19 -27
View File
@@ -1,9 +1,9 @@
import 'dart:convert';
import 'package:colorful_print/colorful_print.dart';
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 +11,7 @@ import '../models/token.dart';
final tokenProvider = StateProvider<Token?>((ref) {
final jwt = ref.watch(jwtProvider);
printLime('Current token: $jwt');
printColor('Current token: $jwt', textColor: TextColor.green);
if (jwt == null) return null;
try {
return Token.fromJson(JwtDecoder.decode(jwt));
@@ -37,12 +37,12 @@ class _JwtNotifier extends StateNotifier<String?> {
void setToken(String jwt) {
state = jwt;
printCyan('Loaded jwt into client: $jwt');
printColor('Loaded jwt into client: $jwt', textColor: TextColor.cyan);
ref.read(prefsProvider)?.setString('jwt', jwt);
}
void revokeToken() {
printCyan('jwt token revoked');
printColor('jwt token revoked', textColor: TextColor.cyan);
state = null;
ref.read(prefsProvider)?.remove('jwt');
}
@@ -58,8 +58,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 +68,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 +80,7 @@ class _ApiNotifier extends StateNotifier<Dio> {
}
}
} catch (err) {
printRed('Error in interceptor for token: $err');
printColor('Error in interceptor for token: $err', textColor: TextColor.red);
return handler.next(response);
}
}
@@ -115,8 +112,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 +125,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 +138,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 +156,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 +170,7 @@ class _LoggingInterceptor extends Interceptor {
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
printRed('///*** ERROR RESPONSE ***\\\\\\');
printColor('///*** ERROR RESPONSE ***\\\\\\', textColor: TextColor.red);
logPrint('URI: ${err.requestOptions.uri}');
if (err.response != null) {
logPrint('STATUS CODE: ${err.response?.statusCode?.toString()}');
@@ -192,8 +185,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 +198,7 @@ class _LoggingInterceptor extends Interceptor {
void printKV(String key, Object v) {
if (kDebugMode) {
printOrange('$key: $v');
printColor('$key: $v', textColor: TextColor.orange);
}
}
@@ -215,21 +207,21 @@ class _LoggingInterceptor extends Interceptor {
final data = (s as Map<String, dynamic>?);
if (kDebugMode) {
if (data == null) {
printAmber({});
printColor({}, textColor: TextColor.yellow);
return;
}
JsonEncoder encoder = const JsonEncoder.withIndent(' ');
String prettyprint = encoder.convert(s);
printAmber(prettyprint);
printColor(prettyprint, textColor: TextColor.yellow);
}
} catch (_) {
printAmber(s);
printColor(s, textColor: TextColor.yellow);
}
}
void logPrint(String s) {
if (kDebugMode) {
printOrange(s);
printColor(s, textColor: TextColor.yellow);
}
}
}