♻️ Improved new notifcation style

This commit is contained in:
2026-01-15 01:04:15 +08:00
parent d639df7623
commit 476da28b5e
11 changed files with 414 additions and 75 deletions

View File

@@ -0,0 +1,61 @@
import 'package:island/models/account.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:uuid/uuid.dart';
part 'notification.g.dart';
const kNotificationBaseDuration = Duration(seconds: 5);
const kNotificationStackedDuration = Duration(seconds: 1);
class NotificationItem {
final String id;
final SnNotification notification;
final DateTime createdAt;
final int index;
final Duration duration;
NotificationItem({
String? id,
required this.notification,
DateTime? createdAt,
required this.index,
Duration? duration,
}) : id = id ?? const Uuid().v4(),
createdAt = createdAt ?? DateTime.now(),
duration =
duration ?? kNotificationBaseDuration + Duration(seconds: index);
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is NotificationItem && other.id == id;
}
@override
int get hashCode => id.hashCode;
}
@riverpod
class NotificationState extends _$NotificationState {
@override
List<NotificationItem> build() {
return [];
}
void add(SnNotification notification, {Duration? duration}) {
final newItem = NotificationItem(
notification: notification,
index: state.length,
duration: duration,
);
state = [...state, newItem];
}
void remove(String id) {
state = state.where((item) => item.id != id).toList();
}
void clear() {
state = [];
}
}

View File

@@ -0,0 +1,63 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'notification.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
@ProviderFor(NotificationState)
final notificationStateProvider = NotificationStateProvider._();
final class NotificationStateProvider
extends $NotifierProvider<NotificationState, List<NotificationItem>> {
NotificationStateProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'notificationStateProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$notificationStateHash();
@$internal
@override
NotificationState create() => NotificationState();
/// {@macro riverpod.override_with_value}
Override overrideWithValue(List<NotificationItem> value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<List<NotificationItem>>(value),
);
}
}
String _$notificationStateHash() => r'8625e77d28d71237d86f6d06efab437aa7c09df1';
abstract class _$NotificationState extends $Notifier<List<NotificationItem>> {
List<NotificationItem> build();
@$mustCallSuper
@override
void runBuild() {
final ref =
this.ref as $Ref<List<NotificationItem>, List<NotificationItem>>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<List<NotificationItem>, List<NotificationItem>>,
List<NotificationItem>,
Object?,
Object?
>;
element.handleCreate(ref, build);
}
}