💄 Optimize poll

This commit is contained in:
2025-08-12 22:52:05 +08:00
parent 3a36915140
commit 16987c758e
12 changed files with 653 additions and 332 deletions

View File

@@ -14,17 +14,19 @@ part 'poll_list.g.dart';
@riverpod
class PollListNotifier extends _$PollListNotifier
with CursorPagingNotifierMixin<SnPoll> {
with CursorPagingNotifierMixin<SnPollWithStats> {
static const int _pageSize = 20;
@override
Future<CursorPagingData<SnPoll>> build(String? pubName) {
Future<CursorPagingData<SnPollWithStats>> build(String? pubName) {
// immediately load first page
return fetch(cursor: null);
}
@override
Future<CursorPagingData<SnPoll>> fetch({required String? cursor}) async {
Future<CursorPagingData<SnPollWithStats>> fetch({
required String? cursor,
}) async {
final client = ref.read(apiClientProvider);
final offset = cursor == null ? 0 : int.parse(cursor);
@@ -42,7 +44,7 @@ class PollListNotifier extends _$PollListNotifier
);
final total = int.parse(response.headers.value('X-Total') ?? '0');
final List<dynamic> data = response.data;
final items = data.map((json) => SnPoll.fromJson(json)).toList();
final items = data.map((json) => SnPollWithStats.fromJson(json)).toList();
final hasMore = offset + items.length < total;
final nextCursor = hasMore ? (offset + items.length).toString() : null;
@@ -55,6 +57,13 @@ class PollListNotifier extends _$PollListNotifier
}
}
@riverpod
Future<SnPollWithStats> pollWithStats(Ref ref, String id) async {
final apiClient = ref.watch(apiClientProvider);
final resp = await apiClient.get('/sphere/polls/$id');
return SnPollWithStats.fromJson(resp.data);
}
class CreatorPollListScreen extends HookConsumerWidget {
const CreatorPollListScreen({super.key, required this.pubName});
@@ -64,7 +73,7 @@ class CreatorPollListScreen extends HookConsumerWidget {
final result = await GoRouter.of(
context,
).pushNamed('creatorPollNew', pathParameters: {'name': pubName});
if (result is SnPoll && context.mounted) {
if (result is SnPollWithStats && context.mounted) {
Navigator.of(context).maybePop(result);
}
}
@@ -92,8 +101,11 @@ class CreatorPollListScreen extends HookConsumerWidget {
if (index == widgetCount - 1) {
return endItemView;
}
final poll = data.items[index];
return _CreatorPollItem(poll: poll, pubName: pubName);
final pollWithStats = data.items[index];
return _CreatorPollItem(
pollWithStats: pollWithStats,
pubName: pubName,
);
},
),
),
@@ -106,14 +118,14 @@ class CreatorPollListScreen extends HookConsumerWidget {
class _CreatorPollItem extends StatelessWidget {
final String pubName;
const _CreatorPollItem({required this.poll, required this.pubName});
const _CreatorPollItem({required this.pollWithStats, required this.pubName});
final SnPoll poll;
final SnPollWithStats pollWithStats;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final ended = poll.endedAt;
final ended = pollWithStats.endedAt;
final endedText =
ended == null
? 'No end'
@@ -123,15 +135,16 @@ class _CreatorPollItem extends StatelessWidget {
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
clipBehavior: Clip.antiAlias,
child: ListTile(
title: Text(poll.title ?? 'Untitled poll'),
title: Text(pollWithStats.title ?? 'Untitled poll'),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (poll.description != null && poll.description!.isNotEmpty)
if (pollWithStats.description != null &&
pollWithStats.description!.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
poll.description!,
pollWithStats.description!,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
@@ -139,7 +152,7 @@ class _CreatorPollItem extends StatelessWidget {
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
'Questions: ${poll.questions.length} · Ends: $endedText',
'Questions: ${pollWithStats.questions.length} · Ends: $endedText',
style: theme.textTheme.bodySmall,
),
),
@@ -159,7 +172,7 @@ class _CreatorPollItem extends StatelessWidget {
onTap: () {
GoRouter.of(context).pushNamed(
'creatorPollEdit',
pathParameters: {'name': pubName, 'id': poll.id},
pathParameters: {'name': pubName, 'id': pollWithStats.id},
);
},
),
@@ -170,8 +183,7 @@ class _CreatorPollItem extends StatelessWidget {
context: context,
useRootNavigator: true,
isScrollControlled: true,
builder:
(context) => PollFeedbackSheet(pollId: poll.id, poll: poll),
builder: (context) => PollFeedbackSheet(pollId: pollWithStats.id),
);
},
),

View File

@@ -6,7 +6,7 @@ part of 'poll_list.dart';
// RiverpodGenerator
// **************************************************************************
String _$pollListNotifierHash() => r'd3da24ff6bbb8f35b06d57fc41625dc0312508e4';
String _$pollWithStatsHash() => r'6bb910046ce1e09368f9922dbec52fdc2cc86740';
/// Copied from Dart SDK
class _SystemHash {
@@ -29,11 +29,133 @@ class _SystemHash {
}
}
/// See also [pollWithStats].
@ProviderFor(pollWithStats)
const pollWithStatsProvider = PollWithStatsFamily();
/// See also [pollWithStats].
class PollWithStatsFamily extends Family<AsyncValue<SnPollWithStats>> {
/// See also [pollWithStats].
const PollWithStatsFamily();
/// See also [pollWithStats].
PollWithStatsProvider call(String id) {
return PollWithStatsProvider(id);
}
@override
PollWithStatsProvider getProviderOverride(
covariant PollWithStatsProvider provider,
) {
return call(provider.id);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'pollWithStatsProvider';
}
/// See also [pollWithStats].
class PollWithStatsProvider extends AutoDisposeFutureProvider<SnPollWithStats> {
/// See also [pollWithStats].
PollWithStatsProvider(String id)
: this._internal(
(ref) => pollWithStats(ref as PollWithStatsRef, id),
from: pollWithStatsProvider,
name: r'pollWithStatsProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$pollWithStatsHash,
dependencies: PollWithStatsFamily._dependencies,
allTransitiveDependencies:
PollWithStatsFamily._allTransitiveDependencies,
id: id,
);
PollWithStatsProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.id,
}) : super.internal();
final String id;
@override
Override overrideWith(
FutureOr<SnPollWithStats> Function(PollWithStatsRef provider) create,
) {
return ProviderOverride(
origin: this,
override: PollWithStatsProvider._internal(
(ref) => create(ref as PollWithStatsRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
id: id,
),
);
}
@override
AutoDisposeFutureProviderElement<SnPollWithStats> createElement() {
return _PollWithStatsProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is PollWithStatsProvider && other.id == id;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, id.hashCode);
return _SystemHash.finish(hash);
}
}
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
mixin PollWithStatsRef on AutoDisposeFutureProviderRef<SnPollWithStats> {
/// The parameter `id` of this provider.
String get id;
}
class _PollWithStatsProviderElement
extends AutoDisposeFutureProviderElement<SnPollWithStats>
with PollWithStatsRef {
_PollWithStatsProviderElement(super.provider);
@override
String get id => (origin as PollWithStatsProvider).id;
}
String _$pollListNotifierHash() => r'd5b822e737788be8982f5cb3b501d460441930c1';
abstract class _$PollListNotifier
extends BuildlessAutoDisposeAsyncNotifier<CursorPagingData<SnPoll>> {
extends
BuildlessAutoDisposeAsyncNotifier<CursorPagingData<SnPollWithStats>> {
late final String? pubName;
FutureOr<CursorPagingData<SnPoll>> build(String? pubName);
FutureOr<CursorPagingData<SnPollWithStats>> build(String? pubName);
}
/// See also [PollListNotifier].
@@ -42,7 +164,7 @@ const pollListNotifierProvider = PollListNotifierFamily();
/// See also [PollListNotifier].
class PollListNotifierFamily
extends Family<AsyncValue<CursorPagingData<SnPoll>>> {
extends Family<AsyncValue<CursorPagingData<SnPollWithStats>>> {
/// See also [PollListNotifier].
const PollListNotifierFamily();
@@ -78,7 +200,7 @@ class PollListNotifierProvider
extends
AutoDisposeAsyncNotifierProviderImpl<
PollListNotifier,
CursorPagingData<SnPoll>
CursorPagingData<SnPollWithStats>
> {
/// See also [PollListNotifier].
PollListNotifierProvider(String? pubName)
@@ -109,7 +231,7 @@ class PollListNotifierProvider
final String? pubName;
@override
FutureOr<CursorPagingData<SnPoll>> runNotifierBuild(
FutureOr<CursorPagingData<SnPollWithStats>> runNotifierBuild(
covariant PollListNotifier notifier,
) {
return notifier.build(pubName);
@@ -134,7 +256,7 @@ class PollListNotifierProvider
@override
AutoDisposeAsyncNotifierProviderElement<
PollListNotifier,
CursorPagingData<SnPoll>
CursorPagingData<SnPollWithStats>
>
createElement() {
return _PollListNotifierProviderElement(this);
@@ -157,7 +279,7 @@ class PollListNotifierProvider
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
mixin PollListNotifierRef
on AutoDisposeAsyncNotifierProviderRef<CursorPagingData<SnPoll>> {
on AutoDisposeAsyncNotifierProviderRef<CursorPagingData<SnPollWithStats>> {
/// The parameter `pubName` of this provider.
String? get pubName;
}
@@ -166,7 +288,7 @@ class _PollListNotifierProviderElement
extends
AutoDisposeAsyncNotifierProviderElement<
PollListNotifier,
CursorPagingData<SnPoll>
CursorPagingData<SnPollWithStats>
>
with PollListNotifierRef {
_PollListNotifierProviderElement(super.provider);