From 39ab47c0d5983f5f7fd47dc632dcbacd657b2996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enes=20Kurbeto=C4=9Flu?= Date: Mon, 6 May 2024 16:55:08 +0300 Subject: [PATCH] add ProcessTextIntentHandler to avoid using Future.microtask + disable autofocus --- lib/packages/process_text_intent_handler.dart | 35 +++++++++++++++++++ lib/screens/search/search_screen.dart | 35 ++----------------- pubspec.yaml | 2 +- 3 files changed, 39 insertions(+), 33 deletions(-) create mode 100644 lib/packages/process_text_intent_handler.dart diff --git a/lib/packages/process_text_intent_handler.dart b/lib/packages/process_text_intent_handler.dart new file mode 100644 index 00000000..b7a70e2b --- /dev/null +++ b/lib/packages/process_text_intent_handler.dart @@ -0,0 +1,35 @@ +import "package:flutter/material.dart"; +import "package:flutter/services.dart"; +import "package:jsdict/packages/is_kanji.dart"; +import "package:jsdict/packages/navigation.dart"; +import "package:jsdict/providers/query_provider.dart"; +import "package:jsdict/screens/kanji_details/kanji_details_screen.dart"; + +class ProcessTextIntentHandler { + final BuildContext context; + final TabController tabController; + + ProcessTextIntentHandler(this.context, this.tabController) { + const platform = MethodChannel("io.github.petlyh.jsdict"); + + platform.invokeMethod("getIntent").then((intentMap) { + if (intentMap != null && + intentMap["action"] == "android.intent.action.PROCESS_TEXT") { + final String incomingText = intentMap["text"] as String; + + if (!context.mounted) { + return; + } + + tabController.index = 0; + QueryProvider.of(context).query = incomingText; + QueryProvider.of(context).addToHistoryAndSearch(incomingText); + popAll(context); + + if (incomingText.length == 1 && isKanji(incomingText)) { + pushScreen(context, KanjiDetailsScreen.id(incomingText)).call(); + } + } + }); + } +} diff --git a/lib/screens/search/search_screen.dart b/lib/screens/search/search_screen.dart index 8965b071..c03fb0ac 100644 --- a/lib/screens/search/search_screen.dart +++ b/lib/screens/search/search_screen.dart @@ -1,21 +1,17 @@ -import "dart:async"; - import "package:flutter/material.dart"; import "package:jsdict/jp_text.dart"; import "package:jsdict/models/models.dart"; -import "package:jsdict/packages/is_kanji.dart"; import "package:jsdict/packages/link_handler.dart"; import "package:jsdict/packages/navigation.dart"; +import "package:jsdict/packages/process_text_intent_handler.dart"; import "package:jsdict/packages/share_intent_handler.dart"; import "package:jsdict/providers/query_provider.dart"; -import "package:jsdict/screens/kanji_details/kanji_details_screen.dart"; import "package:jsdict/screens/search/result_page.dart"; import "package:jsdict/screens/search_options/history_selection_screen.dart"; import "package:jsdict/screens/search_options/radical_search_screen.dart"; import "package:jsdict/screens/search_options/tag_selection_screen.dart"; import "package:jsdict/screens/settings_screen.dart"; import "package:provider/provider.dart"; -import "package:flutter/services.dart"; class SearchScreen extends StatefulWidget { const SearchScreen({super.key}); @@ -33,33 +29,17 @@ class _SearchScreenState extends State late LinkHandler _linkHandler; late ShareIntentHandler _shareIntentHandler; late FocusNode _searchFocusNode; - String incomingText = ""; @override void initState() { super.initState(); - _handleIncomingTextIntent(); _tabController = TabController(vsync: this, length: 4); _linkHandler = LinkHandler(context, _tabController); _shareIntentHandler = ShareIntentHandler(context, _tabController); + ProcessTextIntentHandler(context, _tabController); _searchFocusNode = FocusNode(); } - Future _handleIncomingTextIntent() async { - const platform = MethodChannel("io.github.petlyh.jsdict"); - - final Map? intentMap = - await platform.invokeMethod("getIntent"); - - if (intentMap != null && - intentMap["action"] == "android.intent.action.PROCESS_TEXT") { - final dynamic selectedText = intentMap["text"]; - setState(() { - incomingText = selectedText as String; - }); - } - } - @override void dispose() { _tabController.dispose(); @@ -73,15 +53,6 @@ class _SearchScreenState extends State Widget build(BuildContext context) { final queryProvider = QueryProvider.of(context); final searchController = queryProvider.searchController; - if (incomingText != "") { - Future.microtask(() async { - queryProvider.query = incomingText; - queryProvider.addToHistoryAndSearch(incomingText); - if (incomingText.length == 1 && isKanji(incomingText)) { - pushScreen(context, KanjiDetailsScreen.id(incomingText)).call(); - } - }); - } return Scaffold( resizeToAvoidBottomInset: false, @@ -96,7 +67,7 @@ class _SearchScreenState extends State focusNode: _searchFocusNode, controller: searchController, onSubmitted: (text) => queryProvider.addToHistoryAndSearch(text), - autofocus: true, + autofocus: false, decoration: InputDecoration( prefixIcon: const Icon(Icons.search), border: InputBorder.none, diff --git a/pubspec.yaml b/pubspec.yaml index d667a89e..a7386159 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: jsdict description: A new Flutter project. publish_to: 'none' -version: 1.2.7+18 +version: 1.2.7+19 environment: sdk: '>=3.3.0 <4.0.0'