import 'dart:io'; import 'package:file_picker/file_picker.dart'; 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/sound_picker.dart'; import 'package:moor/moor.dart'; import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart'; class SettingsPage extends StatelessWidget { const SettingsPage({super.key, required this.search}); final String search; @override Widget build(BuildContext context) { return Scaffold( body: Center( child: _SettingsPage(search: search), ), ); } } class _SettingsPage extends StatefulWidget { const _SettingsPage({super.key, required this.search}); final String search; @override createState() => _SettingsPageState(); } class _SettingsPageState extends State<_SettingsPage> { late Stream stream; @override void initState() { super.initState(); stream = (db.select(db.settings)..limit(1)).watchSingle(); } @override Widget build(BuildContext context) { return Scaffold( body: StreamBuilder( stream: stream, builder: (context, snapshot) { final settings = snapshot.data; print('build: $settings'); if (settings == null) return const Center(child: CircularProgressIndicator()); 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}, {'title': 'Import', 'value': settings.sound}, ] .where((item) => (item['title'] as String) .toLowerCase() .contains(widget.search.toLowerCase())) .toList(); return material.Column( children: [ Expanded( child: ListView.builder( itemCount: filteredItems.length, itemBuilder: (context, index) { final item = filteredItems[index]; if (item['title'] == 'Import') return Center( child: ElevatedButton( onPressed: () async { final result = await FilePicker.platform.pickFiles( type: FileType.any, ); if (result == null) return; final file = File(result.files.single.path!); final path = await getDatabasesPath(); final to = join(path, 'massive.db'); await db.close(); await file.copy(to); print('Migrating...'); db = MyDatabase(); final migrator = db.createMigrator(); await migrator.createAll(); await db.customInsert(''' INSERT INTO gym_sets(id, name, reps, weight, created, unit, hidden, image, sets, minutes, seconds, steps) SELECT id, name, reps, weight, created, unit, hidden, image, sets, minutes, seconds, steps FROM sets'''); print('Migrated.'); }, child: const Text("Import"))); if (item['title'] == 'Sound') { return Center( child: SoundPicker( path: settings.sound, onSelect: (path) { db .update(db.settings) .write(SettingsCompanion(sound: Value(path))); }, ), ); } return SwitchListTile( title: Text(item['title'].toString()), value: (item['value'] ?? false) as bool, onChanged: (value) { switch (item['title']) { case 'Alarm': db .update(db.settings) .write(SettingsCompanion(alarm: Value(value))); break; case 'Vibrate': db.update(db.settings).write( SettingsCompanion(vibrate: Value(value))); 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( SettingsCompanion(showUnit: Value(value))); break; case 'Steps': db .update(db.settings) .write(SettingsCompanion(steps: Value(value))); break; } }, ); }, ), ), ], ); }, ), ); } }