Fix back button when going back after swiping
This commit is contained in:
parent
3faf5295b1
commit
cdabd1ee94
@ -1,5 +1,4 @@
|
|||||||
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
||||||
import 'package:calorimeter/perdate/perdate_pageview.dart';
|
|
||||||
import 'package:calorimeter/perdate/perdate_pageview_controller.dart';
|
import 'package:calorimeter/perdate/perdate_pageview_controller.dart';
|
||||||
import 'package:calorimeter/storage/storage.dart';
|
import 'package:calorimeter/storage/storage.dart';
|
||||||
import 'package:calorimeter/utils/date_time_helper.dart';
|
import 'package:calorimeter/utils/date_time_helper.dart';
|
||||||
@ -20,14 +19,8 @@ void main() async {
|
|||||||
|
|
||||||
var storage = await FoodStorage.create();
|
var storage = await FoodStorage.create();
|
||||||
await storage.buildFoodLookupDatabase();
|
await storage.buildFoodLookupDatabase();
|
||||||
timeNow = DateTime.now().copyWith(
|
|
||||||
hour: 0,
|
|
||||||
isUtc: true,
|
|
||||||
minute: 0,
|
|
||||||
second: 0,
|
|
||||||
millisecond: 0,
|
|
||||||
microsecond: 0);
|
|
||||||
|
|
||||||
|
timeNow = DateTimeHelper.now();
|
||||||
entriesForToday = await storage.getEntriesForDate(timeNow);
|
entriesForToday = await storage.getEntriesForDate(timeNow);
|
||||||
|
|
||||||
var kcalLimit = await storage.readLimit();
|
var kcalLimit = await storage.readLimit();
|
||||||
|
@ -30,6 +30,10 @@ class PerDatePageView extends StatelessWidget {
|
|||||||
var diff = value - pageController.initialPage;
|
var diff = value - pageController.initialPage;
|
||||||
var newDate = initialDate.subtract(Duration(days: diff));
|
var newDate = initialDate.subtract(Duration(days: diff));
|
||||||
log("newDate = $newDate");
|
log("newDate = $newDate");
|
||||||
|
|
||||||
|
context
|
||||||
|
.read<PageViewStateProvider>()
|
||||||
|
.addVisitedindexIfNotVisitedByBackButton(value);
|
||||||
context.read<PageViewStateProvider>().setDisplayedDate(newDate);
|
context.read<PageViewStateProvider>().setDisplayedDate(newDate);
|
||||||
},
|
},
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
|
@ -33,27 +33,27 @@ class PerDatePageViewController extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BackButtonListener(
|
return ChangeNotifierProvider(
|
||||||
onBackButtonPressed: () async {
|
create: (context) => PageViewStateProvider(
|
||||||
var visitedIndexes =
|
initialDate: initialDate,
|
||||||
context.read<PageViewStateProvider>().visitedIndexes;
|
initialOffset: initialOffset,
|
||||||
if (visitedIndexes.length == 1) {
|
),
|
||||||
return false;
|
child: Builder(builder: (context) {
|
||||||
}
|
return BackButtonListener(
|
||||||
|
onBackButtonPressed: () async {
|
||||||
|
context.read<PageViewStateProvider>().backButtonWasPressed = true;
|
||||||
|
var visitedIndexes =
|
||||||
|
context.read<PageViewStateProvider>().visitedIndexes;
|
||||||
|
if (visitedIndexes.length == 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
visitedIndexes.removeLast();
|
visitedIndexes.removeLast();
|
||||||
|
pageController.jumpToPage(visitedIndexes.last);
|
||||||
|
|
||||||
pageController.jumpToPage(visitedIndexes.last);
|
return true;
|
||||||
|
},
|
||||||
return true;
|
child: Scaffold(
|
||||||
},
|
|
||||||
child: ChangeNotifierProvider(
|
|
||||||
create: (context) => PageViewStateProvider(
|
|
||||||
initialDate: initialDate,
|
|
||||||
initialOffset: initialOffset,
|
|
||||||
),
|
|
||||||
child: Builder(builder: (context) {
|
|
||||||
return Scaffold(
|
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Builder(builder: (context) {
|
title: Builder(builder: (context) {
|
||||||
return Text(DateFormat.yMMMMd('de').format(
|
return Text(DateFormat.yMMMMd('de').format(
|
||||||
@ -62,17 +62,26 @@ class PerDatePageViewController extends StatelessWidget {
|
|||||||
actions: const [ThemeSwitcherButton()],
|
actions: const [ThemeSwitcherButton()],
|
||||||
),
|
),
|
||||||
bottomNavigationBar: BottomAppBar(
|
bottomNavigationBar: BottomAppBar(
|
||||||
shape: const RectangularNotchShape(),
|
shape: const RectangularNotchShape(),
|
||||||
color: Theme.of(context).colorScheme.secondary,
|
color: Theme.of(context).colorScheme.secondary,
|
||||||
child: SumWidget(foodEntries: [])),
|
child: Builder(builder: (context) {
|
||||||
|
return SumWidget(
|
||||||
|
date: context.watch<PageViewStateProvider>().displayedDate);
|
||||||
|
}),
|
||||||
|
),
|
||||||
drawer: const AppDrawer(),
|
drawer: const AppDrawer(),
|
||||||
floatingActionButton: OverflowBar(children: [
|
floatingActionButton: OverflowBar(children: [
|
||||||
ScanFoodFAB(
|
ScanFoodFAB(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
var result = BarcodeScanner.scan();
|
var result = BarcodeScanner.scan();
|
||||||
context
|
context.read<FoodEntryBloc>().add(
|
||||||
.read<FoodEntryBloc>()
|
BarcodeScanned(
|
||||||
.add(BarcodeScanned(scanResultFuture: result));
|
scanResultFuture: result,
|
||||||
|
forDate: context
|
||||||
|
.read<PageViewStateProvider>()
|
||||||
|
.displayedDate,
|
||||||
|
),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
const SizedBox(width: 8),
|
const SizedBox(width: 8),
|
||||||
@ -94,21 +103,22 @@ class PerDatePageViewController extends StatelessWidget {
|
|||||||
pageController: pageController,
|
pageController: pageController,
|
||||||
initialDate: initialDate,
|
initialDate: initialDate,
|
||||||
),
|
),
|
||||||
);
|
),
|
||||||
}),
|
);
|
||||||
),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PageViewStateProvider with ChangeNotifier {
|
class PageViewStateProvider with ChangeNotifier {
|
||||||
DateTime _displayedDate;
|
DateTime _displayedDate;
|
||||||
List<int> visitedIndexes = [];
|
final List<int> _visitedIndexes;
|
||||||
bool _backButtonWasPressed = false;
|
bool _backButtonWasPressed = false;
|
||||||
|
|
||||||
PageViewStateProvider({required DateTime initialDate, int initialOffset = 0})
|
PageViewStateProvider({required DateTime initialDate, int initialOffset = 0})
|
||||||
: _displayedDate = initialDate {
|
: _displayedDate = initialDate,
|
||||||
visitedIndexes.add(initialOffset);
|
_visitedIndexes = [] {
|
||||||
|
_visitedIndexes.add(initialOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
set backButtonWasPressed(val) => _backButtonWasPressed = val;
|
set backButtonWasPressed(val) => _backButtonWasPressed = val;
|
||||||
@ -120,7 +130,18 @@ class PageViewStateProvider with ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void addVisitedindex(int index) {
|
get visitedIndexes => _visitedIndexes;
|
||||||
visitedIndexes.add(index);
|
|
||||||
|
void addVisitedIndex(int index) {
|
||||||
|
_visitedIndexes.add(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addVisitedindexIfNotVisitedByBackButton(int index) {
|
||||||
|
if (_backButtonWasPressed) {
|
||||||
|
_backButtonWasPressed = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
addVisitedIndex(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
||||||
import 'package:calorimeter/perdate/entry_list.dart';
|
import 'package:calorimeter/perdate/entry_list.dart';
|
||||||
import 'package:calorimeter/storage/storage.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:provider/provider.dart';
|
|
||||||
|
|
||||||
class PerDateWidget extends StatefulWidget {
|
class PerDateWidget extends StatefulWidget {
|
||||||
final DateTime date;
|
final DateTime date;
|
||||||
@ -15,52 +13,30 @@ class PerDateWidget extends StatefulWidget {
|
|||||||
|
|
||||||
class _PerDateWidgetState extends State<PerDateWidget>
|
class _PerDateWidgetState extends State<PerDateWidget>
|
||||||
with AutomaticKeepAliveClientMixin<PerDateWidget> {
|
with AutomaticKeepAliveClientMixin<PerDateWidget> {
|
||||||
late FoodStorage storage;
|
|
||||||
late Future<List<FoodEntryState>> entriesFuture;
|
|
||||||
List<FoodEntryState> entries = [];
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
storage = FoodStorage.getInstance();
|
context
|
||||||
entriesFuture = storage.getEntriesForDate(widget.date);
|
.read<FoodEntryBloc>()
|
||||||
entriesFuture.then((val) {
|
.add(PageBeingInitialized(forDate: widget.date));
|
||||||
entries = val;
|
|
||||||
});
|
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
super.build(context);
|
super.build(context);
|
||||||
|
|
||||||
return FutureBuilder(
|
return BlocConsumer<FoodEntryBloc, GlobalEntryState>(
|
||||||
future: entriesFuture,
|
listener: (context, pageState) {
|
||||||
builder: (context, snapshot) {
|
if (pageState.errorString != null) {
|
||||||
return snapshot.connectionState != ConnectionState.done
|
showNewSnackbarWith(context, pageState.errorString!);
|
||||||
? const Center(child: CircularProgressIndicator())
|
}
|
||||||
: BlocProvider(
|
},
|
||||||
create: (context) => FoodEntryBloc(
|
builder: (context, pageState) {
|
||||||
initialState: PageState(foodEntries: entries),
|
return FoodEntryList(
|
||||||
storage: storage,
|
entries: pageState.foodEntries[widget.date] ?? [],
|
||||||
forDate: widget.date,
|
date: widget.date);
|
||||||
),
|
},
|
||||||
child: BlocConsumer<FoodEntryBloc, PageState>(
|
);
|
||||||
listener: (context, pageState) {
|
|
||||||
if (pageState.errorString != null) {
|
|
||||||
showNewSnackbarWith(context, pageState.errorString!);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
builder: (context, pageState) {
|
|
||||||
return FoodEntryList(entries: pageState.foodEntries);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void showNewSnackbarWith(BuildContext context, String text) {
|
void showNewSnackbarWith(BuildContext context, String text) {
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
import 'package:barcode_scan2/barcode_scan2.dart';
|
|
||||||
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
|
||||||
import 'package:calorimeter/food_scan/food_fact_lookup.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
class ScanFoodNotifier with ChangeNotifier {
|
|
||||||
void handleBarcodeScannedEvent(BarcodeScanned barcode) async {
|
|
||||||
var client = FoodFactLookupClient();
|
|
||||||
var scanResult = await barcode.scanResultFuture;
|
|
||||||
|
|
||||||
if (scanResult.type == ResultType.Cancelled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (scanResult.type == ResultType.Error) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var responseFuture = client.retrieveFoodInfo(scanResult.rawContent);
|
|
||||||
|
|
||||||
var newEntryWaiting = FoodEntryState(
|
|
||||||
kcalPer100: 0,
|
|
||||||
name: "",
|
|
||||||
mass: 0,
|
|
||||||
waitingForNetwork: true,
|
|
||||||
isSelected: false,
|
|
||||||
);
|
|
||||||
|
|
||||||
await responseFuture.then((response) async {
|
|
||||||
if (response.status ==
|
|
||||||
FoodFactResponseStatus.foodFactServerNotReachable) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var newEntryFinishedWaiting = FoodEntryState(
|
|
||||||
name: response.food?.name ?? "",
|
|
||||||
mass: response.food?.mass ?? 0,
|
|
||||||
kcalPer100: response.food?.kcalPer100g ?? 0,
|
|
||||||
waitingForNetwork: false,
|
|
||||||
isSelected: false,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class BarcodeScanned {
|
|
||||||
final Future<ScanResult> scanResultFuture;
|
|
||||||
|
|
||||||
BarcodeScanned({required this.scanResultFuture});
|
|
||||||
}
|
|
@ -4,53 +4,57 @@ import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
|||||||
import 'package:calorimeter/utils/settings_bloc.dart';
|
import 'package:calorimeter/utils/settings_bloc.dart';
|
||||||
|
|
||||||
class SumWidget extends StatelessWidget {
|
class SumWidget extends StatelessWidget {
|
||||||
final List<FoodEntryState> foodEntries;
|
final DateTime date;
|
||||||
const SumWidget({required this.foodEntries, super.key});
|
|
||||||
|
const SumWidget({super.key, required this.date});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocBuilder<SettingsDataBloc, SettingsState>(
|
return BlocBuilder<FoodEntryBloc, GlobalEntryState>(
|
||||||
builder: (context, state) {
|
builder: (context, entryState) {
|
||||||
var sum = 0.0;
|
return BlocBuilder<SettingsDataBloc, SettingsState>(
|
||||||
for (var entry in foodEntries) {
|
builder: (context, settingsState) {
|
||||||
sum += entry.kcalPer100 / 100 * entry.mass;
|
var sum = 0.0;
|
||||||
}
|
for (var entry in entryState.foodEntries[date] ?? []) {
|
||||||
var diff = state.kcalLimit - sum;
|
sum += entry.kcalPer100 / 100 * entry.mass;
|
||||||
var diffLimit = state.kcalLimit ~/ 4;
|
}
|
||||||
|
var diff = settingsState.kcalLimit - sum;
|
||||||
|
var diffLimit = settingsState.kcalLimit ~/ 4;
|
||||||
|
|
||||||
var textColor = Theme.of(context).colorScheme.onSecondary;
|
var textColor = Theme.of(context).colorScheme.onSecondary;
|
||||||
var newTextColor = textColor;
|
var newTextColor = textColor;
|
||||||
var brightness = Theme.of(context).brightness;
|
var brightness = Theme.of(context).brightness;
|
||||||
|
|
||||||
switch (brightness) {
|
switch (brightness) {
|
||||||
case Brightness.dark:
|
case Brightness.dark:
|
||||||
if (diff < 0) {
|
if (diff < 0) {
|
||||||
newTextColor = Colors.red[900]!;
|
newTextColor = Colors.red[900]!;
|
||||||
} else if (diff < diffLimit) {
|
} else if (diff < diffLimit) {
|
||||||
newTextColor = Colors.orange[900]!;
|
newTextColor = Colors.orange[900]!;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Brightness.light:
|
case Brightness.light:
|
||||||
if (diff < 0) {
|
if (diff < 0) {
|
||||||
newTextColor = Colors.redAccent;
|
newTextColor = Colors.redAccent;
|
||||||
} else if (diff < diffLimit) {
|
} else if (diff < diffLimit) {
|
||||||
newTextColor = Colors.orangeAccent;
|
newTextColor = Colors.orangeAccent;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Align(
|
return Align(
|
||||||
alignment: Alignment.centerLeft,
|
alignment: Alignment.centerLeft,
|
||||||
child: Text(
|
child: Text(
|
||||||
'kcal heute: ${sum.ceil().toString()}/${state.kcalLimit.ceil()}',
|
'kcal heute: ${sum.ceil().toString()}/${settingsState.kcalLimit.ceil()}',
|
||||||
style: Theme.of(context)
|
style: Theme.of(context)
|
||||||
.textTheme
|
.textTheme
|
||||||
.bodyLarge!
|
.bodyLarge!
|
||||||
.copyWith(color: newTextColor),
|
.copyWith(color: newTextColor),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user