Miscellaneous refactoring

add-more-content-to-search-results
Oystein Kristoffer Tveit 2022-01-23 18:12:42 +01:00
parent 9e50221abd
commit 2251001a27
15 changed files with 209 additions and 212 deletions

View File

@ -33,8 +33,9 @@ class _LanguageSelectorState extends State<LanguageSelector> {
Widget _languageOption(String language) =>
Container(
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Center(child: Text(language)),
child: Text(language),
);
@override

View File

@ -14,7 +14,9 @@ class SearchResultsBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListView(
children: results.map((result) => SearchResultCard(result: result)).toList(),
children: [
for (final result in results) SearchResultCard(result: result)
],
);
}
}

View File

@ -27,6 +27,12 @@ class _AudioPlayerState extends State<AudioPlayer> {
bool _isPlaying(ja.PlayerState? state) => state != null && state.playing;
@override
void initState() {
player.setUrl(widget.audio.uri);
super.initState();
}
@override
Widget build(BuildContext context) {
return BlocBuilder<ThemeBloc, ThemeState>(
@ -70,10 +76,4 @@ class _AudioPlayerState extends State<AudioPlayer> {
},
);
}
@override
void initState() {
player.setUrl(widget.audio.uri);
super.initState();
}
}

View File

@ -4,8 +4,11 @@ class Badge extends StatelessWidget {
final Widget? child;
final Color color;
const Badge({this.child, required this.color, Key? key,}) : super(key: key);
const Badge({
Key? key,
this.child,
required this.color,
}) : super(key: key);
@override
Widget build(BuildContext context) {
@ -18,10 +21,7 @@ class Badge extends StatelessWidget {
shape: BoxShape.circle,
color: color,
),
child: FittedBox(
child: Center(
child: child,
),
),
); }
child: FittedBox(child: child),
);
}
}

View File

@ -55,18 +55,17 @@ class KanjiRow extends StatelessWidget {
Wrap(
spacing: 10,
runSpacing: 10,
children: kanji
.map(
(k) => InkWell(
onTap: () => Navigator.pushNamed(
context,
Routes.kanjiSearch,
arguments: k,
),
child: _kanjiBox(k),
children: [
for (final k in kanji)
InkWell(
onTap: () => Navigator.pushNamed(
context,
Routes.kanjiSearch,
arguments: k,
),
child: _kanjiBox(k),
)
.toList(),
],
),
],
);

View File

@ -68,7 +68,8 @@ final Map<RegExp, Widget Function(String)> _patterns = {
_wiki(link: l, isJapanese: false),
RegExp(r'^Read “.+” on Japanese Wikipedia$'): (l) =>
_wiki(link: l, isJapanese: true),
RegExp(r'^Read “.+” on DBpedia$'): _dbpedia,
// DBpedia comes through attribution.
// RegExp(r'^Read “.+” on DBpedia$'): _dbpedia,
};
class Links extends StatelessWidget {
@ -86,24 +87,34 @@ class Links extends StatelessWidget {
// Copy sense.links so that it doesn't need to be modified.
final List<JishoSenseLink> newLinks = List.from(links);
final List<String> newStringLinks = [for (final l in newLinks) l.url];
final Map<RegExp, int> matches = {};
for (int i = 0; i < newLinks.length; i++)
for (final RegExp p in _patterns.keys)
if (p.hasMatch(newLinks[i].text)) matches[p] = i;
final List<String> newStringLinks = newLinks.map((l) => l.url).toList();
final List<Widget> icons = [
...matches.entries
.map((m) => _patterns[m.key]!(newStringLinks[m.value]))
.toList(),
...[
for (final match in matches.entries)
_patterns[match.key]!(newStringLinks[match.value])
],
if (attribution.dbpedia != null) _dbpedia(attribution.dbpedia!)
];
(matches.values.toList()..sort()).reversed.forEach(newLinks.removeAt);
final List<Widget> otherLinks =
newLinks.map((e) => Text('[${e.text} -> ${e.url}]')).toList();
final List<Widget> otherLinks = [
for (final link in newLinks) ...[
InkWell(
onTap: () => _launch(link.url),
child: Text(
link.text,
style: const TextStyle(color: Colors.blue),
),
)
]
];
return [
const Text('Links:', style: TextStyle(fontWeight: FontWeight.bold)),

View File

@ -5,16 +5,14 @@ class Notes extends StatelessWidget {
const Notes({Key? key, required this.notes}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Notes:',
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(notes.join(', ')),
],
);
}
Widget build(BuildContext context) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Notes:',
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(notes.join(', ')),
],
);
}

View File

@ -10,19 +10,18 @@ class OtherForms extends StatelessWidget {
const OtherForms({required this.forms, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: forms.isNotEmpty
? [
const Text(
'Other Forms:',
style: TextStyle(fontWeight: FontWeight.bold),
),
Wrap(
children: forms
.map(
(form) => BlocBuilder<ThemeBloc, ThemeState>(
Widget build(BuildContext context) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: forms.isNotEmpty
? [
const Text(
'Other Forms:',
style: TextStyle(fontWeight: FontWeight.bold),
),
Wrap(
children: [
for (final form in forms)
BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
return KanjiKanaBox(
word: form,
@ -30,11 +29,9 @@ class OtherForms extends StatelessWidget {
);
},
),
)
.toList(),
),
]
: [],
);
}
],
),
]
: [],
);
}

View File

@ -1,15 +1,20 @@
import 'package:flutter/material.dart';
import 'package:jisho_study_tool/routing/routes.dart';
import '../../../../../models/themes/theme.dart';
import '../../../../../routing/routes.dart';
import 'search_chip.dart';
class Antonyms extends StatelessWidget {
final List<String> antonyms;
final ColorSet colors;
const Antonyms({
Key? key,
required this.antonyms,
this.colors = const ColorSet(
foreground: Colors.white,
background: Colors.blue,
),
}) : super(key: key);
@override
@ -25,20 +30,20 @@ class Antonyms extends StatelessWidget {
Wrap(
spacing: 5,
runSpacing: 5,
children: antonyms
.map(
(a) => InkWell(
onTap: () => Navigator.pushNamed(context, Routes.search, arguments: a),
child: SearchChip(
text: a,
colors: const ColorSet(
foreground: Colors.white,
background: Colors.blue,
),
),
children: [
for (final antonym in antonyms)
InkWell(
onTap: () => Navigator.pushNamed(
context,
Routes.search,
arguments: antonym,
),
)
.toList(),
child: SearchChip(
text: antonym,
colors: colors,
),
),
],
)
],
);

View File

@ -18,8 +18,9 @@ class EnglishDefinitions extends StatelessWidget {
runSpacing: 10.0,
spacing: 5,
crossAxisAlignment: WrapCrossAlignment.center,
children: englishDefinitions
.map((def) => SearchChip(text: def, colors: colors))
.toList(),
children: [
for (final def in englishDefinitions)
SearchChip(text: def, colors: colors)
],
);
}

View File

@ -20,6 +20,9 @@ class Sense extends StatelessWidget {
this.meaning,
}) : super(key: key);
// TODO: This assumes that there is only one antonym. However, the
// antonym system is made with the case of multiple antonyms
// in mind.
List<String> _removeAntonyms(List<String> supplementalInfo) {
for (int i = 0; i < supplementalInfo.length; i++) {
if (RegExp(r'^Antonym: .*$').hasMatch(supplementalInfo[i])) {
@ -30,9 +33,9 @@ class Sense extends StatelessWidget {
return supplementalInfo;
}
List<String>? get _supplementalWithoutAntonyms =>
(meaning == null) ? null :
_removeAntonyms(List.from(meaning!.supplemental));
List<String>? get _supplementalWithoutAntonyms => meaning == null
? null
: _removeAntonyms(List.from(meaning!.supplemental));
bool get hasSupplementalInfo =>
sense.info.isNotEmpty ||
@ -42,50 +45,47 @@ class Sense extends StatelessWidget {
@override
Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 5),
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: state.theme.menuGreyLight.background,
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'${index + 1}. ${sense.partsOfSpeech.join(', ')}',
style: const TextStyle(fontWeight: FontWeight.bold),
textAlign: TextAlign.left,
builder: (context, state) => Container(
margin: const EdgeInsets.symmetric(vertical: 5),
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: state.theme.menuGreyLight.background,
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'${index + 1}. ${sense.partsOfSpeech.join(', ')}',
style: const TextStyle(fontWeight: FontWeight.bold),
textAlign: TextAlign.left,
),
EnglishDefinitions(
englishDefinitions: sense.englishDefinitions,
colors: state.theme.menuGreyNormal,
),
if (hasSupplementalInfo)
SupplementalInfo(
sense: sense,
supplementalInfo: _supplementalWithoutAntonyms,
),
EnglishDefinitions(
englishDefinitions: sense.englishDefinitions,
colors: state.theme.menuGreyNormal,
if (meaning?.definitionAbstract != null)
DefinitionAbstract(
text: meaning!.definitionAbstract!,
color: state.theme.foreground,
),
if (hasSupplementalInfo)
SupplementalInfo(
sense: sense,
supplementalInfo: _supplementalWithoutAntonyms,
if (sense.antonyms.isNotEmpty) Antonyms(antonyms: sense.antonyms),
if (meaning != null && meaning!.sentences.isNotEmpty)
Sentences(sentences: meaning!.sentences)
]
.map(
(e) => Container(
margin: const EdgeInsets.symmetric(vertical: 5),
child: e,
),
if (meaning?.definitionAbstract != null)
DefinitionAbstract(
text: meaning!.definitionAbstract!,
color: state.theme.foreground,
),
if (sense.antonyms.isNotEmpty)
Antonyms(antonyms: sense.antonyms),
if (meaning != null && meaning!.sentences.isNotEmpty)
Sentences(sentences: meaning!.sentences)
]
.map(
(e) => Container(
margin: const EdgeInsets.symmetric(vertical: 5),
child: e,
),
)
.toList(),
),
);
},
)
.toList(),
),
),
);
}

View File

@ -25,29 +25,27 @@ class Sentences extends StatelessWidget {
children: [
Wrap(
runSpacing: 10,
children: [
...sentence.pieces
.map(
(p) => JishoJapaneseWord(
word: p.unlifted,
reading: p.lifted,
),
)
.map(
(word) => KanjiKanaBox(
word: word,
showRomajiBelow: true,
margin: EdgeInsets.zero,
padding: EdgeInsets.zero,
centerFurigana: false,
autoTransliterateRomaji: false,
kanjiFontsize: 15,
furiganaFontsize: 12,
colors: colors,
),
)
.toList(),
],
children: sentence.pieces
.map(
(p) => JishoJapaneseWord(
word: p.unlifted,
reading: p.lifted,
),
)
.map(
(word) => KanjiKanaBox(
word: word,
showRomajiBelow: true,
margin: EdgeInsets.zero,
padding: EdgeInsets.zero,
centerFurigana: false,
autoTransliterateRomaji: false,
kanjiFontsize: 15,
furiganaFontsize: 12,
colors: colors,
),
)
.toList(),
),
Divider(
height: 20,
@ -63,9 +61,6 @@ class Sentences extends StatelessWidget {
);
@override
Widget build(BuildContext context) {
return Column(
children: sentences.map((s) => _sentence(s)).toList(),
);
}
Widget build(BuildContext context) =>
Column(children: [for (final s in sentences) _sentence(s)]);
}

View File

@ -18,7 +18,7 @@ class SupplementalInfo extends StatelessWidget {
if (restrictions.isNotEmpty)
restrictions[0] = 'Only applies to ${restrictions[0]}';
final List<String> combinedInfo = sense.tags + restrictions;
final List<String> combinedInfo = sense.tags + sense.info + restrictions;
return Text(
combinedInfo.join(', '),
@ -32,8 +32,10 @@ class SupplementalInfo extends StatelessWidget {
return [
if (sense.source.isNotEmpty)
Text('From ${sense.source[0].language} ${sense.source[0].word}'),
if (sense.tags.isNotEmpty || sense.restrictions.isNotEmpty) _info(sense),
if (sense.info.isNotEmpty) Text(sense.info.join(', '))
if (sense.tags.isNotEmpty ||
sense.restrictions.isNotEmpty ||
sense.info.isNotEmpty)
_info(sense),
];
}

View File

@ -14,34 +14,20 @@ class Senses extends StatelessWidget {
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final List<Widget> senseWidgets = senses
.asMap()
.map(
(k, v) => MapEntry(
v,
extraData?.firstWhereOrNull(
(m) => m.definition == v.englishDefinitions.join('; '),
List<Widget> get _senseWidgets => [
for (int i = 0; i < senses.length; i++)
Sense(
index: i,
sense: senses[i],
meaning: extraData?.firstWhereOrNull(
(m) => m.definition == senses[i].englishDefinitions.join('; '),
),
),
)
.entries
.toList()
.asMap()
.entries
.map(
(e) => Sense(
index: e.key,
sense: e.value.key,
meaning: e.value.value,
),
)
.toList();
];
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: senseWidgets,
);
}
@override
Widget build(BuildContext context) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: _senseWidgets,
);
}

View File

@ -41,7 +41,7 @@ class _SearchResultCardState extends State<SearchResultCard> {
: Future(() => null);
List<JishoSenseLink> get links =>
widget.result.senses.map((s) => s.links).expand((l) => l).toList();
[for (final sense in widget.result.senses) ...sense.links];
bool get hasAttribution =>
widget.result.attribution.jmdict ||
@ -55,12 +55,15 @@ class _SearchResultCardState extends State<SearchResultCard> {
return jlpt.last;
}
List<String> get kanji =>
RegExp(r'(\p{Script=Hani})', unicode: true)
.allMatches(widget.result.japanese.map((w) => '${w.word ?? ""}${w.reading ?? ""}').join())
.map((match) => match.group(0)!)
.toSet()
.toList();
List<String> get kanji => RegExp(r'(\p{Script=Hani})', unicode: true)
.allMatches(
widget.result.japanese
.map((w) => '${w.word ?? ""}${w.reading ?? ""}')
.join(),
)
.map((match) => match.group(0)!)
.toSet()
.toList();
Widget get _header => IntrinsicWidth(
child: Row(
@ -83,6 +86,10 @@ class _SearchResultCardState extends State<SearchResultCard> {
),
);
static const _margin = SizedBox(height: 20);
List<Widget> _withMargin(Widget w) => [_margin, w];
Widget _body({PhrasePageScrapeResultData? extendedData}) => Container(
padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 10),
child: Column(
@ -98,25 +105,18 @@ class _SearchResultCardState extends State<SearchResultCard> {
senses: widget.result.senses,
extraData: extendedData?.meanings,
),
if (widget.otherForms.isNotEmpty) ...[
const SizedBox(height: 20),
OtherForms(forms: widget.otherForms),
],
if (extendedData != null && extendedData.notes.isNotEmpty) ...[
const SizedBox(height: 20),
Notes(notes: extendedData.notes),
],
if (kanji.isNotEmpty) ...[
const SizedBox(height: 20),
KanjiRow(kanji: kanji),
],
if (links.isNotEmpty || hasAttribution) ...[
const SizedBox(height: 20),
Links(
links: links,
attribution: widget.result.attribution,
),
]
if (widget.otherForms.isNotEmpty)
..._withMargin(OtherForms(forms: widget.otherForms)),
if (extendedData != null && extendedData.notes.isNotEmpty)
..._withMargin(Notes(notes: extendedData.notes)),
if (kanji.isNotEmpty) ..._withMargin(KanjiRow(kanji: kanji)),
if (links.isNotEmpty || hasAttribution)
..._withMargin(
Links(
links: links,
attribution: widget.result.attribution,
),
)
],
),
);