2020-07-09 22:17:10 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:unofficial_jisho_api/api.dart';
|
|
|
|
|
2021-09-07 00:05:33 +02:00
|
|
|
import './parts/common_badge.dart';
|
2021-03-02 23:09:56 +01:00
|
|
|
import './parts/header.dart';
|
2021-09-07 00:05:33 +02:00
|
|
|
import './parts/jlpt_badge.dart';
|
2021-03-02 23:09:56 +01:00
|
|
|
import './parts/other_forms.dart';
|
2021-09-07 00:05:33 +02:00
|
|
|
import './parts/senses.dart';
|
|
|
|
import './parts/wanikani_badge.dart';
|
2022-01-23 03:15:33 +01:00
|
|
|
import '../../../settings.dart';
|
|
|
|
import 'parts/audio_player.dart';
|
|
|
|
import 'parts/links.dart';
|
|
|
|
import 'parts/notes.dart';
|
2020-08-23 00:38:42 +02:00
|
|
|
|
2022-01-23 03:15:33 +01:00
|
|
|
class SearchResultCard extends StatefulWidget {
|
2021-03-03 00:24:25 +01:00
|
|
|
final JishoResult result;
|
2021-07-26 21:39:17 +02:00
|
|
|
late final JishoJapaneseWord mainWord;
|
|
|
|
late final List<JishoJapaneseWord> otherForms;
|
2020-08-23 00:06:09 +02:00
|
|
|
|
2021-12-01 23:09:53 +01:00
|
|
|
SearchResultCard({
|
|
|
|
required this.result,
|
|
|
|
Key? key,
|
|
|
|
}) : mainWord = result.japanese[0],
|
|
|
|
otherForms = result.japanese.sublist(1),
|
|
|
|
super(key: key);
|
2020-08-23 00:06:09 +02:00
|
|
|
|
|
|
|
@override
|
2022-01-23 03:15:33 +01:00
|
|
|
_SearchResultCardState createState() => _SearchResultCardState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SearchResultCardState extends State<SearchResultCard> {
|
|
|
|
PhrasePageScrapeResultData? extraData;
|
|
|
|
|
|
|
|
Future<PhrasePageScrapeResult?> _scrape(JishoResult result) =>
|
|
|
|
(!(result.japanese[0].word == null && result.japanese[0].reading == null))
|
|
|
|
? scrapeForPhrase(
|
|
|
|
widget.result.japanese[0].word ??
|
|
|
|
widget.result.japanese[0].reading!,
|
|
|
|
)
|
|
|
|
: Future(() => null);
|
|
|
|
|
|
|
|
List<JishoSenseLink> get links =>
|
|
|
|
widget.result.senses.map((s) => s.links).expand((l) => l).toList();
|
|
|
|
|
|
|
|
bool get hasAttribution =>
|
|
|
|
widget.result.attribution.jmdict ||
|
|
|
|
widget.result.attribution.jmnedict ||
|
|
|
|
(widget.result.attribution.dbpedia != null);
|
|
|
|
|
2022-01-23 03:45:53 +01:00
|
|
|
String? get jlptLevel {
|
|
|
|
if (widget.result.jlpt.isEmpty) return null;
|
|
|
|
final jlpt = List.from(widget.result.jlpt);
|
|
|
|
jlpt.sort();
|
|
|
|
return jlpt.last;
|
|
|
|
}
|
|
|
|
|
2022-01-23 03:15:33 +01:00
|
|
|
Widget get _header => IntrinsicWidth(
|
2021-04-11 01:48:43 +02:00
|
|
|
child: Row(
|
2021-12-01 23:09:53 +01:00
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
2021-04-11 01:48:43 +02:00
|
|
|
children: [
|
2022-01-23 03:15:33 +01:00
|
|
|
JapaneseHeader(word: widget.mainWord),
|
2021-04-11 01:48:43 +02:00
|
|
|
Row(
|
|
|
|
children: [
|
2021-12-01 23:09:53 +01:00
|
|
|
WKBadge(
|
2022-01-23 03:15:33 +01:00
|
|
|
level: widget.result.tags.firstWhere(
|
2021-12-01 23:09:53 +01:00
|
|
|
(tag) => tag.contains('wanikani'),
|
|
|
|
orElse: () => '',
|
|
|
|
),
|
|
|
|
),
|
2022-01-23 03:45:53 +01:00
|
|
|
JLPTBadge(jlptLevel: jlptLevel),
|
2022-01-23 03:15:33 +01:00
|
|
|
CommonBadge(isCommon: widget.result.isCommon ?? false)
|
2021-04-11 01:48:43 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2022-01-23 03:15:33 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
Widget _body({PhrasePageScrapeResultData? extendedData}) => Container(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 10),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
if (extendedData != null && extendedData.audio.isNotEmpty) ...[
|
|
|
|
// TODO: There's usually multiple mimetypes in the data.
|
|
|
|
// If one mimetype fails, the app should try to use another one.
|
|
|
|
AudioPlayer(audio: extendedData.audio.first),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
],
|
|
|
|
Senses(
|
|
|
|
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),
|
2021-04-11 01:48:43 +02:00
|
|
|
],
|
2022-01-23 03:15:33 +01:00
|
|
|
if (links.isNotEmpty || hasAttribution) ...[
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
Links(
|
|
|
|
links: links,
|
|
|
|
attribution: widget.result.attribution,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final backgroundColor = Theme.of(context).scaffoldBackgroundColor;
|
|
|
|
|
|
|
|
return ExpansionTile(
|
|
|
|
collapsedBackgroundColor: backgroundColor,
|
|
|
|
backgroundColor: backgroundColor,
|
|
|
|
onExpansionChanged: (b) async {
|
|
|
|
if (extensiveSearchEnabled && extraData == null) {
|
|
|
|
final data = await _scrape(widget.result);
|
|
|
|
setState(() {
|
|
|
|
extraData = (data != null && data.found) ? data.data : null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
title: _header,
|
|
|
|
children: [
|
|
|
|
if (extensiveSearchEnabled && extraData == null)
|
|
|
|
const Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 10),
|
|
|
|
child: Center(child: CircularProgressIndicator()),
|
|
|
|
)
|
|
|
|
else if (extraData != null)
|
|
|
|
_body(extendedData: extraData)
|
|
|
|
else
|
|
|
|
_body()
|
2020-08-24 23:45:47 +02:00
|
|
|
],
|
2020-08-23 00:06:09 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|