Skip to content

Commit

Permalink
Toast handler supported in all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
jhomlala committed Apr 2, 2021
1 parent 1c1245b commit dd6a154
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 26 deletions.
5 changes: 0 additions & 5 deletions example/lib/basic_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import 'package:flutter/material.dart';

void main() {
CatcherOptions debugOptions = CatcherOptions(DialogReportMode(), [
//EmailManualHandler(["[email protected]"]),
ToastHandler(),
HttpHandler(HttpRequestType.post,
Uri.parse("https://jsonplaceholder.typicode.com/posts"),
printLogs: true),
ConsoleHandler()
]);
CatcherOptions releaseOptions = CatcherOptions(PageReportMode(), [
EmailManualHandler(["[email protected]"])
Expand Down
14 changes: 11 additions & 3 deletions lib/core/catcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class Catcher with ReportModeAction {
}

///Remove excluded parameters from device parameters.
void _removeExcludedParameters(){
void _removeExcludedParameters() {
_currentConfig.excludedParameters.forEach((parameter) {
_deviceParameters.remove(parameter);
});
Expand Down Expand Up @@ -607,7 +607,15 @@ class Catcher with ReportModeAction {
return;
}

reportHandler.handle(report).catchError((dynamic handlerError) {
if (reportHandler.isContextRequired() && !_isContextValid()) {
_logger.warning(
"Couldn't use report handler because you didn't provide navigator key. Add navigator key to use this report mode.");
return;
}

reportHandler
.handle(report, _getContext())
.catchError((dynamic handlerError) {
_logger.warning(
"Error occurred in ${reportHandler.toString()}: ${handlerError.toString()}");
}).then((result) {
Expand All @@ -618,7 +626,7 @@ class Catcher with ReportModeAction {
_cachedReports.remove(report);
}
}).timeout(Duration(milliseconds: _currentConfig.handlerTimeout),
onTimeout: () {
onTimeout: () {
_logger.warning(
"${reportHandler.toString()} failed to report error because of timeout");
});
Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/console_handler.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:catcher/model/platform_type.dart';
import 'package:catcher/model/report.dart';
import 'package:catcher/model/report_handler.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';

class ConsoleHandler extends ReportHandler {
Expand All @@ -18,7 +19,7 @@ class ConsoleHandler extends ReportHandler {
});

@override
Future<bool> handle(Report report) {
Future<bool> handle(Report report, BuildContext? context) {
_logger.info(
"============================== CATCHER LOG ==============================");
_logger.info("Crash occured on ${report.dateTime}");
Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/discord_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:catcher/model/report.dart';
import 'package:catcher/model/report_handler.dart';
import 'package:catcher/utils/catcher_utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';

class DiscordHandler extends ReportHandler {
Expand All @@ -29,7 +30,7 @@ class DiscordHandler extends ReportHandler {
});

@override
Future<bool> handle(Report report) async {
Future<bool> handle(Report report, BuildContext? context) async {
if (report.platformType != PlatformType.web) {
if (!(await CatcherUtils.isInternetConnectionAvailable())) {
_printLog("No internet connection available");
Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/email_auto_handler.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:catcher/handlers/base_email_handler.dart';
import 'package:catcher/model/platform_type.dart';
import 'package:catcher/model/report.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';
Expand Down Expand Up @@ -44,7 +45,7 @@ class EmailAutoHandler extends BaseEmailHandler {
);

@override
Future<bool> handle(Report error) {
Future<bool> handle(Report error, BuildContext? context) {
return _sendMail(error);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/email_manual_handler.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:catcher/handlers/base_email_handler.dart';
import 'package:catcher/model/platform_type.dart';
import 'package:catcher/model/report.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mailer/flutter_mailer.dart';
import 'package:logging/logging.dart';

Expand Down Expand Up @@ -31,7 +32,7 @@ class EmailManualHandler extends BaseEmailHandler {
);

@override
Future<bool> handle(Report report) async {
Future<bool> handle(Report report, BuildContext? context) async {
return _sendEmail(report);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/file_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'package:catcher/model/platform_type.dart';
import 'package:catcher/model/report.dart';
import 'package:catcher/model/report_handler.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';

class FileHandler extends ReportHandler {
Expand All @@ -29,7 +30,7 @@ class FileHandler extends ReportHandler {
});

@override
Future<bool> handle(Report report) async {
Future<bool> handle(Report report, BuildContext? context) async {
try {
if (!_fileValidated) {
_fileValidationResult = await _checkFile();
Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/http_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:catcher/model/report.dart';
import 'package:catcher/model/report_handler.dart';
import 'package:catcher/utils/catcher_utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';

class HttpHandler extends ReportHandler {
Expand Down Expand Up @@ -37,7 +38,7 @@ class HttpHandler extends ReportHandler {
}) : headers = headers ?? <String, dynamic>{};

@override
Future<bool> handle(Report error) async {
Future<bool> handle(Report error, BuildContext? context) async {
if (error.platformType != PlatformType.web) {
if (!(await CatcherUtils.isInternetConnectionAvailable())) {
_printLog("No internet connection available");
Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/sentry_handler.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:catcher/model/platform_type.dart';
import 'package:catcher/model/report.dart';
import 'package:catcher/model/report_handler.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:sentry/sentry.dart';

Expand Down Expand Up @@ -42,7 +43,7 @@ class SentryHandler extends ReportHandler {
});

@override
Future<bool> handle(Report error) async {
Future<bool> handle(Report error, BuildContext? context) async {
try {
_printLog("Logging to sentry...");

Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/slack_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:catcher/model/report.dart';
import 'package:catcher/model/report_handler.dart';
import 'package:catcher/utils/catcher_utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';

//Slack webhook API doesn't allow file attachments
Expand Down Expand Up @@ -31,7 +32,7 @@ class SlackHandler extends ReportHandler {
this.enableCustomParameters = false});

@override
Future<bool> handle(Report report) async {
Future<bool> handle(Report report, BuildContext? context) async {
try {
if (!(await CatcherUtils.isInternetConnectionAvailable())) {
_printLog("No internet connection available");
Expand Down
106 changes: 97 additions & 9 deletions lib/handlers/toast_handler.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:catcher/core/application_profile_manager.dart';
import 'package:catcher/model/platform_type.dart';
import 'package:catcher/model/report.dart';
import 'package:catcher/model/report_handler.dart';
Expand All @@ -23,16 +24,41 @@ class ToastHandler extends ReportHandler {
this.customMessage,
});

FToast? fToast;

@override
Future<bool> handle(Report error) async {
Fluttertoast.showToast(
msg: _getErrorMessage(error),
toastLength: _getLength(),
gravity: _getGravity(),
timeInSecForIosWeb: _getLengthIos(),
backgroundColor: backgroundColor,
textColor: textColor,
fontSize: textSize);
Future<bool> handle(Report error, BuildContext? buildContext) async {
if (ApplicationProfileManager.isAndroid() ||
ApplicationProfileManager.isIos() ||
ApplicationProfileManager.isWeb()) {
Fluttertoast.showToast(
msg: _getErrorMessage(error),
toastLength: _getLength(),
gravity: _getGravity(),
timeInSecForIosWeb: _getLengthIos(),
backgroundColor: backgroundColor,
textColor: textColor,
fontSize: textSize);
} else {
Future.delayed(
const Duration(milliseconds: 500),
() {
Navigator.push<void>(
buildContext!,
PageRouteBuilder(
opaque: false,
pageBuilder: (_, __, ___) => FlutterToastPage(
_getErrorMessage(error),
_getGravity(),
Duration(seconds: _getLengthIos()),
backgroundColor,
textColor,
textSize),
),
);
},
);
}

return true;
}
Expand Down Expand Up @@ -78,4 +104,66 @@ class ToastHandler extends ReportHandler {
PlatformType.android,
PlatformType.iOS,
];

@override
bool isContextRequired() {
return true;
}
}

class FlutterToastPage extends StatefulWidget {
final String text;
final ToastGravity gravity;
final Duration duration;
final Color backgroundColor;
final Color textColor;
final double textSize;

const FlutterToastPage(this.text, this.gravity, this.duration,
this.backgroundColor, this.textColor, this.textSize,
{Key? key})
: super(key: key);

@override
_FlutterToastPageState createState() => _FlutterToastPageState();
}

class _FlutterToastPageState extends State<FlutterToastPage> {
FToast fToast = FToast();

@override
void initState() {
fToast.init(context);
WidgetsBinding.instance!.addPostFrameCallback((_) {
showToast();
});
super.initState();
}

void showToast() {
fToast.showToast(
child: Container(
color: widget.backgroundColor,
child: Text(
widget.text,
style: TextStyle(
color: widget.textColor,
fontSize: widget.textSize,
),
),
),
gravity: widget.gravity,
toastDuration: widget.duration);
Future.delayed(
Duration(milliseconds: widget.duration.inMilliseconds + 100),
() {
Navigator.of(context).pop();
},
);
}

@override
Widget build(BuildContext context) {
return const SizedBox();
}
}
8 changes: 7 additions & 1 deletion lib/model/report_handler.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'package:catcher/model/platform_type.dart';
import 'package:catcher/model/report.dart';
import 'package:flutter/material.dart';

import 'localization_options.dart';

abstract class ReportHandler {
/// Method called when report has been accepted by user
Future<bool> handle(Report error);
Future<bool> handle(Report error, BuildContext? context);

/// Get list of supported platforms
List<PlatformType> getSupportedPlatforms();
Expand All @@ -22,4 +23,9 @@ abstract class ReportHandler {
void setLocalizationOptions(LocalizationOptions? localizationOptions) {
_localizationOptions = localizationOptions;
}

/// Check if given report mode requires context to run
bool isContextRequired() {
return false;
}
}

0 comments on commit dd6a154

Please sign in to comment.