Skip to content

Commit

Permalink
Add fluttertoast module, DebugUtils#exception, failed, toast, alert
Browse files Browse the repository at this point in the history
  • Loading branch information
djkim committed Jul 31, 2019
1 parent 59f2d2d commit 45bf9c9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
77 changes: 77 additions & 0 deletions lib/base/debug_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,82 @@
import 'package:fimber/fimber.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'noti_manager.dart';

class DebugUtils {
static exception(String tag, String message, dynamic ex) {
if (kReleaseMode) {
// TODO send to firebase
} else {
Log.e(tag, message, ex: ex);
NotiManager.instance.showAlert(tag, message, ex.toString());
toast(tag, "$message\n${ex.toString()}");
}
}

static failed(String tag, String message) {
if (kReleaseMode) {
// TODO send to firebase
} else {
Log.e(tag, message);
NotiManager.instance.showAlert(tag, message, currentStackTrace());
toast(tag, message);
}
}

static toast(String tag, String message) {
if (!kReleaseMode) {
Fluttertoast.showToast(msg: "[$tag]\n$message");
}
}

static alert(BuildContext context, String tag, String message) {
if (kReleaseMode) {
return;
}

showDialog(
context: context,
barrierDismissible: false,
builder: (_) {
return AlertDialog(
title: Text(tag),
content: Text(message + "\n\n" + currentStackTrace()),
actions: <Widget>[
FlatButton(
child: Text("CANCEL"),
onPressed: () => Navigator.pop(context))
],
);
});
}

static void checkNotNull(Object o, {String message}) {
if (!kReleaseMode && o == null) {
NotiManager.instance
.showAlert("checkNotNull!!", message, currentStackTrace());
toast("checkNotNull!!", "$message\n\n${currentStackTrace()}");
}
}

static void checkState(bool state, {String message}) {
if (!kReleaseMode && state == false) {
NotiManager.instance
.showAlert("checkState!!", message, currentStackTrace());
toast("checkState!!", "$message\n\n${currentStackTrace()}");
}
}

static void notify(String tag, String message) {
if (!kReleaseMode) {
NotiManager.instance.showAlert(tag, message, currentStackTrace());
}
}

static String currentStackTrace() => StackTrace.current.toString();
}

class Log {
static initialize() {
Expand Down
19 changes: 11 additions & 8 deletions lib/base/noti_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class NotiManager {
FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin;

NotiManager._privateConstructor() {
init();
_init();
}

void init() {
void _init() {
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
// If you have skipped STEP 3 then change app_icon to @mipmap/ic_launcher
var initializationSettingsAndroid =
new AndroidInitializationSettings('app_icon');
new AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
Expand All @@ -22,20 +22,23 @@ class NotiManager {

static final NotiManager instance = NotiManager._privateConstructor();

void showAlert(String message) async {
void showAlert(String title, String message, String payload) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'CHANNEL_FOR_DEBUG', 'Debug', 'Notification for debug',
playSound: false, importance: Importance.Max, priority: Priority.High);
playSound: false,
importance: Importance.Default,
priority: Priority.Default,
autoCancel: false);
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails(presentSound: false);
new IOSNotificationDetails(presentSound: false);
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await _flutterLocalNotificationsPlugin.show(
message.hashCode,
'Debug!!',
title,
message,
platformChannelSpecifics,
payload: message,
payload: payload,
);
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies:

# notification
flutter_local_notifications:
fluttertoast:

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 45bf9c9

Please sign in to comment.