2023-11-29 16:57:19 +13:00
|
|
|
import 'dart:convert';
|
2023-04-17 11:51:14 +12:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-11-29 16:57:19 +13:00
|
|
|
import 'package:csv/csv.dart';
|
2023-04-17 11:51:14 +12:00
|
|
|
import 'package:file_picker/file_picker.dart';
|
2023-04-07 11:50:02 +12:00
|
|
|
import 'package:flutter/material.dart';
|
2023-04-09 14:39:41 +12:00
|
|
|
import 'package:flutter/material.dart' as material;
|
|
|
|
import 'package:fmassive/database.dart';
|
2023-11-29 16:57:19 +13:00
|
|
|
import 'package:fmassive/delete_all_sets.dart';
|
2023-04-09 14:39:41 +12:00
|
|
|
import 'package:fmassive/main.dart';
|
2023-04-13 15:10:09 +12:00
|
|
|
import 'package:fmassive/sound_picker.dart';
|
2023-04-09 14:39:41 +12:00
|
|
|
import 'package:moor/moor.dart';
|
2023-04-07 11:50:02 +12:00
|
|
|
|
|
|
|
class SettingsPage extends StatelessWidget {
|
2023-04-13 17:23:26 +12:00
|
|
|
const SettingsPage({super.key, required this.search});
|
|
|
|
|
|
|
|
final String search;
|
2023-04-07 11:50:02 +12:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-04-13 17:23:26 +12:00
|
|
|
return Scaffold(
|
2023-04-11 11:41:10 +12:00
|
|
|
body: Center(
|
2023-04-13 17:23:26 +12:00
|
|
|
child: _SettingsPage(search: search),
|
2023-04-07 11:50:02 +12:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 14:39:41 +12:00
|
|
|
|
|
|
|
class _SettingsPage extends StatefulWidget {
|
2023-11-29 13:01:11 +13:00
|
|
|
const _SettingsPage({required this.search});
|
2023-04-13 17:23:26 +12:00
|
|
|
|
|
|
|
final String search;
|
2023-04-09 14:39:41 +12:00
|
|
|
|
|
|
|
@override
|
|
|
|
createState() => _SettingsPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SettingsPageState extends State<_SettingsPage> {
|
|
|
|
late Stream<Setting> stream;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-11-10 16:16:12 +13:00
|
|
|
stream = (db.select(db.settings)..limit(1)).watchSingle();
|
2023-04-09 14:39:41 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
body: StreamBuilder<Setting>(
|
2023-04-13 17:23:26 +12:00
|
|
|
stream: stream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
final settings = snapshot.data;
|
2023-11-10 16:16:12 +13:00
|
|
|
print('build: $settings');
|
2023-04-09 14:39:41 +12:00
|
|
|
|
2023-11-30 10:37:50 +13:00
|
|
|
if (settings == null) return Container();
|
2023-04-09 14:39:41 +12:00
|
|
|
|
2023-04-13 17:23:26 +12:00
|
|
|
final filteredItems = [
|
|
|
|
{'title': 'Alarm', 'value': settings.alarm},
|
|
|
|
{'title': 'Vibrate', 'value': settings.vibrate},
|
|
|
|
{'title': 'Notify', 'value': settings.notify},
|
|
|
|
{'title': 'Images', 'value': settings.images},
|
|
|
|
{'title': 'Show Unit', 'value': settings.showUnit},
|
|
|
|
{'title': 'Steps', 'value': settings.steps},
|
|
|
|
{'title': 'Sound', 'value': settings.sound},
|
2023-11-30 10:27:23 +13:00
|
|
|
{'title': 'Import sets', 'value': null},
|
2023-11-29 16:57:19 +13:00
|
|
|
{'title': 'Delete all sets', 'value': null},
|
2023-04-13 17:23:26 +12:00
|
|
|
]
|
|
|
|
.where((item) => (item['title'] as String)
|
|
|
|
.toLowerCase()
|
2023-11-30 13:02:50 +13:00
|
|
|
.contains(widget.search.toLowerCase()),)
|
2023-04-13 17:23:26 +12:00
|
|
|
.toList();
|
|
|
|
|
|
|
|
return material.Column(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: filteredItems.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final item = filteredItems[index];
|
|
|
|
|
2023-11-29 16:57:19 +13:00
|
|
|
if (item['title'] == 'Delete all sets')
|
|
|
|
return DeleteAllSets(mounted: mounted);
|
|
|
|
|
2023-11-30 10:27:23 +13:00
|
|
|
if (item['title'] == 'Import sets')
|
2023-04-17 11:51:14 +12:00
|
|
|
return Center(
|
|
|
|
child: ElevatedButton(
|
2023-11-30 10:27:23 +13:00
|
|
|
child: const Text("Import sets"),
|
2023-11-29 16:57:19 +13:00
|
|
|
onPressed: () async {
|
|
|
|
final result = await FilePicker.platform.pickFiles(
|
|
|
|
type: FileType.custom,
|
2023-11-30 13:02:50 +13:00
|
|
|
allowedExtensions: ['csv'],);
|
2023-11-29 16:57:19 +13:00
|
|
|
if (result == null) return;
|
|
|
|
|
|
|
|
final file = File(result.files.single.path!);
|
|
|
|
final input = file.openRead();
|
|
|
|
final fields = await input
|
|
|
|
.transform(utf8.decoder)
|
|
|
|
.transform(const CsvToListConverter(eol: "\n"))
|
|
|
|
.skip(1)
|
|
|
|
.toList();
|
|
|
|
final gymSets = fields.map((row) => GymSetsCompanion(
|
|
|
|
id: Value(int.tryParse(row[0]) ?? 0),
|
|
|
|
name: Value(row[1]),
|
|
|
|
reps: Value(int.tryParse(row[2]) ?? 0),
|
|
|
|
weight: Value(double.tryParse(row[3]) ?? 0),
|
|
|
|
created: Value(row[4]),
|
|
|
|
unit: Value(row[5]),
|
|
|
|
hidden: Value(row[6] == 'true'),
|
|
|
|
image: Value(row[7]),
|
|
|
|
sets: Value(int.tryParse(row[8]) ?? 0),
|
|
|
|
minutes: Value(int.tryParse(row[9]) ?? 0),
|
|
|
|
seconds: Value(int.tryParse(row[10]) ?? 0),
|
|
|
|
steps: Value(row[11]),
|
2023-11-30 13:02:50 +13:00
|
|
|
),);
|
2023-11-29 16:57:19 +13:00
|
|
|
await db.batch(
|
2023-11-30 13:02:50 +13:00
|
|
|
(batch) => batch.insertAll(db.gymSets, gymSets),);
|
2023-11-29 16:57:19 +13:00
|
|
|
if (!mounted) return;
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2023-11-30 13:02:50 +13:00
|
|
|
const SnackBar(content: Text('Imported sets')),);
|
2023-11-29 16:57:19 +13:00
|
|
|
},
|
2023-11-30 13:02:50 +13:00
|
|
|
),);
|
2023-04-17 11:51:14 +12:00
|
|
|
|
2023-04-13 17:23:26 +12:00
|
|
|
if (item['title'] == 'Sound') {
|
|
|
|
return Center(
|
|
|
|
child: SoundPicker(
|
2023-04-13 15:10:09 +12:00
|
|
|
path: settings.sound,
|
|
|
|
onSelect: (path) {
|
|
|
|
db
|
|
|
|
.update(db.settings)
|
|
|
|
.write(SettingsCompanion(sound: Value(path)));
|
2023-04-13 17:23:26 +12:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SwitchListTile(
|
|
|
|
title: Text(item['title'].toString()),
|
2023-11-10 16:16:12 +13:00
|
|
|
value: (item['value'] ?? false) as bool,
|
2023-04-13 17:23:26 +12:00
|
|
|
onChanged: (value) {
|
|
|
|
switch (item['title']) {
|
|
|
|
case 'Alarm':
|
|
|
|
db
|
|
|
|
.update(db.settings)
|
|
|
|
.write(SettingsCompanion(alarm: Value(value)));
|
|
|
|
break;
|
|
|
|
case 'Vibrate':
|
|
|
|
db.update(db.settings).write(
|
2023-11-30 13:02:50 +13:00
|
|
|
SettingsCompanion(vibrate: Value(value)),);
|
2023-04-13 17:23:26 +12:00
|
|
|
break;
|
|
|
|
case 'Notify':
|
|
|
|
db
|
|
|
|
.update(db.settings)
|
|
|
|
.write(SettingsCompanion(notify: Value(value)));
|
|
|
|
break;
|
|
|
|
case 'Images':
|
|
|
|
db
|
|
|
|
.update(db.settings)
|
|
|
|
.write(SettingsCompanion(images: Value(value)));
|
|
|
|
break;
|
|
|
|
case 'Show Unit':
|
|
|
|
db.update(db.settings).write(
|
2023-11-30 13:02:50 +13:00
|
|
|
SettingsCompanion(showUnit: Value(value)),);
|
2023-04-13 17:23:26 +12:00
|
|
|
break;
|
|
|
|
case 'Steps':
|
|
|
|
db
|
|
|
|
.update(db.settings)
|
|
|
|
.write(SettingsCompanion(steps: Value(value)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2023-04-09 14:39:41 +12:00
|
|
|
),
|
2023-04-13 17:23:26 +12:00
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2023-04-09 14:39:41 +12:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|