feat: add HTML providers for Realbooru and Paheal, hidden posts screen, and UI/search enhancements
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../../backend/backend.dart';
|
||||
@@ -8,8 +10,12 @@ final searchControllerProvider =
|
||||
AsyncNotifierProvider<SearchController, SearchState>(SearchController.new);
|
||||
|
||||
class SearchController extends AsyncNotifier<SearchState> {
|
||||
Timer? _suggestionDebounce;
|
||||
int _suggestionRequestId = 0;
|
||||
|
||||
@override
|
||||
Future<SearchState> build() async {
|
||||
ref.onDispose(() => _suggestionDebounce?.cancel());
|
||||
final recent = await _recent();
|
||||
return SearchState(recent: recent);
|
||||
}
|
||||
@@ -17,18 +23,42 @@ class SearchController extends AsyncNotifier<SearchState> {
|
||||
Future<void> updateQuery(String query) async {
|
||||
final lastToken =
|
||||
query.trim().isEmpty ? '' : query.trim().split(RegExp(r'\s+')).last;
|
||||
final suggestionsResult = await ref
|
||||
.read(searchServiceProvider)
|
||||
.autocompleteDetailed(lastToken, limit: 16);
|
||||
final suggestions = suggestionsResult is Success<List<TagSuggestion>>
|
||||
? suggestionsResult.data
|
||||
: <TagSuggestion>[];
|
||||
final requestId = ++_suggestionRequestId;
|
||||
_suggestionDebounce?.cancel();
|
||||
if (lastToken.isEmpty) {
|
||||
state = AsyncData(
|
||||
(state.value ?? const SearchState()).copyWith(
|
||||
query: query,
|
||||
suggestions: const [],
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
state = AsyncData(
|
||||
(state.value ?? const SearchState()).copyWith(
|
||||
query: query,
|
||||
suggestions: suggestions,
|
||||
),
|
||||
);
|
||||
_suggestionDebounce = Timer(const Duration(milliseconds: 280), () async {
|
||||
final suggestionsResult = await ref
|
||||
.read(searchServiceProvider)
|
||||
.autocompleteDetailed(lastToken, limit: 16);
|
||||
if (requestId != _suggestionRequestId) return;
|
||||
final normalizedToken = lastToken.trim().toLowerCase();
|
||||
final suggestions = suggestionsResult is Success<List<TagSuggestion>>
|
||||
? suggestionsResult.data
|
||||
.where((item) => item.name.toLowerCase().startsWith(
|
||||
normalizedToken,
|
||||
))
|
||||
.toList(growable: false)
|
||||
: <TagSuggestion>[];
|
||||
state = AsyncData(
|
||||
(state.value ?? const SearchState()).copyWith(
|
||||
query: query,
|
||||
suggestions: suggestions,
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> clearHistory() async {
|
||||
|
||||
@@ -2,24 +2,45 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../backend/backend.dart';
|
||||
|
||||
class RecentSearches extends StatelessWidget {
|
||||
class RecentSearches extends StatefulWidget {
|
||||
const RecentSearches({required this.items, required this.onTap, super.key});
|
||||
|
||||
final List<SearchHistory> items;
|
||||
final ValueChanged<String> onTap;
|
||||
|
||||
@override
|
||||
State<RecentSearches> createState() => _RecentSearchesState();
|
||||
}
|
||||
|
||||
class _RecentSearchesState extends State<RecentSearches> {
|
||||
bool _expanded = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (items.isEmpty) return const Text('No recent searches');
|
||||
if (widget.items.isEmpty) return const Text('No recent searches');
|
||||
final visibleItems =
|
||||
_expanded ? widget.items : widget.items.take(30).toList();
|
||||
return Column(
|
||||
children: [
|
||||
for (final item in items)
|
||||
for (final item in visibleItems)
|
||||
ListTile(
|
||||
leading: const Icon(Icons.history_rounded),
|
||||
title: Text(item.query),
|
||||
subtitle:
|
||||
Text('${item.tags.join(' ')} · ${item.resultCount} results'),
|
||||
onTap: () => onTap(item.query),
|
||||
Text('${item.tags.join(' ')} - ${item.resultCount} results'),
|
||||
onTap: () => widget.onTap(item.query),
|
||||
),
|
||||
if (widget.items.length > 30)
|
||||
TextButton.icon(
|
||||
onPressed: () => setState(() => _expanded = !_expanded),
|
||||
icon: Icon(
|
||||
_expanded ? Icons.unfold_less_rounded : Icons.unfold_more_rounded,
|
||||
),
|
||||
label: Text(
|
||||
_expanded
|
||||
? 'Show latest 30'
|
||||
: 'Show all ${widget.items.length} searches',
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user