File tree 3 files changed +42
-0
lines changed
3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ import 'package:flutter/material.dart' ;
2
+
3
+ import 'widgets/app.dart' ;
1
4
2
5
/// Whether [debugLog] should do anything.
3
6
///
@@ -30,3 +33,19 @@ bool debugLog(String message) {
30
33
}());
31
34
return true ;
32
35
}
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
+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
5
5
import 'package:flutter/scheduler.dart' ;
6
6
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart' ;
7
7
8
+ import '../log.dart' ;
8
9
import '../model/localizations.dart' ;
9
10
import '../model/narrow.dart' ;
10
11
import 'about_zulip.dart' ;
@@ -92,9 +93,14 @@ class ZulipApp extends StatefulWidget {
92
93
/// Useful in tests.
93
94
final List <NavigatorObserver >? navigatorObservers;
94
95
96
+ static void _reportErrorToUserBriefly (String message) {
97
+ scaffoldMessenger? .showSnackBar (SnackBar (content: Text (message)));
98
+ }
99
+
95
100
void _declareReady () {
96
101
assert (navigatorKey.currentContext != null );
97
102
_ready.value = true ;
103
+ reportErrorToUserBriefly = _reportErrorToUserBriefly;
98
104
}
99
105
100
106
@override
Original file line number Diff line number Diff line change 1
1
import 'package:checks/checks.dart' ;
2
2
import 'package:flutter/material.dart' ;
3
3
import 'package:flutter_test/flutter_test.dart' ;
4
+ import 'package:zulip/log.dart' ;
4
5
import 'package:zulip/model/database.dart' ;
5
6
import 'package:zulip/widgets/app.dart' ;
6
7
import 'package:zulip/widgets/inbox.dart' ;
@@ -164,5 +165,21 @@ void main() {
164
165
check (ZulipApp .scaffoldMessenger).isNotNull ();
165
166
check (ZulipApp .ready).value.isTrue ();
166
167
});
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
+ });
167
184
});
168
185
}
You can’t perform that action at this time.
0 commit comments