[storage] change schema a little

This commit is contained in:
Piotr Domański 2024-08-13 09:48:44 +02:00
parent 85cdd68ac1
commit 94042eb213
4 changed files with 100 additions and 1 deletions

56
lib/screens/settings.dart Normal file
View file

@ -0,0 +1,56 @@
import 'package:flutter/material.dart';
import 'package:fooder/screens/based.dart';
import 'package:fooder/components/button.dart';
class SettingsScreen extends BasedScreen {
const SettingsScreen({super.key, required super.ctx});
@override
State<SettingsScreen> createState() => _SettingsScreen();
}
class _SettingsScreen extends BasedState<SettingsScreen> {
Future<void> resetStorage() async {
try {
ctx.storage.reset();
showText("Storage reset");
} catch (e) {
showError(e.toString());
}
}
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var colorScheme = theme.colorScheme;
return Scaffold(
extendBodyBehindAppBar: false,
extendBody: true,
appBar: appBar(),
bottomNavigationBar: navBar(),
body: Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 600),
padding: const EdgeInsets.all(10),
child: AutofillGroup(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Icon(
Icons.verified_user_sharp,
size: 100,
color: colorScheme.primary.withOpacity(0.85),
),
FButton(
labelText: 'Reset local storage',
onPressed: resetStorage,
),
],
),
),
),
),
);
}
}

43
lib/storage/diary.dart Normal file
View file

@ -0,0 +1,43 @@
import 'dart:async';
import 'dart:convert';
import 'package:sqflite/sqflite.dart';
import 'package:fooder/models/diary.dart';
import 'package:fooder/storage/based.dart';
class DiaryStorage extends StorageBased {
DiaryStorage({required super.db});
static Future<void> createTable(Batch batch) async {
batch.execute('''
CREATE TABLE diary(
date TEXT PRIMARY KEY,
content TEXT,
needs_sync BOOLEAN,
last_sync TEXT
)
''');
}
Future<Diary?> get({required DateTime date}) async {
var result = await db
.query('diary', where: 'date = ?', whereArgs: [date.toIso8601String()]);
if (result.isEmpty) {
return null;
}
return Diary.fromJson(
jsonDecode((result.first as Map<String, dynamic>)['content']));
}
Future<void> insert(Diary diary, {bool needsSync = false}) async {
var data = {
'id': diary.id,
'date': diary.date.toIso8601String(),
'content': jsonEncode(diary.toMap()),
'needs_sync': needsSync,
'last_sync': needsSync ? DateTime.now().toIso8601String() : null,
};
await db.insert('diary', data,
conflictAlgorithm: ConflictAlgorithm.replace);
}
}

0
lib/storage/preset.dart Normal file
View file

View file

@ -10,7 +10,7 @@ class ProductStorage extends StorageBased {
static Future<void> createTable(Batch batch) async { static Future<void> createTable(Batch batch) async {
batch.execute(''' batch.execute('''
CREATE TABLE product( CREATE TABLE product(
id INTEGER, id INTEGER PRIMARY KEY,
name TEXT, name TEXT,
barcode TEXT, barcode TEXT,
calories REAL, calories REAL,