mirror of
https://github.com/krille-chan/fluffychat
synced 2024-09-10 10:25:11 +00:00
chore: Follow up search
This commit is contained in:
parent
67f047ecfc
commit
d819128881
5 changed files with 105 additions and 45 deletions
|
@ -28,11 +28,12 @@ class ChatSearchFilesTab extends StatelessWidget {
|
|||
return StreamBuilder(
|
||||
stream: searchStream,
|
||||
builder: (context, snapshot) {
|
||||
if (searchStream == null) {
|
||||
final events = snapshot.data?.$1;
|
||||
if (searchStream == null || events == null) {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.search_outlined, size: 64),
|
||||
const CircularProgressIndicator.adaptive(strokeWidth: 2),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
L10n.of(context)!.searchIn(
|
||||
|
@ -44,10 +45,6 @@ class ChatSearchFilesTab extends StatelessWidget {
|
|||
],
|
||||
);
|
||||
}
|
||||
final events = snapshot.data?.$1
|
||||
.where((event) => event.messageType == MessageTypes.File)
|
||||
.toList() ??
|
||||
[];
|
||||
|
||||
if (events.isEmpty) {
|
||||
return Column(
|
||||
|
|
|
@ -27,11 +27,12 @@ class ChatSearchImagesTab extends StatelessWidget {
|
|||
return StreamBuilder(
|
||||
stream: searchStream,
|
||||
builder: (context, snapshot) {
|
||||
if (searchStream == null) {
|
||||
final events = snapshot.data?.$1;
|
||||
if (searchStream == null || events == null) {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.search_outlined, size: 64),
|
||||
const CircularProgressIndicator.adaptive(strokeWidth: 2),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
L10n.of(context)!.searchIn(
|
||||
|
@ -43,10 +44,6 @@ class ChatSearchImagesTab extends StatelessWidget {
|
|||
],
|
||||
);
|
||||
}
|
||||
final events = snapshot.data?.$1
|
||||
.where((event) => event.messageType == MessageTypes.Image)
|
||||
.toList() ??
|
||||
[];
|
||||
if (events.isEmpty) {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
|
|
|
@ -49,15 +49,7 @@ class ChatSearchMessageTab extends StatelessWidget {
|
|||
],
|
||||
);
|
||||
}
|
||||
final events = snapshot.data?.$1
|
||||
.where(
|
||||
(event) => {
|
||||
MessageTypes.Text,
|
||||
MessageTypes.Notice,
|
||||
}.contains(event.messageType),
|
||||
)
|
||||
.toList() ??
|
||||
[];
|
||||
final events = snapshot.data?.$1 ?? [];
|
||||
|
||||
return SelectionArea(
|
||||
child: ListView.separated(
|
||||
|
@ -166,14 +158,17 @@ class _MessageSearchResultListTile extends StatelessWidget {
|
|||
decorationColor: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
onOpen: (url) => UrlLauncher(context, url.url).launchUrl(),
|
||||
text: event.calcLocalizedBodyFallback(
|
||||
plaintextBody: true,
|
||||
removeMarkdown: true,
|
||||
MatrixLocals(
|
||||
L10n.of(context)!,
|
||||
),
|
||||
),
|
||||
maxLines: 4,
|
||||
text: event
|
||||
.calcLocalizedBodyFallback(
|
||||
plaintextBody: true,
|
||||
removeMarkdown: true,
|
||||
MatrixLocals(
|
||||
L10n.of(context)!,
|
||||
),
|
||||
)
|
||||
.trim(),
|
||||
maxLines: 7,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: const Icon(
|
||||
|
|
|
@ -25,9 +25,11 @@ class ChatSearchController extends State<ChatSearchPage>
|
|||
Timeline? timeline;
|
||||
|
||||
Stream<(List<Event>, String?)>? searchStream;
|
||||
Stream<(List<Event>, String?)>? galleryStream;
|
||||
Stream<(List<Event>, String?)>? fileStream;
|
||||
|
||||
void restartSearch() {
|
||||
if (tabController.index == 0 && searchController.text.isEmpty) {
|
||||
if (searchController.text.isEmpty) {
|
||||
setState(() {
|
||||
searchStream = null;
|
||||
});
|
||||
|
@ -37,11 +39,11 @@ class ChatSearchController extends State<ChatSearchPage>
|
|||
searchStream = const Stream.empty();
|
||||
});
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
||||
startSearch();
|
||||
startMessageSearch();
|
||||
});
|
||||
}
|
||||
|
||||
void startSearch({
|
||||
void startMessageSearch({
|
||||
String? prevBatch,
|
||||
List<Event>? previousSearchResult,
|
||||
}) async {
|
||||
|
@ -54,12 +56,7 @@ class ChatSearchController extends State<ChatSearchPage>
|
|||
setState(() {
|
||||
searchStream = timeline
|
||||
.startSearch(
|
||||
searchTerm: tabController.index == 0 ? searchController.text : null,
|
||||
searchFunc: switch (tabController.index) {
|
||||
1 => (event) => event.messageType == MessageTypes.Image,
|
||||
2 => (event) => event.messageType == MessageTypes.File,
|
||||
int() => null,
|
||||
},
|
||||
searchTerm: searchController.text,
|
||||
prevBatch: prevBatch,
|
||||
requestHistoryCount: 1000,
|
||||
limit: 32,
|
||||
|
@ -77,16 +74,90 @@ class ChatSearchController extends State<ChatSearchPage>
|
|||
});
|
||||
}
|
||||
|
||||
void startGallerySearch({
|
||||
String? prevBatch,
|
||||
List<Event>? previousSearchResult,
|
||||
}) async {
|
||||
final timeline = this.timeline ??= await room!.getTimeline();
|
||||
|
||||
setState(() {
|
||||
galleryStream = timeline
|
||||
.startSearch(
|
||||
searchFunc: (event) =>
|
||||
event.messageType == MessageTypes.File ||
|
||||
(event.messageType == MessageTypes.Audio &&
|
||||
!event.content.containsKey('org.matrix.msc3245.voice')),
|
||||
prevBatch: prevBatch,
|
||||
requestHistoryCount: 1000,
|
||||
limit: 32,
|
||||
)
|
||||
.map(
|
||||
(result) => (
|
||||
[
|
||||
if (previousSearchResult != null) ...previousSearchResult,
|
||||
...result.$1,
|
||||
],
|
||||
result.$2,
|
||||
),
|
||||
)
|
||||
.asBroadcastStream();
|
||||
});
|
||||
}
|
||||
|
||||
void startFileSearch({
|
||||
String? prevBatch,
|
||||
List<Event>? previousSearchResult,
|
||||
}) async {
|
||||
final timeline = this.timeline ??= await room!.getTimeline();
|
||||
|
||||
setState(() {
|
||||
fileStream = timeline
|
||||
.startSearch(
|
||||
searchFunc: (event) => {
|
||||
MessageTypes.Image,
|
||||
MessageTypes.Video,
|
||||
}.contains(event.messageType),
|
||||
prevBatch: prevBatch,
|
||||
requestHistoryCount: 1000,
|
||||
limit: 32,
|
||||
)
|
||||
.map(
|
||||
(result) => (
|
||||
[
|
||||
if (previousSearchResult != null) ...previousSearchResult,
|
||||
...result.$1,
|
||||
],
|
||||
result.$2,
|
||||
),
|
||||
)
|
||||
.asBroadcastStream();
|
||||
});
|
||||
}
|
||||
|
||||
void _onTabChanged() {
|
||||
switch (tabController.index) {
|
||||
case 1:
|
||||
startGallerySearch();
|
||||
break;
|
||||
case 2:
|
||||
startFileSearch();
|
||||
break;
|
||||
default:
|
||||
restartSearch();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
tabController = TabController(initialIndex: 0, length: 3, vsync: this);
|
||||
tabController.addListener(restartSearch);
|
||||
tabController.addListener(_onTabChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
tabController.removeListener(restartSearch);
|
||||
tabController.removeListener(_onTabChanged);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
|
|
|
@ -81,18 +81,18 @@ class ChatSearchView extends StatelessWidget {
|
|||
ChatSearchMessageTab(
|
||||
searchQuery: controller.searchController.text,
|
||||
room: room,
|
||||
startSearch: controller.startSearch,
|
||||
startSearch: controller.startMessageSearch,
|
||||
searchStream: controller.searchStream,
|
||||
),
|
||||
ChatSearchImagesTab(
|
||||
room: room,
|
||||
startSearch: controller.startSearch,
|
||||
searchStream: controller.searchStream,
|
||||
startSearch: controller.startGallerySearch,
|
||||
searchStream: controller.galleryStream,
|
||||
),
|
||||
ChatSearchFilesTab(
|
||||
room: room,
|
||||
startSearch: controller.startSearch,
|
||||
searchStream: controller.searchStream,
|
||||
startSearch: controller.startFileSearch,
|
||||
searchStream: controller.fileStream,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
Loading…
Reference in a new issue