Skip to content

Commit

Permalink
refactor: OrderSubmissionStatusDialog to TaskStatusDialog
Browse files Browse the repository at this point in the history
The `OrderSubmissionStatusDialog` is actually useful for any kind of task showing a pending, failed or success state.
  • Loading branch information
holzeis committed Sep 15, 2023
1 parent 4acd051 commit 9e0c11a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 92 deletions.
18 changes: 2 additions & 16 deletions mobile/lib/common/recover_dlc_change_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:get_10101/bridge_generated/bridge_definitions.dart' as bridge;
import 'package:get_10101/common/application/event_service.dart';
import 'package:get_10101/common/domain/background_task.dart';
import 'package:get_10101/common/global_keys.dart';
import 'package:get_10101/features/trade/order_submission_status_dialog.dart';
import 'package:get_10101/common/task_status_dialog.dart';
import 'package:provider/provider.dart';

class RecoverDlcChangeNotifier extends ChangeNotifier implements Subscriber {
Expand All @@ -28,22 +28,8 @@ class RecoverDlcChangeNotifier extends ChangeNotifier implements Subscriber {
context: shellNavigatorKey.currentContext!,
builder: (context) {
TaskStatus status = context.watch<RecoverDlcChangeNotifier>().taskStatus;

// todo(holzeis): Reusing the order submission status dialog is not nice, but it's actually suitable for any task execution that has pending,
// failed and success states. We may should consider renaming this dialog for its more generic purpose.
OrderSubmissionStatusDialogType type = OrderSubmissionStatusDialogType.pendingSubmit;
switch (status) {
case TaskStatus.pending:
type = OrderSubmissionStatusDialogType.successfulSubmit;
case TaskStatus.failed:
type = OrderSubmissionStatusDialogType.failedFill;
case TaskStatus.success:
type = OrderSubmissionStatusDialogType.filled;
}

late Widget content = const Text("Recovering your dlc channel");

return OrderSubmissionStatusDialog(title: "Catching up!", type: type, content: content);
return TaskStatusDialog(title: "Catching up!", status: status, content: content);
},
);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
import 'package:confetti/confetti.dart';
import 'package:flutter/material.dart';
import 'package:get_10101/common/domain/background_task.dart';
import 'package:go_router/go_router.dart';

enum OrderSubmissionStatusDialogType {
pendingSubmit,
successfulSubmit,
filled,
failedFill,
failedSubmit
}

class OrderSubmissionStatusDialog extends StatefulWidget {
class TaskStatusDialog extends StatefulWidget {
final String title;
final OrderSubmissionStatusDialogType type;
final TaskStatus status;
final Widget content;
final String buttonText;
final EdgeInsets insetPadding;
final String navigateToRoute;

const OrderSubmissionStatusDialog(
const TaskStatusDialog(
{super.key,
required this.title,
required this.type,
required this.status,
required this.content,
this.buttonText = "Close",
this.insetPadding = const EdgeInsets.all(50),
this.navigateToRoute = ""});

@override
State<OrderSubmissionStatusDialog> createState() => _OrderSubmissionStatusDialog();
State<TaskStatusDialog> createState() => _TaskStatusDialog();
}

class _OrderSubmissionStatusDialog extends State<OrderSubmissionStatusDialog> {
class _TaskStatusDialog extends State<TaskStatusDialog> {
late final ConfettiController _confettiController;

@override
Expand All @@ -48,8 +41,7 @@ class _OrderSubmissionStatusDialog extends State<OrderSubmissionStatusDialog> {

@override
Widget build(BuildContext context) {
bool isPending = widget.type == OrderSubmissionStatusDialogType.successfulSubmit ||
widget.type == OrderSubmissionStatusDialogType.pendingSubmit;
bool isPending = widget.status == TaskStatus.pending;

WidgetsBinding.instance.addPostFrameCallback((_) {
_confettiController.play();
Expand All @@ -67,18 +59,16 @@ class _OrderSubmissionStatusDialog extends State<OrderSubmissionStatusDialog> {

AlertDialog dialog = AlertDialog(
icon: (() {
switch (widget.type) {
case OrderSubmissionStatusDialogType.pendingSubmit:
case OrderSubmissionStatusDialogType.successfulSubmit:
switch (widget.status) {
case TaskStatus.pending:
return const Center(
child: SizedBox(width: 20, height: 20, child: CircularProgressIndicator()));
case OrderSubmissionStatusDialogType.failedFill:
case OrderSubmissionStatusDialogType.failedSubmit:
case TaskStatus.failed:
return const Icon(
Icons.cancel,
color: Colors.red,
);
case OrderSubmissionStatusDialogType.filled:
case TaskStatus.success:
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -101,14 +91,12 @@ class _OrderSubmissionStatusDialog extends State<OrderSubmissionStatusDialog> {
}
})(),
title: Text("${widget.title} ${(() {
switch (widget.type) {
case OrderSubmissionStatusDialogType.pendingSubmit:
case OrderSubmissionStatusDialogType.successfulSubmit:
switch (widget.status) {
case TaskStatus.pending:
return "Pending";
case OrderSubmissionStatusDialogType.filled:
case TaskStatus.success:
return "Success";
case OrderSubmissionStatusDialogType.failedSubmit:
case OrderSubmissionStatusDialogType.failedFill:
case TaskStatus.failed:
return "Failure";
}
})()}"),
Expand Down
14 changes: 7 additions & 7 deletions mobile/lib/features/trade/async_order_change_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import 'package:get_10101/bridge_generated/bridge_definitions.dart' as bridge;
import 'package:get_10101/common/application/event_service.dart';
import 'package:get_10101/common/domain/background_task.dart';
import 'package:get_10101/common/global_keys.dart';
import 'package:get_10101/common/task_status_dialog.dart';
import 'package:get_10101/features/trade/application/order_service.dart';
import 'package:get_10101/features/trade/domain/order.dart';
import 'package:get_10101/features/trade/order_submission_status_dialog.dart';
import 'package:provider/provider.dart';

class AsyncOrderChangeNotifier extends ChangeNotifier implements Subscriber {
Expand Down Expand Up @@ -39,16 +39,16 @@ class AsyncOrderChangeNotifier extends ChangeNotifier implements Subscriber {
builder: (context) {
Order? asyncOrder = context.watch<AsyncOrderChangeNotifier>().asyncOrder;

OrderSubmissionStatusDialogType type = OrderSubmissionStatusDialogType.pendingSubmit;
TaskStatus status = TaskStatus.pending;
switch (asyncOrder?.state) {
case OrderState.open:
type = OrderSubmissionStatusDialogType.successfulSubmit;
status = TaskStatus.pending;
case OrderState.failed:
type = OrderSubmissionStatusDialogType.failedFill;
status = TaskStatus.failed;
case OrderState.filled:
type = OrderSubmissionStatusDialogType.filled;
status = TaskStatus.success;
case null:
type = OrderSubmissionStatusDialogType.pendingSubmit;
status = TaskStatus.pending;
}

late Widget content;
Expand All @@ -60,7 +60,7 @@ class AsyncOrderChangeNotifier extends ChangeNotifier implements Subscriber {
content = Container();
}

return OrderSubmissionStatusDialog(title: "Catching up!", type: type, content: content);
return TaskStatusDialog(title: "Catching up!", status: status, content: content);
},
);
} else if (event is bridge.Event_OrderUpdateNotification) {
Expand Down
18 changes: 2 additions & 16 deletions mobile/lib/features/trade/rollover_change_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:get_10101/bridge_generated/bridge_definitions.dart' as bridge;
import 'package:get_10101/common/application/event_service.dart';
import 'package:get_10101/common/domain/background_task.dart';
import 'package:get_10101/common/global_keys.dart';
import 'package:get_10101/features/trade/order_submission_status_dialog.dart';
import 'package:get_10101/common/task_status_dialog.dart';
import 'package:provider/provider.dart';

class RolloverChangeNotifier extends ChangeNotifier implements Subscriber {
Expand All @@ -29,22 +29,8 @@ class RolloverChangeNotifier extends ChangeNotifier implements Subscriber {
context: shellNavigatorKey.currentContext!,
builder: (context) {
TaskStatus status = context.watch<RolloverChangeNotifier>().taskStatus;

// todo(holzeis): Reusing the order submission status dialog is not nice, but it's actually suitable for any task execution that has pending,
// failed and success states. We may should consider renaming this dialog for its more generic purpose.
OrderSubmissionStatusDialogType type = OrderSubmissionStatusDialogType.pendingSubmit;
switch (status) {
case TaskStatus.pending:
type = OrderSubmissionStatusDialogType.successfulSubmit;
case TaskStatus.failed:
type = OrderSubmissionStatusDialogType.failedFill;
case TaskStatus.success:
type = OrderSubmissionStatusDialogType.filled;
}

late Widget content = const Text("Rolling over your position");

return OrderSubmissionStatusDialog(title: "Catching up!", type: type, content: content);
return TaskStatusDialog(title: "Catching up!", status: status, content: content);
},
);
} else {
Expand Down
41 changes: 16 additions & 25 deletions mobile/lib/features/trade/trade_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import 'dart:io' show Platform;

import 'package:candlesticks/candlesticks.dart';
import 'package:flutter/material.dart';
import 'package:get_10101/common/domain/background_task.dart';
import 'package:get_10101/common/domain/model.dart';
import 'package:get_10101/common/task_status_dialog.dart';
import 'package:get_10101/common/value_data_row.dart';
import 'package:get_10101/features/trade/candlestick_change_notifier.dart';
import 'package:get_10101/features/trade/contract_symbol_icon.dart';
Expand All @@ -13,19 +18,15 @@ import 'package:get_10101/features/trade/position_change_notifier.dart';
import 'package:get_10101/features/trade/position_list_item.dart';
import 'package:get_10101/features/trade/submit_order_change_notifier.dart';
import 'package:get_10101/features/trade/trade_bottom_sheet.dart';
import 'package:candlesticks/candlesticks.dart';
import 'package:get_10101/features/trade/trade_bottom_sheet_confirmation.dart';
import 'package:get_10101/features/trade/trade_tabs.dart';
import 'package:get_10101/features/trade/trade_theme.dart';
import 'package:get_10101/features/trade/trade_value_change_notifier.dart';
import 'package:get_10101/util/constants.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:get_10101/util/constants.dart';
import 'package:share_plus/share_plus.dart';
import 'package:social_share/social_share.dart';
import 'dart:io' show Platform;

import 'order_submission_status_dialog.dart';

class TradeScreen extends StatelessWidget {
static const route = "/trade";
Expand Down Expand Up @@ -73,31 +74,21 @@ class TradeScreen extends StatelessWidget {

switch (state) {
case PendingOrderState.submitting:
return OrderSubmissionStatusDialog(
title: "Submit Order",
type: OrderSubmissionStatusDialogType.pendingSubmit,
content: body);
return TaskStatusDialog(
title: "Submit Order", status: TaskStatus.pending, content: body);
case PendingOrderState.submittedSuccessfully:
return OrderSubmissionStatusDialog(
title: "Fill Order",
type: OrderSubmissionStatusDialogType.successfulSubmit,
content: body);
return TaskStatusDialog(
title: "Fill Order", status: TaskStatus.success, content: body);
case PendingOrderState.submissionFailed:
// TODO: This failure case has to be handled differently; are we planning to show orders that failed to submit in the order history?
return OrderSubmissionStatusDialog(
title: "Submit Order",
type: OrderSubmissionStatusDialogType.failedSubmit,
content: body);
return TaskStatusDialog(
title: "Submit Order", status: TaskStatus.failed, content: body);
case PendingOrderState.orderFilled:
return OrderSubmissionStatusDialog(
title: "Fill Order",
type: OrderSubmissionStatusDialogType.filled,
content: body);
return TaskStatusDialog(
title: "Fill Order", status: TaskStatus.success, content: body);
case PendingOrderState.orderFailed:
return OrderSubmissionStatusDialog(
title: "Fill Order",
type: OrderSubmissionStatusDialogType.failedFill,
content: body);
return TaskStatusDialog(
title: "Fill Order", status: TaskStatus.failed, content: body);
}
},
);
Expand Down

0 comments on commit 9e0c11a

Please sign in to comment.