✨ Poll feedback
This commit is contained in:
235
lib/widgets/poll/poll_feedback.dart
Normal file
235
lib/widgets/poll/poll_feedback.dart
Normal file
@@ -0,0 +1,235 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/poll.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
import 'package:island/widgets/content/sheet.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:riverpod_paging_utils/riverpod_paging_utils.dart';
|
||||
import 'package:styled_widget/styled_widget.dart';
|
||||
|
||||
part 'poll_feedback.g.dart';
|
||||
|
||||
@riverpod
|
||||
class PollFeedbackNotifier extends _$PollFeedbackNotifier
|
||||
with CursorPagingNotifierMixin<SnPollAnswer> {
|
||||
static const int _pageSize = 20;
|
||||
|
||||
@override
|
||||
Future<CursorPagingData<SnPollAnswer>> build(String id) {
|
||||
// immediately load first page
|
||||
return fetch(cursor: null);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<CursorPagingData<SnPollAnswer>> fetch({
|
||||
required String? cursor,
|
||||
}) async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
final offset = cursor == null ? 0 : int.parse(cursor);
|
||||
|
||||
final queryParams = {'offset': offset, 'take': _pageSize};
|
||||
|
||||
final response = await client.get(
|
||||
'/sphere/polls/$id/feedback',
|
||||
queryParameters: queryParams,
|
||||
);
|
||||
final total = int.parse(response.headers.value('X-Total') ?? '0');
|
||||
final List<dynamic> data = response.data;
|
||||
final items = data.map((json) => SnPollAnswer.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 PollFeedbackSheet extends HookConsumerWidget {
|
||||
final String pollId;
|
||||
final String? title;
|
||||
final SnPoll poll;
|
||||
final Map<String, dynamic>? stats; // stats object similar to PollSubmit
|
||||
const PollFeedbackSheet({
|
||||
super.key,
|
||||
required this.pollId,
|
||||
required this.poll,
|
||||
this.title,
|
||||
this.stats,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return SheetScaffold(
|
||||
titleText: title ?? 'Poll feedback',
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
_PollHeader(poll: poll, stats: stats),
|
||||
const Divider(height: 1),
|
||||
Expanded(
|
||||
child: PagingHelperView(
|
||||
provider: pollFeedbackNotifierProvider(pollId),
|
||||
futureRefreshable: pollFeedbackNotifierProvider(pollId).future,
|
||||
notifierRefreshable:
|
||||
pollFeedbackNotifierProvider(pollId).notifier,
|
||||
contentBuilder:
|
||||
(data, widgetCount, endItemView) => ListView.separated(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||
itemCount: widgetCount,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == widgetCount - 1) {
|
||||
// Provided by PagingHelperView to indicate end/loading
|
||||
return endItemView;
|
||||
}
|
||||
final answer = data.items[index];
|
||||
return _PollAnswerTile(answer: answer, poll: poll);
|
||||
},
|
||||
separatorBuilder:
|
||||
(context, index) =>
|
||||
const Divider(height: 1).padding(vertical: 4),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PollHeader extends StatelessWidget {
|
||||
const _PollHeader({required this.poll, this.stats});
|
||||
final SnPoll poll;
|
||||
final Map<String, dynamic>? stats;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (poll.title != null)
|
||||
Text(poll.title!, style: theme.textTheme.titleLarge),
|
||||
if (poll.description != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 2),
|
||||
child: Text(
|
||||
poll.description!,
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
color: theme.textTheme.bodyMedium?.color?.withOpacity(0.7),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
).padding(horizontal: 20, vertical: 16);
|
||||
}
|
||||
}
|
||||
|
||||
class _PollAnswerTile extends StatelessWidget {
|
||||
final SnPollAnswer answer;
|
||||
final SnPoll poll;
|
||||
const _PollAnswerTile({required this.answer, required this.poll});
|
||||
|
||||
String _formatPerQuestionAnswer(
|
||||
SnPollQuestion q,
|
||||
Map<String, dynamic> ansMap,
|
||||
) {
|
||||
switch (q.type) {
|
||||
case SnPollQuestionType.singleChoice:
|
||||
final val = ansMap[q.id];
|
||||
if (val is String) {
|
||||
final opt = q.options?.firstWhere(
|
||||
(o) => o.id == val,
|
||||
orElse: () => SnPollOption(id: val, label: '#$val', order: 0),
|
||||
);
|
||||
return opt?.label ?? '#$val';
|
||||
}
|
||||
return '—';
|
||||
case SnPollQuestionType.multipleChoice:
|
||||
final val = ansMap[q.id];
|
||||
if (val is List) {
|
||||
final ids = val.whereType<String>().toList();
|
||||
if (ids.isEmpty) return '—';
|
||||
final labels =
|
||||
ids.map((id) {
|
||||
final opt = q.options?.firstWhere(
|
||||
(o) => o.id == id,
|
||||
orElse: () => SnPollOption(id: id, label: '#$id', order: 0),
|
||||
);
|
||||
return opt?.label ?? '#$id';
|
||||
}).toList();
|
||||
return labels.join(', ');
|
||||
}
|
||||
return '—';
|
||||
case SnPollQuestionType.yesNo:
|
||||
final val = ansMap[q.id];
|
||||
if (val is bool) {
|
||||
return val ? 'Yes' : 'No';
|
||||
}
|
||||
return '—';
|
||||
case SnPollQuestionType.rating:
|
||||
final val = ansMap[q.id];
|
||||
if (val is int) return val.toString();
|
||||
if (val is num) return val.toString();
|
||||
return '—';
|
||||
case SnPollQuestionType.freeText:
|
||||
final val = ansMap[q.id];
|
||||
if (val is String && val.trim().isNotEmpty) return val;
|
||||
return '—';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Submit date/time (title)
|
||||
final submitText = answer.createdAt.toLocal().toString();
|
||||
|
||||
// Compose content from poll questions if provided, otherwise fallback to joined key-values
|
||||
String content;
|
||||
if (poll.questions.isNotEmpty) {
|
||||
final questions = [...poll.questions]
|
||||
..sort((a, b) => a.order.compareTo(b.order));
|
||||
final buffer = StringBuffer();
|
||||
for (final q in questions) {
|
||||
final formatted = _formatPerQuestionAnswer(q, answer.answer);
|
||||
buffer.writeln('${q.title}: $formatted');
|
||||
}
|
||||
content = buffer.toString().trimRight();
|
||||
} else {
|
||||
// Fallback formatting without poll context. We still want to show the question title
|
||||
// instead of the raw question id key if we can derive it from the answer map itself.
|
||||
// Since we don't have poll metadata here, we cannot resolve the title; therefore we
|
||||
// will show only values line-by-line without exposing the raw id.
|
||||
if (answer.answer.isEmpty) {
|
||||
content = '—';
|
||||
} else {
|
||||
final parts = <String>[];
|
||||
answer.answer.forEach((key, value) {
|
||||
var question = poll.questions.firstWhere((q) => q.id == key);
|
||||
if (value is List) {
|
||||
parts.add('${question.title}: ${value.join(', ')}');
|
||||
} else {
|
||||
parts.add('${question.title}: $value');
|
||||
}
|
||||
});
|
||||
content = parts.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
isThreeLine: true,
|
||||
leading: const CircleAvatar(
|
||||
radius: 16,
|
||||
child: Icon(Icons.how_to_vote, size: 16),
|
||||
),
|
||||
title: Text(submitText),
|
||||
subtitle: Text(content),
|
||||
trailing: null,
|
||||
);
|
||||
}
|
||||
}
|
180
lib/widgets/poll/poll_feedback.g.dart
Normal file
180
lib/widgets/poll/poll_feedback.g.dart
Normal file
@@ -0,0 +1,180 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'poll_feedback.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$pollFeedbackNotifierHash() =>
|
||||
r'1bf3925b5b751cfd1a9abafb75274f1e95e7f27e';
|
||||
|
||||
/// 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 _$PollFeedbackNotifier
|
||||
extends BuildlessAutoDisposeAsyncNotifier<CursorPagingData<SnPollAnswer>> {
|
||||
late final String id;
|
||||
|
||||
FutureOr<CursorPagingData<SnPollAnswer>> build(String id);
|
||||
}
|
||||
|
||||
/// See also [PollFeedbackNotifier].
|
||||
@ProviderFor(PollFeedbackNotifier)
|
||||
const pollFeedbackNotifierProvider = PollFeedbackNotifierFamily();
|
||||
|
||||
/// See also [PollFeedbackNotifier].
|
||||
class PollFeedbackNotifierFamily
|
||||
extends Family<AsyncValue<CursorPagingData<SnPollAnswer>>> {
|
||||
/// See also [PollFeedbackNotifier].
|
||||
const PollFeedbackNotifierFamily();
|
||||
|
||||
/// See also [PollFeedbackNotifier].
|
||||
PollFeedbackNotifierProvider call(String id) {
|
||||
return PollFeedbackNotifierProvider(id);
|
||||
}
|
||||
|
||||
@override
|
||||
PollFeedbackNotifierProvider getProviderOverride(
|
||||
covariant PollFeedbackNotifierProvider 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'pollFeedbackNotifierProvider';
|
||||
}
|
||||
|
||||
/// See also [PollFeedbackNotifier].
|
||||
class PollFeedbackNotifierProvider
|
||||
extends
|
||||
AutoDisposeAsyncNotifierProviderImpl<
|
||||
PollFeedbackNotifier,
|
||||
CursorPagingData<SnPollAnswer>
|
||||
> {
|
||||
/// See also [PollFeedbackNotifier].
|
||||
PollFeedbackNotifierProvider(String id)
|
||||
: this._internal(
|
||||
() => PollFeedbackNotifier()..id = id,
|
||||
from: pollFeedbackNotifierProvider,
|
||||
name: r'pollFeedbackNotifierProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$pollFeedbackNotifierHash,
|
||||
dependencies: PollFeedbackNotifierFamily._dependencies,
|
||||
allTransitiveDependencies:
|
||||
PollFeedbackNotifierFamily._allTransitiveDependencies,
|
||||
id: id,
|
||||
);
|
||||
|
||||
PollFeedbackNotifierProvider._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
|
||||
FutureOr<CursorPagingData<SnPollAnswer>> runNotifierBuild(
|
||||
covariant PollFeedbackNotifier notifier,
|
||||
) {
|
||||
return notifier.build(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Override overrideWith(PollFeedbackNotifier Function() create) {
|
||||
return ProviderOverride(
|
||||
origin: this,
|
||||
override: PollFeedbackNotifierProvider._internal(
|
||||
() => create()..id = id,
|
||||
from: from,
|
||||
name: null,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
debugGetCreateSourceHash: null,
|
||||
id: id,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
AutoDisposeAsyncNotifierProviderElement<
|
||||
PollFeedbackNotifier,
|
||||
CursorPagingData<SnPollAnswer>
|
||||
>
|
||||
createElement() {
|
||||
return _PollFeedbackNotifierProviderElement(this);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is PollFeedbackNotifierProvider && 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 PollFeedbackNotifierRef
|
||||
on AutoDisposeAsyncNotifierProviderRef<CursorPagingData<SnPollAnswer>> {
|
||||
/// The parameter `id` of this provider.
|
||||
String get id;
|
||||
}
|
||||
|
||||
class _PollFeedbackNotifierProviderElement
|
||||
extends
|
||||
AutoDisposeAsyncNotifierProviderElement<
|
||||
PollFeedbackNotifier,
|
||||
CursorPagingData<SnPollAnswer>
|
||||
>
|
||||
with PollFeedbackNotifierRef {
|
||||
_PollFeedbackNotifierProviderElement(super.provider);
|
||||
|
||||
@override
|
||||
String get id => (origin as PollFeedbackNotifierProvider).id;
|
||||
}
|
||||
|
||||
// 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
|
Reference in New Issue
Block a user