Refactoring and cleaning

This commit is contained in:
Oystein Kristoffer Tveit 2022-01-19 02:10:05 +01:00
parent 859b7753af
commit 17d6f862a2
63 changed files with 783 additions and 719 deletions

View File

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import '../../../models/themes/theme.dart';
import '../../models/themes/theme.dart';
class SplashScreen extends StatelessWidget {
const SplashScreen({Key? key}) : super(key: key);

View File

@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:signature/signature.dart';
import '../../../bloc/theme/theme_bloc.dart';
import '../../../services/handwriting.dart';
import '../../bloc/theme/theme_bloc.dart';
import '../../services/handwriting.dart';
class DrawingBoard extends StatefulWidget {
final Function(String)? onSuggestionChosen;

View File

@ -1,49 +1,44 @@
import 'package:flutter/material.dart';
import '../../../bloc/theme/theme_bloc.dart';
import '../../../models/themes/theme.dart';
import '../../bloc/theme/theme_bloc.dart';
class DateDivider extends StatelessWidget {
final String? text;
final DateTime? date;
final EdgeInsets? margin;
const DateDivider({
this.text,
this.date,
this.margin = const EdgeInsets.symmetric(vertical: 10),
Key? key,
}) : super(key: key);
}) : assert((text == null) ^ (date == null)),
super(key: key);
String getHumanReadableDate(DateTime date) {
const Map<int, String> monthTable = {
1: 'Jan',
2: 'Feb',
3: 'Mar',
4: 'Apr',
5: 'May',
6: 'Jun',
7: 'Jul',
8: 'Aug',
9: 'Sep',
10: 'Oct',
11: 'Nov',
12: 'Dec',
};
const List<String> monthTable = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
final int day = date.day;
final String month = monthTable[date.month]!;
final String month = monthTable[date.month - 1];
final int year = date.year;
return '$day. $month $year';
}
@override
Widget build(BuildContext context) {
final Widget header = (text != null)
? Text(text!)
: (date != null)
? Text(getHumanReadableDate(date!))
: const SizedBox.shrink();
final Widget header =
(text != null) ? Text(text!) : Text(getHumanReadableDate(date!));
final ColorSet _menuColors =
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyNormal;
@ -54,7 +49,6 @@ class DateDivider extends StatelessWidget {
vertical: 5,
horizontal: 10,
),
margin: margin,
child: DefaultTextStyle.merge(
child: header,
style: TextStyle(color: _menuColors.foreground),

View File

@ -0,0 +1,42 @@
import 'package:flutter/material.dart';
import '../../bloc/theme/theme_bloc.dart';
class KanjiBox extends StatelessWidget {
final String kanji;
const KanjiBox({
Key? key,
required this.kanji,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final ColorSet menuColors =
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyLight;
return IntrinsicHeight(
child: AspectRatio(
aspectRatio: 1,
child: Container(
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: menuColors.background,
borderRadius: BorderRadius.circular(10.0),
),
child: Center(
child: FittedBox(
child: Text(
kanji,
style: TextStyle(
color: menuColors.foreground,
fontSize: 25,
),
),
),
),
),
),
);
}
}

View File

@ -0,0 +1,77 @@
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import '../../models/history/search.dart';
class SearchItem extends StatelessWidget {
final DateTime time;
final Widget search;
final int objectKey;
final void Function()? onDelete;
final void Function()? onFavourite;
final void Function()? onTap;
const SearchItem({
required this.time,
required this.search,
required this.objectKey,
this.onDelete,
this.onFavourite,
this.onTap,
Key? key,
}) : super(key: key);
String getTime() {
final hours = time.hour.toString().padLeft(2, '0');
final mins = time.minute.toString().padLeft(2, '0');
return '$hours:$mins';
}
@override
Widget build(BuildContext context) {
return Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
label: 'Favourite',
backgroundColor: Colors.yellow,
icon: Icons.star,
onPressed: (_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('TODO: implement favourites')),
);
onFavourite?.call();
},
),
SlidableAction(
label: 'Delete',
backgroundColor: Colors.red,
icon: Icons.delete,
onPressed: (_) {
final Database db = GetIt.instance.get<Database>();
Search.store.record(objectKey).delete(db);
onDelete?.call();
},
),
],
),
child: Container(
padding: const EdgeInsets.symmetric(vertical: 10),
child: ListTile(
onTap: onTap,
contentPadding: EdgeInsets.zero,
title: Row(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Text(getTime()),
),
search,
],
),
),
),
);
}
}

View File

@ -24,34 +24,61 @@ class KanjiResultBody extends StatelessWidget {
resultData = result.data!;
}
Widget get headerRow => Container(
margin: const EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 30.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Flexible(
fit: FlexFit.tight,
child: Center(child: SizedBox()),
),
Flexible(
fit: FlexFit.tight,
child: Center(child: Header(kanji: query)),
),
Flexible(
fit: FlexFit.tight,
child: Center(
child: (resultData.radical != null)
? Radical(radical: resultData.radical!)
: const SizedBox(),
),
),
],
),
);
Widget get rankingColumn => Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Text('JLPT: ', style: TextStyle(fontSize: 20.0)),
JlptLevel(jlptLevel: resultData.jlptLevel ?? ''),
],
),
Row(
children: [
const Text('Grade: ', style: TextStyle(fontSize: 20.0)),
Grade(grade: resultData.taughtIn),
],
),
Row(
children: [
const Text('Rank: ', style: TextStyle(fontSize: 20.0)),
Rank(rank: resultData.newspaperFrequencyRank),
],
),
],
);
@override
Widget build(BuildContext context) {
return ListView(
children: [
Container(
margin: const EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 30.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Flexible(
fit: FlexFit.tight,
child: Center(child: SizedBox()),
),
Flexible(
fit: FlexFit.tight,
child: Center(child: Header(kanji: query)),
),
Flexible(
fit: FlexFit.tight,
child: Center(
child: (resultData.radical != null)
? Radical(radical: resultData.radical!)
: const SizedBox(),
),
),
],
),
),
headerRow,
YomiChips(yomi: resultData.meaning.split(', '), type: YomiType.meaning),
(resultData.onyomi.isNotEmpty)
? YomiChips(yomi: resultData.onyomi, type: YomiType.onyomi)
@ -64,30 +91,7 @@ class KanjiResultBody extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
StrokeOrderGif(uri: resultData.strokeOrderGifUri),
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Text('JLPT: ', style: TextStyle(fontSize: 20.0)),
JlptLevel(jlptLevel: resultData.jlptLevel ?? ''),
],
),
Row(
children: [
const Text('Grade: ', style: TextStyle(fontSize: 20.0)),
Grade(grade: resultData.taughtIn ?? ''),
],
),
Row(
children: [
const Text('Rank: ', style: TextStyle(fontSize: 20.0)),
Rank(rank: resultData.newspaperFrequencyRank ?? -1),
],
),
],
),
rankingColumn,
],
),
),

View File

@ -0,0 +1,153 @@
import 'package:flutter/material.dart';
import 'package:unofficial_jisho_api/api.dart';
import '../../../bloc/theme/theme_bloc.dart';
class Examples extends StatelessWidget {
final List<YomiExample> onyomi;
final List<YomiExample> kunyomi;
const Examples({
required this.onyomi,
required this.kunyomi,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
const textStyle = TextStyle(fontSize: 20);
final yomiWidgets =
onyomi.map((onEx) => _Example(onEx, _KanaType.onyomi)).toList() +
kunyomi.map((kunEx) => _Example(kunEx, _KanaType.kunyomi)).toList();
const noExamplesWidget = [
Padding(
padding: EdgeInsets.symmetric(vertical: 10),
child: Text('No Examples', style: textStyle),
)
];
return Column(
children: <Widget>[
Container(
margin: const EdgeInsets.symmetric(horizontal: 10),
alignment: Alignment.centerLeft,
child: const Text('Examples:', style: textStyle),
)
] +
(onyomi.isEmpty && kunyomi.isEmpty ? noExamplesWidget : yomiWidgets),
);
}
}
enum _KanaType { kunyomi, onyomi }
class _Example extends StatelessWidget {
final _KanaType kanaType;
final YomiExample yomiExample;
const _Example(this.yomiExample, this.kanaType);
@override
Widget build(BuildContext context) {
final theme = BlocProvider.of<ThemeBloc>(context).state.theme;
final menuColors = theme.menuGreyNormal;
final kanaColors =
kanaType == _KanaType.kunyomi ? theme.kunyomiColor : theme.onyomiColor;
return Container(
margin: const EdgeInsets.symmetric(
vertical: 5.0,
horizontal: 10.0,
),
decoration: BoxDecoration(
color: menuColors.background,
borderRadius: BorderRadius.circular(10.0),
),
child: IntrinsicHeight(
child: Row(
children: [
_Kana(colors: kanaColors, example: yomiExample),
_ExampleText(colors: menuColors, example: yomiExample)
],
),
),
);
}
}
class _Kana extends StatelessWidget {
final ColorSet colors;
final YomiExample example;
const _Kana({
Key? key,
required this.colors,
required this.example,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: colors.background,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(10.0),
bottomLeft: Radius.circular(10.0),
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
example.reading,
style: TextStyle(
color: colors.foreground,
fontSize: 15.0,
),
),
const SizedBox(height: 5.0),
Text(
example.example,
style: TextStyle(
color: colors.foreground,
fontSize: 20.0,
),
),
],
),
);
}
}
class _ExampleText extends StatelessWidget {
final ColorSet colors;
final YomiExample example;
const _ExampleText({
Key? key,
required this.colors,
required this.example,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Expanded(
child: Padding(
padding: const EdgeInsets.all(10),
child: Wrap(
children: [
Text(
example.meaning,
style: TextStyle(
color: colors.foreground,
),
)
],
),
),
);
}
}

View File

@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import '../../../bloc/theme/theme_bloc.dart';
class Grade extends StatelessWidget {
final String? grade;
final String ifNullChar;
const Grade({
required this.grade,
this.ifNullChar = '',
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final colors =
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
return Container(
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: colors.background,
shape: BoxShape.circle,
),
child: Text(
grade ?? ifNullChar,
style: TextStyle(
color: colors.foreground,
fontSize: 20.0,
),
),
);
}
}

View File

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import '../../../../bloc/theme/theme_bloc.dart';
import '../../../bloc/theme/theme_bloc.dart';
class Header extends StatelessWidget {
final String kanji;
@ -12,7 +12,7 @@ class Header extends StatelessWidget {
@override
Widget build(BuildContext context) {
final _kanjiColors =
final colors =
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
return AspectRatio(
@ -20,12 +20,12 @@ class Header extends StatelessWidget {
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: _kanjiColors.background,
color: colors.background,
),
child: Center(
child: Text(
kanji,
style: TextStyle(fontSize: 70.0, color: _kanjiColors.foreground),
style: TextStyle(fontSize: 70.0, color: colors.foreground),
),
),
),

View File

@ -1,30 +1,32 @@
import 'package:flutter/material.dart';
import '../../../../bloc/theme/theme_bloc.dart';
import '../../../bloc/theme/theme_bloc.dart';
class JlptLevel extends StatelessWidget {
final String jlptLevel;
final String? jlptLevel;
final String ifNullChar;
const JlptLevel({
required this.jlptLevel,
this.ifNullChar = '',
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final _kanjiColors =
final colors =
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
return Container(
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _kanjiColors.background,
color: colors.background,
),
child: Text(
jlptLevel,
jlptLevel ?? ifNullChar,
style: TextStyle(
color: _kanjiColors.foreground,
color: colors.foreground,
fontSize: 20.0,
),
),

View File

@ -1,28 +1,27 @@
import 'package:flutter/material.dart';
import 'package:unofficial_jisho_api/api.dart' as jisho;
import '../../../../bloc/theme/theme_bloc.dart';
import '../../../bloc/theme/theme_bloc.dart';
class Radical extends StatelessWidget {
final jisho.Radical radical;
const Radical({required this.radical, Key? key,}) : super(key: key);
@override
Widget build(BuildContext context) {
final _kanjiColors = BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
final colors = BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
return Container(
padding: const EdgeInsets.all(10.0),
padding: const EdgeInsets.all(15.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _kanjiColors.background,
color: colors.background,
),
child: Text(
radical.symbol,
style: TextStyle(
color: _kanjiColors.foreground,
color: colors.foreground,
fontSize: 40.0,
),
),

View File

@ -0,0 +1,36 @@
import 'package:flutter/material.dart';
import '../../../bloc/theme/theme_bloc.dart';
class Rank extends StatelessWidget {
final int? rank;
final String ifNullChar;
const Rank({
required this.rank,
this.ifNullChar = '',
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final colors =
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
return Container(
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
shape: (rank == null) ? BoxShape.circle : BoxShape.rectangle,
borderRadius: (rank == null) ? null : BorderRadius.circular(10.0),
color: colors.background,
),
child: Text(
rank != null ? '${rank.toString()} / 2500' : ifNullChar,
style: TextStyle(
color: colors.foreground,
fontSize: 20.0,
),
),
);
}
}

View File

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import '../../../../bloc/theme/theme_bloc.dart';
import '../../../bloc/theme/theme_bloc.dart';
class StrokeOrderGif extends StatelessWidget {
final String uri;
@ -10,13 +10,13 @@ class StrokeOrderGif extends StatelessWidget {
@override
Widget build(BuildContext context) {
final _kanjiColors = BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
final colors = BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
return Container(
margin: const EdgeInsets.symmetric(vertical: 20.0),
padding: const EdgeInsets.all(5.0),
decoration: BoxDecoration(
color: _kanjiColors.background,
color: colors.background,
borderRadius: BorderRadius.circular(15.0),
),
child: ClipRRect(

View File

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import '../../../../bloc/theme/theme_bloc.dart';
import '../../../bloc/theme/theme_bloc.dart';
enum YomiType {
onyomi,
@ -47,18 +47,17 @@ class YomiChips extends StatelessWidget {
bool get isExpandable => yomi.length > 6;
Widget yomiCard({
required BuildContext context,
required String yomi,
required ColorSet colors,
}) =>
Container(
margin: const EdgeInsets.symmetric(horizontal: 10.0),
margin: const EdgeInsets.symmetric(horizontal: 5),
padding: const EdgeInsets.symmetric(
vertical: 10.0,
horizontal: 10.0,
),
decoration: BoxDecoration(
color: type.getColors(context).background,
color: colors.background,
borderRadius: BorderRadius.circular(10.0),
),
child: Text(
@ -72,41 +71,38 @@ class YomiChips extends StatelessWidget {
Widget yomiWrapper(BuildContext context) {
final yomiCards = yomi
.map(
(y) => yomiCard(
context: context,
yomi: y,
colors: type.getColors(context),
),
)
.map((y) => yomiCard(yomi: y, colors: type.getColors(context)))
.toList();
final yomiCardsWithTitle = <Widget>[
if (type != YomiType.meaning)
yomiCard(
yomi: type == YomiType.kunyomi ? 'Kun:' : 'On:',
colors: ColorSet(
foreground: type.getColors(context).background,
background: const Color(0x000000ff),
),
),
] +
yomiCards;
final wrap = Wrap(
runSpacing: 10.0,
crossAxisAlignment: WrapCrossAlignment.center,
children: yomiCardsWithTitle,
);
if (!isExpandable)
return Wrap(
runSpacing: 10.0,
children: yomiCards,
);
return wrap;
else
return ExpansionTile(
// initiallyExpanded: false,
title: Center(
child: yomiCard(
context: context,
yomi: type.title,
colors: type.getColors(context),
),
child: yomiCard(yomi: type.title, colors: type.getColors(context)),
),
children: [
const SizedBox(
height: 20.0,
),
Wrap(
runSpacing: 10.0,
children: yomiCards,
),
const SizedBox(
height: 25.0,
),
const SizedBox(height: 20.0),
wrap,
const SizedBox(height: 25.0),
],
);
}

View File

@ -1,7 +1,7 @@
import 'package:animated_size_and_fade/animated_size_and_fade.dart';
import 'package:flutter/material.dart';
import '../../../services/kanji_suggestions.dart';
import '../../services/kanji_suggestions.dart';
import 'kanji_search_body/kanji_grid.dart';
import 'kanji_search_body/kanji_search_bar.dart';
import 'kanji_search_body/kanji_search_options_bar.dart';
@ -59,7 +59,7 @@ class _KanjiSearchBodyState extends State<KanjiSearchBody>
}
return true;
},
child: GestureDetector(
child: InkWell(
onTap: () => FocusScope.of(context).unfocus(),
child: Container(
decoration: const BoxDecoration(),
@ -89,6 +89,7 @@ class _KanjiSearchBodyState extends State<KanjiSearchBody>
}),
),
),
const SizedBox(height: 20),
AnimatedSizeAndFade(
fadeDuration: const Duration(milliseconds: 200),
sizeDuration: const Duration(milliseconds: 300),

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import '../../../../bloc/theme/theme_bloc.dart';
import '../../../bloc/theme/theme_bloc.dart';
import '../../../routing/routes.dart';
class KanjiGrid extends StatelessWidget {
final List<String> suggestions;
@ -33,7 +34,7 @@ class _GridItem extends StatelessWidget {
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.pushNamed(context, '/kanjiSearch', arguments: kanji);
Navigator.pushNamed(context, Routes.kanjiSearch, arguments: kanji);
},
child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {

View File

@ -21,20 +21,18 @@ class KanjiSearchBarState extends State<KanjiSearchBar> {
super.initState();
}
void runOnChanged() {
if (widget.onChanged != null) widget.onChanged!(textController.text);
}
void onChanged() => widget.onChanged?.call(textController.text);
void clearText() {
textController.text = '';
runOnChanged();
onChanged();
}
Future<void> pasteText() async {
final ClipboardData? clipboardData = await Clipboard.getData('text/plain');
if (clipboardData != null && clipboardData.text != null) {
textController.text = clipboardData.text!;
runOnChanged();
onChanged();
}
}
@ -52,15 +50,11 @@ class KanjiSearchBarState extends State<KanjiSearchBar> {
return TextField(
controller: textController,
onChanged: (text) {
if (widget.onChanged != null) widget.onChanged!(text);
},
onChanged: (text) => onChanged(),
onSubmitted: (_) => {},
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
hintText: 'Search',
// fillColor: Colors.white,
// filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import '../../../../bloc/theme/theme_bloc.dart';
import '../../../bloc/theme/theme_bloc.dart';
import '../../../routing/routes.dart';
class KanjiSearchOptionsBar extends StatelessWidget {
const KanjiSearchOptionsBar({Key? key}) : super(key: key);
@ -14,17 +15,17 @@ class KanjiSearchOptionsBar extends StatelessWidget {
_IconButton(
icon: const Icon(Icons.pie_chart),
onPressed: () =>
Navigator.pushNamed(context, '/kanjiSearch/radicals'),
Navigator.pushNamed(context, Routes.kanjiSearchRadicals),
),
const SizedBox(width: 10,),
const SizedBox(width: 10),
_IconButton(
icon: const Icon(Icons.school),
onPressed: () => Navigator.pushNamed(context, '/kanjiSearch/grade'),
onPressed: () => Navigator.pushNamed(context, Routes.kanjiSearchGrade),
),
const SizedBox(width: 10,),
const SizedBox(width: 10),
_IconButton(
icon: const Icon(Icons.mode),
onPressed: () => Navigator.pushNamed(context, '/kanjiSearch/draw'),
onPressed: () => Navigator.pushNamed(context, Routes.kanjiSearchDraw),
),
],
),

View File

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../models/themes/theme.dart';
import '../../models/themes/theme.dart';
class LanguageSelector extends StatefulWidget {
const LanguageSelector({Key? key}) : super(key: key);

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import '../../routing/routes.dart';
import 'language_selector.dart';
class SearchBar extends StatelessWidget {
@ -13,7 +14,7 @@ class SearchBar extends StatelessWidget {
children: [
TextField(
onSubmitted: (text) =>
Navigator.pushNamed(context, '/search', arguments: text),
Navigator.pushNamed(context, Routes.search, arguments: text),
controller: TextEditingController(),
decoration: InputDecoration(
labelText: 'Search',

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:unofficial_jisho_api/api.dart';
import '../../../../../bloc/theme/theme_bloc.dart';
import '../../../../bloc/theme/theme_bloc.dart';
class OtherForms extends StatelessWidget {
final List<JishoJapaneseWord> forms;

View File

@ -9,7 +9,7 @@ import 'package:sembast/sembast_io.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'bloc/theme/theme_bloc.dart';
import 'router.dart';
import 'routing/router.dart';
Future<void> setupDatabase() async {
final Directory appDocDir = await getApplicationDocumentsDirectory();

View File

@ -3,6 +3,9 @@ import 'package:sembast/sembast.dart';
import './kanji_query.dart';
import './word_query.dart';
export 'package:get_it/get_it.dart';
export 'package:sembast/sembast.dart';
class Search {
final DateTime timestamp;
final WordQuery? wordQuery;

View File

@ -1,4 +1,3 @@
import './word_result.dart';
class WordQuery {
final String query;

View File

@ -1,43 +0,0 @@
import 'package:flutter/material.dart';
import 'view/home.dart';
import 'view/screens/search/kanji_result_page.dart';
import 'view/screens/search/search_mechanisms/drawing.dart';
import 'view/screens/search/search_mechanisms/grade_list.dart';
import 'view/screens/search/search_mechanisms/radical_list.dart';
import 'view/screens/search/search_results_page.dart';
Route<Widget> generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => const Home());
case '/search':
final searchTerm = args! as String;
return MaterialPageRoute(
builder: (_) => SearchResultsPage(searchTerm: searchTerm),
);
case '/kanjiSearch':
final searchTerm = args! as String;
return MaterialPageRoute(
builder: (_) => KanjiResultPage(kanjiSearchTerm: searchTerm),
);
case '/kanjiSearch/draw':
return MaterialPageRoute(builder: (_) => const KanjiDrawingSearch());
case '/kanjiSearch/grade':
return MaterialPageRoute(builder: (_) => const KanjiGradeSearch());
case '/kanjiSearch/radicals':
return MaterialPageRoute(builder: (_) => const KanjiRadicalSearch());
default:
return MaterialPageRoute(
builder: (_) => const Text('ERROR: this route does not exist'),
);
}
}

52
lib/routing/router.dart Normal file
View File

@ -0,0 +1,52 @@
import 'package:flutter/material.dart';
import '../screens/home.dart';
import '../screens/search/kanji_result_page.dart';
import '../screens/search/search_mechanisms/drawing.dart';
import '../screens/search/search_mechanisms/grade_list.dart';
import '../screens/search/search_mechanisms/radical_list.dart';
import '../screens/search/search_results_page.dart';
import 'routes.dart';
Route<Widget> generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case Routes.root:
return MaterialPageRoute(builder: (_) => const Home());
case Routes.search:
final searchTerm = args! as String;
return MaterialPageRoute(
builder: (_) => SearchResultsPage(searchTerm: searchTerm),
);
case Routes.kanjiSearch:
final searchTerm = args! as String;
return MaterialPageRoute(
builder: (_) => KanjiResultPage(kanjiSearchTerm: searchTerm),
);
case Routes.kanjiSearchDraw:
return MaterialPageRoute(builder: (_) => const KanjiDrawingSearch());
case Routes.kanjiSearchGrade:
return MaterialPageRoute(builder: (_) => const KanjiGradeSearch());
case Routes.kanjiSearchRadicals:
return MaterialPageRoute(builder: (_) => const KanjiRadicalSearch());
// TODO: Add more specific error screens.
case Routes.errorNotFound:
case Routes.errorNetwork:
case Routes.errorOther:
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
appBar:
AppBar(title: const Text('Error'), backgroundColor: Colors.red),
body: Center(child: ErrorWidget('Some kind of error occured')),
),
);
}
}

11
lib/routing/routes.dart Normal file
View File

@ -0,0 +1,11 @@
class Routes {
static const String root = '/';
static const String search = '/search';
static const String kanjiSearch = '/kanjiSearch';
static const String kanjiSearchDraw = '/kanjiSearch/draw';
static const String kanjiSearchGrade = '/kanjiSearch/grade';
static const String kanjiSearchRadicals = '/kanjiSearch/radicals';
static const String errorNotFound = '/error/404';
static const String errorNetwork = '/error/network';
static const String errorOther = '/error/other';
}

27
lib/screens/debug.dart Normal file
View File

@ -0,0 +1,27 @@
import 'package:flutter/material.dart';
import '../components/drawing_board/drawing_board.dart';
class DebugView extends StatelessWidget {
const DebugView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
DrawingBoard(
allowHiragana: true,
allowKatakana: true,
allowOther: true,
onSuggestionChosen: (s) => ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Chose: $s'),
duration: const Duration(milliseconds: 600),
),
),
),
],
);
}
}

123
lib/screens/history.dart Normal file
View File

@ -0,0 +1,123 @@
import 'package:flutter/material.dart';
import '../components/common/loading.dart';
import '../components/common/opaque_box.dart';
import '../components/history/date_divider.dart';
import '../components/history/kanji_box.dart';
import '../components/history/search_item.dart';
import '../models/history/search.dart';
import '../routing/routes.dart';
class HistoryView extends StatelessWidget {
const HistoryView({Key? key}) : super(key: key);
Database get _db => GetIt.instance.get<Database>();
Stream<Map<int, Search>> get searchStream => Search.store
.query(finder: Finder(sortOrders: [SortOrder('timestamp', false)]))
.onSnapshots(_db)
.map(
(snapshot) => Map.fromEntries(
snapshot.where((snap) => snap.value != null).map(
(snap) => MapEntry(
snap.key,
Search.fromJson(snap.value! as Map<String, Object?>),
),
),
),
);
@override
Widget build(BuildContext context) {
return StreamBuilder<Map<int, Search>>(
stream: searchStream,
builder: (context, snapshot) {
if (!snapshot.hasData) return const LoadingScreen();
final Map<int, Search> data = snapshot.data!;
if (data.isEmpty)
return const Center(
child: Text('The history is empty.\nTry searching for something!'),
);
return OpaqueBox(
child: ListView.separated(
itemCount: data.length + 1,
itemBuilder: historyEntryWithData(data),
separatorBuilder:
historyEntrySeparatorWithData(data.values.toList()),
),
);
},
);
}
Widget Function(BuildContext, int) historyEntryWithData(
Map<int, Search> data,
) =>
(context, index) {
if (index == 0) return const SizedBox.shrink();
final Search search = data.values.toList()[index - 1];
final int objectKey = data.keys.toList()[index - 1];
late final Widget child;
late final void Function() onTap;
if (search.isKanji) {
child = KanjiBox(kanji: search.kanjiQuery!.kanji);
onTap = () => Navigator.pushNamed(
context,
Routes.kanjiSearch,
arguments: search.kanjiQuery!.kanji,
);
} else {
child = Text(search.wordQuery!.query);
onTap = () => Navigator.pushNamed(
context,
Routes.search,
arguments: search.wordQuery!.query,
);
}
return SearchItem(
time: search.timestamp,
search: child,
objectKey: objectKey,
onTap: onTap,
onDelete: () => build(context),
);
};
DateTime roundToDay(DateTime date) =>
DateTime(date.year, date.month, date.day);
bool dateChangedFromLastSearch(Search prevSearch, DateTime searchDate) {
final DateTime prevSearchDate = roundToDay(prevSearch.timestamp);
return prevSearchDate != searchDate;
}
DateTime get today => roundToDay(DateTime.now());
DateTime get yesterday =>
roundToDay(DateTime.now().subtract(const Duration(days: 1)));
Widget Function(BuildContext, int) historyEntrySeparatorWithData(
List<Search> data,
) =>
(context, index) {
final Search search = data[index];
final DateTime searchDate = roundToDay(search.timestamp);
if (index == 0 ||
dateChangedFromLastSearch(data[index - 1], searchDate)) {
if (searchDate == today)
return const DateDivider(text: 'Today');
else if (searchDate == yesterday)
return const DateDivider(text: 'Yesterday');
else
return DateDivider(date: searchDate);
}
return const Divider(height: 0);
};
}

View File

@ -1,11 +1,13 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mdi/mdi.dart';
import '../bloc/theme/theme_bloc.dart';
import 'screens/history.dart';
import 'screens/search/kanji_view.dart';
import 'screens/search/search_view.dart';
import 'screens/settings.dart';
import 'debug.dart';
import 'history.dart';
import 'search/kanji_view.dart';
import 'search/search_view.dart';
import 'settings.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@ -68,7 +70,7 @@ class _HomeState extends State<Home> {
),
const _Page(
content: KanjiView(),
titleBar: Text('Kanji'),
titleBar: Text('Kanji Search'),
item: BottomNavigationBarItem(
label: 'Kanji',
icon: Icon(Mdi.ideogramCjk, size: 30),
@ -98,6 +100,16 @@ class _HomeState extends State<Home> {
icon: Icon(Icons.settings),
),
),
if (kDebugMode) ...[
const _Page(
content: DebugView(),
titleBar: Text('Debug Page'),
item: BottomNavigationBarItem(
label: 'Debug',
icon: Icon(Icons.biotech),
),
)
],
];
}

View File

@ -1,25 +1,32 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:sembast/sembast.dart';
import '../../../models/history/kanji_query.dart';
import '../../../models/history/search.dart';
import '../../../services/jisho_api/kanji_search.dart';
import '../../components/common/loading.dart';
import '../../components/kanji/kanji_result_body.dart';
import '../../models/history/kanji_query.dart';
import '../../models/history/search.dart';
import '../../services/jisho_api/kanji_search.dart';
class KanjiResultPage extends StatelessWidget {
class KanjiResultPage extends StatefulWidget {
final String kanjiSearchTerm;
bool addedToDatabase = false;
KanjiResultPage({required this.kanjiSearchTerm, Key? key}) : super(key: key);
const KanjiResultPage({
Key? key,
required this.kanjiSearchTerm,
}) : super(key: key);
@override
_KanjiResultPageState createState() => _KanjiResultPageState();
}
class _KanjiResultPageState extends State<KanjiResultPage> {
bool addedToDatabase = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder<KanjiResult>(
future: fetchKanji(kanjiSearchTerm),
future: fetchKanji(widget.kanjiSearchTerm),
builder: (context, snapshot) {
if (!snapshot.hasData) return const LoadingScreen();
if (snapshot.hasError) return ErrorWidget(snapshot.error!);
@ -29,7 +36,7 @@ class KanjiResultPage extends StatelessWidget {
GetIt.instance.get<Database>(),
Search.fromKanjiQuery(
timestamp: DateTime.now(),
kanjiQuery: KanjiQuery(kanji: kanjiSearchTerm),
kanjiQuery: KanjiQuery(kanji: widget.kanjiSearchTerm),
).toJson(),
);
addedToDatabase = true;

View File

@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import '../../../components/drawing_board/drawing_board.dart';
import '../../../routing/routes.dart';
class KanjiDrawingSearch extends StatelessWidget {
const KanjiDrawingSearch({Key? key}) : super(key: key);
@ -15,7 +17,7 @@ class KanjiDrawingSearch extends StatelessWidget {
onlyOneCharacterSuggestions: true,
onSuggestionChosen: (suggestion) => Navigator.popAndPushNamed(
context,
'/kanjiSearch',
Routes.kanjiSearch,
arguments: suggestion,
),
),

View File

@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
import '../../../../data/grades.dart';
import '../../../../models/themes/theme.dart';
import '../../../../routing/routes.dart';
import '../../../components/common/loading.dart';
class KanjiGradeSearch extends StatefulWidget {
const KanjiGradeSearch({Key? key}) : super(key: key);
@ -28,7 +30,7 @@ class _GridItem extends StatelessWidget {
SnackBar(content: Text(text)),
)
: () =>
Navigator.popAndPushNamed(context, '/kanjiSearch', arguments: text);
Navigator.popAndPushNamed(context, Routes.kanjiSearch, arguments: text);
return InkWell(
onTap: onTap,
@ -102,7 +104,7 @@ class _KanjiGradeSearchState extends State<KanjiGradeSearch> {
appBar: AppBar(title: const Text('Choose by grade')),
body: FutureBuilder<Widget>(
future: makeGrids,
initialData: const Center(child: CircularProgressIndicator()),
initialData: const LoadingScreen(),
builder: (context, snapshot) => snapshot.data!,
),
);

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import '../../../../bloc/theme/theme_bloc.dart';
import '../../../../data/radicals.dart';
import '../../../../routing/routes.dart';
import '../../../../services/jisho_api/radicals_search.dart';
class KanjiRadicalSearch extends StatefulWidget {
@ -124,7 +125,7 @@ class _KanjiRadicalSearchState extends State<KanjiRadicalSearch> {
return InkWell(
onTap: () => Navigator.popAndPushNamed(
context,
'/kanjiSearch',
Routes.kanjiSearch,
arguments: kanji,
),
child: Container(

View File

@ -1,28 +1,33 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:sembast/sembast.dart';
import '../../../models/history/search.dart';
import '../../../models/history/word_query.dart';
import '../../../services/jisho_api/jisho_search.dart';
import '../../components/common/loading.dart';
import '../../components/search/search_result_body.dart';
import '../../models/history/search.dart';
import '../../models/history/word_query.dart';
import '../../services/jisho_api/jisho_search.dart';
class SearchResultsPage extends StatelessWidget {
// TODO: merge with KanjiResultPage
class SearchResultsPage extends StatefulWidget {
final String searchTerm;
final Future<JishoAPIResult> results;
bool addedToDatabase = false;
SearchResultsPage({required this.searchTerm, Key? key})
: results = fetchJishoResults(searchTerm),
super(key: key);
const SearchResultsPage({
Key? key,
required this.searchTerm,
}) : super(key: key);
@override
_SearchResultsPageState createState() => _SearchResultsPageState();
}
class _SearchResultsPageState extends State<SearchResultsPage> {
bool addedToDatabase = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder<JishoAPIResult>(
future: results,
future: fetchJishoResults(widget.searchTerm),
builder: (context, snapshot) {
if (!snapshot.hasData) return const LoadingScreen();
if (snapshot.hasError || snapshot.data!.data == null)
@ -33,7 +38,7 @@ class SearchResultsPage extends StatelessWidget {
GetIt.instance.get<Database>(),
Search.fromWordQuery(
timestamp: DateTime.now(),
wordQuery: WordQuery(query: searchTerm),
wordQuery: WordQuery(query: widget.searchTerm),
).toJson(),
);
addedToDatabase = true;

View File

@ -1,12 +1,10 @@
import 'package:confirm_dialog/confirm_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_settings_ui/flutter_settings_ui.dart';
import 'package:get_it/get_it.dart';
import 'package:sembast/sembast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../bloc/theme/theme_bloc.dart';
import '../../models/history/search.dart';
import '../bloc/theme/theme_bloc.dart';
import '../models/history/search.dart';
class SettingsView extends StatefulWidget {
const SettingsView({Key? key}) : super(key: key);
@ -18,18 +16,22 @@ class SettingsView extends StatefulWidget {
class _SettingsViewState extends State<SettingsView> {
final SharedPreferences prefs = GetIt.instance.get<SharedPreferences>();
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);
}
@ -55,6 +57,19 @@ class _SettingsViewState extends State<SettingsView> {
backgroundColor: Colors.transparent,
contentPadding: const EdgeInsets.symmetric(vertical: 10),
sections: <SettingsSection>[
SettingsSection(
title: 'Dictionary',
titleTextStyle: _titleTextStyle,
tiles: <SettingsTile>[
SettingsTile.switchTile(
title: 'Use romaji',
onToggle: (b) {}, //TODO: implement
switchValue: romajiEnabled,
enabled: false,
switchActiveColor: AppTheme.jishoGreen.background,
),
],
),
SettingsSection(
title: 'Theme',
titleTextStyle: _titleTextStyle,

View File

@ -1,4 +1,3 @@
import 'package:flutter/material.dart';
import 'package:unofficial_jisho_api/api.dart' as jisho;
export 'package:unofficial_jisho_api/api.dart' show KanjiResult;
@ -13,8 +12,6 @@ String? _convertGrade(String grade) {
'junior high': ''
};
debugPrint('conversion run: $grade -> ${conversionTable[grade]}');
return conversionTable[grade];
}

View File

@ -1,84 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import './search_item.dart';
import '../../../bloc/theme/theme_bloc.dart';
import '../../../models/history/kanji_query.dart';
import '../../../models/themes/theme.dart';
class _KanjiBox extends StatelessWidget {
final String kanji;
const _KanjiBox(this.kanji);
@override
Widget build(BuildContext context) {
final ColorSet _menuColors =
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyLight;
return IntrinsicHeight(
child: AspectRatio(
aspectRatio: 1,
child: Container(
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: _menuColors.background,
borderRadius: BorderRadius.circular(10.0),
),
child: Center(
child: FittedBox(
child: Text(
kanji,
style: TextStyle(
color: _menuColors.foreground,
fontSize: 25,
),
),
),
),
),
),
);
}
}
class KanjiSearchItem extends StatelessWidget {
final KanjiQuery result;
final DateTime timestamp;
const KanjiSearchItem({
required this.result,
required this.timestamp,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
label: 'Favourite',
backgroundColor: Colors.yellow,
icon: Icons.star,
onPressed: (_) {},
),
SlidableAction(
label: 'Delete',
backgroundColor: Colors.red,
icon: Icons.delete,
onPressed: (_) {},
),
],
),
child: SearchItem(
onTap: () {
Navigator.pushNamed(context, '/kanjiSearch', arguments: result.kanji);
},
time: timestamp,
search: _KanjiBox(result.kanji),
),
);
}
}

View File

@ -1,44 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import './search_item.dart';
import '../../../models/history/word_query.dart';
class PhraseSearchItem extends StatelessWidget {
final WordQuery search;
final DateTime timestamp;
const PhraseSearchItem({
required this.search,
required this.timestamp,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
label: 'Delete',
backgroundColor: Colors.red,
icon: Icons.delete,
onPressed: (_) {},
),
],
),
child: SearchItem(
onTap: () => Navigator.pushNamed(
context,
'/search',
arguments: search.query,
),
time: timestamp,
search: Text(search.query),
),
);
}
}

View File

@ -1,37 +0,0 @@
import 'package:flutter/material.dart';
class SearchItem extends StatelessWidget {
final DateTime time;
final Widget search;
final void Function()? onTap;
const SearchItem({
required this.time,
required this.search,
this.onTap,
Key? key,
}) : super(key: key);
String getTime() {
final hours = time.hour.toString().padLeft(2, '0');
final mins = time.minute.toString().padLeft(2, '0');
return '$hours:$mins';
}
@override
Widget build(BuildContext context) {
return ListTile(
onTap: onTap,
contentPadding: EdgeInsets.zero,
title: Row(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Text(getTime()),
),
search,
],
),
);
}
}

View File

@ -1,123 +0,0 @@
import 'package:flutter/material.dart';
import 'package:unofficial_jisho_api/api.dart';
import '../../../../bloc/theme/theme_bloc.dart';
class Examples extends StatelessWidget {
final List<YomiExample> onyomi;
final List<YomiExample> kunyomi;
const Examples({
required this.onyomi,
required this.kunyomi,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
[
Container(
margin: const EdgeInsets.symmetric(horizontal: 10),
alignment: Alignment.centerLeft,
child: const Text(
'Examples:',
style: TextStyle(fontSize: 20),
),
)
],
onyomi
.map((onyomiExample) => _Example(onyomiExample, _KanaType.onyomi))
.toList(),
kunyomi
.map(
(kunyomiExample) => _Example(kunyomiExample, _KanaType.kunyomi),
)
.toList(),
].expand((list) => list).toList(),
);
}
}
enum _KanaType { kunyomi, onyomi }
class _Example extends StatelessWidget {
final _KanaType kanaType;
final YomiExample yomiExample;
const _Example(this.yomiExample, this.kanaType);
@override
Widget build(BuildContext context) {
final _themeData = BlocProvider.of<ThemeBloc>(context).state.theme;
final _kanaColors = kanaType == _KanaType.kunyomi
? _themeData.kunyomiColor
: _themeData.onyomiColor;
final _menuColors = _themeData.menuGreyNormal;
return Container(
margin: const EdgeInsets.symmetric(
vertical: 5.0,
horizontal: 10.0,
),
decoration: BoxDecoration(
color: _menuColors.background,
borderRadius: BorderRadius.circular(10.0),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.symmetric(
vertical: 10.0,
horizontal: 10.0,
),
decoration: BoxDecoration(
color: _kanaColors.background,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(10.0),
bottomLeft: Radius.circular(10.0),
),
),
child: Column(
children: [
Text(
yomiExample.reading,
style: TextStyle(
color: _kanaColors.foreground,
fontSize: 15.0,
),
),
const SizedBox(
height: 5.0,
),
Text(
yomiExample.example,
style: TextStyle(
color: _kanaColors.foreground,
fontSize: 20.0,
),
),
],
),
),
const SizedBox(
width: 15.0,
),
Expanded(
child: Wrap(
children: [
Text(
yomiExample.meaning,
style: TextStyle(
color: _menuColors.foreground,
),
)
],
),
),
],
),
);
}
}

View File

@ -1,29 +0,0 @@
import 'package:flutter/material.dart';
import '../../../../bloc/theme/theme_bloc.dart';
class Grade extends StatelessWidget {
final String grade;
const Grade({required this.grade, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final _kanjiColors = BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
return Container(
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: _kanjiColors.background,
shape: BoxShape.circle,
),
child: Text(
grade,
style: TextStyle(
color: _kanjiColors.foreground,
fontSize: 20.0,
),
),
);
}
}

View File

@ -1,30 +0,0 @@
import 'package:flutter/material.dart';
import '../../../../bloc/theme/theme_bloc.dart';
class Rank extends StatelessWidget {
final int rank;
const Rank({required this.rank, Key? key,}) : super(key: key);
@override
Widget build(BuildContext context) {
final _kanjiColors = BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
return Container(
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: _kanjiColors.background,
),
child: Text(
'${rank.toString()} / 2500',
style: TextStyle(
color: _kanjiColors.foreground,
fontSize: 20.0,
),
),
);
}
}

View File

@ -1,113 +0,0 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:jisho_study_tool/view/components/common/loading.dart';
import 'package:sembast/sembast.dart';
import '../../models/history/search.dart';
import '../components/history/date_divider.dart';
import '../components/history/kanji_search_item.dart';
import '../components/history/phrase_search_item.dart';
import '../components/opaque_box.dart';
class HistoryView extends StatelessWidget {
const HistoryView({Key? key}) : super(key: key);
Database get _db => GetIt.instance.get<Database>();
Stream<List<Search>> get searchStream => Search.store
.query(
finder: Finder(
sortOrders: [SortOrder('timestamp', false)],
),
)
.onSnapshots(_db)
.map((snapshot) {
return snapshot
.map<Search?>(
(snap) => (snap.value != null)
? Search.fromJson(snap.value! as Map<String, Object?>)
: null,
)
.where((s) => s != null)
.map<Search>((s) => s!)
.toList();
});
@override
Widget build(BuildContext context) {
return StreamBuilder<List<Search>>(
stream: searchStream,
builder: (context, snapshot) {
if (!snapshot.hasData) return const LoadingScreen();
final List<Search> data = snapshot.data!;
if (data.isEmpty)
return const Center(
child: Text('The history is empty.\nTry searching for something!'),
);
return OpaqueBox(
child: ListView.separated(
itemCount: data.length + 1,
itemBuilder: historyEntryWithData(data),
separatorBuilder: historyEntrySeparatorWithData(data),
),
);
},
);
}
Widget Function(BuildContext, int) historyEntryWithData(List<Search> data) =>
(context, index) {
if (index == 0) return Container();
final Search search = data[index - 1];
return (search.isKanji)
? KanjiSearchItem(
result: search.kanjiQuery!,
timestamp: search.timestamp,
)
: PhraseSearchItem(
search: search.wordQuery!,
timestamp: search.timestamp,
);
};
DateTime roundToDay(DateTime date) =>
DateTime(date.year, date.month, date.day);
bool dateChangedFromLastSearch(Search prevSearch, DateTime searchDate) {
final DateTime prevSearchDate = roundToDay(prevSearch.timestamp);
return prevSearchDate != searchDate;
}
DateTime get today => roundToDay(DateTime.now());
DateTime get yesterday =>
roundToDay(DateTime.now().subtract(const Duration(days: 1)));
Widget Function(BuildContext, int) historyEntrySeparatorWithData(
List<Search> data,
) =>
(context, index) {
final Search search = data[index];
final DateTime searchDate = roundToDay(search.timestamp);
EdgeInsets? margin;
if (index != 0) {
margin = const EdgeInsets.only(bottom: 10);
}
if (index == 0 ||
dateChangedFromLastSearch(data[index - 1], searchDate)) {
if (searchDate == today)
return DateDivider(text: 'Today', margin: margin);
else if (searchDate == yesterday)
return DateDivider(text: 'Yesterday', margin: margin);
else
return DateDivider(date: searchDate, margin: margin);
}
return const Divider();
};
}

View File

@ -1,25 +0,0 @@
import 'package:flutter/material.dart';
import '../../../components/kanji/kanji_search_body/kanji_grid.dart';
import '../../../components/kanji/kanji_search_body/kanji_search_bar.dart';
class SearchGrid extends StatelessWidget {
final List<String> suggestions;
const SearchGrid({
required this.suggestions,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
const SizedBox(height: 10),
const KanjiSearchBar(),
const SizedBox(height: 10),
Expanded(child: KanjiGrid(suggestions: suggestions))
],
);
}
}