refactor collection provider

This commit is contained in:
Ashita Prasad
2024-12-16 00:10:49 +05:30
parent bc2fc49fc7
commit b3be6a9fa5
4 changed files with 22 additions and 27 deletions

View File

@@ -249,35 +249,35 @@ class CollectionStateNotifier
unsave();
}
Future<void> sendRequest(String id) async {
Future<void> sendRequest() async {
final requestId = ref.read(selectedIdStateProvider);
ref.read(codePaneVisibleStateProvider.notifier).state = false;
final defaultUriScheme = ref.read(
settingsProvider.select(
(value) => value.defaultUriScheme,
),
);
final defaultUriScheme = ref.read(settingsProvider).defaultUriScheme;
RequestModel requestModel = state![id]!;
if (requestId == null || state == null) {
return;
}
RequestModel? requestModel = state![requestId];
if (requestModel.httpRequestModel == null) {
if (requestModel?.httpRequestModel == null) {
return;
}
HttpRequestModel substitutedHttpRequestModel =
getSubstitutedHttpRequestModel(requestModel.httpRequestModel!);
getSubstitutedHttpRequestModel(requestModel!.httpRequestModel!);
// set current model's isWorking to true and update state
var map = {...state!};
map[id] = requestModel.copyWith(
map[requestId] = requestModel.copyWith(
isWorking: true,
sendingTime: DateTime.now(),
);
state = map;
(HttpResponse?, Duration?, String?)? responseRec = await request(
requestId,
substitutedHttpRequestModel,
defaultUriScheme: defaultUriScheme,
requestId: id,
);
late final RequestModel newRequestModel;
@@ -304,7 +304,7 @@ class CollectionStateNotifier
historyId: newHistoryId,
metaData: HistoryMetaModel(
historyId: newHistoryId,
requestId: id,
requestId: requestId,
name: requestModel.name,
url: substitutedHttpRequestModel.url,
method: substitutedHttpRequestModel.method,
@@ -319,13 +319,14 @@ class CollectionStateNotifier
// update state with response data
map = {...state!};
map[id] = newRequestModel;
map[requestId] = newRequestModel;
state = map;
unsave();
}
void cancelRequest(String id) {
void cancelRequest() {
final id = ref.read(selectedIdStateProvider);
httpClientManager.cancelRequest(id);
unsave();
}

View File

@@ -97,9 +97,7 @@ class URLTextField extends ConsumerWidget {
.update(selectedId, url: value);
},
onFieldSubmitted: (value) {
ref
.read(collectionStateNotifierProvider.notifier)
.sendRequest(selectedId);
ref.read(collectionStateNotifierProvider.notifier).sendRequest();
},
);
}
@@ -114,7 +112,7 @@ class SendRequestButton extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final selectedId = ref.watch(selectedIdStateProvider);
ref.watch(selectedIdStateProvider);
final isWorking = ref.watch(
selectedRequestModelProvider.select((value) => value?.isWorking));
@@ -122,14 +120,10 @@ class SendRequestButton extends ConsumerWidget {
isWorking: isWorking ?? false,
onTap: () {
onTap?.call();
ref
.read(collectionStateNotifierProvider.notifier)
.sendRequest(selectedId!);
ref.read(collectionStateNotifierProvider.notifier).sendRequest();
},
onCancel: () {
ref
.read(collectionStateNotifierProvider.notifier)
.cancelRequest(selectedId!);
ref.read(collectionStateNotifierProvider.notifier).cancelRequest();
},
);
}

View File

@@ -19,8 +19,8 @@ class HttpClientManager {
return client;
}
void cancelRequest(String requestId) {
if (_clients.containsKey(requestId)) {
void cancelRequest(String? requestId) {
if (requestId != null && _clients.containsKey(requestId)) {
_clients[requestId]?.close();
_clients.remove(requestId);

View File

@@ -11,9 +11,9 @@ import 'http_client_manager.dart';
typedef HttpResponse = http.Response;
Future<(HttpResponse?, Duration?, String?)> request(
String requestId,
HttpRequestModel requestModel, {
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
String? requestId,
}) async {
final clientManager = HttpClientManager();
http.Client? client;