Skip to content

Commit

Permalink
Fixes #1378 Update Talawa to give a meaningful message if the URL isn…
Browse files Browse the repository at this point in the history
…'t available. (#1380)

* feat: Add proper error handling if url not available.

* Fix #1378 Update Talawa to give a meaningful message if the URL isn't available.

* fixes the failed checks.

* Make the suggested changes

* Add error widget

* Add custom error snackbar for Talawa

* Make the notif bar scrollable.

* Remove the useless part

* Add dialog box with scroll instead of snackbar

* added the custom widget

* added the custom widget

* Completed the request changes.

* removed all the warnings

* changed the app-setting test

* Add test for custom error widget and did err-handling in qrScan

* removed all warnings

* Update build.gradle

* Delete google-services.json

* Update build.gradle

* Delete GoogleService-Info.plist

* Delete firebase_app_id_file.json
  • Loading branch information
Ayush0Chaudhary authored Jan 27, 2023
1 parent 9765102 commit eeabcb5
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 24 deletions.
7 changes: 6 additions & 1 deletion lib/services/database_mutation_functions.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:talawa/locator.dart';
Expand Down Expand Up @@ -37,7 +39,10 @@ class DataBaseMutationFunctions {
if (exception.linkException != null) {
debugPrint(exception.linkException.toString());
if (showSnackBar) {
navigationService.showSnackBar("Server not running/wrong url");
Timer(const Duration(seconds: 2), () {
navigationService
.showTalawaErrorDialog("Server not running/wrong url");
});
}
return false;
}
Expand Down
28 changes: 28 additions & 0 deletions lib/services/navigation_service.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:flutter/material.dart';

import 'package:talawa/widgets/talawa_error_dialog.dart';
import 'package:talawa/widgets/talawa_error_widget.dart';

class NavigationService {
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

Expand Down Expand Up @@ -62,6 +65,31 @@ class NavigationService {
);
}

void showTalawaErrorWidget(
String errorMessage, {
Duration duration = const Duration(seconds: 2),
}) {
ScaffoldMessenger.of(navigatorKey.currentContext!).showSnackBar(SnackBar(
padding: EdgeInsets.zero,
content: TalawaErrorWidget(
errorMessage: errorMessage,
),
// backgroundColor: Colors.grey,
backgroundColor: const Color.fromRGBO(65, 65, 66, 1),
));
}

void showTalawaErrorDialog(String errorMessage) {
showDialog(
context: navigatorKey.currentContext!,
barrierColor: Colors.transparent,
barrierDismissible: false,
builder: (BuildContext context) {
return TalawaErrorDialog(errorMessage);
},
);
}

void pop() {
return navigatorKey.currentState!.pop();
}
Expand Down
21 changes: 17 additions & 4 deletions lib/view_model/pre_auth_view_models/set_url_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/validators.dart';
Expand Down Expand Up @@ -87,8 +88,10 @@ class SetUrlViewModel extends BaseModel {
graphqlConfig.getOrgUrl();
navigationService.pushScreen(navigateTo, arguments: argument);
} else {
navigationService
.showSnackBar("URL doesn't exist/no connection please check");
// navigationService
// .showSnackBar("URL doesn't exist/no connection please check");
navigationService.showTalawaErrorWidget(
"URL doesn't exist/no connection please check");
}
}
}
Expand Down Expand Up @@ -165,9 +168,19 @@ class SetUrlViewModel extends BaseModel {
graphqlConfig.getOrgUrl();
Navigator.pop(navigationService.navigatorKey.currentContext!);
navigationService.pushScreen('/selectOrg', arguments: orgId);
} on CameraException catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorWidget("The Camera is not working");
} on QrEmbeddedImageException catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorDialog("The QR is not Working");
} on QrUnsupportedVersionException catch (e) {
debugPrint(e.toString());
navigationService
.showTalawaErrorDialog("This QR version is not Supported.");
} on Exception catch (e) {
print(e);
print('invalid app qr');
debugPrint(e.toString());
navigationService.showTalawaErrorWidget("This QR is not for the App");
}
}
});
Expand Down
7 changes: 6 additions & 1 deletion lib/widgets/organization_list.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
Expand All @@ -16,6 +18,7 @@ class OrganizationList extends StatelessWidget {
@override
Widget build(BuildContext context) {
model.organizations = [];
int noOfRefetch = 0;
return GraphQLProvider(
client: ValueNotifier<GraphQLClient>(graphqlConfig.clientToQuery()),
child: Query(
Expand All @@ -34,13 +37,15 @@ class OrganizationList extends StatelessWidget {
if (result.hasException) {
final isException = databaseFunctions.encounteredExceptionOrError(
result.exception!,
showSnackBar: false,
showSnackBar: noOfRefetch == 0,
);
if (isException != null) {
if (isException) {
refetch!();
noOfRefetch++;
} else {
refetch!();
noOfRefetch++;
}
}
} else {
Expand Down
35 changes: 35 additions & 0 deletions lib/widgets/talawa_error_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';

class TalawaErrorDialog extends StatelessWidget {
const TalawaErrorDialog(this.errorMessage, {Key? key}) : super(key: key);
final String errorMessage;
@override
Widget build(BuildContext context) {
return SizedBox(
child: AlertDialog(
title: const Text(
"Error",
style: TextStyle(color: Colors.red),
),
content: SizedBox(
width: 200,
height: 75,
child: AutoSizeText(
errorMessage,
style: const TextStyle(fontSize: 20),
maxLines: 3,
),
),
actions: <Widget>[
TextButton(
child: const Text('Close'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
);
}
}
40 changes: 40 additions & 0 deletions lib/widgets/talawa_error_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter/material.dart';

class TalawaErrorWidget extends StatelessWidget {
const TalawaErrorWidget({Key? key, required this.errorMessage})
: super(key: key);
final String errorMessage;
@override
Widget build(BuildContext context) {
return Row(
children: [
Container(
width: 20,
height: 80,
decoration: const BoxDecoration(color: Colors.red),
),
const SizedBox(
width: 10,
),
const Icon(
Icons.error,
color: Colors.red,
size: 35,
),
const SizedBox(
width: 10,
),
Expanded(
flex: 1,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Text(
errorMessage,
style: const TextStyle(color: Colors.white),
),
),
)
],
);
}
}
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.9.0"
auto_size_text:
dependency: "direct main"
description:
name: auto_size_text
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
boolean_selector:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
auto_size_text: ^3.0.0
cached_network_image: ^3.1.0
connectivity_plus: ^2.3.1
cupertino_icons: ^1.0.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:mockito/mockito.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/validators.dart';
Expand Down Expand Up @@ -89,8 +90,8 @@ Future<void> main() async {

await model.checkURLandNavigate('/', 'arguments');

verify(navigationService
.showSnackBar("URL doesn't exist/no connection please check"));
verify(navigationService.showTalawaErrorWidget(
"URL doesn't exist/no connection please check"));
});

testWidgets('Check if scanQR() is working fine', (tester) async {
Expand Down Expand Up @@ -123,7 +124,82 @@ Future<void> main() async {
.onQRViewCreated(controller);
});

testWidgets('Check if _onQRViewCreated() is working fine when throws',
testWidgets(
'Check if _onQRViewCreated() is working fine when throws CameraException',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: TestWidget(model),
navigatorKey: navigationService.navigatorKey,
),
);

final controller = MockQRViewController();
when(controller.scannedDataStream).thenAnswer((_) async* {
yield Barcode('qr?orgId=1&scan', BarcodeFormat.qrcode, null);
});
// when(controller.stopCamera())
// .thenThrow(Exception({"errorType": "error"}));

when(controller.stopCamera())
.thenThrow(CameraException("200", "cameraException"));
await tester.tap(find.byType(FloatingActionButton));
await tester.pump();

(tester.widget(find.byType(QRView)) as QRView)
.onQRViewCreated(controller);
});
testWidgets(
'Check if _onQRViewCreated() is working fine when throws QrEmbeddedImageException',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: TestWidget(model),
navigatorKey: navigationService.navigatorKey,
),
);

final controller = MockQRViewController();
when(controller.scannedDataStream).thenAnswer((_) async* {
yield Barcode('qr?orgId=1&scan', BarcodeFormat.qrcode, null);
});
// when(controller.stopCamera())
// .thenThrow(Exception({"errorType": "error"}));

when(controller.stopCamera())
.thenThrow(QrEmbeddedImageException("error"));
await tester.tap(find.byType(FloatingActionButton));
await tester.pump();

(tester.widget(find.byType(QRView)) as QRView)
.onQRViewCreated(controller);
});
testWidgets(
'Check if _onQRViewCreated() is working fine when throws QrUnsupportedVersionException',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: TestWidget(model),
navigatorKey: navigationService.navigatorKey,
),
);

final controller = MockQRViewController();
when(controller.scannedDataStream).thenAnswer((_) async* {
yield Barcode('qr?orgId=1&scan', BarcodeFormat.qrcode, null);
});
// when(controller.stopCamera())
// .thenThrow(Exception({"errorType": "error"}));

when(controller.stopCamera()).thenThrow(QrUnsupportedVersionException(0));
await tester.tap(find.byType(FloatingActionButton));
await tester.pump();

(tester.widget(find.byType(QRView)) as QRView)
.onQRViewCreated(controller);
});
testWidgets(
'Check if _onQRViewCreated() is working fine when throws Exception',
(tester) async {
await tester.pumpWidget(
MaterialApp(
Expand All @@ -136,8 +212,10 @@ Future<void> main() async {
when(controller.scannedDataStream).thenAnswer((_) async* {
yield Barcode('qr?orgId=1&scan', BarcodeFormat.qrcode, null);
});
when(controller.stopCamera()).thenThrow(Exception());
// when(controller.stopCamera())
// .thenThrow(Exception({"errorType": "error"}));

when(controller.stopCamera()).thenThrow(Exception(0));
await tester.tap(find.byType(FloatingActionButton));
await tester.pump();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
import 'package:talawa/constants/constants.dart';
import 'package:talawa/constants/custom_theme.dart';
import 'package:talawa/models/language/language_model.dart';
import 'package:talawa/models/organization/org_info.dart';
import 'package:talawa/models/user/user_info.dart';
import 'package:talawa/router.dart' as router;
import 'package:talawa/services/graphql_config.dart';
import 'package:talawa/services/navigation_service.dart';
Expand Down Expand Up @@ -90,14 +85,14 @@ Future<void> main() async {
locator<GraphqlConfig>().test();
locator<SizeConfig>().test();

final Directory dir = Directory('temporaryPath');
Hive
..init(dir.path)
..registerAdapter(UserAdapter())
..registerAdapter(OrgInfoAdapter());
await Hive.openBox<User>('currentUser');
await Hive.openBox<OrgInfo>('currentOrg');
await Hive.openBox('url');
// final Directory dir = Directory('temporaryPath');
// Hive
// ..init(dir.path)
// ..registerAdapter(UserAdapter())
// ..registerAdapter(OrgInfoAdapter());
// await Hive.openBox<User>('currentUser');
// await Hive.openBox<OrgInfo>('currentOrg');
// await Hive.openBox('url');
group('Setting Page Screen Widget Test in dark mode', () {
testWidgets("Testing if Settings Screen shows up", (tester) async {
await tester.pumpWidget(createChangePassScreenDark());
Expand Down
1 change: 0 additions & 1 deletion test/widget_tests/widgets/custom_alert_dialog_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
// import 'package:talawa/locator.dart' as loc;
import 'package:talawa/services/navigation_service.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/app_localization.dart';
Expand Down
Loading

0 comments on commit eeabcb5

Please sign in to comment.