Update collection_providers.dart

This commit is contained in:
Ankit Mahato
2024-04-19 19:34:16 +05:30
parent 58ace08ded
commit 4fb79ebd25

View File

@ -46,7 +46,7 @@ class CollectionStateNotifier
final Ref ref; final Ref ref;
final HiveHandler hiveHandler; final HiveHandler hiveHandler;
final baseResponseModel = const ResponseModel(); final baseResponseModel = const HttpResponseModel();
bool hasId(String id) => state?.keys.contains(id) ?? false; bool hasId(String id) => state?.keys.contains(id) ?? false;
@ -58,6 +58,7 @@ class CollectionStateNotifier
final id = getNewUuid(); final id = getNewUuid();
final newRequestModel = RequestModel( final newRequestModel = RequestModel(
id: id, id: id,
httpRequestModel: const HttpRequestModel(),
); );
var map = {...state!}; var map = {...state!};
map[id] = newRequestModel; map[id] = newRequestModel;
@ -103,10 +104,10 @@ class CollectionStateNotifier
void clearResponse(String? id) { void clearResponse(String? id) {
if (id == null || state?[id] == null) return; if (id == null || state?[id] == null) return;
var currentModel = state![id]!; var currentModel = state![id]!;
final newModel = currentModel.duplicate( final newModel = currentModel.copyWith(
id: id, responseStatus: null,
name: currentModel.name, message: null,
requestTabIndex: currentModel.requestTabIndex, httpResponseModel: null,
); );
var map = {...state!}; var map = {...state!};
map[id] = newModel; map[id] = newModel;
@ -119,9 +120,16 @@ class CollectionStateNotifier
var itemIds = ref.read(requestSequenceProvider); var itemIds = ref.read(requestSequenceProvider);
int idx = itemIds.indexOf(id); int idx = itemIds.indexOf(id);
var currentModel = state![id]!;
final newModel = state![id]!.duplicate( final newModel = currentModel.copyWith(
id: newId, id: newId,
name: "${currentModel.name} (copy)",
requestTabIndex: 0,
responseStatus: null,
message: null,
httpResponseModel: null,
isWorking: false,
sendingTime: null,
); );
itemIds.insert(idx + 1, newId); itemIds.insert(idx + 1, newId);
@ -141,33 +149,40 @@ class CollectionStateNotifier
String? name, String? name,
String? description, String? description,
int? requestTabIndex, int? requestTabIndex,
List<NameValueModel>? requestHeaders, List<NameValueModel>? headers,
List<NameValueModel>? requestParams, List<NameValueModel>? params,
List<bool>? isHeaderEnabledList, List<bool>? isHeaderEnabledList,
List<bool>? isParamEnabledList, List<bool>? isParamEnabledList,
ContentType? requestBodyContentType, ContentType? bodyContentType,
String? requestBody, String? body,
List<FormDataModel>? requestFormDataList, List<FormDataModel>? formData,
int? responseStatus, int? responseStatus,
String? message, String? message,
ResponseModel? responseModel, HttpResponseModel? httpResponseModel,
}) { }) {
final newModel = state![id]!.copyWith( var currentModel = state![id]!;
method: method, var currentHttpRequestModel = currentModel.httpRequestModel;
url: url, final newModel = currentModel.copyWith(
name: name, name: name ?? currentModel.name,
description: description, description: description ?? currentModel.description,
requestTabIndex: requestTabIndex, requestTabIndex: requestTabIndex ?? currentModel.requestTabIndex,
requestHeaders: requestHeaders, httpRequestModel: currentHttpRequestModel?.copyWith(
requestParams: requestParams, method: method ?? currentHttpRequestModel.method,
isHeaderEnabledList: isHeaderEnabledList, url: url ?? currentHttpRequestModel.url,
isParamEnabledList: isParamEnabledList, headers: headers ?? currentHttpRequestModel.headers,
requestBodyContentType: requestBodyContentType, params: params ?? currentHttpRequestModel.params,
requestBody: requestBody, isHeaderEnabledList:
requestFormDataList: requestFormDataList, isHeaderEnabledList ?? currentHttpRequestModel.isHeaderEnabledList,
isParamEnabledList:
isParamEnabledList ?? currentHttpRequestModel.isParamEnabledList,
bodyContentType:
bodyContentType ?? currentHttpRequestModel.bodyContentType,
body: body ?? currentHttpRequestModel.body,
formData: formData ?? currentHttpRequestModel.formData,
),
responseStatus: responseStatus, responseStatus: responseStatus,
message: message, message: message,
responseModel: responseModel, httpResponseModel: httpResponseModel,
); );
//print(newModel); //print(newModel);
var map = {...state!}; var map = {...state!};
@ -194,8 +209,12 @@ class CollectionStateNotifier
); );
state = map; state = map;
if (requestModel.httpRequestModel == null) {
return;
}
(http.Response?, Duration?, String?)? responseRec = await request( (http.Response?, Duration?, String?)? responseRec = await request(
requestModel, requestModel.httpRequestModel!,
defaultUriScheme: defaultUriScheme, defaultUriScheme: defaultUriScheme,
); );
late final RequestModel newRequestModel; late final RequestModel newRequestModel;
@ -214,7 +233,7 @@ class CollectionStateNotifier
newRequestModel = requestModel.copyWith( newRequestModel = requestModel.copyWith(
responseStatus: statusCode, responseStatus: statusCode,
message: kResponseCodeReasons[statusCode], message: kResponseCodeReasons[statusCode],
responseModel: responseModel, httpResponseModel: responseModel,
isWorking: false, isWorking: false,
); );
} }
@ -243,6 +262,7 @@ class CollectionStateNotifier
state = { state = {
newId: RequestModel( newId: RequestModel(
id: newId, id: newId,
httpRequestModel: const HttpRequestModel(),
), ),
}; };
return true; return true;
@ -251,8 +271,13 @@ class CollectionStateNotifier
for (var id in ids) { for (var id in ids) {
var jsonModel = hiveHandler.getRequestModel(id); var jsonModel = hiveHandler.getRequestModel(id);
if (jsonModel != null) { if (jsonModel != null) {
var requestModel = var jsonMap = Map<String, Object?>.from(jsonModel);
RequestModel.fromJson(Map<String, dynamic>.from(jsonModel)); var requestModel = RequestModel.fromJson(jsonMap);
if (requestModel.httpRequestModel == null) {
requestModel = requestModel.copyWith(
httpRequestModel: const HttpRequestModel(),
);
}
data[id] = requestModel; data[id] = requestModel;
} }
} }
@ -269,7 +294,9 @@ class CollectionStateNotifier
for (var id in ids) { for (var id in ids) {
await hiveHandler.setRequestModel( await hiveHandler.setRequestModel(
id, id,
state?[id]?.toJson(includeResponse: saveResponse), saveResponse
? (state?[id])?.toJson()
: (state?[id]?.copyWith(httpResponseModel: null))?.toJson(),
); );
} }
await hiveHandler.removeUnused(); await hiveHandler.removeUnused();