Skip to content

Commit

Permalink
Reset feedback form, even when closing fails (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy authored Nov 28, 2023
1 parent eeddef1 commit b61757b
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 112 deletions.
28 changes: 16 additions & 12 deletions lib/src/core/wiredash_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,23 @@ class WiredashModel with ChangeNotifier {
Future<void> hide({
bool discardFeedback = false,
}) async {
await services.backdropController.animateToClosed();
isWiredashActive = false;

// reset options from show() call
themeFromContext = null;
feedbackOptionsOverride = null;

if (discardFeedback) {
services.discardFeedback();
try {
await services.backdropController.animateToClosed();

isWiredashActive = false;
// reset options from show() call
themeFromContext = null;
feedbackOptionsOverride = null;
} catch (e) {
// might fail when the user holds the app open while hide is called
} finally {
if (discardFeedback) {
services.discardFeedback();
}
// always discard promoter score rating on close
services.discardPs();
notifyListeners();
}
// always discard promoter score rating on close
services.discardPs();
notifyListeners();
}

/// Collects metadata from the user via [Wiredash.collectMetaData] or
Expand Down
8 changes: 4 additions & 4 deletions lib/src/feedback/feedback_backdrop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FeedbackBackdrop extends StatelessWidget {
contentBuilder: (context) {
return WiredashFeedbackFlow(
// this allows discarding feedback in the message step
key: ValueKey(context.feedbackModel),
key: ValueKey(context.watchFeedbackModel),
);
},
foregroundLayerBuilder: (c, r, mq) {
Expand Down Expand Up @@ -67,7 +67,7 @@ Widget? _buildForegroundLayer(
return BackButtonAction.consumed;
}

context.feedbackModel.cancelScreenshotCapturingMode();
context.readFeedbackModel.cancelScreenshotCapturingMode();
return BackButtonAction.consumed;
}
return BackButtonAction.ignored;
Expand Down Expand Up @@ -109,7 +109,7 @@ Widget? _buildForegroundLayer(
),
offset: Offset(
0,
context.feedbackModel.feedbackFlowStatus ==
context.watchFeedbackModel.feedbackFlowStatus ==
FeedbackFlowStatus.screenshotDrawing
? 0
: 1,
Expand Down Expand Up @@ -178,7 +178,7 @@ Widget? _buildBackgroundLayer(
),
offset: Offset(
0,
context.feedbackModel.feedbackFlowStatus ==
context.watchFeedbackModel.feedbackFlowStatus ==
FeedbackFlowStatus.screenshotNavigating
? 0
: 4,
Expand Down
25 changes: 12 additions & 13 deletions lib/src/feedback/feedback_flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ class _WiredashFeedbackFlowState extends State<WiredashFeedbackFlow>
@override
void initState() {
super.initState();
_index =
FeedbackModelProvider.of(context, listen: false).currentStepIndex ?? 0;
_index = context.readFeedbackModel.currentStepIndex ?? 0;
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
if (_index >= context.feedbackModel.steps.length) {
_index = context.feedbackModel.steps.length - 1;
if (_index >= context.watchFeedbackModel.steps.length) {
_index = context.watchFeedbackModel.steps.length - 1;
}

final oldIndex = _index;
final newIndex = context.feedbackModel.currentStepIndex;
final newIndex = context.watchFeedbackModel.currentStepIndex;
if (newIndex == null) {
// state not in stack, stay at current page
return;
Expand All @@ -52,31 +51,31 @@ class _WiredashFeedbackFlowState extends State<WiredashFeedbackFlow>

@override
Widget build(BuildContext context) {
final feedbackModel = context.feedbackModel;
final larryPageView = LarryPageView(
key: _lpvKey,
stepCount: feedbackModel.steps.length,
stepCount: context.watchFeedbackModel.steps.length,
pageIndex: _index,
onPageChanged: (index) {
setState(() {
_index = index;
final stepIndex = feedbackModel.currentStepIndex;
final stepIndex = context.readFeedbackModel.currentStepIndex;
if (stepIndex == null) {
return;
}

if (stepIndex < _index) {
feedbackModel.goToNextStep();
context.readFeedbackModel.goToNextStep();
}

if (stepIndex > _index) {
feedbackModel.goToPreviousStep();
context.readFeedbackModel.goToPreviousStep();
}
});
},
builder: (context) {
final index = _index;
final FeedbackFlowStatus status = () {
final feedbackModel = context.watchFeedbackModel;
if (feedbackModel.steps.length <= index) {
final stackIndex = feedbackModel.currentStepIndex;
if (stackIndex == null) {
Expand Down Expand Up @@ -125,11 +124,11 @@ class _WiredashFeedbackFlowState extends State<WiredashFeedbackFlow>
if (_index == 0) {
return BackButtonAction.ignored;
}
feedbackModel.goToPreviousStep();
context.readFeedbackModel.goToPreviousStep();
return BackButtonAction.consumed;
},
child: Form(
key: feedbackModel.stepFormKey,
key: context.watchFeedbackModel.stepFormKey,
child: larryPageView,
),
),
Expand All @@ -154,7 +153,7 @@ class FeedbackProgressIndicator extends StatefulWidget {
class _FeedbackProgressIndicatorState extends State<FeedbackProgressIndicator> {
@override
Widget build(BuildContext context) {
final feedbackModel = context.feedbackModel;
final feedbackModel = context.watchFeedbackModel;
final stepIndex = feedbackModel.indexForFlowStatus(widget.flowStatus);
var currentStep = stepIndex + 1;
final total = feedbackModel.maxSteps;
Expand Down
18 changes: 5 additions & 13 deletions lib/src/feedback/feedback_model_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@ class FeedbackModelProvider extends InheritedNotifier<FeedbackModel> {
required FeedbackModel feedbackModel,
required super.child,
}) : super(notifier: feedbackModel);

static FeedbackModel of(BuildContext context, {bool listen = true}) {
if (listen) {
return context
.dependOnInheritedWidgetOfExactType<FeedbackModelProvider>()!
.notifier!;
} else {
return context
.findAncestorWidgetOfExactType<FeedbackModelProvider>()!
.notifier!;
}
}
}

extension FeedbackModelExtension on BuildContext {
FeedbackModel get feedbackModel => FeedbackModelProvider.of(this);
FeedbackModel get watchFeedbackModel =>
dependOnInheritedWidgetOfExactType<FeedbackModelProvider>()!.notifier!;

FeedbackModel get readFeedbackModel =>
findAncestorWidgetOfExactType<FeedbackModelProvider>()!.notifier!;
}
12 changes: 6 additions & 6 deletions lib/src/feedback/steps/step_1_feedback_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class _Step1FeedbackMessageState extends State<Step1FeedbackMessage>
void initState() {
super.initState();
_controller = TextEditingController(
text: FeedbackModelProvider.of(context, listen: false).feedbackMessage,
text: context.readFeedbackModel.feedbackMessage,
)..addListener(() {
final text = _controller.text;
if (context.feedbackModel.feedbackMessage != text) {
context.feedbackModel.feedbackMessage = text;
if (context.watchFeedbackModel.feedbackMessage != text) {
context.watchFeedbackModel.feedbackMessage = text;
}
});
}
Expand Down Expand Up @@ -116,9 +116,9 @@ class _Step1FeedbackMessageState extends State<Step1FeedbackMessage>
child: TronButton(
label: context.l10n.feedbackNextButton,
trailingIcon: Wirecons.arrow_right,
onTap: context.feedbackModel.feedbackMessage == null
? context.feedbackModel.validateForm
: context.feedbackModel.goToNextStep,
onTap: context.readFeedbackModel.feedbackMessage == null
? context.readFeedbackModel.validateForm
: context.readFeedbackModel.goToNextStep,
),
),
],
Expand Down
6 changes: 3 additions & 3 deletions lib/src/feedback/steps/step_2_labels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class _Step2LabelsState extends State<Step2Labels>
with TickerProviderStateMixin {
@override
Widget build(BuildContext context) {
final feedbackModel = context.feedbackModel;
final feedbackModel = context.watchFeedbackModel;
final selectedLabels = feedbackModel.selectedLabels;
return StepPageScaffold(
indicator: const FeedbackProgressIndicator(
Expand Down Expand Up @@ -54,12 +54,12 @@ class _Step2LabelsState extends State<Step2Labels>
color: context.theme.secondaryColor,
leadingIcon: Wirecons.arrow_left,
label: context.l10n.feedbackBackButton,
onTap: context.feedbackModel.goToPreviousStep,
onTap: context.readFeedbackModel.goToPreviousStep,
),
TronButton(
label: context.l10n.feedbackNextButton,
trailingIcon: Wirecons.arrow_right,
onTap: context.feedbackModel.goToNextStep,
onTap: context.readFeedbackModel.goToNextStep,
),
],
),
Expand Down
21 changes: 11 additions & 10 deletions lib/src/feedback/steps/step_3_screenshot_overview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class _Step3ScreenshotOverviewState extends State<Step3ScreenshotOverview> {
WiredashBackdrop.maybeOf(context)?.animateSizeChange = true;
},
child: () {
if (!context.feedbackModel.hasAttachments) {
if (!context.watchFeedbackModel.hasAttachments) {
return const Step3NoAttachments();
}
return const Step3WithGallery();
Expand Down Expand Up @@ -66,7 +66,7 @@ class _Step3NoAttachmentsState extends State<Step3NoAttachments> {
color: context.theme.secondaryColor,
leadingIcon: Wirecons.arrow_left,
label: context.l10n.feedbackBackButton,
onTap: context.feedbackModel.goToPreviousStep,
onTap: context.watchFeedbackModel.goToPreviousStep,
),
const SizedBox(width: 10),
Expanded(
Expand All @@ -84,15 +84,15 @@ class _Step3NoAttachmentsState extends State<Step3NoAttachments> {
trailingIcon: Wirecons.chevron_double_right,
onTap: () async {
if (!mounted) return;
await context.feedbackModel.skipScreenshot();
await context.readFeedbackModel.skipScreenshot();
},
),
TronButton(
label: context.l10n
.feedbackStep3ScreenshotOverviewAddScreenshotButton,
trailingIcon: Wirecons.arrow_right,
maxWidth: 250,
onTap: () => context.feedbackModel
onTap: () => context.readFeedbackModel
.enterScreenshotCapturingMode(),
),
],
Expand Down Expand Up @@ -138,7 +138,8 @@ class Step3WithGallery extends StatelessWidget {
child: Center(
child: Row(
children: [
for (final att in context.feedbackModel.attachments)
for (final att
in context.watchFeedbackModel.attachments)
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: constraints.maxWidth / 2.5,
Expand All @@ -148,7 +149,7 @@ class Step3WithGallery extends StatelessWidget {
child: AttachmentPreview(attachment: att),
),
),
if (context.feedbackModel.attachments.length < 3)
if (context.watchFeedbackModel.attachments.length < 3)
const NewAttachment(),
],
),
Expand All @@ -163,12 +164,12 @@ class Step3WithGallery extends StatelessWidget {
color: context.theme.secondaryColor,
leadingIcon: Wirecons.arrow_left,
label: context.l10n.feedbackBackButton,
onTap: context.feedbackModel.goToPreviousStep,
onTap: context.readFeedbackModel.goToPreviousStep,
),
TronButton(
label: context.l10n.feedbackNextButton,
trailingIcon: Wirecons.arrow_right,
onTap: context.feedbackModel.goToNextStep,
onTap: context.readFeedbackModel.goToNextStep,
),
],
),
Expand Down Expand Up @@ -213,7 +214,7 @@ class AttachmentPreview extends StatelessWidget {
child: TronButton(
color: context.theme.primaryContainerColor,
onTap: () {
context.feedbackModel.deleteAttachment(attachment);
context.readFeedbackModel.deleteAttachment(attachment);
},
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
Expand All @@ -239,7 +240,7 @@ class NewAttachment extends StatelessWidget {
aspectRatio: context.theme.windowSize.aspectRatio,
child: AnimatedClickTarget(
onTap: () {
context.feedbackModel.enterScreenshotCapturingMode();
context.readFeedbackModel.enterScreenshotCapturingMode();
},
builder: (context, state, anims) {
Color hoverColorAdjustment(Color color) {
Expand Down
19 changes: 9 additions & 10 deletions lib/src/feedback/steps/step_5_email.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ class _Step5EmailState extends State<Step5Email> with TickerProviderStateMixin {
@override
void initState() {
super.initState();
final feedbackModel = FeedbackModelProvider.of(context, listen: false);
_controller = TextEditingController(
// read, not watch, because initState
text: feedbackModel.hasEmailBeenEdited
? feedbackModel.userEmail
text: context.readFeedbackModel.hasEmailBeenEdited
? context.readFeedbackModel.userEmail
: WiredashModelProvider.of(context, listen: false)
.customizableMetaData
.userEmail,
)..addListener(() {
final text = _controller.text;
if (context.feedbackModel.userEmail != text) {
context.feedbackModel.userEmail = text;
if (context.readFeedbackModel.userEmail != text) {
context.readFeedbackModel.userEmail = text;
}
});
widgetsBindingInstance.addPostFrameCallback((_) {
if (!mounted) return;
feedbackModel.userEmail = _controller.text;
context.readFeedbackModel.userEmail = _controller.text;
});
}

Expand Down Expand Up @@ -74,8 +73,8 @@ class _Step5EmailState extends State<Step5Email> with TickerProviderStateMixin {
cursorColor: context.theme.primaryColor,
style: context.text.input.onSurface,
onFieldSubmitted: (_) {
if (context.feedbackModel.validateForm()) {
context.feedbackModel.goToNextStep();
if (context.readFeedbackModel.validateForm()) {
context.readFeedbackModel.goToNextStep();
}
},
validator: (data) {
Expand Down Expand Up @@ -115,14 +114,14 @@ class _Step5EmailState extends State<Step5Email> with TickerProviderStateMixin {
color: context.theme.secondaryColor,
leadingIcon: Wirecons.arrow_left,
label: context.l10n.feedbackBackButton,
onTap: context.feedbackModel.goToPreviousStep,
onTap: context.readFeedbackModel.goToPreviousStep,
),
TronButton(
label: context.l10n.feedbackNextButton,
trailingIcon: Wirecons.arrow_right,
onTap: () {
try {
context.feedbackModel.goToNextStep();
context.readFeedbackModel.goToNextStep();
} on FormValidationException {
// validator triggered, TextFormField shows error
}
Expand Down
Loading

0 comments on commit b61757b

Please sign in to comment.