[settings] blank page for now with just local storage reset [storage] implement diary

This commit is contained in:
Piotr Domański 2024-08-09 14:48:27 +02:00
parent 33fe0eff39
commit 85cdd68ac1
6 changed files with 84 additions and 3 deletions

View file

@ -32,4 +32,17 @@ class Diary {
carb = map['carb'] as double, carb = map['carb'] as double,
fat = map['fat'] as double, fat = map['fat'] as double,
fiber = map['fiber'] as double; fiber = map['fiber'] as double;
Map<String, Object?> toMap() {
return {
'id': id,
'date': date.toIso8601String(),
'meals': meals.map((e) => e.toMap()).toList(),
'calories': calories,
'protein': protein,
'carb': carb,
'fat': fat,
'fiber': fiber,
};
}
} }

View file

@ -33,4 +33,18 @@ class Entry {
fat = map['fat'] as double, fat = map['fat'] as double,
fiber = map['fiber'] as double, fiber = map['fiber'] as double,
carb = map['carb'] as double; carb = map['carb'] as double;
Map<String, Object?> toMap() {
return {
'id': id,
'grams': grams,
'product': product.toMap(),
'mealId': mealId,
'calories': calories,
'protein': protein,
'fat': fat,
'fiber': fiber,
'carb': carb,
};
}
} }

View file

@ -38,4 +38,18 @@ class Meal {
fat = map['fat'] as double, fat = map['fat'] as double,
fiber = map['fiber'] as double, fiber = map['fiber'] as double,
diaryId = map['diary_id'] as int; diaryId = map['diary_id'] as int;
Map<String, Object?> toMap() {
return {
'id': id,
'name': name,
'order': order,
'calories': calories,
'protein': protein,
'fat': fat,
'fiber': fiber,
'carb': carb,
'diaryId': diaryId,
};
}
} }

View file

@ -25,4 +25,16 @@ class Preset {
carb = map['carb'] as double, carb = map['carb'] as double,
fat = map['fat'] as double, fat = map['fat'] as double,
fiber = map['fiber'] as double; fiber = map['fiber'] as double;
Map<String, Object?> toMap() {
return {
'id': id,
'name': name,
'calories': calories,
'protein': protein,
'fat': fat,
'fiber': fiber,
'carb': carb,
};
}
} }

View file

@ -6,6 +6,7 @@ import 'package:fooder/components/app_bar.dart';
import 'package:fooder/components/navigation_bar.dart'; import 'package:fooder/components/navigation_bar.dart';
import 'package:fooder/screens/login.dart'; import 'package:fooder/screens/login.dart';
import 'package:fooder/screens/main.dart'; import 'package:fooder/screens/main.dart';
import 'package:fooder/screens/settings.dart';
TextStyle logoStyle(context) { TextStyle logoStyle(context) {
return Theme.of(context).textTheme.labelLarge!.copyWith( return Theme.of(context).textTheme.labelLarge!.copyWith(
@ -30,6 +31,7 @@ abstract class BasedState<T extends BasedScreen> extends State<T> {
} }
void backToLogin() { void backToLogin() {
Navigator.of(context).popUntil((route) => route.isFirst);
Navigator.pushReplacement( Navigator.pushReplacement(
context, context,
MaterialPageRoute( MaterialPageRoute(
@ -39,6 +41,7 @@ abstract class BasedState<T extends BasedScreen> extends State<T> {
} }
void backToDiary() { void backToDiary() {
Navigator.of(context).popUntil((route) => route.isFirst);
Navigator.pushReplacement( Navigator.pushReplacement(
context, context,
MaterialPageRoute( MaterialPageRoute(
@ -47,6 +50,15 @@ abstract class BasedState<T extends BasedScreen> extends State<T> {
); );
} }
void navigateToSettings() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SettingsScreen(ctx: ctx),
),
);
}
FAppBar appBar() { FAppBar appBar() {
return FAppBar( return FAppBar(
actions: [ actions: [
@ -93,7 +105,7 @@ abstract class BasedState<T extends BasedScreen> extends State<T> {
Icons.person, Icons.person,
color: Theme.of(context).colorScheme.onSurfaceVariant, color: Theme.of(context).colorScheme.onSurfaceVariant,
), ),
onPressed: () {}, onPressed: navigateToSettings,
), ),
], ],
), ),

View file

@ -4,14 +4,16 @@ import 'package:flutter/widgets.dart';
import 'package:path/path.dart'; import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart'; import 'package:sqflite/sqflite.dart';
import 'package:fooder/storage/product.dart'; import 'package:fooder/storage/product.dart';
import 'package:fooder/storage/diary.dart';
class Storage { class Storage {
Database db; Database db;
ProductStorage product; ProductStorage product;
DiaryStorage diary;
static const String path = "storage.db"; static const String path = "storage.db";
Storage({required this.db, required this.product}); Storage({required this.db, required this.product, required this.diary});
static Future<Storage> create() async { static Future<Storage> create() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
@ -20,12 +22,26 @@ class Storage {
onCreate: createTables, onCreate: createTables,
version: 1, version: 1,
); );
return Storage(db: db, product: ProductStorage(db: db)); return Storage(
db: db, product: ProductStorage(db: db), diary: DiaryStorage(db: db));
}
Future<void> reset() async {
await db.close();
await deleteDatabase(join(await getDatabasesPath(), path));
db = await openDatabase(
join(await getDatabasesPath(), path),
onCreate: createTables,
version: 1,
);
product = ProductStorage(db: db);
diary = DiaryStorage(db: db);
} }
static Future<void> createTables(Database db, int version) async { static Future<void> createTables(Database db, int version) async {
var batch = db.batch(); var batch = db.batch();
await ProductStorage.createTable(batch); await ProductStorage.createTable(batch);
await DiaryStorage.createTable(batch);
await batch.commit(noResult: true); await batch.commit(noResult: true);
} }
} }