✨ Better loading animation in paginationed list
This commit is contained in:
@@ -3,6 +3,7 @@ import 'dart:math' as math;
|
||||
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/account.dart';
|
||||
@@ -10,7 +11,6 @@ import 'package:island/pods/network.dart';
|
||||
import 'package:island/pods/paging.dart';
|
||||
import 'package:island/pods/websocket.dart';
|
||||
import 'package:island/route.dart';
|
||||
import 'package:island/widgets/alert.dart';
|
||||
import 'package:island/widgets/content/cloud_files.dart';
|
||||
import 'package:island/widgets/content/markdown.dart';
|
||||
import 'package:island/widgets/content/sheet.dart';
|
||||
@@ -101,11 +101,10 @@ class NotificationListNotifier extends AsyncNotifier<List<SnNotification>>
|
||||
queryParameters: queryParams,
|
||||
);
|
||||
totalCount = int.parse(response.headers.value('X-Total') ?? '0');
|
||||
final notifications =
|
||||
response.data
|
||||
.map((json) => SnNotification.fromJson(json))
|
||||
.cast<SnNotification>()
|
||||
.toList();
|
||||
final notifications = response.data
|
||||
.map((json) => SnNotification.fromJson(json))
|
||||
.cast<SnNotification>()
|
||||
.toList();
|
||||
|
||||
final unreadCount = notifications.where((n) => n.viewedAt == null).length;
|
||||
ref.read(notificationUnreadCountProvider.notifier).decrement(unreadCount);
|
||||
@@ -145,15 +144,22 @@ class NotificationSheet extends HookConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
// Refresh unread count when sheet opens to sync across devices
|
||||
ref.read(notificationUnreadCountProvider.notifier).refresh();
|
||||
useEffect(() {
|
||||
Future(() {
|
||||
ref.read(notificationUnreadCountProvider.notifier).refresh();
|
||||
});
|
||||
return null;
|
||||
}, []);
|
||||
|
||||
final isLoading = useState(false);
|
||||
|
||||
Future<void> markAllRead() async {
|
||||
showLoadingModal(context);
|
||||
isLoading.value = true;
|
||||
final apiClient = ref.watch(apiClientProvider);
|
||||
await apiClient.post('/ring/notifications/all/read');
|
||||
if (!context.mounted) return;
|
||||
hideLoadingModal(context);
|
||||
ref.invalidate(notificationListProvider);
|
||||
isLoading.value = false;
|
||||
ref.read(notificationListProvider.notifier).refresh();
|
||||
ref.watch(notificationUnreadCountProvider.notifier).clear();
|
||||
}
|
||||
|
||||
@@ -165,108 +171,125 @@ class NotificationSheet extends HookConsumerWidget {
|
||||
icon: const Icon(Symbols.mark_as_unread),
|
||||
),
|
||||
],
|
||||
child: PaginationList(
|
||||
provider: notificationListProvider,
|
||||
notifier: notificationListProvider.notifier,
|
||||
itemBuilder: (context, index, notification) {
|
||||
final pfp = notification.meta['pfp'] as String?;
|
||||
final images = notification.meta['images'] as List?;
|
||||
final imageIds = images?.cast<String>() ?? [];
|
||||
|
||||
return ListTile(
|
||||
isThreeLine: true,
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
leading:
|
||||
pfp != null
|
||||
? ProfilePictureWidget(fileId: pfp, radius: 20)
|
||||
: CircleAvatar(
|
||||
backgroundColor:
|
||||
Theme.of(context).colorScheme.primaryContainer,
|
||||
child: Icon(
|
||||
_getNotificationIcon(notification.topic),
|
||||
color: Theme.of(context).colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
title: Text(notification.title),
|
||||
subtitle: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (notification.subtitle.isNotEmpty)
|
||||
Text(notification.subtitle).bold(),
|
||||
Row(
|
||||
spacing: 6,
|
||||
children: [
|
||||
Text(
|
||||
DateFormat().format(notification.createdAt.toLocal()),
|
||||
).fontSize(11),
|
||||
Text('·').fontSize(11).bold(),
|
||||
Text(
|
||||
RelativeTime(
|
||||
context,
|
||||
).format(notification.createdAt.toLocal()),
|
||||
).fontSize(11),
|
||||
],
|
||||
).opacity(0.75).padding(bottom: 4),
|
||||
MarkdownTextContent(
|
||||
content: notification.content,
|
||||
textStyle: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(
|
||||
context,
|
||||
).colorScheme.onSurface.withOpacity(0.8),
|
||||
),
|
||||
),
|
||||
if (imageIds.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children:
|
||||
imageIds.map((imageId) {
|
||||
return SizedBox(
|
||||
width: 80,
|
||||
height: 80,
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: CloudImageWidget(
|
||||
fileId: imageId,
|
||||
aspectRatio: 1,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
child: Column(
|
||||
children: [
|
||||
if (isLoading.value)
|
||||
LinearProgressIndicator(
|
||||
minHeight: 2,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
trailing:
|
||||
notification.viewedAt != null
|
||||
? null
|
||||
: Container(
|
||||
width: 12,
|
||||
height: 12,
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.blue,
|
||||
shape: BoxShape.circle,
|
||||
Expanded(
|
||||
child: PaginationList(
|
||||
provider: notificationListProvider,
|
||||
notifier: notificationListProvider.notifier,
|
||||
itemBuilder: (context, index, notification) {
|
||||
final pfp = notification.meta['pfp'] as String?;
|
||||
final images = notification.meta['images'] as List?;
|
||||
final imageIds = images?.cast<String>() ?? [];
|
||||
|
||||
return ListTile(
|
||||
isThreeLine: true,
|
||||
contentPadding: EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 8,
|
||||
),
|
||||
leading: pfp != null
|
||||
? ProfilePictureWidget(fileId: pfp, radius: 20)
|
||||
: CircleAvatar(
|
||||
backgroundColor: Theme.of(
|
||||
context,
|
||||
).colorScheme.primaryContainer,
|
||||
child: Icon(
|
||||
_getNotificationIcon(notification.topic),
|
||||
color: Theme.of(
|
||||
context,
|
||||
).colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
title: Text(notification.title),
|
||||
subtitle: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (notification.subtitle.isNotEmpty)
|
||||
Text(notification.subtitle).bold(),
|
||||
Row(
|
||||
spacing: 6,
|
||||
children: [
|
||||
Text(
|
||||
DateFormat().format(
|
||||
notification.createdAt.toLocal(),
|
||||
),
|
||||
).fontSize(11),
|
||||
Text('·').fontSize(11).bold(),
|
||||
Text(
|
||||
RelativeTime(
|
||||
context,
|
||||
).format(notification.createdAt.toLocal()),
|
||||
).fontSize(11),
|
||||
],
|
||||
).opacity(0.75).padding(bottom: 4),
|
||||
MarkdownTextContent(
|
||||
content: notification.content,
|
||||
textStyle: Theme.of(context).textTheme.bodyMedium
|
||||
?.copyWith(
|
||||
color: Theme.of(
|
||||
context,
|
||||
).colorScheme.onSurface.withOpacity(0.8),
|
||||
),
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
if (notification.meta['action_uri'] != null) {
|
||||
var uri = notification.meta['action_uri'] as String;
|
||||
if (uri.startsWith('/')) {
|
||||
// In-app routes
|
||||
rootNavigatorKey.currentContext?.push(
|
||||
notification.meta['action_uri'],
|
||||
);
|
||||
} else {
|
||||
// External URLs
|
||||
launchUrlString(uri);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
if (imageIds.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: imageIds.map((imageId) {
|
||||
return SizedBox(
|
||||
width: 80,
|
||||
height: 80,
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: CloudImageWidget(
|
||||
fileId: imageId,
|
||||
aspectRatio: 1,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
trailing: notification.viewedAt != null
|
||||
? null
|
||||
: Container(
|
||||
width: 12,
|
||||
height: 12,
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.blue,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
if (notification.meta['action_uri'] != null) {
|
||||
var uri = notification.meta['action_uri'] as String;
|
||||
if (uri.startsWith('/')) {
|
||||
// In-app routes
|
||||
rootNavigatorKey.currentContext?.push(
|
||||
notification.meta['action_uri'],
|
||||
);
|
||||
} else {
|
||||
// External URLs
|
||||
launchUrlString(uri);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user