fmassive/lib/settings_page.dart

167 lines
6.0 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:file_picker/file_picker.dart';
2023-04-07 11:50:02 +12:00
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as material;
import 'package:fmassive/database.dart';
import 'package:fmassive/main.dart';
2023-04-13 15:10:09 +12:00
import 'package:fmassive/sound_picker.dart';
import 'package:moor/moor.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.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
),
);
}
}
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;
@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();
}
@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-13 17:23:26 +12:00
if (settings == null)
return const Center(child: CircularProgressIndicator());
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},
{'title': 'Import', 'value': settings.sound},
2023-04-13 17:23:26 +12:00
]
.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();
// ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member
final migrator = db.createMigrator();
await migrator.createAll();
await db.update(db.settings).write(
const SettingsCompanion(
sound: Value(null)));
print('Migrated.');
},
child: const Text("Import")));
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(
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;
}
},
);
},
),
),
2023-04-13 17:23:26 +12:00
],
);
},
),
);
}
}