♻️ Improved new notifcation style
This commit is contained in:
61
lib/pods/notification.dart
Normal file
61
lib/pods/notification.dart
Normal 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 = [];
|
||||
}
|
||||
}
|
||||
63
lib/pods/notification.g.dart
Normal file
63
lib/pods/notification.g.dart
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user