Skip to content

Commit

Permalink
fixes for refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
tpkowastaken committed Jul 16, 2023
1 parent 6a4fe76 commit 75e4a4f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 48 deletions.
4 changes: 4 additions & 0 deletions lib/methods/icanteen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class CanteenData {

late Canteen canteenInstance;
late CanteenData canteenData;
bool refreshing = false;

/// Returns a [Canteen] instance with logged in user.
/// Has to be called before using [canteenInstance].
Expand Down Expand Up @@ -143,6 +144,9 @@ Future<Jidelnicek> ziskatJidelnicekDen(DateTime den) async {
Future<void> preIndexLunches(DateTime start, int howManyDays) async {
for (int i = 0; i < howManyDays; i++) {
try {
if (refreshing) {
return;
}
canteenData.jidelnicky[start.add(Duration(days: i))] =
await getLunchesForDay(start.add(Duration(days: i)));
} catch (e) {
Expand Down
95 changes: 47 additions & 48 deletions lib/pages/jidelna.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import 'package:flutter/cupertino.dart';

import '../methods/icanteen.dart';
import './all.dart';
import './../sharedWidgets/all.dart';

class SnackBarShown {
SnackBarShown({required this.shown});
bool shown = false;
}

SnackBarShown snackbarshown = SnackBarShown(shown: false);

enum StavJidla {
/// je objednano a lze odebrat
Expand Down Expand Up @@ -121,9 +129,26 @@ class PopupMenuButtonInAppbar extends StatelessWidget {
],
),
onTap: () async {
if (refreshing) return;
loading(true);
await initCanteen(hasToBeNew: true);
refreshing = true;
try {
await initCanteen(hasToBeNew: true);
} catch (e) {
// Find the ScaffoldMessenger in the widget tree
// and use it to show a SnackBar.
if (context.mounted && !snackbarshown.shown) {
ScaffoldMessenger.of(context)
.showSnackBar(snackbarFunction(
'nastala chyba při aktualizaci dat, dejte tomu chvilku a zkuste to prosím znovu'))
.closed
.then((SnackBarClosedReason reason) {
snackbarshown.shown = false;
});
}
}
loading(false);
refreshing = false;
setScaffoldBody(
JidelnicekDenWidget(customCanteenData: getCanteenData()));
},
Expand Down Expand Up @@ -488,7 +513,6 @@ class ObjednatJidloTlacitko extends StatefulWidget {
}

class _ObjednatJidloTlacitkoState extends State<ObjednatJidloTlacitko> {
bool snackBarShown = false;
Color? buttonColor;
Widget? icon;
Jidlo? jidlo;
Expand Down Expand Up @@ -546,31 +570,6 @@ class _ObjednatJidloTlacitkoState extends State<ObjednatJidloTlacitko> {
);
}

SnackBar snackbarFunction(String snackBarText) {
return SnackBar(
content:
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(snackBarText),
)),
Builder(builder: (context) {
return ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(
const Color.fromARGB(255, 40, 40, 40)),
),
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
child: const Icon(Icons.close),
);
}),
]),
);
}

return ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(buttonColor),
Expand Down Expand Up @@ -604,14 +603,14 @@ class _ObjednatJidloTlacitkoState extends State<ObjednatJidloTlacitko> {
// and use it to show a SnackBar.
// toto je upozornění dole (Snackbar)
// snackbarshown je aby se snackbar nezobrazil vícekrát
if (context.mounted && snackBarShown == false) {
snackBarShown = true;
if (context.mounted && snackbarshown.shown == false) {
snackbarshown.shown = true;
ScaffoldMessenger.of(context)
.showSnackBar(snackbarFunction(
'nastala chyba při obejnávání jídla: $e'))
.closed
.then((SnackBarClosedReason reason) {
snackBarShown = false;
snackbarshown.shown = false;
});
}
}
Expand All @@ -633,14 +632,14 @@ class _ObjednatJidloTlacitkoState extends State<ObjednatJidloTlacitko> {
// and use it to show a SnackBar.
// toto je upozornění dole (Snackbar)
// snackbarshown je aby se snackbar nezobrazil vícekrát
if (context.mounted && snackBarShown == false) {
snackBarShown = true;
if (context.mounted && snackbarshown.shown == false) {
snackbarshown.shown = true;
ScaffoldMessenger.of(context)
.showSnackBar(snackbarFunction(
'nastala chyba při obejnávání jídla z burzy: $e'))
.closed
.then((SnackBarClosedReason reason) {
snackBarShown = false;
snackbarshown.shown = false;
});
}
}
Expand All @@ -654,14 +653,14 @@ class _ObjednatJidloTlacitkoState extends State<ObjednatJidloTlacitko> {
// and use it to show a SnackBar.
// toto je upozornění dole (Snackbar)
// snackbarshown je aby se snackbar nezobrazil vícekrát
if (context.mounted && snackBarShown == false) {
snackBarShown = true;
if (context.mounted && snackbarshown.shown == false) {
snackbarshown.shown = true;
ScaffoldMessenger.of(context)
.showSnackBar(snackbarFunction(
'Oběd nelze zrušit. Platnost objednávky vypršela. (pravděpodobně je toto oběd z minulosti)'))
.closed
.then((SnackBarClosedReason reason) {
snackBarShown = false;
snackbarshown.shown = false;
});
}
}
Expand All @@ -677,14 +676,14 @@ class _ObjednatJidloTlacitkoState extends State<ObjednatJidloTlacitko> {
// and use it to show a SnackBar.
// toto je upozornění dole (Snackbar)
// snackbarshown je aby se snackbar nezobrazil vícekrát
if (context.mounted && snackBarShown == false) {
snackBarShown = true;
if (context.mounted && snackbarshown.shown == false) {
snackbarshown.shown = true;
ScaffoldMessenger.of(context)
.showSnackBar(snackbarFunction(
'nastala chyba při dávání jídla na burzu: $e'))
.closed
.then((SnackBarClosedReason reason) {
snackBarShown = false;
snackbarshown.shown = false;
});
}
}
Expand All @@ -696,14 +695,14 @@ class _ObjednatJidloTlacitkoState extends State<ObjednatJidloTlacitko> {
// and use it to show a SnackBar.
// toto je upozornění dole (Snackbar)
// snackbarshown je aby se snackbar nezobrazil vícekrát
if (context.mounted && snackBarShown == false) {
snackBarShown = true;
if (context.mounted && snackbarshown.shown == false) {
snackbarshown.shown = true;
ScaffoldMessenger.of(context)
.showSnackBar(snackbarFunction(
'Oběd nelze objednat. (pravděpodobně je toto oběd z minulosti nebo aktuálně není na burze)'))
.closed
.then((SnackBarClosedReason reason) {
snackBarShown = false;
snackbarshown.shown = false;
});
}
}
Expand All @@ -719,14 +718,14 @@ class _ObjednatJidloTlacitkoState extends State<ObjednatJidloTlacitko> {
// and use it to show a SnackBar.
// toto je upozornění dole (Snackbar)
// snackbarshown je aby se snackbar nezobrazil vícekrát
if (context.mounted && snackBarShown == false) {
snackBarShown = true;
if (context.mounted && snackbarshown.shown == false) {
snackbarshown.shown = true;
ScaffoldMessenger.of(context)
.showSnackBar(snackbarFunction(
'nastala chyba při rušení objednávky: $e'))
.closed
.then((SnackBarClosedReason reason) {
snackBarShown = false;
snackbarshown.shown = false;
});
}
}
Expand All @@ -743,14 +742,14 @@ class _ObjednatJidloTlacitkoState extends State<ObjednatJidloTlacitko> {
// and use it to show a SnackBar.
// toto je upozornění dole (Snackbar)
// snackbarshown je aby se snackbar nezobrazil vícekrát
if (context.mounted && snackBarShown == false) {
snackBarShown = true;
if (context.mounted && snackbarshown.shown == false) {
snackbarshown.shown = true;
ScaffoldMessenger.of(context)
.showSnackBar(snackbarFunction(
'nastala chyba při dávání jídla na burzu: $e'))
.closed
.then((SnackBarClosedReason reason) {
snackBarShown = false;
snackbarshown.shown = false;
});
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/sharedWidgets/all.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'snackbar.dart';
25 changes: 25 additions & 0 deletions lib/sharedWidgets/snackbar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';

SnackBar snackbarFunction(String snackBarText) {
return SnackBar(
content: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(snackBarText),
)),
Builder(builder: (context) {
return ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(
const Color.fromARGB(255, 40, 40, 40)),
),
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
child: const Icon(Icons.close),
);
}),
]),
);
}

0 comments on commit 75e4a4f

Please sign in to comment.