Move all navigation from bloc to Navigator
This commit is contained in:
parent
feee76c2b6
commit
9d8c7b8ebb
|
@ -1,20 +0,0 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
export 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
part 'history_event.dart';
|
||||
part 'history_state.dart';
|
||||
|
||||
class HistoryBloc extends Bloc<HistoryEvent, HistoryState> {
|
||||
HistoryBloc() : super(HistoryInitial());
|
||||
|
||||
@override
|
||||
Stream<HistoryState> mapEventToState(
|
||||
HistoryEvent event,
|
||||
) async* {
|
||||
// TODO: implement mapEventToState
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
part of 'history_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class HistoryEvent {}
|
|
@ -1,6 +0,0 @@
|
|||
part of 'history_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class HistoryState {}
|
||||
|
||||
class HistoryInitial extends HistoryState {}
|
|
@ -1,59 +0,0 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:jisho_study_tool/bloc/database/database_bloc.dart';
|
||||
import 'package:jisho_study_tool/models/history/kanji_query.dart';
|
||||
import 'package:jisho_study_tool/models/history/search.dart';
|
||||
|
||||
import './kanji_event.dart';
|
||||
import './kanji_state.dart';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:jisho_study_tool/services/jisho_api/kanji_search.dart';
|
||||
import 'package:jisho_study_tool/services/kanji_suggestions.dart';
|
||||
|
||||
export 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
export './kanji_event.dart';
|
||||
export './kanji_state.dart';
|
||||
|
||||
class KanjiBloc extends Bloc<KanjiEvent, KanjiState> {
|
||||
DatabaseBloc _databaseBloc;
|
||||
|
||||
KanjiBloc(this._databaseBloc) : super(KanjiSearch(KanjiSearchType.Initial));
|
||||
|
||||
void addSearchToDB(kanji) {
|
||||
if (_databaseBloc.state is DatabaseDisconnected)
|
||||
throw DatabaseNotConnectedException;
|
||||
|
||||
(_databaseBloc.state as DatabaseConnected)
|
||||
.database
|
||||
.box<Search>()
|
||||
.put(Search(timestamp: DateTime.now())
|
||||
..kanjiQuery.target = KanjiQuery(
|
||||
kanji: kanji,
|
||||
));
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<KanjiState> mapEventToState(KanjiEvent event) async* {
|
||||
if (event is GetKanji) {
|
||||
yield KanjiSearchLoading();
|
||||
|
||||
try {
|
||||
addSearchToDB(event.kanjiSearchString);
|
||||
final kanji = await fetchKanji(event.kanjiSearchString);
|
||||
if (kanji.found)
|
||||
yield KanjiSearchFinished(kanji: kanji);
|
||||
else
|
||||
yield KanjiSearchError('Something went wrong');
|
||||
} on Exception {
|
||||
yield KanjiSearchError('Something went wrong');
|
||||
}
|
||||
} else if (event is GetKanjiSuggestions) {
|
||||
final suggestions = kanjiSuggestions(event.searchString);
|
||||
yield KanjiSearchKeyboard(KanjiSearchType.Keyboard, suggestions);
|
||||
} else if (event is ReturnToInitialState) {
|
||||
yield KanjiSearch(KanjiSearchType.Initial);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
abstract class KanjiEvent {
|
||||
const KanjiEvent();
|
||||
}
|
||||
|
||||
class GetKanjiSuggestions extends KanjiEvent {
|
||||
final String searchString;
|
||||
const GetKanjiSuggestions(this.searchString);
|
||||
}
|
||||
|
||||
class GetKanji extends KanjiEvent {
|
||||
final String kanjiSearchString;
|
||||
const GetKanji(this.kanjiSearchString);
|
||||
}
|
||||
|
||||
class ReturnToInitialState extends KanjiEvent {
|
||||
const ReturnToInitialState();
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
import 'package:unofficial_jisho_api/api.dart';
|
||||
|
||||
abstract class KanjiState {
|
||||
const KanjiState();
|
||||
}
|
||||
|
||||
enum KanjiSearchType {
|
||||
Initial,
|
||||
Keyboard,
|
||||
Drawing,
|
||||
Radical,
|
||||
Grade
|
||||
}
|
||||
|
||||
class KanjiSearch extends KanjiState {
|
||||
final KanjiSearchType type;
|
||||
const KanjiSearch(this.type);
|
||||
}
|
||||
|
||||
class KanjiSearchKeyboard extends KanjiSearch {
|
||||
final List<String> kanjiSuggestions;
|
||||
const KanjiSearchKeyboard(KanjiSearchType type, this.kanjiSuggestions) : super(type);
|
||||
}
|
||||
|
||||
class KanjiSearchLoading extends KanjiState {
|
||||
const KanjiSearchLoading();
|
||||
}
|
||||
|
||||
class KanjiSearchFinished extends KanjiState {
|
||||
final KanjiResult kanji;
|
||||
final bool starred;
|
||||
|
||||
const KanjiSearchFinished({
|
||||
required this.kanji,
|
||||
this.starred = false,
|
||||
});
|
||||
}
|
||||
|
||||
class KanjiSearchError extends KanjiState {
|
||||
final String message;
|
||||
|
||||
const KanjiSearchError(this.message);
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
import 'package:bloc/bloc.dart';
|
||||
|
||||
import './navigation_event.dart';
|
||||
import './navigation_state.dart';
|
||||
|
||||
export 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
export './navigation_event.dart';
|
||||
export './navigation_state.dart';
|
||||
|
||||
class NavigationBloc extends Bloc<NavigationEvent, NavigationState> {
|
||||
|
||||
NavigationBloc() : super(NavigationPage(0));
|
||||
|
||||
@override
|
||||
Stream<NavigationState> mapEventToState(NavigationEvent event) async* {
|
||||
if (event is ChangePage)
|
||||
yield NavigationPage(event.pageNum);
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
abstract class NavigationEvent {
|
||||
const NavigationEvent();
|
||||
}
|
||||
|
||||
class ChangePage extends NavigationEvent {
|
||||
final int pageNum;
|
||||
const ChangePage(this.pageNum);
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
abstract class NavigationState {
|
||||
const NavigationState();
|
||||
}
|
||||
|
||||
class NavigationPage extends NavigationState {
|
||||
final int pageNum;
|
||||
const NavigationPage(this.pageNum);
|
||||
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/models/history/search.dart';
|
||||
import 'package:jisho_study_tool/models/history/word_query.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import 'package:jisho_study_tool/bloc/database/database_bloc.dart';
|
||||
import 'package:jisho_study_tool/services/jisho_api/jisho_search.dart';
|
||||
import 'package:unofficial_jisho_api/parser.dart';
|
||||
|
||||
export 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
part 'search_event.dart';
|
||||
part 'search_state.dart';
|
||||
|
||||
class SearchBloc extends Bloc<SearchEvent, SearchState> {
|
||||
DatabaseBloc _databaseBloc;
|
||||
|
||||
SearchBloc(this._databaseBloc) : super(SearchInitial());
|
||||
|
||||
void addSearchToDB(searchString) {
|
||||
if (_databaseBloc.state is DatabaseDisconnected)
|
||||
throw DatabaseNotConnectedException;
|
||||
|
||||
(_databaseBloc.state as DatabaseConnected)
|
||||
.database
|
||||
.box<Search>()
|
||||
.put(Search(timestamp: DateTime.now())
|
||||
..wordQuery.target = WordQuery(
|
||||
query: searchString,
|
||||
));
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<SearchState> mapEventToState(
|
||||
SearchEvent event,
|
||||
) async* {
|
||||
if (event is GetSearchResults) {
|
||||
yield SearchLoading();
|
||||
|
||||
try {
|
||||
addSearchToDB(event.searchString);
|
||||
final searchResults = await fetchJishoResults(event.searchString);
|
||||
if (searchResults.meta.status == 200)
|
||||
yield SearchFinished(searchResults.data!);
|
||||
} on Exception {
|
||||
yield SearchError('Something went wrong');
|
||||
}
|
||||
} else if (event is ReturnToInitialState) {
|
||||
yield SearchInitial();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
part of 'search_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class SearchEvent {
|
||||
const SearchEvent();
|
||||
}
|
||||
|
||||
class GetSearchResults extends SearchEvent {
|
||||
final String searchString;
|
||||
const GetSearchResults(this.searchString);
|
||||
}
|
||||
|
||||
class ReturnToInitialState extends SearchEvent {
|
||||
const ReturnToInitialState();
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
part of 'search_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class SearchState {
|
||||
const SearchState();
|
||||
}
|
||||
|
||||
class SearchInitial extends SearchState {
|
||||
const SearchInitial();
|
||||
}
|
||||
|
||||
class SearchLoading extends SearchState {
|
||||
const SearchLoading();
|
||||
}
|
||||
|
||||
class SearchFinished extends SearchState {
|
||||
final List<JishoResult> results;
|
||||
|
||||
const SearchFinished(this.results);
|
||||
}
|
||||
|
||||
class SearchError extends SearchState {
|
||||
final String message;
|
||||
|
||||
const SearchError(this.message);
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/bloc/theme/theme_bloc.dart';
|
||||
import 'package:jisho_study_tool/view/screens/splash.dart';
|
||||
import 'package:jisho_study_tool/router.dart';
|
||||
import 'package:jisho_study_tool/view/components/common/splash.dart';
|
||||
import 'package:mdi/mdi.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart';
|
||||
|
@ -8,13 +9,10 @@ import 'package:path/path.dart';
|
|||
import 'package:jisho_study_tool/objectbox.g.dart';
|
||||
|
||||
import 'package:jisho_study_tool/bloc/database/database_bloc.dart';
|
||||
import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart';
|
||||
import 'package:jisho_study_tool/bloc/search/search_bloc.dart';
|
||||
import 'package:jisho_study_tool/bloc/navigation/navigation_bloc.dart';
|
||||
|
||||
import 'package:jisho_study_tool/view/screens/kanji/view.dart';
|
||||
import 'package:jisho_study_tool/view/screens/search/kanji_view.dart';
|
||||
import 'package:jisho_study_tool/view/screens/history.dart';
|
||||
import 'package:jisho_study_tool/view/screens/search/view.dart';
|
||||
import 'package:jisho_study_tool/view/screens/search/search_view.dart';
|
||||
import 'package:jisho_study_tool/view/screens/settings.dart';
|
||||
|
||||
import 'models/themes/theme.dart';
|
||||
|
@ -60,20 +58,19 @@ class _MyAppState extends State<MyApp> {
|
|||
Widget build(BuildContext context) {
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider(create: (context) => SearchBloc(_databaseBloc)),
|
||||
BlocProvider(create: (context) => KanjiBloc(_databaseBloc)),
|
||||
BlocProvider(create: (context) => _databaseBloc),
|
||||
BlocProvider(create: (context) => NavigationBloc()),
|
||||
BlocProvider(create: (context) => ThemeBloc()),
|
||||
],
|
||||
child: BlocBuilder<ThemeBloc, ThemeState>(
|
||||
builder: (context, themeState) {
|
||||
if (!(dbConnected && themeState.prefsAreLoaded))
|
||||
return SplashScreen();
|
||||
|
||||
return MaterialApp(
|
||||
title: 'Jisho Study Tool',
|
||||
theme: themeState.theme.getMaterialTheme(),
|
||||
home: dbConnected && themeState.prefsAreLoaded
|
||||
? Home()
|
||||
: SplashScreen(),
|
||||
initialRoute: '/',
|
||||
onGenerateRoute: PageRouter.generateRoute,
|
||||
);
|
||||
},
|
||||
),
|
||||
|
@ -81,45 +78,48 @@ class _MyAppState extends State<MyApp> {
|
|||
}
|
||||
}
|
||||
|
||||
class Home extends StatelessWidget {
|
||||
class Home extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() => _HomeState();
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
int pageNum = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<NavigationBloc, NavigationState>(
|
||||
builder: (context, navigationState) {
|
||||
int selectedPage = (navigationState as NavigationPage).pageNum;
|
||||
return BlocBuilder<ThemeBloc, ThemeState>(
|
||||
builder: (context, themeState) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: pages[selectedPage].titleBar,
|
||||
centerTitle: true,
|
||||
backgroundColor: AppTheme.jishoGreen.background,
|
||||
foregroundColor: AppTheme.jishoGreen.foreground,
|
||||
return BlocBuilder<ThemeBloc, ThemeState>(
|
||||
builder: (context, themeState) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: pages[pageNum].titleBar,
|
||||
centerTitle: true,
|
||||
backgroundColor: AppTheme.jishoGreen.background,
|
||||
foregroundColor: AppTheme.jishoGreen.foreground,
|
||||
),
|
||||
body: Stack(
|
||||
children: [
|
||||
Positioned(
|
||||
child: Image.asset(
|
||||
'assets/images/denshi_jisho_background_overlay.png'),
|
||||
right: 30,
|
||||
left: 100,
|
||||
bottom: 30,
|
||||
),
|
||||
body: Stack(
|
||||
children: [
|
||||
Positioned(
|
||||
child: Image.asset(
|
||||
'assets/images/denshi_jisho_background_overlay.png'),
|
||||
right: 30,
|
||||
left: 100,
|
||||
bottom: 30,
|
||||
),
|
||||
pages[selectedPage].content,
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
fixedColor: AppTheme.jishoGreen.background,
|
||||
currentIndex: selectedPage,
|
||||
onTap: (int index) => BlocProvider.of<NavigationBloc>(context)
|
||||
.add(ChangePage(index)),
|
||||
items: pages.map((p) => p.item).toList(),
|
||||
showSelectedLabels: false,
|
||||
showUnselectedLabels: false,
|
||||
unselectedItemColor: themeState.theme.menuGreyDark.background,
|
||||
),
|
||||
);
|
||||
},
|
||||
pages[pageNum].content,
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
fixedColor: AppTheme.jishoGreen.background,
|
||||
currentIndex: pageNum,
|
||||
onTap: (int index) => setState(() {
|
||||
this.pageNum = index;
|
||||
}),
|
||||
items: pages.map((p) => p.item).toList(),
|
||||
showSelectedLabels: false,
|
||||
showUnselectedLabels: false,
|
||||
unselectedItemColor: themeState.theme.menuGreyDark.background,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
@ -149,7 +149,7 @@ final List<_Page> pages = [
|
|||
),
|
||||
_Page(
|
||||
content: KanjiView(),
|
||||
titleBar: KanjiViewBar(),
|
||||
titleBar: Text('Kanji'),
|
||||
item: BottomNavigationBarItem(
|
||||
label: 'Kanji', icon: Icon(Mdi.ideogramCjk, size: 30)),
|
||||
),
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/view/screens/search/kanji_result_page.dart';
|
||||
import 'package:jisho_study_tool/view/screens/search/search_results_page.dart';
|
||||
|
||||
import 'main.dart';
|
||||
|
||||
class PageRouter {
|
||||
static Route<dynamic> generateRoute(RouteSettings settings) {
|
||||
final args = settings.arguments;
|
||||
|
||||
switch (settings.name) {
|
||||
case '/':
|
||||
return MaterialPageRoute(builder: (_) => 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));
|
||||
|
||||
default:
|
||||
return MaterialPageRoute(builder: (_) => Text("ERROR: this route does not exist"));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
import 'package:unofficial_jisho_api/api.dart' as jisho;
|
||||
export 'package:unofficial_jisho_api/api.dart' show JishoAPIResult;
|
||||
|
||||
Future<jisho.JishoAPIResult> fetchJishoResults(searchTerm) async {
|
||||
return await jisho.searchForPhrase(searchTerm);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import 'package:unofficial_jisho_api/api.dart' as jisho;
|
||||
export 'package:unofficial_jisho_api/api.dart' show KanjiResult;
|
||||
|
||||
String? _convertGrade(String grade) {
|
||||
const conversionTable = {
|
||||
|
|
|
@ -9,7 +9,7 @@ class SplashScreen extends StatelessWidget {
|
|||
return Container(
|
||||
decoration: BoxDecoration(color: AppTheme.jishoGreen.background),
|
||||
child: Center(
|
||||
child: Image.asset('assets/images/logo/logo_icon_transparent.png'),
|
||||
child: Image(image: AssetImage('assets/images/logo/logo_icon_transparent.png'),)
|
||||
),
|
||||
);
|
||||
}
|
|
@ -1,7 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_slidable/flutter_slidable.dart';
|
||||
import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart';
|
||||
import 'package:jisho_study_tool/bloc/navigation/navigation_bloc.dart';
|
||||
import 'package:jisho_study_tool/bloc/theme/theme_bloc.dart';
|
||||
import 'package:jisho_study_tool/models/history/kanji_query.dart';
|
||||
import 'package:jisho_study_tool/models/themes/theme.dart';
|
||||
|
@ -58,8 +56,7 @@ class KanjiSearchItem extends StatelessWidget {
|
|||
return Slidable(
|
||||
child: SearchItem(
|
||||
onTap: () {
|
||||
BlocProvider.of<NavigationBloc>(context).add(ChangePage(1));
|
||||
BlocProvider.of<KanjiBloc>(context).add(GetKanji(this.result.kanji));
|
||||
Navigator.pushNamed(context, '/kanjiSearch', arguments: this.result.kanji);
|
||||
},
|
||||
time: timestamp,
|
||||
search: _KanjiBox(result.kanji),
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_slidable/flutter_slidable.dart';
|
||||
import 'package:jisho_study_tool/bloc/navigation/navigation_bloc.dart';
|
||||
import 'package:jisho_study_tool/bloc/search/search_bloc.dart';
|
||||
import 'package:jisho_study_tool/models/history/word_query.dart';
|
||||
|
||||
import './search_item.dart';
|
||||
|
@ -26,8 +24,7 @@ class PhraseSearchItem extends StatelessWidget {
|
|||
],
|
||||
child: SearchItem(
|
||||
onTap: () {
|
||||
BlocProvider.of<NavigationBloc>(context).add(ChangePage(0));
|
||||
BlocProvider.of<SearchBloc>(context).add(GetSearchResults(this.search.query));
|
||||
Navigator.pushNamed(context, '/search', arguments: this.search.query);
|
||||
},
|
||||
time: timestamp,
|
||||
search: Text(search.query),
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:unofficial_jisho_api/api.dart' as jisho;
|
||||
|
||||
import 'package:jisho_study_tool/view/components/kanji/result/examples.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/result/grade.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/result/header.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/result/jlpt_level.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/result/radical.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/result/rank.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/result/stroke_order_gif.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/result/yomi_chips.dart';
|
||||
import './kanji_result_body/examples.dart';
|
||||
import './kanji_result_body/grade.dart';
|
||||
import './kanji_result_body/header.dart';
|
||||
import './kanji_result_body/jlpt_level.dart';
|
||||
import './kanji_result_body/radical.dart';
|
||||
import './kanji_result_body/rank.dart';
|
||||
import './kanji_result_body/stroke_order_gif.dart';
|
||||
import './kanji_result_body/yomi_chips.dart';
|
||||
|
||||
class KanjiResultCard extends StatelessWidget {
|
||||
class KanjiResultBody extends StatelessWidget {
|
||||
late final String query;
|
||||
late final jisho.KanjiResultData resultData;
|
||||
|
||||
KanjiResultCard({required jisho.KanjiResult result}) {
|
||||
KanjiResultBody({required jisho.KanjiResult result}) {
|
||||
|
||||
query = result.query;
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:animated_size_and_fade/animated_size_and_fade.dart';
|
||||
import 'package:jisho_study_tool/services/kanji_suggestions.dart';
|
||||
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_search_body/kanji_grid.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_search_body/kanji_search_bar.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_search_body/kanji_search_options_bar.dart';
|
||||
|
||||
class KanjiSearchBody extends StatefulWidget {
|
||||
KanjiSearchBody({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_KanjiSearchBodyState createState() => _KanjiSearchBodyState();
|
||||
}
|
||||
|
||||
class _KanjiSearchBodyState extends State<KanjiSearchBody>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final AnimationController _controller;
|
||||
late final Animation _searchbarMovementAnimation;
|
||||
final FocusNode focus = FocusNode();
|
||||
final GlobalKey<KanjiSearchBarState> _kanjiSearchBarState =
|
||||
GlobalKey<KanjiSearchBarState>();
|
||||
List<String> suggestions = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_controller = AnimationController(
|
||||
vsync: this,
|
||||
duration: Duration(milliseconds: 200),
|
||||
);
|
||||
|
||||
_searchbarMovementAnimation = AlignmentTween(
|
||||
begin: Alignment.center,
|
||||
end: Alignment.topCenter,
|
||||
).animate(CurvedAnimation(
|
||||
parent: _controller,
|
||||
curve: Curves.easeInOut,
|
||||
));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return WillPopScope(
|
||||
onWillPop: () async {
|
||||
if (_controller.value == 1) {
|
||||
focus.unfocus();
|
||||
_kanjiSearchBarState.currentState!.clearText();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
child: GestureDetector(
|
||||
onTap: () => FocusScope.of(context).unfocus(),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(),
|
||||
alignment: Alignment.center,
|
||||
padding: EdgeInsets.symmetric(horizontal: 20),
|
||||
child: AnimatedBuilder(
|
||||
animation: _searchbarMovementAnimation,
|
||||
builder: (BuildContext context, _) {
|
||||
return Container(
|
||||
alignment: _searchbarMovementAnimation.value,
|
||||
padding: EdgeInsets.symmetric(vertical: 10.0),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Focus(
|
||||
focusNode: focus,
|
||||
onFocusChange: (hasFocus) {
|
||||
if (hasFocus)
|
||||
_controller.forward();
|
||||
else
|
||||
_controller.reverse();
|
||||
},
|
||||
child: KanjiSearchBar(
|
||||
key: _kanjiSearchBarState,
|
||||
onChanged: (text) => setState(() {
|
||||
this.suggestions = kanjiSuggestions(text);
|
||||
}),
|
||||
),
|
||||
),
|
||||
AnimatedSizeAndFade(
|
||||
vsync: this,
|
||||
child: _controller.value == 1
|
||||
? KanjiGrid(this.suggestions)
|
||||
: KanjiSearchOptionsBar(),
|
||||
fadeDuration: const Duration(milliseconds: 200),
|
||||
sizeDuration: const Duration(milliseconds: 300),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart';
|
||||
import 'package:jisho_study_tool/bloc/theme/theme_bloc.dart';
|
||||
|
||||
class KanjiGrid extends StatelessWidget {
|
||||
|
@ -33,29 +32,28 @@ class _GridItem extends StatelessWidget {
|
|||
Widget build(BuildContext context) {
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
BlocProvider.of<KanjiBloc>(context).add(GetKanji(kanji));
|
||||
Navigator.pushNamed(context, '/kanjiSearch', arguments: kanji);
|
||||
},
|
||||
child: BlocBuilder<ThemeBloc, ThemeState>(
|
||||
builder: (context, state) {
|
||||
final _menuColors = state.theme.menuGreyLight;
|
||||
return
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: _menuColors.background,
|
||||
borderRadius: BorderRadius.circular(20.0),
|
||||
),
|
||||
child: Container(
|
||||
margin: EdgeInsets.all(10.0),
|
||||
child: FittedBox(
|
||||
child: Text(
|
||||
kanji,
|
||||
style: TextStyle(color: _menuColors.foreground),
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: _menuColors.background,
|
||||
borderRadius: BorderRadius.circular(20.0),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
child: Container(
|
||||
margin: EdgeInsets.all(10.0),
|
||||
child: FittedBox(
|
||||
child: Text(
|
||||
kanji,
|
||||
style: TextStyle(color: _menuColors.foreground),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,41 +1,43 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart';
|
||||
|
||||
class KanjiSearchBar extends StatefulWidget {
|
||||
final Function(String)? onChanged;
|
||||
|
||||
const KanjiSearchBar();
|
||||
KanjiSearchBar({this.onChanged, Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_KanjiSearchBarState createState() => new _KanjiSearchBarState();
|
||||
KanjiSearchBarState createState() => new KanjiSearchBarState(this.onChanged);
|
||||
}
|
||||
|
||||
enum TextFieldButton {clear, paste}
|
||||
enum TextFieldButton { clear, paste }
|
||||
|
||||
class _KanjiSearchBarState extends State<KanjiSearchBar> {
|
||||
class KanjiSearchBarState extends State<KanjiSearchBar> {
|
||||
final TextEditingController textController = new TextEditingController();
|
||||
TextFieldButton button = TextFieldButton.paste;
|
||||
final Function(String)? onChanged;
|
||||
|
||||
KanjiSearchBarState(this.onChanged);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void _getKanjiSuggestions(String text) =>
|
||||
BlocProvider.of<KanjiBloc>(context).add(GetKanjiSuggestions(text));
|
||||
|
||||
void updateSuggestions() => _getKanjiSuggestions(textController.text);
|
||||
|
||||
void _clearText() {
|
||||
textController.text = '';
|
||||
updateSuggestions();
|
||||
void runOnChanged() {
|
||||
if (onChanged != null) onChanged!(textController.text);
|
||||
}
|
||||
|
||||
void _pasteText() async {
|
||||
void clearText() {
|
||||
textController.text = '';
|
||||
runOnChanged();
|
||||
}
|
||||
|
||||
void pasteText() async {
|
||||
ClipboardData? clipboardData = await Clipboard.getData('text/plain');
|
||||
if (clipboardData != null && clipboardData.text != null) {
|
||||
textController.text = clipboardData.text!;
|
||||
updateSuggestions();
|
||||
runOnChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,29 +45,32 @@ class _KanjiSearchBarState extends State<KanjiSearchBar> {
|
|||
Widget build(BuildContext context) {
|
||||
IconButton clearButton = IconButton(
|
||||
icon: Icon(Icons.clear),
|
||||
onPressed: () => _clearText(),
|
||||
onPressed: () => clearText(),
|
||||
);
|
||||
|
||||
IconButton pasteButton = IconButton(
|
||||
icon: Icon(Icons.content_paste),
|
||||
onPressed: () => _pasteText(),
|
||||
onPressed: () => pasteText(),
|
||||
);
|
||||
|
||||
return TextField(
|
||||
controller: textController,
|
||||
onChanged: (text) => _getKanjiSuggestions(text),
|
||||
onChanged: (text) {
|
||||
if (this.onChanged != null) this.onChanged!(text);
|
||||
},
|
||||
onSubmitted: (_) => {},
|
||||
decoration: new InputDecoration(
|
||||
prefixIcon: Icon(Icons.search),
|
||||
hintText: 'Search',
|
||||
// fillColor: Colors.white,
|
||||
// filled: true,
|
||||
// filled: true,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
),
|
||||
isDense: false,
|
||||
suffixIcon: (button == TextFieldButton.clear) ? clearButton : pasteButton,
|
||||
suffixIcon:
|
||||
(button == TextFieldButton.clear) ? clearButton : pasteButton,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart';
|
||||
|
||||
//TODO: Make buttons have an effect
|
||||
|
||||
|
@ -20,18 +19,15 @@ class KanjiSearchOptionsBar extends StatelessWidget {
|
|||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
onPressed: () =>
|
||||
BlocProvider.of<KanjiBloc>(context).add(ReturnToInitialState()),
|
||||
onPressed: () {}
|
||||
),
|
||||
_IconButton(
|
||||
icon: Icon(Icons.category),
|
||||
onPressed: () =>
|
||||
BlocProvider.of<KanjiBloc>(context).add(ReturnToInitialState()),
|
||||
onPressed: () {}
|
||||
),
|
||||
_IconButton(
|
||||
icon: Icon(Icons.mode),
|
||||
onPressed: () =>
|
||||
BlocProvider.of<KanjiBloc>(context).add(ReturnToInitialState()),
|
||||
onPressed: () {}
|
||||
),
|
||||
],
|
||||
),
|
|
@ -1,9 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/bloc/search/search_bloc.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/language_selector.dart';
|
||||
|
||||
class SearchBar extends StatelessWidget {
|
||||
|
||||
const SearchBar();
|
||||
|
||||
@override
|
||||
|
@ -13,10 +11,8 @@ class SearchBar extends StatelessWidget {
|
|||
child: Column(
|
||||
children: [
|
||||
TextField(
|
||||
onSubmitted: (text) {
|
||||
BlocProvider.of<SearchBloc>(context)
|
||||
.add(GetSearchResults(text));
|
||||
},
|
||||
onSubmitted: (text) =>
|
||||
Navigator.pushNamed(context, '/search', arguments: text),
|
||||
controller: TextEditingController(),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Search',
|
||||
|
@ -33,4 +29,4 @@ class SearchBar extends StatelessWidget {
|
|||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/search_results_body/search_card.dart';
|
||||
import 'package:unofficial_jisho_api/api.dart';
|
||||
|
||||
class SearchResultsBody extends StatelessWidget {
|
||||
final List<JishoResult> results;
|
||||
|
||||
const SearchResultsBody({
|
||||
required this.results,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
children: results.map((result) => SearchResultCard(result)).toList(),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/search_result_page/parts/badge.dart';
|
||||
import './badge.dart';
|
||||
|
||||
class CommonBadge extends StatelessWidget {
|
||||
final bool isCommon;
|
|
@ -1,5 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/search_result_page/parts/badge.dart';
|
||||
import './badge.dart';
|
||||
|
||||
class JLPTBadge extends StatelessWidget {
|
||||
final String jlptLevel;
|
|
@ -1,5 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/search_result_page/parts/badge.dart';
|
||||
import './badge.dart';
|
||||
|
||||
class WKBadge extends StatelessWidget {
|
||||
final String wkLevel;
|
|
@ -1,13 +1,12 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/search_result_page/parts/common_badge.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/search_result_page/parts/jlpt_badge.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/search_result_page/parts/wanikani_badge.dart';
|
||||
|
||||
import 'package:unofficial_jisho_api/api.dart';
|
||||
|
||||
import './parts/common_badge.dart';
|
||||
import './parts/header.dart';
|
||||
import './parts/senses.dart';
|
||||
import './parts/jlpt_badge.dart';
|
||||
import './parts/other_forms.dart';
|
||||
import './parts/senses.dart';
|
||||
import './parts/wanikani_badge.dart';
|
||||
|
||||
class SearchResultCard extends StatelessWidget {
|
||||
final JishoResult result;
|
|
@ -1,94 +0,0 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart';
|
||||
import 'package:animated_size_and_fade/animated_size_and_fade.dart';
|
||||
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_grid.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_search_bar.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_search_options_bar.dart';
|
||||
|
||||
class SearchScreen extends StatefulWidget {
|
||||
SearchScreen({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_SearchScreenState createState() => _SearchScreenState();
|
||||
}
|
||||
|
||||
class _SearchScreenState extends State<SearchScreen>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final AnimationController _controller;
|
||||
late final Animation _searchbarMovementAnimation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_controller = AnimationController(
|
||||
vsync: this,
|
||||
duration: Duration(milliseconds: 200),
|
||||
);
|
||||
|
||||
_searchbarMovementAnimation = AlignmentTween(
|
||||
begin: Alignment.center,
|
||||
end: Alignment.topCenter,
|
||||
).animate(CurvedAnimation(
|
||||
parent: _controller,
|
||||
curve: Curves.easeInOut,
|
||||
));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () => FocusScope.of(context).unfocus(),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(),
|
||||
alignment: Alignment.center,
|
||||
padding: EdgeInsets.symmetric(horizontal: 20),
|
||||
child: AnimatedBuilder(
|
||||
animation: _searchbarMovementAnimation,
|
||||
builder: (BuildContext context, _) {
|
||||
return Container(
|
||||
alignment: _searchbarMovementAnimation.value,
|
||||
padding: EdgeInsets.symmetric(vertical: 10.0),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Focus(
|
||||
onFocusChange: (hasFocus) {
|
||||
if (hasFocus)
|
||||
_controller.forward();
|
||||
else
|
||||
_controller.reverse();
|
||||
},
|
||||
child: KanjiSearchBar(),
|
||||
),
|
||||
BlocBuilder<KanjiBloc, KanjiState>(
|
||||
builder: (context, state) {
|
||||
return AnimatedSizeAndFade(
|
||||
vsync: this,
|
||||
child: _controller.value == 1
|
||||
? KanjiGrid((state is KanjiSearchKeyboard)
|
||||
? state.kanjiSuggestions
|
||||
: [])
|
||||
// ? Container()
|
||||
: KanjiSearchOptionsBar(),
|
||||
fadeDuration: const Duration(milliseconds: 200),
|
||||
sizeDuration: const Duration(milliseconds: 300),
|
||||
);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart';
|
||||
import 'package:jisho_study_tool/view/screens/loading.dart';
|
||||
|
||||
import 'search.dart';
|
||||
import 'result.dart';
|
||||
|
||||
class KanjiView extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<KanjiBloc, KanjiState>(
|
||||
listener: (context, state) {
|
||||
if (state is KanjiSearch && state.type == KanjiSearchType.Initial) {
|
||||
FocusScope.of(context).unfocus();
|
||||
} else if (state is KanjiSearchLoading) {
|
||||
FocusScope.of(context).unfocus();
|
||||
}
|
||||
},
|
||||
child: BlocBuilder<KanjiBloc, KanjiState>(
|
||||
builder: (context, state) {
|
||||
if (state is KanjiSearch) {
|
||||
if (state.type == KanjiSearchType.Initial) return SearchScreen();
|
||||
else if (state is KanjiSearchKeyboard) return SearchScreen();
|
||||
}
|
||||
else if (state is KanjiSearchLoading) return LoadingScreen();
|
||||
else if (state is KanjiSearchFinished)
|
||||
return WillPopScope(
|
||||
child: KanjiResultCard(result: state.kanji),
|
||||
onWillPop: () async {
|
||||
BlocProvider.of<KanjiBloc>(context)
|
||||
.add(ReturnToInitialState());
|
||||
return false;
|
||||
});
|
||||
throw 'No such event found';
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class KanjiViewBar extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: Icon(Icons.arrow_back),
|
||||
onPressed: () =>
|
||||
BlocProvider.of<KanjiBloc>(context).add(ReturnToInitialState()),
|
||||
),
|
||||
// Expanded(
|
||||
// child: Container(
|
||||
// child: KanjiSearchBar(),
|
||||
// ),
|
||||
// ),
|
||||
// IconButton(
|
||||
// icon: Icon(Icons.star_border),
|
||||
// onPressed: null,
|
||||
// ),
|
||||
// IconButton(
|
||||
// icon: Icon(Icons.add),
|
||||
// onPressed: null,
|
||||
// ),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/bloc/database/database_bloc.dart';
|
||||
import 'package:jisho_study_tool/models/history/kanji_query.dart';
|
||||
import 'package:jisho_study_tool/models/history/search.dart';
|
||||
import 'package:jisho_study_tool/view/components/common/loading.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_result_body.dart';
|
||||
import 'package:jisho_study_tool/services/jisho_api/kanji_search.dart';
|
||||
|
||||
class KanjiResultPage extends StatelessWidget {
|
||||
final String kanjiSearchTerm;
|
||||
bool addedToDatabase = false;
|
||||
|
||||
KanjiResultPage({required this.kanjiSearchTerm, Key? key})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: FutureBuilder(
|
||||
future: fetchKanji(this.kanjiSearchTerm),
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData) return LoadingScreen();
|
||||
if (snapshot.hasError) return ErrorWidget(snapshot.error!);
|
||||
|
||||
if (!this.addedToDatabase) {
|
||||
(BlocProvider.of<DatabaseBloc>(context).state as DatabaseConnected)
|
||||
.database
|
||||
.box<Search>()
|
||||
.put(Search(timestamp: DateTime.now())
|
||||
..kanjiQuery.target = KanjiQuery(
|
||||
kanji: this.kanjiSearchTerm,
|
||||
));
|
||||
this.addedToDatabase = true;
|
||||
}
|
||||
|
||||
return KanjiResultBody(result: (snapshot.data as KanjiResult));
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_search_body.dart';
|
||||
|
||||
class KanjiView extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return KanjiSearchBody();
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/bloc/search/search_bloc.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/search_result_page/search_card.dart';
|
||||
import 'package:unofficial_jisho_api/api.dart';
|
||||
|
||||
class SearchResults extends StatelessWidget {
|
||||
final List<JishoResult> results;
|
||||
|
||||
const SearchResults({
|
||||
required this.results,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return WillPopScope(
|
||||
child: ListView(
|
||||
children: results.map((result) => SearchResultCard(result)).toList(),
|
||||
),
|
||||
onWillPop: () async {
|
||||
BlocProvider.of<SearchBloc>(context).add(ReturnToInitialState());
|
||||
return false;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_grid.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_search_bar.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_search_body/kanji_grid.dart';
|
||||
import 'package:jisho_study_tool/view/components/kanji/kanji_search_body/kanji_search_bar.dart';
|
||||
|
||||
class SearchGrid extends StatelessWidget {
|
||||
final List<String> suggestions;
|
|
@ -0,0 +1,48 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:jisho_study_tool/bloc/database/database_bloc.dart';
|
||||
import 'package:jisho_study_tool/models/history/search.dart';
|
||||
import 'package:jisho_study_tool/models/history/word_query.dart';
|
||||
import 'package:jisho_study_tool/view/components/common/loading.dart';
|
||||
import 'package:jisho_study_tool/view/components/search/search_result_body.dart';
|
||||
import 'package:jisho_study_tool/services/jisho_api/jisho_search.dart';
|
||||
|
||||
class SearchResultsPage extends StatelessWidget {
|
||||
final String searchTerm;
|
||||
final Future<JishoAPIResult> results;
|
||||
bool addedToDatabase = false;
|
||||
|
||||
SearchResultsPage({required this.searchTerm, Key? key})
|
||||
: this.results = fetchJishoResults(searchTerm),
|
||||
super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: FutureBuilder(
|
||||
future: results,
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData) return LoadingScreen();
|
||||
if (snapshot.hasError ||
|
||||
(snapshot.data as JishoAPIResult).data == null)
|
||||
return ErrorWidget(snapshot.error!);
|
||||
|
||||
if (!this.addedToDatabase) {
|
||||
(BlocProvider.of<DatabaseBloc>(context).state as DatabaseConnected)
|
||||
.database
|
||||
.box<Search>()
|
||||
.put(Search(timestamp: DateTime.now())
|
||||
..wordQuery.target = WordQuery(
|
||||
query: this.searchTerm,
|
||||
));
|
||||
this.addedToDatabase = true;
|
||||
}
|
||||
|
||||
return SearchResultsBody(
|
||||
results: (snapshot.data as JishoAPIResult).data!,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
14
14