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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user