🐛 Fix notification push doesn't work
This commit is contained in:
parent
5c0f7225e6
commit
217a0c0a54
@ -104,7 +104,7 @@ void main() async {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final _appRouter = AppRouter();
|
final appRouter = AppRouter();
|
||||||
|
|
||||||
class IslandApp extends HookConsumerWidget {
|
class IslandApp extends HookConsumerWidget {
|
||||||
const IslandApp({super.key});
|
const IslandApp({super.key});
|
||||||
@ -118,7 +118,7 @@ class IslandApp extends HookConsumerWidget {
|
|||||||
var uri = notification.data['action_uri'] as String;
|
var uri = notification.data['action_uri'] as String;
|
||||||
if (uri.startsWith('/')) {
|
if (uri.startsWith('/')) {
|
||||||
// In-app routes
|
// In-app routes
|
||||||
_appRouter.pushPath(notification.data['action_uri']);
|
appRouter.pushPath(notification.data['action_uri']);
|
||||||
} else {
|
} else {
|
||||||
// External links
|
// External links
|
||||||
launchUrlString(uri);
|
launchUrlString(uri);
|
||||||
@ -164,7 +164,7 @@ class IslandApp extends HookConsumerWidget {
|
|||||||
theme: theme?.light,
|
theme: theme?.light,
|
||||||
darkTheme: theme?.dark,
|
darkTheme: theme?.dark,
|
||||||
themeMode: ThemeMode.system,
|
themeMode: ThemeMode.system,
|
||||||
routerConfig: _appRouter.config(
|
routerConfig: appRouter.config(
|
||||||
navigatorObservers:
|
navigatorObservers:
|
||||||
() => [
|
() => [
|
||||||
TabNavigationObserver(
|
TabNavigationObserver(
|
||||||
@ -187,9 +187,9 @@ class IslandApp extends HookConsumerWidget {
|
|||||||
OverlayEntry(
|
OverlayEntry(
|
||||||
builder:
|
builder:
|
||||||
(_) => WindowScaffold(
|
(_) => WindowScaffold(
|
||||||
router: _appRouter,
|
router: appRouter,
|
||||||
child: TabsNavigationWidget(
|
child: TabsNavigationWidget(
|
||||||
router: _appRouter,
|
router: appRouter,
|
||||||
child: child ?? const SizedBox.shrink(),
|
child: child ?? const SizedBox.shrink(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
import 'dart:io';
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
|
|
||||||
import 'package:auto_route/auto_route.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
|
import 'package:island/main.dart';
|
||||||
import 'package:island/models/user.dart';
|
import 'package:island/models/user.dart';
|
||||||
import 'package:island/pods/websocket.dart';
|
import 'package:island/pods/websocket.dart';
|
||||||
import 'package:island/widgets/content/cloud_files.dart';
|
import 'package:island/widgets/content/cloud_files.dart';
|
||||||
@ -30,6 +29,12 @@ class AppNotificationToast extends HookConsumerWidget {
|
|||||||
// Create a global key for AnimatedList
|
// Create a global key for AnimatedList
|
||||||
final listKey = useMemoized(() => GlobalKey<AnimatedListState>());
|
final listKey = useMemoized(() => GlobalKey<AnimatedListState>());
|
||||||
|
|
||||||
|
// Track visual notification count (including those being animated out)
|
||||||
|
final visualCount = useState(notifications.length);
|
||||||
|
|
||||||
|
// Track notifications being removed to manage visual count
|
||||||
|
final animatingOutIds = useState<Set<String>>({});
|
||||||
|
|
||||||
// Track previous notifications to detect changes
|
// Track previous notifications to detect changes
|
||||||
final previousNotifications = usePrevious(notifications) ?? [];
|
final previousNotifications = usePrevious(notifications) ?? [];
|
||||||
|
|
||||||
@ -41,6 +46,11 @@ class AppNotificationToast extends HookConsumerWidget {
|
|||||||
// Find new notifications (added)
|
// Find new notifications (added)
|
||||||
final newIds = currentIds.difference(previousIds);
|
final newIds = currentIds.difference(previousIds);
|
||||||
|
|
||||||
|
// Update visual count for new notifications
|
||||||
|
if (newIds.isNotEmpty) {
|
||||||
|
visualCount.value += newIds.length;
|
||||||
|
}
|
||||||
|
|
||||||
// Insert new notifications with animation
|
// Insert new notifications with animation
|
||||||
for (final id in newIds) {
|
for (final id in newIds) {
|
||||||
final index = notifications.indexWhere((n) => n.data.id == id);
|
final index = notifications.indexWhere((n) => n.data.id == id);
|
||||||
@ -68,7 +78,8 @@ class AppNotificationToast extends HookConsumerWidget {
|
|||||||
left: 16,
|
left: 16,
|
||||||
right: 16,
|
right: 16,
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
height: notifications.length * 80,
|
// Use visualCount instead of notifications.length for height calculation
|
||||||
|
height: visualCount.value * 80,
|
||||||
child: AnimatedList(
|
child: AnimatedList(
|
||||||
physics: NeverScrollableScrollPhysics(),
|
physics: NeverScrollableScrollPhysics(),
|
||||||
padding: EdgeInsets.zero,
|
padding: EdgeInsets.zero,
|
||||||
@ -107,6 +118,15 @@ class AppNotificationToast extends HookConsumerWidget {
|
|||||||
(n) => n.data.id == notification.data.id,
|
(n) => n.data.id == notification.data.id,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Add to animating out set
|
||||||
|
final notificationId = notification.data.id;
|
||||||
|
if (!animatingOutIds.value.contains(notificationId)) {
|
||||||
|
animatingOutIds.value = {
|
||||||
|
...animatingOutIds.value,
|
||||||
|
notificationId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (currentIndex != -1 &&
|
if (currentIndex != -1 &&
|
||||||
listKey.currentState != null &&
|
listKey.currentState != null &&
|
||||||
currentIndex >= 0 &&
|
currentIndex >= 0 &&
|
||||||
@ -126,13 +146,31 @@ class AppNotificationToast extends HookConsumerWidget {
|
|||||||
() {}, // Empty because it's being removed
|
() {}, // Empty because it's being removed
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
duration: const Duration(
|
duration: const Duration(milliseconds: 150),
|
||||||
milliseconds: 150,
|
// When animation completes, update the visual count
|
||||||
), // Make removal animation faster
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Schedule decrementing the visual count after animation completes
|
||||||
|
Future.delayed(const Duration(milliseconds: 150), () {
|
||||||
|
if (animatingOutIds.value.contains(notificationId)) {
|
||||||
|
visualCount.value =
|
||||||
|
visualCount.value > 0 ? visualCount.value - 1 : 0;
|
||||||
|
animatingOutIds.value =
|
||||||
|
animatingOutIds.value
|
||||||
|
.where((id) => id != notificationId)
|
||||||
|
.toSet();
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Log error but don't crash the app
|
// Log error but don't crash the app
|
||||||
debugPrint('Error removing notification: $e');
|
log('[Notification] Error removing notification: $e');
|
||||||
|
// Still update visual count in case of error
|
||||||
|
visualCount.value =
|
||||||
|
visualCount.value > 0 ? visualCount.value - 1 : 0;
|
||||||
|
animatingOutIds.value =
|
||||||
|
animatingOutIds.value
|
||||||
|
.where((id) => id != notificationId)
|
||||||
|
.toSet();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,11 +244,12 @@ class _NotificationCard extends HookConsumerWidget {
|
|||||||
var uri = notification.data.meta['action_uri'] as String;
|
var uri = notification.data.meta['action_uri'] as String;
|
||||||
if (uri.startsWith('/')) {
|
if (uri.startsWith('/')) {
|
||||||
// In-app routes
|
// In-app routes
|
||||||
context.router.pushPath(notification.data.meta['action_uri']);
|
appRouter.pushPath(notification.data.meta['action_uri']);
|
||||||
} else {
|
} else {
|
||||||
// External URLs
|
// External URLs
|
||||||
launchUrlString(uri);
|
launchUrlString(uri);
|
||||||
}
|
}
|
||||||
|
onDismiss();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: Column(
|
child: Column(
|
||||||
@ -228,7 +267,7 @@ class _NotificationCard extends HookConsumerWidget {
|
|||||||
),
|
),
|
||||||
value: 1.0 - progressState.value,
|
value: 1.0 - progressState.value,
|
||||||
backgroundColor: Colors.transparent,
|
backgroundColor: Colors.transparent,
|
||||||
color: Theme.of(context).colorScheme.secondary,
|
color: Theme.of(context).colorScheme.tertiary,
|
||||||
minHeight: 3,
|
minHeight: 3,
|
||||||
stopIndicatorColor: Colors.transparent,
|
stopIndicatorColor: Colors.transparent,
|
||||||
stopIndicatorRadius: 0,
|
stopIndicatorRadius: 0,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user