Skip to content

Commit

Permalink
Refactor of the toast widget and deletion of snackbar.dart (#813)
Browse files Browse the repository at this point in the history
* Replacing extra widgets for toasts with Custom Toast and making it uniform

* removed snackbar.dart due to no implementation

* flutter format

* merge conflict resolved
  • Loading branch information
nayakastha authored Jun 16, 2021
1 parent 79cb88d commit e07d4eb
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 204 deletions.
82 changes: 26 additions & 56 deletions lib/controllers/org_controller.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//flutter packages are called here
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';
import 'package:talawa/controllers/auth_controller.dart';

//pages are called here
import 'package:talawa/services/preferences.dart';
import 'package:talawa/services/queries_.dart';
import 'package:talawa/utils/custom_toast.dart';
import 'package:talawa/utils/globals.dart';
import 'package:talawa/utils/gql_client.dart';
import 'package:talawa/views/pages/home_page.dart';
Expand Down Expand Up @@ -82,7 +83,7 @@ class OrgController with ChangeNotifier {
);

if (result.hasException || userDetailsResult.hasException) {
print(result.exception);
debugPrint(result.exception.toString());
showError(result.exception.toString());
} else if (!result.hasException &&
!disposed &&
Expand All @@ -105,7 +106,7 @@ class OrgController with ChangeNotifier {

// Filtering out organizations that are already joined by user.
joinedOrganizationsIds.forEach((e) {
print(e);
debugPrint(e.toString());
organizationInfo =
organizationInfo.where((element) => element['_id'] != e).toList();
});
Expand All @@ -114,7 +115,7 @@ class OrgController with ChangeNotifier {
}

/// FUNCTION CALLED TP JOIN THE PRIVATE ORGANIZATION
Future joinPrivateOrg(BuildContext context, FToast fToast, String itemIndex,
Future joinPrivateOrg(BuildContext context, String itemIndex,
{@required bool fromProfile}) async {
final GraphQLClient _client = graphQLConfiguration.authClient();

Expand All @@ -124,14 +125,14 @@ class OrgController with ChangeNotifier {
if (result.hasException &&
result.exception.toString().substring(16) == accessTokenException) {
_authController.getNewToken();
return joinPrivateOrg(context, fToast, itemIndex,
fromProfile: fromProfile);
return joinPrivateOrg(context, itemIndex, fromProfile: fromProfile);
} else if (result.hasException &&
result.exception.toString().substring(16) != accessTokenException) {
_exceptionToast(result.exception.toString().substring(16), fToast);
CustomToast.exceptionToast(
msg: result.exception.toString().substring(16),
);
} else if (!result.hasException && !result.loading) {
print(result.data);
_successToast("Request Sent to Organization Admin", fToast);
CustomToast.sucessToast(msg: "Request Sent to Organization Admin");

if (fromProfile) {
Navigator.pop(context);
Expand All @@ -147,22 +148,30 @@ class OrgController with ChangeNotifier {

/// Function which will be called if the person wants to join the public organization
Future<void> joinPublicOrg(
String orgName, String itemIndex, FToast fToast, BuildContext context,
String orgName, String itemIndex, BuildContext context,
{bool fromProfile}) async {
final GraphQLClient _client = graphQLConfiguration.authClient();
debugPrint(orgName);

print(orgName);

final QueryResult result = await _client
.mutate(MutationOptions(documentNode: gql(_query.getOrgId(itemIndex))));
final QueryResult result = await _client.mutate(
MutationOptions(
documentNode: gql(
_query.getOrgId(
itemIndex,
),
),
),
);

if (result.hasException &&
result.exception.toString().substring(16) == accessTokenException) {
_authController.getNewToken();
_exceptionToast(result.exception.toString().substring(16), fToast);
CustomToast.exceptionToast(
msg: result.exception.toString().substring(16));
} else if (result.hasException &&
result.exception.toString().substring(16) != accessTokenException) {
_exceptionToast(result.exception.toString().substring(16), fToast);
CustomToast.exceptionToast(
msg: result.exception.toString().substring(16));
} else if (!result.hasException && !result.loading) {
joinedOrg =
result.data['joinPublicOrganization']['joinedOrganizations'] as List;
Expand Down Expand Up @@ -203,7 +212,7 @@ class OrgController with ChangeNotifier {
}
}
}
_successToast("Success!", fToast);
CustomToast.sucessToast(msg: "Success!");

if (fromProfile) {
pushNewScreen(
Expand Down Expand Up @@ -231,43 +240,4 @@ class OrgController with ChangeNotifier {
),
);
}

_successToast(String msg, FToast fToast) {
final Widget toast = Container(
padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
color: Colors.green,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(msg),
],
),
);

fToast.showToast(
child: toast,
gravity: ToastGravity.BOTTOM,
toastDuration: const Duration(seconds: 3),
);
}

_exceptionToast(String msg, FToast fToast) {
final Widget toast = Container(
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 14.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
color: Colors.red,
),
child: Text(msg),
);

fToast.showToast(
child: toast,
gravity: ToastGravity.BOTTOM,
toastDuration: const Duration(seconds: 3),
);
}
}
7 changes: 2 additions & 5 deletions lib/utils/loghelper.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:io';

import 'package:flutter_logs/flutter_logs.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:path_provider/path_provider.dart';
import 'package:talawa/utils/custom_toast.dart';

class LogHelper {
init() async {
Expand Down Expand Up @@ -45,10 +45,7 @@ class LogHelper {
// var responseCode = await ApiFunctions().sendLogs(file.path);
// if(responseCode == 200){
// }
Fluttertoast.showToast(
msg: "The Logs are saved in ${file.path}",
toastLength: Toast.LENGTH_LONG,
);
CustomToast.sucessToast(msg: "The Logs are saved in ${file.path}");
} else {
FlutterLogs.logError(
"LogHelper",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:provider/provider.dart';
import 'package:talawa/controllers/post_controller.dart';
Expand Down Expand Up @@ -45,8 +44,10 @@ class NewsArticleViewModel extends BaseModel {
this.context = context;
this.index = index;
_commentController = TextEditingController(
text: Provider.of<CommentHandler>(context, listen: false)
.comment(post.id.toString()));
text: Provider.of<CommentHandler>(context, listen: false).comment(
post.id.toString(),
),
);
_commentController.addListener(_notifyData);
fetchUserDetails();
}
Expand Down Expand Up @@ -133,24 +134,22 @@ class NewsArticleViewModel extends BaseModel {
FocusScope.of(context).unfocus();
String queryText = '';
if (commentController.text.isNotEmpty) {
Fluttertoast.showToast(msg: "Adding Comment...");
CustomToast.sucessToast(msg: "Adding Comment...");

queryText = commentController.text.replaceAll("\n", newLineKey).trim();
final Map result =
await Queries().createComments(post.id.toString(), queryText) as Map;
if (result == null) {
Fluttertoast.showToast(
msg: "Sorry, this comment could not be posted.",
);
CustomToast.exceptionToast(
msg: "Sorry, this comment could not be posted.");
} else {
_isCommentAdded = true;
FocusScope.of(context).requestFocus(FocusNode());
commentController.text = '';
await Fluttertoast.showToast(
msg: "Comment added.",
);
CustomToast.exceptionToast(msg: "Comment added.");
}
} else {
Fluttertoast.showToast(msg: "Please write comment");
CustomToast.exceptionToast(msg: "Please write comment");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';
import 'package:talawa/enums/event_recurrance.dart';
import 'package:talawa/enums/viewstate.dart';
Expand Down
36 changes: 18 additions & 18 deletions lib/view_models/page_view_model/join_organization_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:talawa/controllers/auth_controller.dart';
import 'package:talawa/enums/exception_type.dart';
import 'package:talawa/enums/org_filter.dart';
import 'package:talawa/services/exception.dart';
import 'package:talawa/services/preferences.dart';
import 'package:talawa/services/queries_.dart';
import 'package:talawa/utils/custom_toast.dart';
import 'package:talawa/utils/gql_client.dart';
import 'package:talawa/view_models/base_model.dart';
import 'package:talawa/views/widgets/shared/toast_container.dart';

import '../../locator.dart';

class JoinOrgnizationViewModel extends BaseModel {
final Queries _query = Queries();
final Preferences _pref = Preferences();
String token, _itemIndex, _isPublic, _currentUserId = "";
GraphQLConfiguration graphQLConfiguration = locator<GraphQLConfiguration>();
FToast fToast;

List _organizationInfo = [],
joinedOrg = [],
joinedOrganizations = [],
Expand All @@ -41,8 +42,6 @@ class JoinOrgnizationViewModel extends BaseModel {
String get isPublic => _isPublic;
String get itemIndex => _itemIndex;
void initialise(BuildContext context, OrganisationFilter filter) {
fToast = FToast();
fToast.init(context);
fetchOrg(filter);
}

Expand Down Expand Up @@ -144,13 +143,6 @@ class JoinOrgnizationViewModel extends BaseModel {
}
}

showToast(String msg, Color color) {
fToast.showToast(
child: toastContainer(msg, color),
gravity: ToastGravity.BOTTOM,
toastDuration: const Duration(seconds: 3));
}

void searchOrgName(String orgName) {
//it is the search bar to search the organization
_filteredOrgInfo.clear();
Expand Down Expand Up @@ -238,15 +230,18 @@ class JoinOrgnizationViewModel extends BaseModel {
final ExceptionType exceptionType = retrieveExceptionType(result);
if (exceptionType == ExceptionType.accesstokenException) {
_authController.getNewToken();
showToast(result.exception.toString().substring(16), Colors.red);
CustomToast.exceptionToast(
msg: result.exception.toString().substring(16));

return joinPrivateOrg(context);
} else {
showToast(result.exception.toString().substring(16), Colors.red);
CustomToast.exceptionToast(
msg: result.exception.toString().substring(16));
}
return;
}
if (!result.loading) {
showToast("Request Sent to Organization Admin", Colors.green);
CustomToast.sucessToast(msg: "Request Sent to Organization Admin");
Navigator.pop(context);
}
}
Expand All @@ -261,10 +256,14 @@ class JoinOrgnizationViewModel extends BaseModel {
final ExceptionType exceptionType = retrieveExceptionType(result);
if (exceptionType == ExceptionType.accesstokenException) {
_authController.getNewToken();
showToast(result.exception.toString().substring(16), Colors.red);
CustomToast.exceptionToast(
msg: result.exception.toString().substring(16));

return joinPublicOrg(context, orgName);
} else {
showToast(result.exception.toString().substring(16), Colors.red);
CustomToast.exceptionToast(
msg: result.exception.toString().substring(16),
);
}
return;
}
Expand Down Expand Up @@ -306,7 +305,8 @@ class JoinOrgnizationViewModel extends BaseModel {
}
}
}
showToast("Success!", Colors.green);
CustomToast.sucessToast(msg: "Success!");

Navigator.pop(context);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/views/pages/login_signup/login_form.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//flutter packages are called here

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

Expand Down
5 changes: 1 addition & 4 deletions lib/views/pages/organization/join_organization_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ class _JoinOrganizationState extends State<JoinOrganization> {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
TextEditingController searchController = TextEditingController();

FToast fToast;
bool disposed = false;
String searchText;
String filter = "Show All";

@override
void initState() {
super.initState();
fToast = FToast();
fToast.init(context);

Provider.of<OrgController>(
context,
listen: false,
Expand Down Expand Up @@ -158,7 +156,6 @@ class _JoinOrganizationState extends State<JoinOrganization> {
color: Colors.white,
child: OrganizationBody(
fromProfile: widget.fromProfile,
fToast: fToast,
scaffoldKey: scaffoldKey,
filter: filter,
query: searchText,
Expand Down
Loading

0 comments on commit e07d4eb

Please sign in to comment.