Poll editor basic

This commit is contained in:
2025-08-05 17:40:47 +08:00
parent a706f127b6
commit d345c00e84
8 changed files with 2539 additions and 0 deletions

View File

@@ -380,6 +380,23 @@ class CreatorHubScreen extends HookConsumerWidget {
);
},
),
ListTile(
minTileHeight: 48,
title: const Text('Polls'),
trailing: const Icon(Symbols.chevron_right),
leading: const Icon(Symbols.poll),
contentPadding: const EdgeInsets.symmetric(
horizontal: 24,
),
onTap: () {
context.pushNamed(
'creatorPolls',
pathParameters: {
'name': currentPublisher.value!.name,
},
);
},
),
ListTile(
minTileHeight: 48,
title: Text('publisherMembers').tr(),

View File

@@ -0,0 +1,175 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:island/models/poll.dart';
import 'package:island/pods/network.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:riverpod_paging_utils/riverpod_paging_utils.dart';
part 'poll_list.g.dart';
@riverpod
class PollListNotifier extends _$PollListNotifier
with CursorPagingNotifierMixin<SnPoll> {
static const int _pageSize = 20;
@override
Future<CursorPagingData<SnPoll>> build(String? pubName) {
// immediately load first page
return fetch(cursor: null);
}
@override
Future<CursorPagingData<SnPoll>> fetch({required String? cursor}) async {
final client = ref.read(apiClientProvider);
final offset = cursor == null ? 0 : int.parse(cursor);
// read the current family argument passed to provider
final currentPub = pubName;
final queryParams = {
'offset': offset,
'take': _pageSize,
if (currentPub != null) 'pub': currentPub,
};
final response = await client.get(
'/sphere/polls/me',
queryParameters: queryParams,
);
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 hasMore = offset + items.length < total;
final nextCursor = hasMore ? (offset + items.length).toString() : null;
return CursorPagingData(
items: items,
hasMore: hasMore,
nextCursor: nextCursor,
);
}
}
class CreatorPollListScreen extends HookConsumerWidget {
const CreatorPollListScreen({super.key, required this.pubName});
final String pubName;
Future<void> _createPoll(BuildContext context) async {
// Use named route defined in router with :name param (creatorPollNew)
final result = await GoRouter.of(
context,
).pushNamed('creatorPollNew', pathParameters: {'name': pubName});
// If PollEditorScreen returns a created SnPoll on success, pop back with it
if (result is SnPoll && context.mounted) {
Navigator.of(context).maybePop(result);
}
}
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(title: const Text('Polls')),
floatingActionButton: FloatingActionButton(
onPressed: () => _createPoll(context),
child: const Icon(Icons.add),
),
body: RefreshIndicator(
onRefresh: () => ref.refresh(pollListNotifierProvider(pubName).future),
child: CustomScrollView(
slivers: [
PagingHelperSliverView(
provider: pollListNotifierProvider(pubName),
futureRefreshable: pollListNotifierProvider(pubName).future,
notifierRefreshable: pollListNotifierProvider(pubName).notifier,
contentBuilder:
(data, widgetCount, endItemView) => SliverList.builder(
itemCount: widgetCount,
itemBuilder: (context, index) {
if (index == widgetCount - 1) {
return endItemView;
}
final poll = data.items[index];
return _CreatorPollItem(poll: poll);
},
),
),
],
),
),
);
}
}
class _CreatorPollItem extends StatelessWidget {
const _CreatorPollItem({required this.poll});
final SnPoll poll;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final ended = poll.endedAt;
final endedText =
ended == null
? 'No end'
: MaterialLocalizations.of(context).formatFullDate(ended);
return Card(
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
clipBehavior: Clip.antiAlias,
child: ListTile(
title: Text(poll.title ?? 'Untitled poll'),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (poll.description != null && poll.description!.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
poll.description!,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
'Questions: ${poll.questions.length} · Ends: $endedText',
style: theme.textTheme.bodySmall,
),
),
],
),
trailing: PopupMenuButton<String>(
onSelected: (v) {
switch (v) {
case 'edit':
// Use global router helper if desired
// context.push('/creators/${poll.publisher?.name ?? ''}/polls/${poll.id}/edit');
Navigator.of(context).pushNamed(
'creatorPollEdit',
arguments: {
'name': poll.publisher?.name ?? '',
'id': poll.id,
},
);
break;
}
},
itemBuilder:
(context) => [
const PopupMenuItem(value: 'edit', child: Text('Edit')),
],
),
onTap: () {
// Open editor for edit
// Navigator push by path to keep consistency with rest of app:
// Note: pub name string may be required in route; when absent, route may need query or pick later.
// For safety, just do nothing if no publisher in list item.
},
),
);
}
}

View File

@@ -0,0 +1,179 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'poll_list.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$pollListNotifierHash() => r'd3da24ff6bbb8f35b06d57fc41625dc0312508e4';
/// Copied from Dart SDK
class _SystemHash {
_SystemHash._();
static int combine(int hash, int value) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + value);
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
static int finish(int hash) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
// ignore: parameter_assignments
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
}
abstract class _$PollListNotifier
extends BuildlessAutoDisposeAsyncNotifier<CursorPagingData<SnPoll>> {
late final String? pubName;
FutureOr<CursorPagingData<SnPoll>> build(String? pubName);
}
/// See also [PollListNotifier].
@ProviderFor(PollListNotifier)
const pollListNotifierProvider = PollListNotifierFamily();
/// See also [PollListNotifier].
class PollListNotifierFamily
extends Family<AsyncValue<CursorPagingData<SnPoll>>> {
/// See also [PollListNotifier].
const PollListNotifierFamily();
/// See also [PollListNotifier].
PollListNotifierProvider call(String? pubName) {
return PollListNotifierProvider(pubName);
}
@override
PollListNotifierProvider getProviderOverride(
covariant PollListNotifierProvider provider,
) {
return call(provider.pubName);
}
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'pollListNotifierProvider';
}
/// See also [PollListNotifier].
class PollListNotifierProvider
extends
AutoDisposeAsyncNotifierProviderImpl<
PollListNotifier,
CursorPagingData<SnPoll>
> {
/// See also [PollListNotifier].
PollListNotifierProvider(String? pubName)
: this._internal(
() => PollListNotifier()..pubName = pubName,
from: pollListNotifierProvider,
name: r'pollListNotifierProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$pollListNotifierHash,
dependencies: PollListNotifierFamily._dependencies,
allTransitiveDependencies:
PollListNotifierFamily._allTransitiveDependencies,
pubName: pubName,
);
PollListNotifierProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.pubName,
}) : super.internal();
final String? pubName;
@override
FutureOr<CursorPagingData<SnPoll>> runNotifierBuild(
covariant PollListNotifier notifier,
) {
return notifier.build(pubName);
}
@override
Override overrideWith(PollListNotifier Function() create) {
return ProviderOverride(
origin: this,
override: PollListNotifierProvider._internal(
() => create()..pubName = pubName,
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
pubName: pubName,
),
);
}
@override
AutoDisposeAsyncNotifierProviderElement<
PollListNotifier,
CursorPagingData<SnPoll>
>
createElement() {
return _PollListNotifierProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is PollListNotifierProvider && other.pubName == pubName;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, pubName.hashCode);
return _SystemHash.finish(hash);
}
}
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
mixin PollListNotifierRef
on AutoDisposeAsyncNotifierProviderRef<CursorPagingData<SnPoll>> {
/// The parameter `pubName` of this provider.
String? get pubName;
}
class _PollListNotifierProviderElement
extends
AutoDisposeAsyncNotifierProviderElement<
PollListNotifier,
CursorPagingData<SnPoll>
>
with PollListNotifierRef {
_PollListNotifierProviderElement(super.provider);
@override
String? get pubName => (origin as PollListNotifierProvider).pubName;
}
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package

File diff suppressed because it is too large Load Diff