Skip to content

Commit

Permalink
Fixed issue snackbar
Browse files Browse the repository at this point in the history
  • Loading branch information
JGeek00 committed Oct 9, 2022
1 parent d532411 commit 66e41af
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 184 deletions.
24 changes: 24 additions & 0 deletions lib/functions/snackbar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';

import 'package:adguard_home_manager/providers/app_config_provider.dart';

void showSnacbkar({
required BuildContext context,
required AppConfigProvider appConfigProvider,
required String label,
required Color color
}) async {
if (appConfigProvider.showingSnackbar == true) {
ScaffoldMessenger.of(context).clearSnackBars();
await Future.delayed(const Duration(milliseconds: 500));
}
appConfigProvider.setShowingSnackbar(true);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(label),
backgroundColor: color,
)
).closed.then((value) => appConfigProvider.setShowingSnackbar(false));
}
2 changes: 1 addition & 1 deletion lib/l10n/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
"addCustomRule": "Añadir regla personalizada",
"removeCustomRule": "Eliminar regla personalizada",
"removeCustomRuleMessage": "¿Estás seguro que deseas eliminar esta regla personalizada?",
"updatingRules": "Updating custom rules...",
"updatingRules": "Actualizando reglas personalizadas...",
"ruleRemovedSuccessfully": "Regla eliminada correctamente",
"ruleNotRemoved": "No se ha podido eliminar la regla",
"ruleAddedSuccessfully": "Regla añadida correctamente",
Expand Down
7 changes: 2 additions & 5 deletions lib/providers/app_config_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,8 @@ class AppConfigProvider with ChangeNotifier {
notifyListeners();
}

void setShowingSnackbar() async {
_showingSnackbar = true;
notifyListeners();
await Future.delayed(const Duration(milliseconds: 4500));
_showingSnackbar = false;
void setShowingSnackbar(bool status) async {
_showingSnackbar = status;
notifyListeners();
}

Expand Down
49 changes: 25 additions & 24 deletions lib/screens/clients/added_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:adguard_home_manager/screens/clients/fab.dart';
import 'package:adguard_home_manager/screens/clients/options_modal.dart';
import 'package:adguard_home_manager/screens/clients/client_modal.dart';

import 'package:adguard_home_manager/functions/snackbar.dart';
import 'package:adguard_home_manager/classes/process_modal.dart';
import 'package:adguard_home_manager/services/http_requests.dart';
import 'package:adguard_home_manager/models/clients.dart';
Expand Down Expand Up @@ -87,22 +88,22 @@ class _AddedListState extends State<AddedList> {
}
}).toList();
serversProvider.setClientsData(clientsData);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.clientUpdatedSuccessfully),
backgroundColor: Colors.green,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientUpdatedSuccessfully,
color: Colors.green
);
}
else {
appConfigProvider.addLog(result['log']);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.clientNotUpdated),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientNotUpdated,
color: Colors.red
);
}
}
Expand All @@ -119,22 +120,22 @@ class _AddedListState extends State<AddedList> {
ClientsData clientsData = serversProvider.clients.data!;
clientsData.clients = clientsData.clients.where((c) => c.name != client.name).toList();
serversProvider.setClientsData(clientsData);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.clientDeletedSuccessfully),
backgroundColor: Colors.green,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientDeletedSuccessfully,
color: Colors.green
);
}
else {
appConfigProvider.addLog(result['log']);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.clientNotDeleted),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientNotDeleted,
color: Colors.red
);
}
}
Expand Down
60 changes: 30 additions & 30 deletions lib/screens/clients/fab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import 'package:adguard_home_manager/screens/clients/client_modal.dart';

import 'package:adguard_home_manager/functions/snackbar.dart';
import 'package:adguard_home_manager/models/clients.dart';
import 'package:adguard_home_manager/services/http_requests.dart';
import 'package:adguard_home_manager/models/clients_allowed_blocked.dart';
Expand Down Expand Up @@ -52,31 +53,30 @@ class ClientsFab extends StatelessWidget {
blockedHosts: body['blocked_hosts'] ?? [],
)
);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.clientAddedSuccessfully),
backgroundColor: Colors.green,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientAddedSuccessfully,
color: Colors.green
);
}
else if (result['result'] == 'error' && result['message'] == 'client_another_list') {
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.clientAnotherList),
backgroundColor: Colors.red,
)
showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientAnotherList,
color: Colors.red
);
}
else {
appConfigProvider.addLog(result['log']);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.clientNotAdded),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientNotAdded,
color: Colors.red
);
}
}
Expand All @@ -93,22 +93,22 @@ class ClientsFab extends StatelessWidget {
ClientsData clientsData = serversProvider.clients.data!;
clientsData.clients.add(client);
serversProvider.setClientsData(clientsData);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.clientAddedSuccessfully),
backgroundColor: Colors.green,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientAddedSuccessfully,
color: Colors.green
);
}
else {
appConfigProvider.addLog(result['log']);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.clientNotAdded),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientNotAdded,
color: Colors.red
);
}
}
Expand Down
25 changes: 13 additions & 12 deletions lib/screens/filters/custom_rules_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:adguard_home_manager/screens/filters/fab.dart';
import 'package:adguard_home_manager/screens/filters/remove_custom_rule_modal.dart';

import 'package:adguard_home_manager/functions/snackbar.dart';
import 'package:adguard_home_manager/models/filtering.dart';
import 'package:adguard_home_manager/providers/app_config_provider.dart';
import 'package:adguard_home_manager/services/http_requests.dart';
Expand Down Expand Up @@ -75,22 +76,22 @@ class _CustomRulesListState extends State<CustomRulesList> {
FilteringData filteringData = serversProvider.filtering.data!;
filteringData.userRules = newRules;
serversProvider.setFilteringData(filteringData);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.ruleRemovedSuccessfully),
backgroundColor: Colors.green,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.ruleRemovedSuccessfully,
color: Colors.green
);
}
else {
appConfigProvider.addLog(result['log']);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.ruleNotRemoved),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.ruleNotRemoved,
color: Colors.red
);
}
}
Expand Down
86 changes: 43 additions & 43 deletions lib/screens/filters/fab.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// ignore_for_file: use_build_context_synchronously

import 'package:adguard_home_manager/screens/filters/add_list_modal.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import 'package:adguard_home_manager/screens/filters/add_custom_rule.dart';
import 'package:adguard_home_manager/screens/filters/add_list_modal.dart';

import 'package:adguard_home_manager/functions/snackbar.dart';
import 'package:adguard_home_manager/services/http_requests.dart';
import 'package:adguard_home_manager/classes/process_modal.dart';
import 'package:adguard_home_manager/providers/app_config_provider.dart';
Expand Down Expand Up @@ -41,22 +42,22 @@ class FiltersFab extends StatelessWidget {
FilteringData filteringData = serversProvider.filtering.data!;
filteringData.userRules = newRules;
serversProvider.setFilteringData(filteringData);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.ruleAddedSuccessfully),
backgroundColor: Colors.green,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.ruleAddedSuccessfully,
color: Colors.green
);
}
else {
appConfigProvider.addLog(result['log']);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.ruleNotAdded),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.ruleNotAdded,
color: Colors.red
);
}
}
Expand Down Expand Up @@ -98,56 +99,55 @@ class FiltersFab extends StatelessWidget {

processModal.close();

appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("${AppLocalizations.of(context)!.listAdded} $items."),
backgroundColor: Colors.green,
)
showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: "${AppLocalizations.of(context)!.listAdded} $items.",
color: Colors.green
);
}
else {
processModal.close();
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.listNotAdded),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.listNotAdded,
color: Colors.red
);
}
}
else if (result1['result'] == 'error' && result1['log'].statusCode == '400' && result1['log'].resBody.toString().contains("Couldn't fetch filter from url")) {
processModal.close();
appConfigProvider.addLog(result1['log']);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.listUrlInvalid),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.listUrlInvalid,
color: Colors.red
);
}
else if (result1['result'] == 'error' && result1['log'].statusCode == '400' && result1['log'].resBody.toString().contains('Filter URL already added')) {
processModal.close();
appConfigProvider.addLog(result1['log']);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.listAlreadyAdded),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.listAlreadyAdded,
color: Colors.red
);
}
else {
processModal.close();
appConfigProvider.addLog(result1['log']);
appConfigProvider.setShowingSnackbar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.listNotAdded),
backgroundColor: Colors.red,
)

showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.listNotAdded,
color: Colors.red
);
}
}
Expand Down
Loading

0 comments on commit 66e41af

Please sign in to comment.