-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
252 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,19 @@ | ||
import "package:flutter/material.dart"; | ||
import "package:flutter_riverpod/flutter_riverpod.dart" hide Provider; | ||
import "package:jsdict/packages/remove_tags.dart"; | ||
import "package:provider/provider.dart"; | ||
|
||
class QueryProvider extends ChangeNotifier { | ||
static QueryProvider of(BuildContext context) { | ||
return Provider.of<QueryProvider>(context, listen: false); | ||
} | ||
|
||
TextEditingController searchController = TextEditingController(); | ||
|
||
String _query = ""; | ||
String get query => _query; | ||
final queryProvider = | ||
NotifierProvider<QueryController, String>(QueryController.new); | ||
|
||
set query(String text) { | ||
searchController.text = text; | ||
updateQuery(); | ||
} | ||
|
||
void sanitizeText() { | ||
searchController.text = removeTypeTags(searchController.text) | ||
.trim() | ||
.replaceAll(RegExp(r"\s+"), " "); | ||
} | ||
|
||
void updateQuery() { | ||
sanitizeText(); | ||
_query = searchController.text; | ||
notifyListeners(); | ||
} | ||
|
||
void updateQueryIfChanged() { | ||
if (_query != searchController.text) { | ||
updateQuery(); | ||
} | ||
} | ||
|
||
void addTag(String tag) { | ||
sanitizeText(); | ||
if (searchController.text.isNotEmpty) { | ||
searchController.text += " "; | ||
} | ||
searchController.text += "#$tag"; | ||
} | ||
|
||
void clearTags() { | ||
searchController.text = removeTags(searchController.text); | ||
sanitizeText(); | ||
} | ||
|
||
void insertText(String text) { | ||
final selection = searchController.selection; | ||
final selectionStart = selection.baseOffset; | ||
class QueryController extends Notifier<String> { | ||
@override | ||
String build() => ""; | ||
|
||
if (selectionStart == -1) { | ||
searchController.text += text; | ||
return; | ||
void update(String newQuery) { | ||
if (state != newQuery) { | ||
state = _sanitizeQuery(newQuery); | ||
} | ||
|
||
final newText = searchController.text | ||
.replaceRange(selectionStart, selection.extentOffset, text); | ||
searchController.text = newText; | ||
searchController.selection = | ||
TextSelection.collapsed(offset: selectionStart + 1); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
searchController.dispose(); | ||
super.dispose(); | ||
} | ||
String _sanitizeQuery(String query) => | ||
removeTypeTags(query).trim().replaceAll(RegExp(r"\s+"), " "); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.