yokutango-mobile-reader/lib/models/kanji_entry.dart

54 lines
1.3 KiB
Dart

part of 'data_entry.dart';
class KanjiEntry extends DataEntry {
final String kanji;
final List<WordConstruct> kana;
KanjiEntry.fromJson(Map<String, dynamic> json)
: kanji = json['kanji'] as String,
kana = [
for (final j in json['kana'] as List) WordConstruct.fromJson(j)
];
@override
String toString() {
return '$kanji - ${kana.join('')}';
}
}
class WordConstruct {
final List<WordPiece> pieces;
WordConstruct({required this.pieces});
WordConstruct.fromJson(dynamic json)
: pieces = (json is String)
? [WordPiece(word: json, isActive: true)]
: [for (final j in json as List) WordPiece.fromJson(j)];
@override
String toString() {
return pieces.map((p) => p.isActive ? p.word : '(${p.word})').join('');
}
}
class WordPiece {
final String? kana;
final String? romaji;
final String word;
final bool isActive;
const WordPiece({
required this.word,
this.kana,
this.romaji,
this.isActive = false,
});
WordPiece.fromJson(Map<String, dynamic> json)
: kana = json['kana'] as String?,
romaji = json['romaji'] as String?,
word = json['text'] as String,
isActive = json['active'] as bool? ?? false;
}