From b86e0ae2f295a23dcfc5c6dabc3eab83ffaa4aaf Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 19 Jul 2021 01:49:18 +0200 Subject: [PATCH] Separate history entries --- lib/bloc/kanji/kanji_bloc.dart | 11 +- lib/bloc/search/search_bloc.dart | 7 +- .../{search.dart => kanji_result.dart} | 16 +- lib/models/history/search_string.dart | 26 ++ lib/models/history/word_result.dart | 25 ++ lib/models/storage/search_result.dart | 72 ++++ lib/objectbox-model.json | 320 +++++++++++++++++- .../components/history/kanji_search_item.dart | 40 +++ lib/view/components/history/search_item.dart | 41 +++ .../components/history/word_search_item.dart | 0 lib/view/components/search/search_bar.dart | 1 - lib/view/screens/history.dart | 22 +- 12 files changed, 540 insertions(+), 41 deletions(-) rename lib/models/history/{search.dart => kanji_result.dart} (52%) create mode 100644 lib/models/history/search_string.dart create mode 100644 lib/models/history/word_result.dart create mode 100644 lib/models/storage/search_result.dart create mode 100644 lib/view/components/history/kanji_search_item.dart create mode 100644 lib/view/components/history/search_item.dart create mode 100644 lib/view/components/history/word_search_item.dart diff --git a/lib/bloc/kanji/kanji_bloc.dart b/lib/bloc/kanji/kanji_bloc.dart index dc82681..05207b4 100644 --- a/lib/bloc/kanji/kanji_bloc.dart +++ b/lib/bloc/kanji/kanji_bloc.dart @@ -1,7 +1,7 @@ import 'dart:async'; 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/kanji_result.dart'; import './kanji_event.dart'; import './kanji_state.dart'; @@ -19,17 +19,16 @@ class KanjiBloc extends Bloc { KanjiBloc(this._databaseBloc) : super(KanjiSearch(KanjiSearchType.Initial)); - void addSearchToDB(searchString) { + void addSearchToDB(kanji) { if (_databaseBloc.state is DatabaseDisconnected) throw DatabaseNotConnectedException; (_databaseBloc.state as DatabaseConnected) .database - .box() - .put(Search( - query: searchString, + .box() + .put(KanjiResult( + kanji: kanji, timestamp: DateTime.now(), - type: "kanji" )); } diff --git a/lib/bloc/search/search_bloc.dart b/lib/bloc/search/search_bloc.dart index 8a50031..6ce75cc 100644 --- a/lib/bloc/search/search_bloc.dart +++ b/lib/bloc/search/search_bloc.dart @@ -3,7 +3,7 @@ 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/search_string.dart'; import 'package:meta/meta.dart'; import 'package:jisho_study_tool/bloc/database/database_bloc.dart'; @@ -25,11 +25,10 @@ class SearchBloc extends Bloc { (_databaseBloc.state as DatabaseConnected) .database - .box() - .put(Search( + .box() + .put(SearchString( query: searchString, timestamp: DateTime.now(), - type: "search" )); } diff --git a/lib/models/history/search.dart b/lib/models/history/kanji_result.dart similarity index 52% rename from lib/models/history/search.dart rename to lib/models/history/kanji_result.dart index 4211153..2d7864c 100644 --- a/lib/models/history/search.dart +++ b/lib/models/history/kanji_result.dart @@ -1,26 +1,22 @@ + import 'package:objectbox/objectbox.dart'; @Entity() -class Search { +class KanjiResult { int id = 0; @Property(type: PropertyType.date) DateTime timestamp; - String query; + String kanji; - String type; - - Search({ - this.id, + KanjiResult({ this.timestamp, - this.query, - this.type, + this.kanji, }); @override String toString() { - return "${timestamp.toIso8601String()} [${type.toUpperCase()}] - $query"; + return "[${timestamp.toIso8601String()}] - $kanji"; } - } \ No newline at end of file diff --git a/lib/models/history/search_string.dart b/lib/models/history/search_string.dart new file mode 100644 index 0000000..b85aa07 --- /dev/null +++ b/lib/models/history/search_string.dart @@ -0,0 +1,26 @@ +import 'package:objectbox/objectbox.dart'; + +import 'package:jisho_study_tool/models/history/word_result.dart'; + +@Entity() +class SearchString { + int id = 0; + + @Property(type: PropertyType.date) + DateTime timestamp; + + String query; + + @Backlink() + final chosenResults = ToMany(); + + SearchString({ + this.timestamp, + this.query, + }); + + @override + String toString() { + return "[${timestamp.toIso8601String()}] \"$query\""; + } +} \ No newline at end of file diff --git a/lib/models/history/word_result.dart b/lib/models/history/word_result.dart new file mode 100644 index 0000000..5e3cf7c --- /dev/null +++ b/lib/models/history/word_result.dart @@ -0,0 +1,25 @@ +import 'package:objectbox/objectbox.dart'; + +import 'package:jisho_study_tool/models/history/search_string.dart'; + +@Entity() +class WordResult { + int id = 0; + + @Property(type: PropertyType.date) + DateTime timestamp; + + String word; + + final searchString = ToOne(); + + WordResult({ + this.timestamp, + this.word, + }); + + @override + String toString() { + return "[${timestamp.toIso8601String()}] - $word"; + } +} \ No newline at end of file diff --git a/lib/models/storage/search_result.dart b/lib/models/storage/search_result.dart new file mode 100644 index 0000000..19783af --- /dev/null +++ b/lib/models/storage/search_result.dart @@ -0,0 +1,72 @@ +import 'package:objectbox/objectbox.dart'; +import 'package:unofficial_jisho_api/api.dart' as jisho; + +@Entity() +class SearchResult { + int id = 0; + final meta = ToOne(); + final data = ToMany(); + + // SearchResult(JishoAPIResult result) { + // this.data = result.data; + // this.meta = result.meta; + // } + + // JishoAPIResult toJishoAPIResult() { + // return JishoAPIResult(meta: this.meta, data: this.data); + // } +} + +@Entity() +class JishoResultMeta { + int id = 0; + int status; +} + +@Entity() +class JishoResult { + int id = 0; + final attribution = ToOne(); + bool is_common; + final japanese = ToMany(); + List jlpt; + final senses = ToMany(); + String slug; + List tags; +} + +@Entity() +class JishoAttribution { + int id = 0; + String dbpedia; + String jmdict; + bool jmnedict; +} + +@Entity() +class JishoJapaneseWord { + int id = 0; + String reading; + String word; +} + +@Entity() +class JishoWordSense { + int id = 0; + List antonyms; + List english_definitions; + List info; + final links = ToMany(); + List parts_of_speech; + List restrictions; + List see_also; + List source; + List tags; +} + +@Entity() +class JishoSenseLink { + int id = 0; + String text; + String url; +} diff --git a/lib/objectbox-model.json b/lib/objectbox-model.json index ae67043..483b1a3 100644 --- a/lib/objectbox-model.json +++ b/lib/objectbox-model.json @@ -4,44 +4,336 @@ "_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.", "entities": [ { - "id": "1:7471000004971513655", - "lastPropertyId": "4:1530573958565295186", - "name": "Search", + "id": "3:7851566044119418641", + "lastPropertyId": "4:2352718426928758061", + "name": "JishoAttribution", "properties": [ { - "id": "1:182004738902401315", + "id": "1:9162407962473480509", "name": "id", "type": 6, "flags": 1 }, { - "id": "2:647032929519296287", + "id": "2:708495477684386", + "name": "dbpedia", + "type": 9 + }, + { + "id": "3:6579248575703756688", + "name": "jmdict", + "type": 9 + }, + { + "id": "4:2352718426928758061", + "name": "jmnedict", + "type": 1 + } + ], + "relations": [] + }, + { + "id": "4:5830034738494786681", + "lastPropertyId": "3:1420686825377652298", + "name": "JishoJapaneseWord", + "properties": [ + { + "id": "1:5568051277997102473", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:8794655557751948952", + "name": "reading", + "type": 9 + }, + { + "id": "3:1420686825377652298", + "name": "word", + "type": 9 + } + ], + "relations": [] + }, + { + "id": "5:814015574151442186", + "lastPropertyId": "6:5908719510010444548", + "name": "JishoResult", + "properties": [ + { + "id": "1:4779715838063307925", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:398633124413531447", + "name": "attributionId", + "type": 11, + "flags": 520, + "indexId": "1:3741479335507224998", + "relationTarget": "JishoAttribution" + }, + { + "id": "3:3743357097628620453", + "name": "is_common", + "type": 1 + }, + { + "id": "4:2403901886209575067", + "name": "jlpt", + "type": 30 + }, + { + "id": "5:9147057123672087105", + "name": "slug", + "type": 9 + }, + { + "id": "6:5908719510010444548", + "name": "tags", + "type": 30 + } + ], + "relations": [ + { + "id": "1:1762405519516054052", + "name": "japanese", + "targetId": "4:5830034738494786681" + }, + { + "id": "2:5662556255405307520", + "name": "senses", + "targetId": "8:7499450265299287403" + } + ] + }, + { + "id": "6:6175990861942758315", + "lastPropertyId": "2:6228403848602783530", + "name": "JishoResultMeta", + "properties": [ + { + "id": "1:2609861734144336951", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:6228403848602783530", + "name": "status", + "type": 6 + } + ], + "relations": [] + }, + { + "id": "7:8388739279951258717", + "lastPropertyId": "3:6475272726803052671", + "name": "JishoSenseLink", + "properties": [ + { + "id": "1:9038298290884158108", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:3536322409202325005", + "name": "text", + "type": 9 + }, + { + "id": "3:6475272726803052671", + "name": "url", + "type": 9 + } + ], + "relations": [] + }, + { + "id": "8:7499450265299287403", + "lastPropertyId": "8:8987388154341761734", + "name": "JishoWordSense", + "properties": [ + { + "id": "1:3215440847258041904", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:2015550844387658352", + "name": "antonyms", + "type": 30 + }, + { + "id": "3:5828592811743363528", + "name": "english_definitions", + "type": 30 + }, + { + "id": "4:5593994833762859570", + "name": "info", + "type": 30 + }, + { + "id": "5:2972516533060921751", + "name": "parts_of_speech", + "type": 30 + }, + { + "id": "6:116167531949398184", + "name": "restrictions", + "type": 30 + }, + { + "id": "7:5268205144755091970", + "name": "see_also", + "type": 30 + }, + { + "id": "8:8987388154341761734", + "name": "tags", + "type": 30 + } + ], + "relations": [ + { + "id": "4:554428283260148269", + "name": "links", + "targetId": "7:8388739279951258717" + } + ] + }, + { + "id": "9:6483665137820532859", + "lastPropertyId": "2:8047458152016460696", + "name": "SearchResult", + "properties": [ + { + "id": "1:1744658189464710184", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:8047458152016460696", + "name": "metaId", + "type": 11, + "flags": 520, + "indexId": "2:2854883655423126351", + "relationTarget": "JishoResultMeta" + } + ], + "relations": [ + { + "id": "3:9171394818028481706", + "name": "data", + "targetId": "5:814015574151442186" + } + ] + }, + { + "id": "10:2511293769307589652", + "lastPropertyId": "3:7310514284446208088", + "name": "KanjiResult", + "properties": [ + { + "id": "1:4875831913401260727", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:2639524138758004976", "name": "timestamp", "type": 10 }, { - "id": "3:8448353731705407210", + "id": "3:7310514284446208088", + "name": "kanji", + "type": 9 + } + ], + "relations": [] + }, + { + "id": "11:8718716930258187923", + "lastPropertyId": "3:6047999002753663080", + "name": "SearchString", + "properties": [ + { + "id": "1:8056495551508046109", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:5805071287032353000", + "name": "timestamp", + "type": 10 + }, + { + "id": "3:6047999002753663080", "name": "query", "type": 9 + } + ], + "relations": [] + }, + { + "id": "12:8709403319711854658", + "lastPropertyId": "4:4071472200738865726", + "name": "WordResult", + "properties": [ + { + "id": "1:2725285997242709614", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:6271798504226631398", + "name": "timestamp", + "type": 10 + }, + { + "id": "3:5467536225310784192", + "name": "word", + "type": 9 }, { - "id": "4:1530573958565295186", - "name": "type", - "type": 9 + "id": "4:4071472200738865726", + "name": "searchStringId", + "type": 11, + "flags": 520, + "indexId": "3:5534321127094586184", + "relationTarget": "SearchString" } ], "relations": [] } ], - "lastEntityId": "1:7471000004971513655", - "lastIndexId": "0:0", - "lastRelationId": "0:0", + "lastEntityId": "12:8709403319711854658", + "lastIndexId": "3:5534321127094586184", + "lastRelationId": "4:554428283260148269", "lastSequenceId": "0:0", "modelVersion": 5, "modelVersionParserMinimum": 5, - "retiredEntityUids": [], + "retiredEntityUids": [ + 2061102273968386021, + 7471000004971513655 + ], "retiredIndexUids": [], - "retiredPropertyUids": [], + "retiredPropertyUids": [ + 1377817599424560887, + 182004738902401315, + 647032929519296287, + 8448353731705407210, + 1530573958565295186 + ], "retiredRelationUids": [], "version": 1 } \ No newline at end of file diff --git a/lib/view/components/history/kanji_search_item.dart b/lib/view/components/history/kanji_search_item.dart new file mode 100644 index 0000000..3a229a2 --- /dev/null +++ b/lib/view/components/history/kanji_search_item.dart @@ -0,0 +1,40 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_slidable/flutter_slidable.dart'; +import 'package:jisho_study_tool/models/history/kanji_result.dart'; + +class _KanjiSearchItemHeader extends StatelessWidget { + final KanjiResult result; + + const _KanjiSearchItemHeader(this.result, {Key key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return Text("[KANJI] ${result.kanji} - ${result.timestamp.toIso8601String()}"); + } +} + +class KanjiSearchItem extends StatelessWidget { + final KanjiResult result; + + const KanjiSearchItem(this.result,{Key key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return Slidable( + child: ListTile(title: _KanjiSearchItemHeader(result)), + actionPane: SlidableScrollActionPane(), + secondaryActions: [ + IconSlideAction( + caption: "Favourite", + color: Colors.yellow, + icon: Icons.star + ), + IconSlideAction( + caption: "Delete", + color: Colors.red, + icon: Icons.delete + ) + ], + ); + } +} diff --git a/lib/view/components/history/search_item.dart b/lib/view/components/history/search_item.dart new file mode 100644 index 0000000..e058ea1 --- /dev/null +++ b/lib/view/components/history/search_item.dart @@ -0,0 +1,41 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_slidable/flutter_slidable.dart'; +import 'package:jisho_study_tool/models/history/search_string.dart'; + +class SearchItemHeader extends StatelessWidget { + final SearchString _search; + + const SearchItemHeader(this._search, {Key key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return Container( + child: Text("[SEARCH] ${_search.query} - ${_search.timestamp.toString()}"), + ); + } +} + +class SearchItem extends StatelessWidget { + final SearchString _search; + + const SearchItem(this._search, {Key key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return Slidable( + actionPane: SlidableScrollActionPane(), + secondaryActions: [ + IconSlideAction( + caption: "Delete", + color: Colors.red, + icon: Icons.delete + ) + ], + child: ExpansionTile( + title: SearchItemHeader(_search), + expandedAlignment: Alignment.topCenter, + children: [ListTile(title: Text(_search.timestamp.toIso8601String()),)], + ) + ); + } +} diff --git a/lib/view/components/history/word_search_item.dart b/lib/view/components/history/word_search_item.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/view/components/search/search_bar.dart b/lib/view/components/search/search_bar.dart index 7738ce7..9df9943 100644 --- a/lib/view/components/search/search_bar.dart +++ b/lib/view/components/search/search_bar.dart @@ -12,7 +12,6 @@ class SearchBar extends StatelessWidget { children: [ TextField( onSubmitted: (text) { - BlocProvider.of(context) .add(GetSearchResults(text)); }, diff --git a/lib/view/screens/history.dart b/lib/view/screens/history.dart index ebaf41f..1f00b69 100644 --- a/lib/view/screens/history.dart +++ b/lib/view/screens/history.dart @@ -1,7 +1,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.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/kanji_result.dart'; +import 'package:jisho_study_tool/models/history/search_string.dart'; +import 'package:jisho_study_tool/view/components/history/kanji_search_item.dart'; +import 'package:jisho_study_tool/view/components/history/search_item.dart'; class HistoryView extends StatelessWidget { @override @@ -13,13 +16,20 @@ class HistoryView extends StatelessWidget { builder: (context, state) { if (state is DatabaseDisconnected) throw DatabaseNotConnectedException(); - return Text( - (state as DatabaseConnected) + return ListView( + children: (state as DatabaseConnected) .database - .box() + .box() .getAll() - .map((e) => e.toString()) - .toString() + .map((e) => SearchItem(e) as Widget) + .toList() + + + (state as DatabaseConnected) + .database + .box() + .getAll() + .map((e) => KanjiSearchItem(e) as Widget) + .toList(), ); }, );