💄 Optimize the notification overlays
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:island/models/account.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
@@ -13,6 +15,7 @@ class NotificationItem {
|
||||
final DateTime createdAt;
|
||||
final int index;
|
||||
final Duration duration;
|
||||
final bool dismissed;
|
||||
|
||||
NotificationItem({
|
||||
String? id,
|
||||
@@ -20,11 +23,30 @@ class NotificationItem {
|
||||
DateTime? createdAt,
|
||||
required this.index,
|
||||
Duration? duration,
|
||||
this.dismissed = false,
|
||||
}) : id = id ?? const Uuid().v4(),
|
||||
createdAt = createdAt ?? DateTime.now(),
|
||||
duration =
|
||||
duration ?? kNotificationBaseDuration + Duration(seconds: index);
|
||||
|
||||
NotificationItem copyWith({
|
||||
String? id,
|
||||
SnNotification? notification,
|
||||
DateTime? createdAt,
|
||||
int? index,
|
||||
Duration? duration,
|
||||
bool? dismissed,
|
||||
}) {
|
||||
return NotificationItem(
|
||||
id: id ?? this.id,
|
||||
notification: notification ?? this.notification,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
index: index ?? this.index,
|
||||
duration: duration ?? this.duration,
|
||||
dismissed: dismissed ?? this.dismissed,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
@@ -37,6 +59,8 @@ class NotificationItem {
|
||||
|
||||
@riverpod
|
||||
class NotificationState extends _$NotificationState {
|
||||
final Map<String, Timer> _timers = {};
|
||||
|
||||
@override
|
||||
List<NotificationItem> build() {
|
||||
return [];
|
||||
@@ -49,6 +73,16 @@ class NotificationState extends _$NotificationState {
|
||||
duration: duration,
|
||||
);
|
||||
state = [...state, newItem];
|
||||
_timers[newItem.id] = Timer(newItem.duration, () => dismiss(newItem.id));
|
||||
}
|
||||
|
||||
void dismiss(String id) {
|
||||
_timers[id]?.cancel();
|
||||
_timers.remove(id);
|
||||
final index = state.indexWhere((item) => item.id == id);
|
||||
if (index != -1) {
|
||||
state = List.from(state)..[index] = state[index].copyWith(dismissed: true);
|
||||
}
|
||||
}
|
||||
|
||||
void remove(String id) {
|
||||
@@ -56,6 +90,10 @@ class NotificationState extends _$NotificationState {
|
||||
}
|
||||
|
||||
void clear() {
|
||||
for (final timer in _timers.values) {
|
||||
timer.cancel();
|
||||
}
|
||||
_timers.clear();
|
||||
state = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user