Skip to content

Commit

Permalink
fix: rank online higher, fix results persistence (#705)
Browse files Browse the repository at this point in the history
* fix: rank online higher

* fix flash of unstyled content

* make stateful to persist
  • Loading branch information
kevmo314 authored Sep 10, 2022
1 parent 34b2dc3 commit 23661c6
Showing 1 changed file with 55 additions and 31 deletions.
86 changes: 55 additions & 31 deletions lib/components/channel_search_results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ Future<List<SearchResult>> fastSearch() async {
}).toList();
}

Stream<List<SearchResult>> search(String query, bool isShowOnlyOnline) async* {
final fast = await fastSearch();
final fastFiltered = fast.where((result) =>
result.displayName.toLowerCase().contains(query.toLowerCase()));
final fastRanked = [
...fastFiltered.where((element) => element.isOnline),
...fastFiltered.where((element) => !element.isOnline)
].take(5);
yield fastRanked.toList();
final slow = await _search(query).then((result) {
return (result.data as List<dynamic>)
.map((data) => SearchResult(
channelId: data['channelId'],
provider: data['provider'],
displayName: data['displayName'],
isOnline: data['isOnline'],
imageUrl: Uri.parse(data['imageUrl']),
title: data['title'],
isPromoted: false))
.toList();
});
final slowFiltered = slow.where((result) =>
!fastFiltered.any((element) => element.channelId == result.channelId) &&
(!isShowOnlyOnline || result.isOnline));
yield [...fastRanked, ...slowFiltered];
}

final url = Uri.https('chat.rtirl.com', '/auth/twitch/redirect');

class SearchResult {
Expand All @@ -56,54 +83,51 @@ class SearchResult {
required this.isPromoted});
}

class ChannelSearchResultsWidget extends StatelessWidget {
class ChannelSearchResultsWidget extends StatefulWidget {
final String query;
final bool isShowOnlyOnline;
final Function(Channel) onChannelSelect;
final ScrollController? controller;
final Future<List<SearchResult>> _fastSearch = fastSearch();
final bool isShowOnlyOnline;

ChannelSearchResultsWidget(
const ChannelSearchResultsWidget(
{Key? key,
required this.query,
required this.onChannelSelect,
required this.isShowOnlyOnline,
this.controller})
: super(key: key);

Stream<List<SearchResult>> search() async* {
final fast = await _fastSearch;
final fastFiltered = fast
.where((result) =>
result.displayName.toLowerCase().contains(query.toLowerCase()) &&
(!isShowOnlyOnline || result.isOnline))
.take(5);
yield fastFiltered.toList();
final slow = await _search(query).then((result) {
return (result.data as List<dynamic>)
.map((data) => SearchResult(
channelId: data['channelId'],
provider: data['provider'],
displayName: data['displayName'],
isOnline: data['isOnline'],
imageUrl: Uri.parse(data['imageUrl']),
title: data['title'],
isPromoted: false))
.toList();
});
final slowFiltered = slow.where((result) =>
!fastFiltered.any((element) => element.channelId == result.channelId) &&
(!isShowOnlyOnline || result.isOnline));
yield [...fastFiltered, ...slowFiltered];
@override
State<ChannelSearchResultsWidget> createState() =>
_ChannelSearchResultsWidgetState();
}

class _ChannelSearchResultsWidgetState
extends State<ChannelSearchResultsWidget> {
late Stream<List<SearchResult>> _results;

@override
void initState() {
super.initState();
_results = search(widget.query, widget.isShowOnlyOnline);
}

@override
void didUpdateWidget(covariant ChannelSearchResultsWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.query != widget.query ||
oldWidget.isShowOnlyOnline != widget.isShowOnlyOnline) {
_results = search(widget.query, widget.isShowOnlyOnline);
}
}

@override
Widget build(BuildContext context) {
return ListView(
controller: controller,
controller: widget.controller,
children: [
StreamBuilder<List<SearchResult>>(
stream: search(),
stream: _results,
builder: (context, snapshot) {
return Column(
children: (snapshot.data ?? [])
Expand Down Expand Up @@ -145,7 +169,7 @@ class ChannelSearchResultsWidget extends StatelessWidget {
title: Text(result.displayName),
subtitle: Text(result.title),
onTap: () {
onChannelSelect(Channel(
widget.onChannelSelect(Channel(
"twitch",
result.channelId,
result.displayName,
Expand Down

0 comments on commit 23661c6

Please sign in to comment.