Skip to content

Commit 2dd2123

Browse files
gnpricePIG208
authored andcommitted
log: Support reportErrorToUserBriefly.
The imports in `lib/log.dart` are for the code references in reportErrorToUserBriefly's dart doc.
1 parent 86e3a95 commit 2dd2123

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

lib/log.dart

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import 'package:flutter/material.dart';
2+
3+
import 'widgets/app.dart';
14

25
/// Whether [debugLog] should do anything.
36
///
@@ -30,3 +33,19 @@ bool debugLog(String message) {
3033
}());
3134
return true;
3235
}
36+
37+
/// Display an error message.
38+
///
39+
/// This shows a [SnackBar] containing the message after app startup,
40+
/// otherwise logs it to the console.
41+
///
42+
/// See also: [ZulipApp._reportErrorToUserBriefly]
43+
void Function(String message) reportErrorToUserBriefly = _defaultReportErrorToUserBriefly;
44+
45+
void _defaultReportErrorToUserBriefly(String message) {
46+
// If this callback is still in place, then the app's widget tree
47+
// hasn't mounted yet even as far as the [Navigator].
48+
// So there's not much we can do to tell the user;
49+
// just log, in case the user is actually a developer watching the console.
50+
assert(debugLog(message));
51+
}

lib/widgets/app.dart

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
55
import 'package:flutter/scheduler.dart';
66
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
77

8+
import '../log.dart';
89
import '../model/localizations.dart';
910
import '../model/narrow.dart';
1011
import 'about_zulip.dart';
@@ -92,9 +93,14 @@ class ZulipApp extends StatefulWidget {
9293
/// Useful in tests.
9394
final List<NavigatorObserver>? navigatorObservers;
9495

96+
static void _reportErrorToUserBriefly(String message) {
97+
scaffoldMessenger?.showSnackBar(SnackBar(content: Text(message)));
98+
}
99+
95100
void _declareReady() {
96101
assert(navigatorKey.currentContext != null);
97102
_ready.value = true;
103+
reportErrorToUserBriefly = _reportErrorToUserBriefly;
98104
}
99105

100106
@override

test/widgets/app_test.dart

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:checks/checks.dart';
22
import 'package:flutter/material.dart';
33
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:zulip/log.dart';
45
import 'package:zulip/model/database.dart';
56
import 'package:zulip/widgets/app.dart';
67
import 'package:zulip/widgets/inbox.dart';
@@ -164,5 +165,21 @@ void main() {
164165
check(ZulipApp.scaffoldMessenger).isNotNull();
165166
check(ZulipApp.ready).value.isTrue();
166167
});
168+
169+
testWidgets('reportErrorToUserBriefly shows snack bar', (tester) async {
170+
await tester.pumpWidget(const ZulipApp());
171+
172+
// Prior to app startup, reportErrorToUserBriefly only logs.
173+
reportErrorToUserBriefly('test reportErrorToUserBriefly');
174+
check(ZulipApp.ready).value.isFalse();
175+
await tester.pump();
176+
check(find.text('test reportErrorToUserBriefly').evaluate()).isEmpty();
177+
check(ZulipApp.ready).value.isTrue();
178+
179+
// After app startup, reportErrorToUserBriefly displays a snack bar.
180+
reportErrorToUserBriefly('test reportErrorToUserBriefly');
181+
await tester.pump();
182+
check(find.text('test reportErrorToUserBriefly').evaluate()).single;
183+
});
167184
});
168185
}

0 commit comments

Comments
 (0)