Skip to content

Commit

Permalink
Refactor for clear response button
Browse files Browse the repository at this point in the history
  • Loading branch information
animator committed Mar 17, 2024
1 parent da46216 commit 9b40763
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 53 deletions.
3 changes: 2 additions & 1 deletion lib/models/request_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ class RequestModel {

RequestModel duplicate({
required String id,
String? name,
}) {
return RequestModel(
id: id,
method: method,
url: url,
name: "$name (copy)",
name: name ?? "${this.name} (copy)",
description: description,
requestHeaders: requestHeaders != null ? [...requestHeaders!] : null,
requestParams: requestParams != null ? [...requestParams!] : null,
Expand Down
56 changes: 23 additions & 33 deletions lib/providers/collection_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,15 @@ class CollectionStateNotifier
state = map;
}

void clearResponse(String id) {
var map = {...state!};
map[id] = RequestModel(
void clearResponse(String? id) {
if (id == null || state?[id] == null) return;
var currentModel = state![id]!;
final newModel = currentModel.duplicate(
id: id,
method: state![id]!.method,
url: state![id]!.url,
name: state![id]!.name,
description: state![id]!.description,
requestTabIndex: state![id]!.requestTabIndex,
requestHeaders: state![id]!.requestHeaders,
requestParams: state![id]!.requestParams,
isHeaderEnabledList: state![id]!.isHeaderEnabledList,
isParamEnabledList: state![id]!.isParamEnabledList,
requestBodyContentType: state![id]!.requestBodyContentType,
requestBody: state![id]!.requestBody,
requestFormDataList: state![id]!.requestFormDataList,
responseStatus: null,
message: null,
responseModel: null,
name: currentModel.name,
);
var map = {...state!};
map[id] = newModel;
state = map;
}

Expand Down Expand Up @@ -157,21 +146,22 @@ class CollectionStateNotifier
ResponseModel? responseModel,
}) {
final newModel = state![id]!.copyWith(
method: method,
url: url,
name: name,
description: description,
requestTabIndex: requestTabIndex,
requestHeaders: requestHeaders,
requestParams: requestParams,
isHeaderEnabledList: isHeaderEnabledList,
isParamEnabledList: isParamEnabledList,
requestBodyContentType: requestBodyContentType,
requestBody: requestBody,
requestFormDataList: requestFormDataList,
responseStatus: responseStatus,
message: message,
responseModel: responseModel);
method: method,
url: url,
name: name,
description: description,
requestTabIndex: requestTabIndex,
requestHeaders: requestHeaders,
requestParams: requestParams,
isHeaderEnabledList: isHeaderEnabledList,
isParamEnabledList: isParamEnabledList,
requestBodyContentType: requestBodyContentType,
requestBody: requestBody,
requestFormDataList: requestFormDataList,
responseStatus: responseStatus,
message: message,
responseModel: responseModel,
);
//print(newModel);
var map = {...state!};
map[id] = newModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ResponseDetails extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
var sm = ScaffoldMessenger.of(context);
final responseStatus = ref.watch(
selectedRequestModelProvider.select((value) => value?.responseStatus));
final message = ref
Expand All @@ -46,6 +47,14 @@ class ResponseDetails extends ConsumerWidget {
responseStatus: responseStatus,
message: message,
time: responseModel?.time,
onClearResponse: () {
final selectedRequest = ref.read(selectedRequestModelProvider);
ref
.read(collectionStateNotifierProvider.notifier)
.clearResponse(selectedRequest?.id);
sm.hideCurrentSnackBar();
sm.showSnackBar(getSnackBar('Response cleared'));
},
),
const Expanded(
child: ResponseTabs(),
Expand Down
27 changes: 9 additions & 18 deletions lib/widgets/buttons.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:apidash/providers/providers.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:apidash/utils/utils.dart';
import 'package:apidash/consts.dart';
Expand Down Expand Up @@ -238,28 +236,21 @@ class SaveButton extends StatelessWidget {
}
}

class ClearResponseButton extends ConsumerWidget {
const ClearResponseButton({super.key});
class ClearResponseButton extends StatelessWidget {
const ClearResponseButton({
super.key,
this.onPressed,
});

final VoidCallback? onPressed;

@override
Widget build(BuildContext context, WidgetRef ref) {
var sm = ScaffoldMessenger.of(context);
Widget build(BuildContext context) {
return Tooltip(
message: 'Clear response',
child: TextButton(
style: TextButton.styleFrom(minimumSize: const Size(40, 40)),
onPressed: () {
final selectedId = ref.watch(selectedIdStateProvider);
ref
.read(collectionStateNotifierProvider.notifier)
.clearResponse(selectedId!);
sm.hideCurrentSnackBar();
sm.showSnackBar(
const SnackBar(
content: Text('Response cleared'),
),
);
},
onPressed: onPressed,
child: const Icon(
Icons.delete,
size: 20,
Expand Down
6 changes: 5 additions & 1 deletion lib/widgets/response_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ class ResponsePaneHeader extends StatelessWidget {
this.responseStatus,
this.message,
this.time,
this.onClearResponse,
});

final int? responseStatus;
final String? message;
final Duration? time;
final VoidCallback? onClearResponse;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -117,7 +119,9 @@ class ResponsePaneHeader extends StatelessWidget {
),
),
kHSpacer10,
const ClearResponseButton()
ClearResponseButton(
onPressed: onClearResponse,
)
],
),
),
Expand Down

0 comments on commit 9b40763

Please sign in to comment.