Skip to content

Commit

Permalink
add ProcessTextIntentHandler to avoid using Future.microtask + disabl…
Browse files Browse the repository at this point in the history
…e autofocus
  • Loading branch information
3nws committed May 6, 2024
1 parent fbc4c40 commit 39ab47c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
35 changes: 35 additions & 0 deletions lib/packages/process_text_intent_handler.dart
Original file line number Diff line number Diff line change
@@ -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();
}
}
});
}
}
35 changes: 3 additions & 32 deletions lib/screens/search/search_screen.dart
Original file line number Diff line number Diff line change
@@ -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});
Expand All @@ -33,33 +29,17 @@ class _SearchScreenState extends State<SearchScreen>
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<void> _handleIncomingTextIntent() async {
const platform = MethodChannel("io.github.petlyh.jsdict");

final Map<dynamic, dynamic>? 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();
Expand All @@ -73,15 +53,6 @@ class _SearchScreenState extends State<SearchScreen>
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,
Expand All @@ -96,7 +67,7 @@ class _SearchScreenState extends State<SearchScreen>
focusNode: _searchFocusNode,
controller: searchController,
onSubmitted: (text) => queryProvider.addToHistoryAndSearch(text),
autofocus: true,
autofocus: false,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
border: InputBorder.none,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 39ab47c

Please sign in to comment.