1
0
mirror of https://github.com/h7x4/Jisho-Study-Tool.git synced 2024-12-21 21:47:29 +01:00

Connect romaji transliteration to new setting

This commit is contained in:
Oystein Kristoffer Tveit 2022-01-19 13:47:54 +01:00
parent 17d6f862a2
commit 988d5c043c
6 changed files with 63 additions and 42 deletions

View File

@ -2,15 +2,17 @@ import 'package:flutter/material.dart';
import 'package:unofficial_jisho_api/api.dart'; import 'package:unofficial_jisho_api/api.dart';
import '../../../bloc/theme/theme_bloc.dart'; import '../../../bloc/theme/theme_bloc.dart';
import '../../../services/romaji_transliteration.dart';
import '../../../settings.dart';
class Examples extends StatelessWidget { class Examples extends StatelessWidget {
final List<YomiExample> onyomi; final List<YomiExample> onyomi;
final List<YomiExample> kunyomi; final List<YomiExample> kunyomi;
const Examples({ const Examples({
Key? key,
required this.onyomi, required this.onyomi,
required this.kunyomi, required this.kunyomi,
Key? key,
}) : super(key: key); }) : super(key: key);
@override @override
@ -87,6 +89,7 @@ class _Kana extends StatelessWidget {
required this.example, required this.example,
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
@ -102,7 +105,7 @@ class _Kana extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Text( Text(
example.reading, romajiEnabled ? transliterateKanaToLatin(example.reading) : example.reading,
style: TextStyle( style: TextStyle(
color: colors.foreground, color: colors.foreground,
fontSize: 15.0, fontSize: 15.0,

View File

@ -1,6 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../../../bloc/theme/theme_bloc.dart'; import '../../../bloc/theme/theme_bloc.dart';
import '../../../services/romaji_transliteration.dart';
import '../../../settings.dart';
enum YomiType { enum YomiType {
onyomi, onyomi,
@ -71,6 +73,7 @@ class YomiChips extends StatelessWidget {
Widget yomiWrapper(BuildContext context) { Widget yomiWrapper(BuildContext context) {
final yomiCards = yomi final yomiCards = yomi
.map((y) => romajiEnabled ? transliterateKanaToLatin(y) : y)
.map((y) => yomiCard(yomi: y, colors: type.getColors(context))) .map((y) => yomiCard(yomi: y, colors: type.getColors(context)))
.toList(); .toList();

View File

@ -1,6 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:unofficial_jisho_api/api.dart'; import 'package:unofficial_jisho_api/api.dart';
import '../../../../services/romaji_transliteration.dart';
import '../../../../settings.dart';
class JapaneseHeader extends StatelessWidget { class JapaneseHeader extends StatelessWidget {
final JishoJapaneseWord word; final JishoJapaneseWord word;
@ -13,14 +16,23 @@ class JapaneseHeader extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final String? wordReading = word.reading == null
? null
: (romajiEnabled
? transliterateKanaToLatin(word.reading!)
: word.reading!);
return Container( return Container(
alignment: Alignment.centerLeft, alignment: Alignment.centerLeft,
padding: const EdgeInsets.only(left: 10.0), padding: const EdgeInsets.only(left: 10.0),
child: Column( child: Column(
children: [ children: [
// TODO: take a look at this logic // Both wordReading and word.word being present implies that the word has furigana.
hasFurigana ? Text(word.reading!) : const Text(''), // If that's not the case, then the word is usually present in wordReading.
hasFurigana ? Text(word.word!) : Text(word.reading ?? word.word!), // However, there are some exceptions where the reading is placed in word.
// I have no clue why this might be the case.
hasFurigana ? Text(wordReading!) : const Text(''),
hasFurigana ? Text(word.word!) : Text(wordReading ?? word.word!),
], ],
), ),
); );

View File

@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
import 'package:unofficial_jisho_api/api.dart'; import 'package:unofficial_jisho_api/api.dart';
import '../../../../bloc/theme/theme_bloc.dart'; import '../../../../bloc/theme/theme_bloc.dart';
import '../../../../services/romaji_transliteration.dart';
import '../../../../settings.dart';
class OtherForms extends StatelessWidget { class OtherForms extends StatelessWidget {
final List<JishoJapaneseWord> forms; final List<JishoJapaneseWord> forms;
@ -38,6 +40,12 @@ class _KanaBox extends StatelessWidget {
final _menuColors = final _menuColors =
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyLight; BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyLight;
final String? wordReading = word.reading == null
? null
: (romajiEnabled
? transliterateKanaToLatin(word.reading!)
: word.reading!);
return Container( return Container(
margin: const EdgeInsets.symmetric( margin: const EdgeInsets.symmetric(
horizontal: 5.0, horizontal: 5.0,
@ -58,9 +66,9 @@ class _KanaBox extends StatelessWidget {
child: DefaultTextStyle.merge( child: DefaultTextStyle.merge(
child: Column( child: Column(
children: [ children: [
// TODO: take a look at this logic // See header.dart for more details about this logic
hasFurigana ? Text(word.reading ?? '') : const Text(''), hasFurigana ? Text(wordReading ?? '') : const Text(''),
hasFurigana ? Text(word.word!) : Text(word.reading ?? ''), hasFurigana ? Text(word.word!) : Text(wordReading ?? word.word!),
], ],
), ),
style: TextStyle(color: _menuColors.foreground), style: TextStyle(color: _menuColors.foreground),

View File

@ -1,10 +1,10 @@
import 'package:confirm_dialog/confirm_dialog.dart'; import 'package:confirm_dialog/confirm_dialog.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_settings_ui/flutter_settings_ui.dart'; import 'package:flutter_settings_ui/flutter_settings_ui.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../bloc/theme/theme_bloc.dart'; import '../bloc/theme/theme_bloc.dart';
import '../models/history/search.dart'; import '../models/history/search.dart';
import '../settings.dart';
class SettingsView extends StatefulWidget { class SettingsView extends StatefulWidget {
const SettingsView({Key? key}) : super(key: key); const SettingsView({Key? key}) : super(key: key);
@ -14,33 +14,12 @@ class SettingsView extends StatefulWidget {
} }
class _SettingsViewState extends State<SettingsView> { class _SettingsViewState extends State<SettingsView> {
final SharedPreferences prefs = GetIt.instance.get<SharedPreferences>(); final Database db = GetIt.instance.get<Database>();
bool romajiEnabled = false;
bool darkThemeEnabled = false;
bool autoThemeEnabled = false;
@override
void initState() {
super.initState();
romajiEnabled = prefs.getBool('romajiEnabled') ?? romajiEnabled;
darkThemeEnabled = prefs.getBool('darkThemeEnabled') ?? darkThemeEnabled;
autoThemeEnabled = prefs.getBool('autoThemeEnabled') ?? autoThemeEnabled;
}
/// Update stored preferences with values from setting page state
Future<void> _updatePrefs() async {
prefs.setBool('romajiEnabled', romajiEnabled);
prefs.setBool('darkThemeEnabled', darkThemeEnabled);
prefs.setBool('autoThemeEnabled', autoThemeEnabled);
}
Future<void> clearHistory(context) async { Future<void> clearHistory(context) async {
final bool userIsSure = await confirm(context); final bool userIsSure = await confirm(context);
if (userIsSure) { if (userIsSure) {
final Database db = GetIt.instance.get<Database>();
await Search.store.delete(db); await Search.store.delete(db);
} }
} }
@ -63,9 +42,10 @@ class _SettingsViewState extends State<SettingsView> {
tiles: <SettingsTile>[ tiles: <SettingsTile>[
SettingsTile.switchTile( SettingsTile.switchTile(
title: 'Use romaji', title: 'Use romaji',
onToggle: (b) {}, //TODO: implement onToggle: (b) {
setState(() => romajiEnabled = b);
},
switchValue: romajiEnabled, switchValue: romajiEnabled,
enabled: false,
switchActiveColor: AppTheme.jishoGreen.background, switchActiveColor: AppTheme.jishoGreen.background,
), ),
], ],
@ -77,10 +57,7 @@ class _SettingsViewState extends State<SettingsView> {
SettingsTile.switchTile( SettingsTile.switchTile(
title: 'Automatically determine theme', title: 'Automatically determine theme',
onToggle: (b) { onToggle: (b) {
setState(() { setState(() => autoThemeEnabled = b);
autoThemeEnabled = b;
});
_updatePrefs();
}, },
switchValue: autoThemeEnabled, switchValue: autoThemeEnabled,
enabled: false, enabled: false,
@ -91,10 +68,7 @@ class _SettingsViewState extends State<SettingsView> {
onToggle: (b) { onToggle: (b) {
BlocProvider.of<ThemeBloc>(context) BlocProvider.of<ThemeBloc>(context)
.add(SetTheme(themeIsDark: b)); .add(SetTheme(themeIsDark: b));
setState(() { setState(() => darkThemeEnabled = b);
darkThemeEnabled = b;
});
_updatePrefs();
}, },
switchValue: darkThemeEnabled, switchValue: darkThemeEnabled,
enabled: !autoThemeEnabled, enabled: !autoThemeEnabled,

21
lib/settings.dart Normal file
View File

@ -0,0 +1,21 @@
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
final SharedPreferences _prefs = GetIt.instance.get<SharedPreferences>();
const Map<String, dynamic> _defaults = {
'romajiEnabled': false,
'darkThemeEnabled': false,
'autoThemeEnabled': false,
};
bool _getSettingOrDefault(String settingName) =>
_prefs.getBool(settingName) ?? _defaults[settingName];
bool get romajiEnabled => _getSettingOrDefault('romajiEnabled');
bool get darkThemeEnabled => _getSettingOrDefault('darkThemeEnabled');
bool get autoThemeEnabled => _getSettingOrDefault('autoThemeEnabled');
set romajiEnabled(b) => _prefs.setBool('romajiEnabled', b);
set darkThemeEnabled(b) => _prefs.setBool('darkThemeEnabled', b);
set autoThemeEnabled(b) => _prefs.setBool('autoThemeEnabled', b);