✨ 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:easy_localization/easy_localization.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:island/models/account.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/paging.dart';
|
||||||
import 'package:island/pods/websocket.dart';
|
import 'package:island/pods/websocket.dart';
|
||||||
import 'package:island/route.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/cloud_files.dart';
|
||||||
import 'package:island/widgets/content/markdown.dart';
|
import 'package:island/widgets/content/markdown.dart';
|
||||||
import 'package:island/widgets/content/sheet.dart';
|
import 'package:island/widgets/content/sheet.dart';
|
||||||
@@ -101,11 +101,10 @@ class NotificationListNotifier extends AsyncNotifier<List<SnNotification>>
|
|||||||
queryParameters: queryParams,
|
queryParameters: queryParams,
|
||||||
);
|
);
|
||||||
totalCount = int.parse(response.headers.value('X-Total') ?? '0');
|
totalCount = int.parse(response.headers.value('X-Total') ?? '0');
|
||||||
final notifications =
|
final notifications = response.data
|
||||||
response.data
|
.map((json) => SnNotification.fromJson(json))
|
||||||
.map((json) => SnNotification.fromJson(json))
|
.cast<SnNotification>()
|
||||||
.cast<SnNotification>()
|
.toList();
|
||||||
.toList();
|
|
||||||
|
|
||||||
final unreadCount = notifications.where((n) => n.viewedAt == null).length;
|
final unreadCount = notifications.where((n) => n.viewedAt == null).length;
|
||||||
ref.read(notificationUnreadCountProvider.notifier).decrement(unreadCount);
|
ref.read(notificationUnreadCountProvider.notifier).decrement(unreadCount);
|
||||||
@@ -145,15 +144,22 @@ class NotificationSheet extends HookConsumerWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
// Refresh unread count when sheet opens to sync across devices
|
// 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 {
|
Future<void> markAllRead() async {
|
||||||
showLoadingModal(context);
|
isLoading.value = true;
|
||||||
final apiClient = ref.watch(apiClientProvider);
|
final apiClient = ref.watch(apiClientProvider);
|
||||||
await apiClient.post('/ring/notifications/all/read');
|
await apiClient.post('/ring/notifications/all/read');
|
||||||
if (!context.mounted) return;
|
if (!context.mounted) return;
|
||||||
hideLoadingModal(context);
|
isLoading.value = false;
|
||||||
ref.invalidate(notificationListProvider);
|
ref.read(notificationListProvider.notifier).refresh();
|
||||||
ref.watch(notificationUnreadCountProvider.notifier).clear();
|
ref.watch(notificationUnreadCountProvider.notifier).clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,108 +171,125 @@ class NotificationSheet extends HookConsumerWidget {
|
|||||||
icon: const Icon(Symbols.mark_as_unread),
|
icon: const Icon(Symbols.mark_as_unread),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
child: PaginationList(
|
child: Column(
|
||||||
provider: notificationListProvider,
|
children: [
|
||||||
notifier: notificationListProvider.notifier,
|
if (isLoading.value)
|
||||||
itemBuilder: (context, index, notification) {
|
LinearProgressIndicator(
|
||||||
final pfp = notification.meta['pfp'] as String?;
|
minHeight: 2,
|
||||||
final images = notification.meta['images'] as List?;
|
color: Theme.of(context).colorScheme.primary,
|
||||||
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(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
trailing:
|
Expanded(
|
||||||
notification.viewedAt != null
|
child: PaginationList(
|
||||||
? null
|
provider: notificationListProvider,
|
||||||
: Container(
|
notifier: notificationListProvider.notifier,
|
||||||
width: 12,
|
itemBuilder: (context, index, notification) {
|
||||||
height: 12,
|
final pfp = notification.meta['pfp'] as String?;
|
||||||
decoration: const BoxDecoration(
|
final images = notification.meta['images'] as List?;
|
||||||
color: Colors.blue,
|
final imageIds = images?.cast<String>() ?? [];
|
||||||
shape: BoxShape.circle,
|
|
||||||
|
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)
|
||||||
onTap: () {
|
Padding(
|
||||||
if (notification.meta['action_uri'] != null) {
|
padding: const EdgeInsets.only(top: 8),
|
||||||
var uri = notification.meta['action_uri'] as String;
|
child: Wrap(
|
||||||
if (uri.startsWith('/')) {
|
spacing: 8,
|
||||||
// In-app routes
|
runSpacing: 8,
|
||||||
rootNavigatorKey.currentContext?.push(
|
children: imageIds.map((imageId) {
|
||||||
notification.meta['action_uri'],
|
return SizedBox(
|
||||||
);
|
width: 80,
|
||||||
} else {
|
height: 80,
|
||||||
// External URLs
|
child: ClipRRect(
|
||||||
launchUrlString(uri);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:flutter_riverpod/misc.dart';
|
import 'package:flutter_riverpod/misc.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:island/pods/paging.dart';
|
import 'package:island/pods/paging.dart';
|
||||||
@@ -7,6 +8,7 @@ import 'package:island/widgets/extended_refresh_indicator.dart';
|
|||||||
import 'package:island/widgets/response.dart';
|
import 'package:island/widgets/response.dart';
|
||||||
import 'package:material_symbols_icons/material_symbols_icons.dart';
|
import 'package:material_symbols_icons/material_symbols_icons.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
|
import 'package:skeletonizer/skeletonizer.dart';
|
||||||
import 'package:styled_widget/styled_widget.dart';
|
import 'package:styled_widget/styled_widget.dart';
|
||||||
import 'package:super_sliver_list/super_sliver_list.dart';
|
import 'package:super_sliver_list/super_sliver_list.dart';
|
||||||
import 'package:visibility_detector/visibility_detector.dart';
|
import 'package:visibility_detector/visibility_detector.dart';
|
||||||
@@ -48,31 +50,30 @@ class PaginationList<T> extends HookConsumerWidget {
|
|||||||
return isSliver ? SliverFillRemaining(child: content) : content;
|
return isSliver ? SliverFillRemaining(child: content) : content;
|
||||||
}
|
}
|
||||||
|
|
||||||
final listView =
|
final listView = isSliver
|
||||||
isSliver
|
? SuperSliverList.builder(
|
||||||
? SuperSliverList.builder(
|
itemCount: (data.value?.length ?? 0) + 1,
|
||||||
itemCount: (data.value?.length ?? 0) + 1,
|
itemBuilder: (context, idx) {
|
||||||
itemBuilder: (context, idx) {
|
if (idx == data.value?.length) {
|
||||||
if (idx == data.value?.length) {
|
return PaginationListFooter(noti: noti, data: data);
|
||||||
return PaginationListFooter(noti: noti, data: data);
|
}
|
||||||
}
|
final entry = data.value?[idx];
|
||||||
final entry = data.value?[idx];
|
if (entry != null) return itemBuilder(context, idx, entry);
|
||||||
if (entry != null) return itemBuilder(context, idx, entry);
|
return null;
|
||||||
return null;
|
},
|
||||||
},
|
)
|
||||||
)
|
: SuperListView.builder(
|
||||||
: SuperListView.builder(
|
padding: padding,
|
||||||
padding: padding,
|
itemCount: (data.value?.length ?? 0) + 1,
|
||||||
itemCount: (data.value?.length ?? 0) + 1,
|
itemBuilder: (context, idx) {
|
||||||
itemBuilder: (context, idx) {
|
if (idx == data.value?.length) {
|
||||||
if (idx == data.value?.length) {
|
return PaginationListFooter(noti: noti, data: data);
|
||||||
return PaginationListFooter(noti: noti, data: data);
|
}
|
||||||
}
|
final entry = data.value?[idx];
|
||||||
final entry = data.value?[idx];
|
if (entry != null) return itemBuilder(context, idx, entry);
|
||||||
if (entry != null) return itemBuilder(context, idx, entry);
|
return null;
|
||||||
return null;
|
},
|
||||||
},
|
);
|
||||||
);
|
|
||||||
|
|
||||||
return isRefreshable
|
return isRefreshable
|
||||||
? ExtendedRefreshIndicator(onRefresh: noti.refresh, child: listView)
|
? ExtendedRefreshIndicator(onRefresh: noti.refresh, child: listView)
|
||||||
@@ -124,40 +125,56 @@ class PaginationWidget<T> extends HookConsumerWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PaginationListFooter<T> extends StatelessWidget {
|
class PaginationListFooter<T> extends HookConsumerWidget {
|
||||||
final PaginationController<T> noti;
|
final PaginationController<T> noti;
|
||||||
final AsyncValue<List<T>> data;
|
final AsyncValue<List<T>> data;
|
||||||
|
final Widget? skeletonChild;
|
||||||
final bool isSliver;
|
final bool isSliver;
|
||||||
|
|
||||||
const PaginationListFooter({
|
const PaginationListFooter({
|
||||||
super.key,
|
super.key,
|
||||||
required this.noti,
|
required this.noti,
|
||||||
required this.data,
|
required this.data,
|
||||||
|
this.skeletonChild,
|
||||||
this.isSliver = false,
|
this.isSliver = false,
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
|
final hasBeenVisible = useState(false);
|
||||||
|
|
||||||
|
final placeholder = Skeletonizer(
|
||||||
|
enabled: true,
|
||||||
|
child:
|
||||||
|
skeletonChild ??
|
||||||
|
ListTile(
|
||||||
|
title: Text('Some data'),
|
||||||
|
subtitle: const Text('Subtitle here'),
|
||||||
|
trailing: const Icon(Icons.ac_unit),
|
||||||
|
),
|
||||||
|
);
|
||||||
final child = SizedBox(
|
final child = SizedBox(
|
||||||
height: 64,
|
height: 64,
|
||||||
child: Center(
|
child: Center(
|
||||||
child:
|
child: hasBeenVisible.value
|
||||||
data.isLoading
|
? data.isLoading
|
||||||
? CircularProgressIndicator()
|
? placeholder
|
||||||
: Row(
|
: Row(
|
||||||
spacing: 8,
|
spacing: 8,
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
const Icon(Symbols.close, size: 16),
|
const Icon(Symbols.close, size: 16),
|
||||||
Text('noFurtherData').tr().fontSize(13),
|
Text('noFurtherData').tr().fontSize(13),
|
||||||
],
|
],
|
||||||
).opacity(0.9),
|
).opacity(0.9)
|
||||||
|
: placeholder,
|
||||||
).padding(all: 8),
|
).padding(all: 8),
|
||||||
);
|
);
|
||||||
|
|
||||||
return VisibilityDetector(
|
return VisibilityDetector(
|
||||||
key: Key("pagination-list-${noti.hashCode}"),
|
key: Key("pagination-list-${noti.hashCode}"),
|
||||||
onVisibilityChanged: (VisibilityInfo info) {
|
onVisibilityChanged: (VisibilityInfo info) {
|
||||||
|
hasBeenVisible.value = true;
|
||||||
if (!noti.fetchedAll && !data.isLoading && !data.hasError) {
|
if (!noti.fetchedAll && !data.isLoading && !data.hasError) {
|
||||||
noti.fetchFurther();
|
noti.fetchFurther();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2486,6 +2486,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.2.1"
|
version: "0.2.1"
|
||||||
|
skeletonizer:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: skeletonizer
|
||||||
|
sha256: "5d2d44120916cc749ede54c236cef60c2478742806df0b1f065212f00721b185"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.1"
|
||||||
sky_engine:
|
sky_engine:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description: flutter
|
description: flutter
|
||||||
|
|||||||
@@ -170,6 +170,7 @@ dependencies:
|
|||||||
flutter_animate: ^4.5.2
|
flutter_animate: ^4.5.2
|
||||||
http_parser: ^4.1.2
|
http_parser: ^4.1.2
|
||||||
flutter_code_editor: ^0.3.5
|
flutter_code_editor: ^0.3.5
|
||||||
|
skeletonizer: ^2.1.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user