restructured the application

added .end files
switched to BLoC state-management
removed unnecessary helpers
initialized Scenes Page
This commit is contained in:
Mohammad Salameh
2024-02-15 14:00:09 +03:00
parent 16f47f744c
commit 1d18210158
79 changed files with 554 additions and 723 deletions
@@ -0,0 +1,3 @@
abstract class ApiEndpoints {
static const String apiKey = '';
}
+44
View File
@@ -0,0 +1,44 @@
import 'package:dio/dio.dart';
class HTTPInterceptor extends InterceptorsWrapper {
@override
void onResponse(Response response, ResponseInterceptorHandler handler) async {
// Pass the response to the next interceptor or response handler.
return handler.next(response);
}
@override
void onRequest(RequestOptions options,
RequestInterceptorHandler handler) async {
// TODO: Implement logic for adding headers to requests.
// This method is called before a request is sent.
super.onRequest(options, handler);
}
@override
void onError(DioException err, ErrorInterceptorHandler handler) async {
// TODO: Implement error handling logic.
// This method is called when an error occurs during a request.
super.onError(err, handler);
}
/// Validates the response and returns true if it is successful (status code 2xx).
Future<bool> validateResponse(Response response) async {
if (response.statusCode != null) {
if (response.statusCode! >= 200 && response.statusCode! < 300) {
// If the response status code is within the successful range (2xx),
// return true indicating a successful response.
return true;
} else {
// If the response status code is not within the successful range (2xx),
// return false indicating an unsuccessful response.
return false;
}
} else {
// If the response status code is null, return false indicating an unsuccessful response.
return false;
}
}
}
+121
View File
@@ -0,0 +1,121 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:syncrow_app/services/locator.dart';
import 'http_interceptor.dart';
class HTTPService {
final Dio client = locator<Dio>();
final navigatorKey = GlobalKey<NavigatorState>();
String certificateString = "";
static Dio setupDioClient() {
final client = Dio(
BaseOptions(
// TODO add base url
// baseUrl: URLConstants.baseURL,
receiveDataWhenStatusError: true,
followRedirects: false,
connectTimeout: const Duration(milliseconds: 60000),
receiveTimeout: const Duration(milliseconds: 60000),
),
);
// (client.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
// client. = (X509Certificate cert, String host, int port) {
// // TODO add SSL certificate
// // if(cert.pem == certificateString){ // Verify the certificate
// // return true;
// // }
// // return false;
// return true;
// };
// return client;
// };
client.interceptors.add(locator<HTTPInterceptor>());
return client;
}
Future<T> getRequest<T>({
required String path,
Map<String, dynamic>? queryParameters,
required T Function(dynamic) expectedResponseModel,
bool showServerMessage = true,
}) async {
try {
final response = await client.get(
path,
queryParameters: queryParameters,
);
return expectedResponseModel(response.data);
} catch (error) {
debugPrint("******* Error");
debugPrint(error.toString());
rethrow;
}
}
Future<T> postRequest<T>(
{required String path,
Map<String, dynamic>? queryParameters,
Options? options,
dynamic body,
bool showServerMessage = true,
required T Function(dynamic) expectedResponseModel}) async {
try {
final response = await client.post(path,
data: body, queryParameters: queryParameters, options: options);
debugPrint("status code is ${response.statusCode}");
return expectedResponseModel(response.data);
} catch (error) {
debugPrint("******* Error");
debugPrint(error.toString());
rethrow;
}
}
Future<T> patchRequest<T>(
{required String path,
Map<String, dynamic>? queryParameters,
dynamic body,
required T Function(dynamic) expectedResponseModel}) async {
try {
final response = await client.patch(
path,
data: body,
queryParameters: queryParameters,
);
debugPrint("status code is ${response.statusCode}");
return expectedResponseModel(response.data);
} catch (error) {
debugPrint("******* Error");
debugPrint(error.toString());
rethrow;
}
}
Future<T> downloadRequest<T>(
{required String path,
required String savePath,
Map<String, dynamic>? queryParameters,
required T Function(dynamic) expectedResponseModel}) async {
try {
debugPrint("download begins");
final response = await client.download(
path,
savePath,
onReceiveProgress: (current, total) {
debugPrint("current = $current, while total = $total");
},
);
debugPrint("download ends");
return expectedResponseModel(response.data);
// return expectedResponseModel(response.data);
} catch (error) {
debugPrint("******* Error");
debugPrint("download error");
debugPrint(error.toString());
rethrow;
}
}
}
+10
View File
@@ -0,0 +1,10 @@
import 'dart:io';
// We use this class to skip the problem of SSL certification and solve the Image.network(url) issue
class MyHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext? context) {
return super.createHttpClient(context)
..badCertificateCallback = (X509Certificate cert, String host, int port) => true;
}
}
+64
View File
@@ -0,0 +1,64 @@
import 'package:dio/dio.dart';
abstract class Failure {
final String errMessage;
Failure(this.errMessage);
}
class ServerFailure extends Failure {
ServerFailure(super.errMessage);
@override
String toString() {
return errMessage;
}
factory ServerFailure.fromDioError(DioException dioError) {
switch (dioError.type) {
case DioExceptionType.connectionTimeout:
return ServerFailure("Connection timeout with ApiServer.");
case DioExceptionType.sendTimeout:
return ServerFailure("Send timeout with ApiServer.");
case DioExceptionType.receiveTimeout:
return ServerFailure("Receive timeout with ApiServer.");
case DioExceptionType.badCertificate:
return ServerFailure("Bad certificate!");
case DioExceptionType.badResponse:
return ServerFailure.fromResponse(
dioError.response!.statusCode!, dioError.response!.data);
case DioExceptionType.cancel:
return ServerFailure("The request to ApiServer was canceled");
case DioExceptionType.connectionError:
return ServerFailure("No Internet Connection");
case DioExceptionType.unknown:
return ServerFailure("Unexpected Error, Please try again!");
}
}
factory ServerFailure.fromResponse(int statusCode, dynamic response) {
if (statusCode == 400 || statusCode == 401 || statusCode == 403) {
return ServerFailure(response);
} else if (statusCode == 404) {
return ServerFailure("Your request not found, Please try later!");
} else if (statusCode == 500) {
return ServerFailure(response);
} else {
return ServerFailure("Opps there was an Error, Please try again!");
}
}
}
class ResponseFailure extends Failure {
ResponseFailure(super.errMessage);
@override
String toString() {
return errMessage;
}
}