245 lines
8.4 KiB
Dart
245 lines
8.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../../../core/di/injection.dart';
|
|
import '../../../core/errors/app_exception.dart';
|
|
import '../../../core/theme/app_colors.dart';
|
|
import '../../../data/models/faq_models.dart';
|
|
import '../../../domain/repositories/faq_repository.dart';
|
|
import '../../../l10n/app_strings.dart';
|
|
import '../../../shared/widgets/pred_card.dart';
|
|
import '../../../shared/widgets/pred_skeleton.dart';
|
|
import '../../../shared/widgets/section_error.dart';
|
|
import '../../welcome/utils/welcome_navigation.dart';
|
|
|
|
class FaqScreen extends StatefulWidget {
|
|
const FaqScreen({super.key, this.initialCategory});
|
|
|
|
/// Optional FAQ category slug (e.g. `game`) from `/faq?category=...`.
|
|
final String? initialCategory;
|
|
|
|
@override
|
|
State<FaqScreen> createState() => _FaqScreenState();
|
|
}
|
|
|
|
class _FaqScreenState extends State<FaqScreen> {
|
|
final _repo = sl<FaqRepository>();
|
|
final _searchController = TextEditingController();
|
|
List<FaqEntryModel> _entries = [];
|
|
bool _loading = true;
|
|
bool _searching = false;
|
|
bool _retrying = false;
|
|
String? _error;
|
|
String? _activeSearch;
|
|
int _loadGen = 0;
|
|
Future<void>? _sameKeyInFlight;
|
|
String? _sameKey;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final cached = _repo.peekLast(category: widget.initialCategory);
|
|
if (cached != null && cached.isNotEmpty) {
|
|
_entries = cached;
|
|
_loading = false;
|
|
}
|
|
_load(category: widget.initialCategory);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_searchController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _load({
|
|
String? search,
|
|
String? category,
|
|
bool force = false,
|
|
}) {
|
|
final cat = category ?? widget.initialCategory;
|
|
final key = '${cat ?? ''}|${search ?? ''}';
|
|
// Coalesce only identical in-flight queries (retry spam); new searches proceed.
|
|
if (_sameKeyInFlight != null && _sameKey == key) return _sameKeyInFlight!;
|
|
|
|
final gen = ++_loadGen;
|
|
final keepVisible = _entries.isNotEmpty;
|
|
final future = () async {
|
|
setState(() {
|
|
_activeSearch = search;
|
|
_retrying = force;
|
|
if (keepVisible) {
|
|
_searching = true;
|
|
if (!force) _error = null;
|
|
} else {
|
|
_loading = true;
|
|
if (!force) _error = null;
|
|
}
|
|
});
|
|
try {
|
|
final entries = await _repo.listEntries(
|
|
search: search,
|
|
category: cat,
|
|
force: force,
|
|
);
|
|
if (!mounted || gen != _loadGen) return;
|
|
setState(() {
|
|
_entries = entries;
|
|
_loading = false;
|
|
_searching = false;
|
|
_retrying = false;
|
|
_error = null;
|
|
});
|
|
} catch (_) {
|
|
if (!mounted || gen != _loadGen) return;
|
|
setState(() {
|
|
_loading = false;
|
|
_searching = false;
|
|
_retrying = false;
|
|
if (_entries.isEmpty) {
|
|
_error = AppException.connectionFailedMessage;
|
|
}
|
|
});
|
|
}
|
|
}();
|
|
_sameKey = key;
|
|
_sameKeyInFlight = future.whenComplete(() {
|
|
if (_sameKey == key) {
|
|
_sameKeyInFlight = null;
|
|
_sameKey = null;
|
|
}
|
|
});
|
|
return _sameKeyInFlight!;
|
|
}
|
|
|
|
void _clearSearch() {
|
|
_searchController.clear();
|
|
_load(category: widget.initialCategory, force: true);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = context.strings;
|
|
return Scaffold(
|
|
key: const Key('faq_screen'),
|
|
appBar: AppBar(
|
|
leading: welcomeBackButton(context),
|
|
title: Text(s.faqTitle),
|
|
),
|
|
body: Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 16, 20, 0),
|
|
child: TextField(
|
|
key: const Key('faq_search'),
|
|
controller: _searchController,
|
|
decoration: InputDecoration(
|
|
hintText: s.searchFaq,
|
|
prefixIcon: const Icon(Icons.search_rounded),
|
|
suffixIcon: IconButton(
|
|
icon: const Icon(Icons.clear_rounded),
|
|
onPressed: _clearSearch,
|
|
),
|
|
),
|
|
onSubmitted: (q) => _load(
|
|
search: q.trim().isEmpty ? null : q.trim(),
|
|
category: widget.initialCategory,
|
|
force: true,
|
|
),
|
|
),
|
|
),
|
|
if (_searching)
|
|
const LinearProgressIndicator(
|
|
minHeight: 2,
|
|
color: AppColors.primary,
|
|
),
|
|
Expanded(
|
|
child: RefreshIndicator(
|
|
onRefresh: () => _load(
|
|
search: _activeSearch,
|
|
category: widget.initialCategory,
|
|
force: true,
|
|
),
|
|
child: _error != null && _entries.isEmpty
|
|
? ListView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.all(20),
|
|
children: [
|
|
SectionError(
|
|
message: _error!,
|
|
retrying: _retrying,
|
|
onRetry: () => _load(
|
|
search: _activeSearch,
|
|
category: widget.initialCategory,
|
|
force: true,
|
|
),
|
|
),
|
|
],
|
|
)
|
|
: _loading && _entries.isEmpty
|
|
? ListView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.all(20),
|
|
children: const [
|
|
PredSkeletonCard(height: 72),
|
|
SizedBox(height: 10),
|
|
PredSkeletonCard(height: 72),
|
|
SizedBox(height: 10),
|
|
PredSkeletonCard(height: 72),
|
|
],
|
|
)
|
|
: _entries.isEmpty
|
|
? ListView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.all(20),
|
|
children: [
|
|
SizedBox(
|
|
height: 160,
|
|
child: Center(child: Text(s.noFaqResults)),
|
|
),
|
|
],
|
|
)
|
|
: ListView.builder(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.all(20),
|
|
itemCount: _entries.length,
|
|
itemBuilder: (context, index) {
|
|
final entry = _entries[index];
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: 10),
|
|
child: PredCard(
|
|
child: ExpansionTile(
|
|
title: Text(
|
|
entry.question,
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
subtitle: entry.category.isNotEmpty
|
|
? Text(entry.category)
|
|
: null,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(
|
|
16,
|
|
0,
|
|
16,
|
|
16,
|
|
),
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Text(entry.answer),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|