Use moor as a ORM for the settings page

This commit is contained in:
2023-04-09 14:39:41 +12:00
parent 35787cc208
commit d558a8f5ad
13 changed files with 1494 additions and 3 deletions
+35
View File
@@ -0,0 +1,35 @@
import 'dart:io';
import 'package:moor/ffi.dart';
import 'package:moor/moor.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'settings.dart';
part 'database.g.dart';
@UseMoor(tables: [Settings])
class MyDatabase extends _$MyDatabase {
MyDatabase() : super(_openConnection());
@override
int get schemaVersion => 1;
@override
MigrationStrategy get migration => MigrationStrategy(
onCreate: (Migrator m) async {
await m.createAll();
},
onUpgrade: (Migrator m, int from, int to) async {
// no migrations yet
},
);
}
LazyDatabase _openConnection() {
return LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(join(dbFolder.path, 'massive.db'));
return VmDatabase(file, logStatements: true);
});
}
+709
View File
@@ -0,0 +1,709 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'database.dart';
// **************************************************************************
// MoorGenerator
// **************************************************************************
// ignore_for_file: type=lint
class Setting extends DataClass implements Insertable<Setting> {
final bool alarm;
final bool vibrate;
final String sound;
final bool notify;
final bool images;
final bool showUnit;
final String? lightColor;
final String? darkColor;
final bool steps;
final String date;
final bool showDate;
final String theme;
final bool showSets;
final bool noSound;
final bool backup;
Setting(
{required this.alarm,
required this.vibrate,
required this.sound,
required this.notify,
required this.images,
required this.showUnit,
this.lightColor,
this.darkColor,
required this.steps,
required this.date,
required this.showDate,
required this.theme,
required this.showSets,
required this.noSound,
required this.backup});
factory Setting.fromData(Map<String, dynamic> data, GeneratedDatabase db,
{String? prefix}) {
final effectivePrefix = prefix ?? '';
return Setting(
alarm: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}alarm'])!,
vibrate: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}vibrate'])!,
sound: const StringType()
.mapFromDatabaseResponse(data['${effectivePrefix}sound'])!,
notify: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}notify'])!,
images: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}images'])!,
showUnit: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}show_unit'])!,
lightColor: const StringType()
.mapFromDatabaseResponse(data['${effectivePrefix}light_color']),
darkColor: const StringType()
.mapFromDatabaseResponse(data['${effectivePrefix}dark_color']),
steps: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}steps'])!,
date: const StringType()
.mapFromDatabaseResponse(data['${effectivePrefix}date'])!,
showDate: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}show_date'])!,
theme: const StringType()
.mapFromDatabaseResponse(data['${effectivePrefix}theme'])!,
showSets: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}show_sets'])!,
noSound: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}no_sound'])!,
backup: const BoolType()
.mapFromDatabaseResponse(data['${effectivePrefix}backup'])!,
);
}
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{};
map['alarm'] = Variable<bool>(alarm);
map['vibrate'] = Variable<bool>(vibrate);
map['sound'] = Variable<String>(sound);
map['notify'] = Variable<bool>(notify);
map['images'] = Variable<bool>(images);
map['show_unit'] = Variable<bool>(showUnit);
if (!nullToAbsent || lightColor != null) {
map['light_color'] = Variable<String?>(lightColor);
}
if (!nullToAbsent || darkColor != null) {
map['dark_color'] = Variable<String?>(darkColor);
}
map['steps'] = Variable<bool>(steps);
map['date'] = Variable<String>(date);
map['show_date'] = Variable<bool>(showDate);
map['theme'] = Variable<String>(theme);
map['show_sets'] = Variable<bool>(showSets);
map['no_sound'] = Variable<bool>(noSound);
map['backup'] = Variable<bool>(backup);
return map;
}
SettingsCompanion toCompanion(bool nullToAbsent) {
return SettingsCompanion(
alarm: Value(alarm),
vibrate: Value(vibrate),
sound: Value(sound),
notify: Value(notify),
images: Value(images),
showUnit: Value(showUnit),
lightColor: lightColor == null && nullToAbsent
? const Value.absent()
: Value(lightColor),
darkColor: darkColor == null && nullToAbsent
? const Value.absent()
: Value(darkColor),
steps: Value(steps),
date: Value(date),
showDate: Value(showDate),
theme: Value(theme),
showSets: Value(showSets),
noSound: Value(noSound),
backup: Value(backup),
);
}
factory Setting.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
return Setting(
alarm: serializer.fromJson<bool>(json['alarm']),
vibrate: serializer.fromJson<bool>(json['vibrate']),
sound: serializer.fromJson<String>(json['sound']),
notify: serializer.fromJson<bool>(json['notify']),
images: serializer.fromJson<bool>(json['images']),
showUnit: serializer.fromJson<bool>(json['showUnit']),
lightColor: serializer.fromJson<String?>(json['lightColor']),
darkColor: serializer.fromJson<String?>(json['darkColor']),
steps: serializer.fromJson<bool>(json['steps']),
date: serializer.fromJson<String>(json['date']),
showDate: serializer.fromJson<bool>(json['showDate']),
theme: serializer.fromJson<String>(json['theme']),
showSets: serializer.fromJson<bool>(json['showSets']),
noSound: serializer.fromJson<bool>(json['noSound']),
backup: serializer.fromJson<bool>(json['backup']),
);
}
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'alarm': serializer.toJson<bool>(alarm),
'vibrate': serializer.toJson<bool>(vibrate),
'sound': serializer.toJson<String>(sound),
'notify': serializer.toJson<bool>(notify),
'images': serializer.toJson<bool>(images),
'showUnit': serializer.toJson<bool>(showUnit),
'lightColor': serializer.toJson<String?>(lightColor),
'darkColor': serializer.toJson<String?>(darkColor),
'steps': serializer.toJson<bool>(steps),
'date': serializer.toJson<String>(date),
'showDate': serializer.toJson<bool>(showDate),
'theme': serializer.toJson<String>(theme),
'showSets': serializer.toJson<bool>(showSets),
'noSound': serializer.toJson<bool>(noSound),
'backup': serializer.toJson<bool>(backup),
};
}
Setting copyWith(
{bool? alarm,
bool? vibrate,
String? sound,
bool? notify,
bool? images,
bool? showUnit,
String? lightColor,
String? darkColor,
bool? steps,
String? date,
bool? showDate,
String? theme,
bool? showSets,
bool? noSound,
bool? backup}) =>
Setting(
alarm: alarm ?? this.alarm,
vibrate: vibrate ?? this.vibrate,
sound: sound ?? this.sound,
notify: notify ?? this.notify,
images: images ?? this.images,
showUnit: showUnit ?? this.showUnit,
lightColor: lightColor ?? this.lightColor,
darkColor: darkColor ?? this.darkColor,
steps: steps ?? this.steps,
date: date ?? this.date,
showDate: showDate ?? this.showDate,
theme: theme ?? this.theme,
showSets: showSets ?? this.showSets,
noSound: noSound ?? this.noSound,
backup: backup ?? this.backup,
);
@override
String toString() {
return (StringBuffer('Setting(')
..write('alarm: $alarm, ')
..write('vibrate: $vibrate, ')
..write('sound: $sound, ')
..write('notify: $notify, ')
..write('images: $images, ')
..write('showUnit: $showUnit, ')
..write('lightColor: $lightColor, ')
..write('darkColor: $darkColor, ')
..write('steps: $steps, ')
..write('date: $date, ')
..write('showDate: $showDate, ')
..write('theme: $theme, ')
..write('showSets: $showSets, ')
..write('noSound: $noSound, ')
..write('backup: $backup')
..write(')'))
.toString();
}
@override
int get hashCode => Object.hash(
alarm,
vibrate,
sound,
notify,
images,
showUnit,
lightColor,
darkColor,
steps,
date,
showDate,
theme,
showSets,
noSound,
backup);
@override
bool operator ==(Object other) =>
identical(this, other) ||
(other is Setting &&
other.alarm == this.alarm &&
other.vibrate == this.vibrate &&
other.sound == this.sound &&
other.notify == this.notify &&
other.images == this.images &&
other.showUnit == this.showUnit &&
other.lightColor == this.lightColor &&
other.darkColor == this.darkColor &&
other.steps == this.steps &&
other.date == this.date &&
other.showDate == this.showDate &&
other.theme == this.theme &&
other.showSets == this.showSets &&
other.noSound == this.noSound &&
other.backup == this.backup);
}
class SettingsCompanion extends UpdateCompanion<Setting> {
final Value<bool> alarm;
final Value<bool> vibrate;
final Value<String> sound;
final Value<bool> notify;
final Value<bool> images;
final Value<bool> showUnit;
final Value<String?> lightColor;
final Value<String?> darkColor;
final Value<bool> steps;
final Value<String> date;
final Value<bool> showDate;
final Value<String> theme;
final Value<bool> showSets;
final Value<bool> noSound;
final Value<bool> backup;
const SettingsCompanion({
this.alarm = const Value.absent(),
this.vibrate = const Value.absent(),
this.sound = const Value.absent(),
this.notify = const Value.absent(),
this.images = const Value.absent(),
this.showUnit = const Value.absent(),
this.lightColor = const Value.absent(),
this.darkColor = const Value.absent(),
this.steps = const Value.absent(),
this.date = const Value.absent(),
this.showDate = const Value.absent(),
this.theme = const Value.absent(),
this.showSets = const Value.absent(),
this.noSound = const Value.absent(),
this.backup = const Value.absent(),
});
SettingsCompanion.insert({
required bool alarm,
required bool vibrate,
required String sound,
required bool notify,
required bool images,
required bool showUnit,
this.lightColor = const Value.absent(),
this.darkColor = const Value.absent(),
required bool steps,
required String date,
required bool showDate,
required String theme,
required bool showSets,
required bool noSound,
required bool backup,
}) : alarm = Value(alarm),
vibrate = Value(vibrate),
sound = Value(sound),
notify = Value(notify),
images = Value(images),
showUnit = Value(showUnit),
steps = Value(steps),
date = Value(date),
showDate = Value(showDate),
theme = Value(theme),
showSets = Value(showSets),
noSound = Value(noSound),
backup = Value(backup);
static Insertable<Setting> custom({
Expression<bool>? alarm,
Expression<bool>? vibrate,
Expression<String>? sound,
Expression<bool>? notify,
Expression<bool>? images,
Expression<bool>? showUnit,
Expression<String?>? lightColor,
Expression<String?>? darkColor,
Expression<bool>? steps,
Expression<String>? date,
Expression<bool>? showDate,
Expression<String>? theme,
Expression<bool>? showSets,
Expression<bool>? noSound,
Expression<bool>? backup,
}) {
return RawValuesInsertable({
if (alarm != null) 'alarm': alarm,
if (vibrate != null) 'vibrate': vibrate,
if (sound != null) 'sound': sound,
if (notify != null) 'notify': notify,
if (images != null) 'images': images,
if (showUnit != null) 'show_unit': showUnit,
if (lightColor != null) 'light_color': lightColor,
if (darkColor != null) 'dark_color': darkColor,
if (steps != null) 'steps': steps,
if (date != null) 'date': date,
if (showDate != null) 'show_date': showDate,
if (theme != null) 'theme': theme,
if (showSets != null) 'show_sets': showSets,
if (noSound != null) 'no_sound': noSound,
if (backup != null) 'backup': backup,
});
}
SettingsCompanion copyWith(
{Value<bool>? alarm,
Value<bool>? vibrate,
Value<String>? sound,
Value<bool>? notify,
Value<bool>? images,
Value<bool>? showUnit,
Value<String?>? lightColor,
Value<String?>? darkColor,
Value<bool>? steps,
Value<String>? date,
Value<bool>? showDate,
Value<String>? theme,
Value<bool>? showSets,
Value<bool>? noSound,
Value<bool>? backup}) {
return SettingsCompanion(
alarm: alarm ?? this.alarm,
vibrate: vibrate ?? this.vibrate,
sound: sound ?? this.sound,
notify: notify ?? this.notify,
images: images ?? this.images,
showUnit: showUnit ?? this.showUnit,
lightColor: lightColor ?? this.lightColor,
darkColor: darkColor ?? this.darkColor,
steps: steps ?? this.steps,
date: date ?? this.date,
showDate: showDate ?? this.showDate,
theme: theme ?? this.theme,
showSets: showSets ?? this.showSets,
noSound: noSound ?? this.noSound,
backup: backup ?? this.backup,
);
}
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{};
if (alarm.present) {
map['alarm'] = Variable<bool>(alarm.value);
}
if (vibrate.present) {
map['vibrate'] = Variable<bool>(vibrate.value);
}
if (sound.present) {
map['sound'] = Variable<String>(sound.value);
}
if (notify.present) {
map['notify'] = Variable<bool>(notify.value);
}
if (images.present) {
map['images'] = Variable<bool>(images.value);
}
if (showUnit.present) {
map['show_unit'] = Variable<bool>(showUnit.value);
}
if (lightColor.present) {
map['light_color'] = Variable<String?>(lightColor.value);
}
if (darkColor.present) {
map['dark_color'] = Variable<String?>(darkColor.value);
}
if (steps.present) {
map['steps'] = Variable<bool>(steps.value);
}
if (date.present) {
map['date'] = Variable<String>(date.value);
}
if (showDate.present) {
map['show_date'] = Variable<bool>(showDate.value);
}
if (theme.present) {
map['theme'] = Variable<String>(theme.value);
}
if (showSets.present) {
map['show_sets'] = Variable<bool>(showSets.value);
}
if (noSound.present) {
map['no_sound'] = Variable<bool>(noSound.value);
}
if (backup.present) {
map['backup'] = Variable<bool>(backup.value);
}
return map;
}
@override
String toString() {
return (StringBuffer('SettingsCompanion(')
..write('alarm: $alarm, ')
..write('vibrate: $vibrate, ')
..write('sound: $sound, ')
..write('notify: $notify, ')
..write('images: $images, ')
..write('showUnit: $showUnit, ')
..write('lightColor: $lightColor, ')
..write('darkColor: $darkColor, ')
..write('steps: $steps, ')
..write('date: $date, ')
..write('showDate: $showDate, ')
..write('theme: $theme, ')
..write('showSets: $showSets, ')
..write('noSound: $noSound, ')
..write('backup: $backup')
..write(')'))
.toString();
}
}
class $SettingsTable extends Settings with TableInfo<$SettingsTable, Setting> {
@override
final GeneratedDatabase attachedDatabase;
final String? _alias;
$SettingsTable(this.attachedDatabase, [this._alias]);
final VerificationMeta _alarmMeta = const VerificationMeta('alarm');
@override
late final GeneratedColumn<bool?> alarm = GeneratedColumn<bool?>(
'alarm', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (alarm IN (0, 1))');
final VerificationMeta _vibrateMeta = const VerificationMeta('vibrate');
@override
late final GeneratedColumn<bool?> vibrate = GeneratedColumn<bool?>(
'vibrate', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (vibrate IN (0, 1))');
final VerificationMeta _soundMeta = const VerificationMeta('sound');
@override
late final GeneratedColumn<String?> sound = GeneratedColumn<String?>(
'sound', aliasedName, false,
type: const StringType(), requiredDuringInsert: true);
final VerificationMeta _notifyMeta = const VerificationMeta('notify');
@override
late final GeneratedColumn<bool?> notify = GeneratedColumn<bool?>(
'notify', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (notify IN (0, 1))');
final VerificationMeta _imagesMeta = const VerificationMeta('images');
@override
late final GeneratedColumn<bool?> images = GeneratedColumn<bool?>(
'images', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (images IN (0, 1))');
final VerificationMeta _showUnitMeta = const VerificationMeta('showUnit');
@override
late final GeneratedColumn<bool?> showUnit = GeneratedColumn<bool?>(
'show_unit', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (show_unit IN (0, 1))');
final VerificationMeta _lightColorMeta = const VerificationMeta('lightColor');
@override
late final GeneratedColumn<String?> lightColor = GeneratedColumn<String?>(
'light_color', aliasedName, true,
type: const StringType(), requiredDuringInsert: false);
final VerificationMeta _darkColorMeta = const VerificationMeta('darkColor');
@override
late final GeneratedColumn<String?> darkColor = GeneratedColumn<String?>(
'dark_color', aliasedName, true,
type: const StringType(), requiredDuringInsert: false);
final VerificationMeta _stepsMeta = const VerificationMeta('steps');
@override
late final GeneratedColumn<bool?> steps = GeneratedColumn<bool?>(
'steps', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (steps IN (0, 1))');
final VerificationMeta _dateMeta = const VerificationMeta('date');
@override
late final GeneratedColumn<String?> date = GeneratedColumn<String?>(
'date', aliasedName, false,
type: const StringType(), requiredDuringInsert: true);
final VerificationMeta _showDateMeta = const VerificationMeta('showDate');
@override
late final GeneratedColumn<bool?> showDate = GeneratedColumn<bool?>(
'show_date', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (show_date IN (0, 1))');
final VerificationMeta _themeMeta = const VerificationMeta('theme');
@override
late final GeneratedColumn<String?> theme = GeneratedColumn<String?>(
'theme', aliasedName, false,
type: const StringType(), requiredDuringInsert: true);
final VerificationMeta _showSetsMeta = const VerificationMeta('showSets');
@override
late final GeneratedColumn<bool?> showSets = GeneratedColumn<bool?>(
'show_sets', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (show_sets IN (0, 1))');
final VerificationMeta _noSoundMeta = const VerificationMeta('noSound');
@override
late final GeneratedColumn<bool?> noSound = GeneratedColumn<bool?>(
'no_sound', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (no_sound IN (0, 1))');
final VerificationMeta _backupMeta = const VerificationMeta('backup');
@override
late final GeneratedColumn<bool?> backup = GeneratedColumn<bool?>(
'backup', aliasedName, false,
type: const BoolType(),
requiredDuringInsert: true,
defaultConstraints: 'CHECK (backup IN (0, 1))');
@override
List<GeneratedColumn> get $columns => [
alarm,
vibrate,
sound,
notify,
images,
showUnit,
lightColor,
darkColor,
steps,
date,
showDate,
theme,
showSets,
noSound,
backup
];
@override
String get aliasedName => _alias ?? 'settings';
@override
String get actualTableName => 'settings';
@override
VerificationContext validateIntegrity(Insertable<Setting> instance,
{bool isInserting = false}) {
final context = VerificationContext();
final data = instance.toColumns(true);
if (data.containsKey('alarm')) {
context.handle(
_alarmMeta, alarm.isAcceptableOrUnknown(data['alarm']!, _alarmMeta));
} else if (isInserting) {
context.missing(_alarmMeta);
}
if (data.containsKey('vibrate')) {
context.handle(_vibrateMeta,
vibrate.isAcceptableOrUnknown(data['vibrate']!, _vibrateMeta));
} else if (isInserting) {
context.missing(_vibrateMeta);
}
if (data.containsKey('sound')) {
context.handle(
_soundMeta, sound.isAcceptableOrUnknown(data['sound']!, _soundMeta));
} else if (isInserting) {
context.missing(_soundMeta);
}
if (data.containsKey('notify')) {
context.handle(_notifyMeta,
notify.isAcceptableOrUnknown(data['notify']!, _notifyMeta));
} else if (isInserting) {
context.missing(_notifyMeta);
}
if (data.containsKey('images')) {
context.handle(_imagesMeta,
images.isAcceptableOrUnknown(data['images']!, _imagesMeta));
} else if (isInserting) {
context.missing(_imagesMeta);
}
if (data.containsKey('show_unit')) {
context.handle(_showUnitMeta,
showUnit.isAcceptableOrUnknown(data['show_unit']!, _showUnitMeta));
} else if (isInserting) {
context.missing(_showUnitMeta);
}
if (data.containsKey('light_color')) {
context.handle(
_lightColorMeta,
lightColor.isAcceptableOrUnknown(
data['light_color']!, _lightColorMeta));
}
if (data.containsKey('dark_color')) {
context.handle(_darkColorMeta,
darkColor.isAcceptableOrUnknown(data['dark_color']!, _darkColorMeta));
}
if (data.containsKey('steps')) {
context.handle(
_stepsMeta, steps.isAcceptableOrUnknown(data['steps']!, _stepsMeta));
} else if (isInserting) {
context.missing(_stepsMeta);
}
if (data.containsKey('date')) {
context.handle(
_dateMeta, date.isAcceptableOrUnknown(data['date']!, _dateMeta));
} else if (isInserting) {
context.missing(_dateMeta);
}
if (data.containsKey('show_date')) {
context.handle(_showDateMeta,
showDate.isAcceptableOrUnknown(data['show_date']!, _showDateMeta));
} else if (isInserting) {
context.missing(_showDateMeta);
}
if (data.containsKey('theme')) {
context.handle(
_themeMeta, theme.isAcceptableOrUnknown(data['theme']!, _themeMeta));
} else if (isInserting) {
context.missing(_themeMeta);
}
if (data.containsKey('show_sets')) {
context.handle(_showSetsMeta,
showSets.isAcceptableOrUnknown(data['show_sets']!, _showSetsMeta));
} else if (isInserting) {
context.missing(_showSetsMeta);
}
if (data.containsKey('no_sound')) {
context.handle(_noSoundMeta,
noSound.isAcceptableOrUnknown(data['no_sound']!, _noSoundMeta));
} else if (isInserting) {
context.missing(_noSoundMeta);
}
if (data.containsKey('backup')) {
context.handle(_backupMeta,
backup.isAcceptableOrUnknown(data['backup']!, _backupMeta));
} else if (isInserting) {
context.missing(_backupMeta);
}
return context;
}
@override
Set<GeneratedColumn> get $primaryKey => <GeneratedColumn>{};
@override
Setting map(Map<String, dynamic> data, {String? tablePrefix}) {
return Setting.fromData(data, attachedDatabase,
prefix: tablePrefix != null ? '$tablePrefix.' : null);
}
@override
$SettingsTable createAlias(String alias) {
return $SettingsTable(attachedDatabase, alias);
}
}
abstract class _$MyDatabase extends GeneratedDatabase {
_$MyDatabase(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e);
late final $SettingsTable settings = $SettingsTable(this);
@override
Iterable<TableInfo> get allTables => allSchemaEntities.whereType<TableInfo>();
@override
List<DatabaseSchemaEntity> get allSchemaEntities => [settings];
}
+4 -1
View File
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:fmassive/best_page.dart';
import 'package:fmassive/database.dart';
import 'package:fmassive/edit_set.dart';
import 'package:fmassive/gym_set.dart';
import 'package:fmassive/home_page.dart';
@@ -8,6 +9,8 @@ import 'package:fmassive/settings_page.dart';
import 'package:fmassive/timer_page.dart';
import 'package:fmassive/workouts_page.dart';
MyDatabase db = MyDatabase();
void main() {
runApp(MyApp());
}
@@ -28,7 +31,7 @@ class MyApp extends StatelessWidget {
'/best': (context) => const BestPage(),
'/workouts': (context) => const WorkoutsPage(),
'/timer': (context) => const TimerPage(),
'/settings': (context) => const SettingsPage(),
'/settings': (context) => SettingsPage(),
'/edit-set': (context) => edit,
};
+38
View File
@@ -0,0 +1,38 @@
import 'package:fmassive/database.dart';
import 'package:moor/moor.dart';
Setting defaultSettings = Setting(
alarm: true,
vibrate: true,
sound: 'default.mp3',
notify: true,
images: true,
showUnit: true,
lightColor: null,
darkColor: null,
steps: true,
date: 'DD/MM/YYYY',
showDate: true,
theme: 'light',
showSets: true,
noSound: false,
backup: false,
);
class Settings extends Table {
BoolColumn get alarm => boolean()();
BoolColumn get vibrate => boolean()();
TextColumn get sound => text()();
BoolColumn get notify => boolean()();
BoolColumn get images => boolean()();
BoolColumn get showUnit => boolean()();
TextColumn get lightColor => text().nullable()();
TextColumn get darkColor => text().nullable()();
BoolColumn get steps => boolean()();
TextColumn get date => text()();
BoolColumn get showDate => boolean()();
TextColumn get theme => text()();
BoolColumn get showSets => boolean()();
BoolColumn get noSound => boolean()();
BoolColumn get backup => boolean()();
}
+176 -2
View File
@@ -1,7 +1,21 @@
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as material;
import 'package:fmassive/database.dart';
import 'package:fmassive/main.dart';
import 'package:fmassive/settings.dart';
import 'package:moor/moor.dart';
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
SettingsPage({super.key});
final List<Map<String, dynamic>> routes = [
{'title': 'Home', 'icon': Icons.home},
{'title': 'Plans', 'icon': Icons.calendar_today},
{'title': 'Best', 'icon': Icons.star},
{'title': 'Workouts', 'icon': Icons.fitness_center},
{'title': 'Timer', 'icon': Icons.timer},
{'title': 'Settings', 'icon': Icons.settings},
];
@override
Widget build(BuildContext context) {
@@ -9,9 +23,169 @@ class SettingsPage extends StatelessWidget {
appBar: AppBar(
title: const Text('Settings'),
),
drawer: Drawer(
child: ListView.builder(
itemCount: routes.length,
itemBuilder: (context, index) {
return ListTile(
leading: Icon(routes[index]['icon']),
title: Text(routes[index]['title']),
onTap: () {
Navigator.pop(context);
Navigator.pushNamed(
context, '/${routes[index]['title'].toLowerCase()}');
},
);
},
),
),
body: const Center(
child: Text('Welcome to the Settings Page!'),
child: _SettingsPage(),
),
);
}
}
class _SettingsPage extends StatefulWidget {
const _SettingsPage({Key? key}) : super(key: key);
@override
createState() => _SettingsPageState();
}
class _SettingsPageState extends State<_SettingsPage> {
late Stream<Setting> stream;
final TextEditingController searchController = TextEditingController();
void reset() async {
var data = await db.select(db.settings).get();
if (data.isEmpty) await db.into(db.settings).insert(defaultSettings);
setState(() {
if (data.isEmpty) return;
});
}
@override
void initState() {
super.initState();
stream = db.select(db.settings).watchSingle();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<Setting>(
stream: stream,
builder: (context, snapshot) {
final settings = snapshot.data;
if (settings == null)
return const Center(child: CircularProgressIndicator());
return SingleChildScrollView(
padding: const EdgeInsets.all(8.0),
child: material.Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SwitchListTile(
title: Text('Alarm'),
value: settings.alarm,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(alarm: Value(value)));
},
),
SwitchListTile(
title: Text('Vibrate'),
value: settings.vibrate,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(vibrate: Value(value)));
},
),
SwitchListTile(
title: Text('Notify'),
value: settings.notify,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(notify: Value(value)));
},
),
SwitchListTile(
title: Text('Images'),
value: settings.images,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(images: Value(value)));
},
),
SwitchListTile(
title: Text('Show Unit'),
value: settings.showUnit,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(showUnit: Value(value)));
},
),
SwitchListTile(
title: Text('Steps'),
value: settings.steps,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(steps: Value(value)));
},
),
TextField(
decoration: InputDecoration(
labelText: 'Sound',
),
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(sound: Value(value)));
},
),
TextField(
decoration: InputDecoration(
labelText: 'Light Color',
),
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(lightColor: Value(value)));
},
),
TextField(
decoration: InputDecoration(
labelText: 'Dark Color',
),
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(darkColor: Value(value)));
},
),
TextField(
decoration: InputDecoration(
labelText: 'Date',
),
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(date: Value(value)));
},
),
],
),
);
}),
);
}
}