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

View File

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

View File

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

View File

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