2022-05-04 23:01:53 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
2021-08-03 22:04:29 +02:00
|
|
|
import 'package:confirm_dialog/confirm_dialog.dart';
|
2022-05-04 23:01:53 +02:00
|
|
|
import 'package:file_picker/file_picker.dart';
|
2021-08-03 22:04:29 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-01-14 09:01:52 +01:00
|
|
|
import 'package:flutter_settings_ui/flutter_settings_ui.dart';
|
2022-01-23 23:56:26 +01:00
|
|
|
import 'package:mdi/mdi.dart';
|
2022-05-04 23:01:53 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:sembast/sembast_io.dart';
|
|
|
|
import 'package:sembast/utils/sembast_import_export.dart';
|
2021-08-08 23:16:54 +02:00
|
|
|
|
2022-01-19 02:10:05 +01:00
|
|
|
import '../bloc/theme/theme_bloc.dart';
|
2022-01-23 23:56:26 +01:00
|
|
|
import '../components/common/denshi_jisho_background.dart';
|
2022-01-19 02:10:05 +01:00
|
|
|
import '../models/history/search.dart';
|
2022-01-25 00:54:21 +01:00
|
|
|
import '../routing/routes.dart';
|
2022-05-04 23:01:53 +02:00
|
|
|
import '../services/database.dart';
|
2022-01-25 00:54:21 +01:00
|
|
|
import '../services/open_webpage.dart';
|
2022-05-04 23:01:53 +02:00
|
|
|
import '../services/snackbar.dart';
|
2022-01-19 13:47:54 +01:00
|
|
|
import '../settings.dart';
|
2021-12-01 23:09:53 +01:00
|
|
|
|
2021-08-08 23:16:54 +02:00
|
|
|
class SettingsView extends StatefulWidget {
|
2021-12-01 23:09:53 +01:00
|
|
|
const SettingsView({Key? key}) : super(key: key);
|
2021-08-08 23:16:54 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
_SettingsViewState createState() => _SettingsViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SettingsViewState extends State<SettingsView> {
|
2022-01-19 13:47:54 +01:00
|
|
|
final Database db = GetIt.instance.get<Database>();
|
2022-05-04 23:01:53 +02:00
|
|
|
bool dataExportIsLoading = false;
|
|
|
|
bool dataImportIsLoading = false;
|
2021-08-03 22:04:29 +02:00
|
|
|
|
2021-12-04 05:13:13 +01:00
|
|
|
Future<void> clearHistory(context) async {
|
|
|
|
final bool userIsSure = await confirm(context);
|
|
|
|
|
|
|
|
if (userIsSure) {
|
|
|
|
await Search.store.delete(db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:06:03 +01:00
|
|
|
// ignore: avoid_positional_boolean_parameters
|
|
|
|
void toggleAutoTheme(bool b) {
|
|
|
|
final bool newThemeIsDark = b
|
|
|
|
? WidgetsBinding.instance!.window.platformBrightness == Brightness.dark
|
|
|
|
: darkThemeEnabled;
|
|
|
|
|
|
|
|
BlocProvider.of<ThemeBloc>(context)
|
|
|
|
.add(SetTheme(themeIsDark: newThemeIsDark));
|
|
|
|
|
|
|
|
setState(() => autoThemeEnabled = b);
|
|
|
|
}
|
|
|
|
|
2022-05-04 23:01:53 +02:00
|
|
|
Future<void> changeFont(context) async {
|
|
|
|
final int? i = await _chooseFromList(
|
|
|
|
list: [for (final font in JapaneseFont.values) font.name],
|
|
|
|
chosen: japaneseFont.index,
|
|
|
|
)(context);
|
|
|
|
if (i != null)
|
|
|
|
setState(() {
|
|
|
|
japaneseFont = JapaneseFont.values[i];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Can assume Android for time being
|
|
|
|
Future<void> exportData(context) async {
|
|
|
|
setState(() => dataExportIsLoading = true);
|
|
|
|
|
|
|
|
final path = (await getExternalStorageDirectory())!;
|
|
|
|
final dbData = await exportDatabase(db);
|
|
|
|
final file = File('${path.path}/jisho_data.json');
|
|
|
|
file.createSync(recursive: true);
|
|
|
|
await file.writeAsString(jsonEncode(dbData));
|
|
|
|
|
|
|
|
setState(() => dataExportIsLoading = false);
|
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
.showSnackBar(SnackBar(content: Text('Data exported to ${file.path}')));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Can assume Android for time being
|
|
|
|
Future<void> importData(context) async {
|
|
|
|
setState(() => dataImportIsLoading = true);
|
|
|
|
|
|
|
|
final path = await FilePicker.platform.pickFiles(
|
|
|
|
type: FileType.custom,
|
|
|
|
allowedExtensions: ['json'],
|
|
|
|
);
|
|
|
|
final file = File(path!.files[0].path!);
|
|
|
|
|
|
|
|
final List<Search> prevSearches = (await Search.store.find(db))
|
|
|
|
.map((e) => Search.fromJson(e.value! as Map<String, Object?>))
|
|
|
|
.toList();
|
|
|
|
late final List<Search> importedSearches;
|
|
|
|
try {
|
|
|
|
importedSearches = ((((jsonDecode(await file.readAsString())
|
|
|
|
as Map<String, Object?>)['stores']! as List)
|
|
|
|
.map((e) => e as Map)
|
|
|
|
.where((e) => e['name'] == 'search')
|
|
|
|
.first)['values'] as List)
|
|
|
|
.map((item) => Search.fromJson(item))
|
|
|
|
.toList();
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint(e.toString());
|
|
|
|
showSnackbar(
|
|
|
|
context,
|
|
|
|
"Couldn't read file. Did you choose the right one?",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<Search> mergedSearches =
|
|
|
|
mergeSearches(prevSearches, importedSearches);
|
|
|
|
|
|
|
|
// print(mergedSearches);
|
|
|
|
|
|
|
|
await GetIt.instance.get<Database>().close();
|
|
|
|
GetIt.instance.unregister<Database>();
|
|
|
|
|
|
|
|
final importedDb = await importDatabase(
|
|
|
|
{
|
|
|
|
'sembast_export': 1,
|
|
|
|
'version': 1,
|
|
|
|
'stores': [
|
|
|
|
{
|
|
|
|
'name': 'search',
|
|
|
|
'keys': [for (var i = 1; i <= mergedSearches.length; i++) i],
|
|
|
|
'values': mergedSearches.map((e) => e.toJson()).toList(),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
databaseFactoryIo,
|
|
|
|
await databasePath(),
|
|
|
|
);
|
|
|
|
GetIt.instance.registerSingleton<Database>(importedDb);
|
|
|
|
|
|
|
|
setState(() => dataImportIsLoading = false);
|
|
|
|
showSnackbar(context, 'Data imported successfully');
|
|
|
|
}
|
|
|
|
|
2022-01-23 23:56:26 +01:00
|
|
|
Future<int?> Function(BuildContext) _chooseFromList({
|
|
|
|
required List<String> list,
|
|
|
|
int? chosen,
|
|
|
|
String? title,
|
|
|
|
}) =>
|
|
|
|
(context) => Navigator.push<int>(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => Scaffold(
|
|
|
|
appBar: AppBar(title: title == null ? null : Text(title)),
|
|
|
|
body: DenshiJishoBackground(
|
|
|
|
child: ListView.builder(
|
|
|
|
itemBuilder: (context, i) => ListTile(
|
|
|
|
title: Text(list[i]),
|
|
|
|
trailing: (chosen != null && chosen == i)
|
|
|
|
? const Icon(Icons.check)
|
|
|
|
: null,
|
|
|
|
onTap: () => Navigator.pop(context, i),
|
|
|
|
),
|
|
|
|
itemCount: list.length,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2021-12-01 23:09:53 +01:00
|
|
|
@override
|
2022-01-23 04:52:28 +01:00
|
|
|
Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
|
|
|
|
builder: (context, state) {
|
|
|
|
final TextStyle _titleTextStyle = TextStyle(
|
|
|
|
color:
|
|
|
|
state is DarkThemeState ? AppTheme.jishoGreen.background : null,
|
|
|
|
);
|
2021-08-08 23:16:54 +02:00
|
|
|
|
2022-01-23 23:56:26 +01:00
|
|
|
const SettingsTileTheme theme = SettingsTileTheme(
|
|
|
|
horizontalTitleGap: 0,
|
|
|
|
);
|
|
|
|
|
2022-01-23 04:52:28 +01:00
|
|
|
return SettingsList(
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
contentPadding: const EdgeInsets.symmetric(vertical: 10),
|
|
|
|
sections: <SettingsSection>[
|
|
|
|
SettingsSection(
|
|
|
|
title: 'Dictionary',
|
|
|
|
titleTextStyle: _titleTextStyle,
|
|
|
|
tiles: <SettingsTile>[
|
|
|
|
SettingsTile.switchTile(
|
|
|
|
title: 'Use romaji',
|
2022-01-23 23:56:26 +01:00
|
|
|
leading: const Icon(Mdi.alphabetical),
|
2022-05-04 23:01:53 +02:00
|
|
|
onToggle: (b) => setState(() => romajiEnabled = b),
|
2022-01-23 04:52:28 +01:00
|
|
|
switchValue: romajiEnabled,
|
2022-01-23 23:56:26 +01:00
|
|
|
theme: theme,
|
2022-01-23 04:52:28 +01:00
|
|
|
switchActiveColor: AppTheme.jishoGreen.background,
|
|
|
|
),
|
2022-01-23 18:27:00 +01:00
|
|
|
SettingsTile.switchTile(
|
|
|
|
title: 'Extensive search',
|
2022-01-23 23:56:26 +01:00
|
|
|
leading: const Icon(Icons.downloading),
|
2022-05-04 23:01:53 +02:00
|
|
|
onToggle: (b) => setState(() => extensiveSearchEnabled = b),
|
2022-01-23 18:27:00 +01:00
|
|
|
switchValue: extensiveSearchEnabled,
|
2022-01-23 23:56:26 +01:00
|
|
|
theme: theme,
|
2022-01-23 18:27:00 +01:00
|
|
|
switchActiveColor: AppTheme.jishoGreen.background,
|
2022-01-23 23:56:26 +01:00
|
|
|
// subtitle:
|
|
|
|
// 'Gathers extra data when searching for words, at the expense of having to wait for extra word details.',
|
|
|
|
// subtitleWidget:
|
|
|
|
trailing: const Icon(Icons.info),
|
|
|
|
subtitleMaxLines: 3,
|
|
|
|
),
|
|
|
|
SettingsTile(
|
|
|
|
title: 'Japanese font',
|
|
|
|
leading: const Icon(Icons.format_size),
|
2022-05-04 23:01:53 +02:00
|
|
|
onPressed: changeFont,
|
2022-01-23 23:56:26 +01:00
|
|
|
theme: theme,
|
|
|
|
trailing: Text(japaneseFont.name),
|
|
|
|
// subtitle:
|
|
|
|
// 'Which font to use for japanese text. This might be useful if your phone shows kanji with a Chinese font.',
|
2022-01-23 18:27:00 +01:00
|
|
|
subtitleMaxLines: 3,
|
|
|
|
),
|
2022-01-23 04:52:28 +01:00
|
|
|
],
|
|
|
|
),
|
2022-05-04 23:01:53 +02:00
|
|
|
|
2022-01-23 04:52:28 +01:00
|
|
|
SettingsSection(
|
|
|
|
title: 'Theme',
|
|
|
|
titleTextStyle: _titleTextStyle,
|
|
|
|
tiles: <SettingsTile>[
|
|
|
|
SettingsTile.switchTile(
|
2022-01-23 23:56:26 +01:00
|
|
|
title: 'Automatic theme',
|
|
|
|
leading: const Icon(Icons.brightness_auto),
|
2022-01-23 04:52:28 +01:00
|
|
|
onToggle: toggleAutoTheme,
|
|
|
|
switchValue: autoThemeEnabled,
|
2022-01-23 23:56:26 +01:00
|
|
|
theme: theme,
|
2022-01-23 04:52:28 +01:00
|
|
|
switchActiveColor: AppTheme.jishoGreen.background,
|
|
|
|
),
|
|
|
|
SettingsTile.switchTile(
|
|
|
|
title: 'Dark Theme',
|
2022-01-23 23:56:26 +01:00
|
|
|
leading: const Icon(Icons.dark_mode),
|
2022-01-23 04:52:28 +01:00
|
|
|
onToggle: (b) {
|
|
|
|
BlocProvider.of<ThemeBloc>(context)
|
|
|
|
.add(SetTheme(themeIsDark: b));
|
|
|
|
setState(() => darkThemeEnabled = b);
|
|
|
|
},
|
|
|
|
switchValue: darkThemeEnabled,
|
|
|
|
enabled: !autoThemeEnabled,
|
2022-01-23 23:56:26 +01:00
|
|
|
theme: theme,
|
2022-01-23 04:52:28 +01:00
|
|
|
switchActiveColor: AppTheme.jishoGreen.background,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-05-04 23:01:53 +02:00
|
|
|
|
2022-01-25 00:54:21 +01:00
|
|
|
// TODO: This will be left commented until caching is implemented
|
|
|
|
// SettingsSection(
|
|
|
|
// title: 'Cache',
|
|
|
|
// titleTextStyle: _titleTextStyle,
|
|
|
|
// tiles: <SettingsTile>[
|
|
|
|
// SettingsTile.switchTile(
|
|
|
|
// title: 'Cache grade 1-7 kanji',
|
|
|
|
// switchValue: false,
|
|
|
|
// onToggle: (v) {},
|
|
|
|
// enabled: false,
|
|
|
|
// switchActiveColor: AppTheme.jishoGreen.background,
|
|
|
|
// ),
|
|
|
|
// SettingsTile.switchTile(
|
|
|
|
// title: 'Cache grade standard kanji',
|
|
|
|
// switchValue: false,
|
|
|
|
// onToggle: (v) {},
|
|
|
|
// enabled: false,
|
|
|
|
// switchActiveColor: AppTheme.jishoGreen.background,
|
|
|
|
// ),
|
|
|
|
// SettingsTile.switchTile(
|
|
|
|
// title: 'Cache all favourites',
|
|
|
|
// switchValue: false,
|
|
|
|
// onToggle: (v) {},
|
|
|
|
// enabled: false,
|
|
|
|
// switchActiveColor: AppTheme.jishoGreen.background,
|
|
|
|
// ),
|
|
|
|
// SettingsTile.switchTile(
|
|
|
|
// title: 'Cache all searches',
|
|
|
|
// switchValue: false,
|
|
|
|
// onToggle: (v) {},
|
|
|
|
// enabled: false,
|
|
|
|
// switchActiveColor: AppTheme.jishoGreen.background,
|
|
|
|
// ),
|
|
|
|
// ],
|
|
|
|
// ),
|
2022-05-04 23:01:53 +02:00
|
|
|
|
2022-01-23 04:52:28 +01:00
|
|
|
SettingsSection(
|
|
|
|
title: 'Data',
|
|
|
|
titleTextStyle: _titleTextStyle,
|
|
|
|
tiles: <SettingsTile>[
|
2022-05-04 23:01:53 +02:00
|
|
|
SettingsTile(
|
|
|
|
leading: const Icon(Icons.file_upload),
|
|
|
|
title: 'Import Data',
|
|
|
|
onPressed: importData,
|
|
|
|
enabled: Platform.isAndroid,
|
|
|
|
subtitle:
|
|
|
|
Platform.isAndroid ? null : 'Not available on iOS yet',
|
|
|
|
subtitleWidget: dataImportIsLoading
|
|
|
|
? const LinearProgressIndicator()
|
|
|
|
: null,
|
|
|
|
),
|
2022-01-23 04:52:28 +01:00
|
|
|
SettingsTile(
|
|
|
|
leading: const Icon(Icons.file_download),
|
|
|
|
title: 'Export Data',
|
2022-05-04 23:01:53 +02:00
|
|
|
enabled: Platform.isAndroid,
|
|
|
|
subtitle:
|
|
|
|
Platform.isAndroid ? null : 'Not available on iOS yet',
|
|
|
|
subtitleWidget: dataExportIsLoading
|
|
|
|
? const LinearProgressIndicator()
|
|
|
|
: null,
|
2022-01-23 04:52:28 +01:00
|
|
|
),
|
|
|
|
SettingsTile(
|
|
|
|
leading: const Icon(Icons.delete),
|
|
|
|
title: 'Clear History',
|
|
|
|
onPressed: clearHistory,
|
|
|
|
titleTextStyle: const TextStyle(color: Colors.red),
|
|
|
|
),
|
|
|
|
SettingsTile(
|
|
|
|
leading: const Icon(Icons.delete),
|
|
|
|
title: 'Clear Favourites',
|
|
|
|
onPressed: (c) {},
|
|
|
|
titleTextStyle: const TextStyle(color: Colors.red),
|
|
|
|
enabled: false,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2022-05-04 23:01:53 +02:00
|
|
|
|
2022-01-25 00:54:21 +01:00
|
|
|
SettingsSection(
|
|
|
|
title: 'Info',
|
|
|
|
titleTextStyle: _titleTextStyle,
|
|
|
|
tiles: <SettingsTile>[
|
|
|
|
SettingsTile(
|
|
|
|
leading: const Icon(Icons.info),
|
|
|
|
title: 'About',
|
|
|
|
onPressed: (c) =>
|
|
|
|
Navigator.pushNamed(context, Routes.about),
|
|
|
|
),
|
|
|
|
SettingsTile(
|
|
|
|
leading: Image.asset(
|
|
|
|
'assets/images/logo/logo_icon_transparent_green.png',
|
|
|
|
width: 30,
|
|
|
|
),
|
|
|
|
title: 'Jisho',
|
|
|
|
onPressed: (c) => open_webpage('https://jisho.org/about'),
|
|
|
|
),
|
|
|
|
SettingsTile(
|
|
|
|
leading: const Icon(Icons.copyright),
|
|
|
|
title: 'Licenses',
|
|
|
|
onPressed: (c) =>
|
|
|
|
Navigator.pushNamed(context, Routes.aboutLicenses),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2022-01-23 04:52:28 +01:00
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2021-08-03 22:04:29 +02:00
|
|
|
}
|