Skip to content

Commit 4ab2032

Browse files
committed
Add routing from notification click.
1 parent 2787167 commit 4ab2032

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

lib/screens/app_shell.dart

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import '../util/logger.dart';
1818
import '../util/partners_servers.dart';
1919
import '../services/partners_servers_service.dart';
2020
import '../l10n/app_localizations.dart';
21+
import '../services/user_service.dart';
22+
import '../models/user_model.dart';
2123
import 'landing_screen.dart';
2224
import 'home_screen.dart';
2325
import 'skins_screen.dart';
@@ -26,6 +28,7 @@ import 'partner_servers_screen.dart';
2628
import 'player_lookup_screen.dart';
2729
import 'manage_servers_screen.dart';
2830
import 'profile_screen.dart';
31+
import 'chat_screen.dart';
2932

3033
enum _ActiveSheet { none, help, howTo, more, info }
3134

@@ -64,6 +67,7 @@ class _AppShellState extends State<AppShell>
6467
final GlobalKey<ManageServersScreenState> _manageServersKey = GlobalKey();
6568
final GlobalKey<HomeScreenState> _connectorKey = GlobalKey();
6669
final GlobalKey<SkinsScreenState> _skinsKey = GlobalKey();
70+
final GlobalKey<ProfileScreenState> _profileKey = GlobalKey();
6771

6872
late RelayPingResult _selectedRelay;
6973
int _pageIndex = _pageHome;
@@ -153,15 +157,42 @@ class _AppShellState extends State<AppShell>
153157
final type = message.notificationType as String? ?? 'unknown';
154158
switch (type) {
155159
case 'message':
156-
_goTo(_pageProfile);
160+
final username = message.senderUsername as String?;
161+
_goToProfileTab(3);
162+
if (username != null) unawaited(_openChatFromNotification(username));
163+
case 'friend_request':
164+
_goToProfileTab(2);
157165
case 'friend_online':
158166
case 'friend_session':
159-
_goTo(_pageProfile);
167+
_goToProfileTab(1);
160168
default:
161169
_goTo(_pageHome);
162170
}
163171
}
164172

173+
void _goToProfileTab(int tab) {
174+
_goTo(_pageProfile);
175+
WidgetsBinding.instance.addPostFrameCallback((_) {
176+
_profileKey.currentState?.switchToTab(tab);
177+
});
178+
}
179+
180+
Future<void> _openChatFromNotification(String username) async {
181+
final user = await UserService.getProfile(username);
182+
if (!mounted || user == null) return;
183+
final friend = FriendModel(
184+
firebaseUid: '',
185+
username: user.username,
186+
displayName: user.displayName,
187+
avatarUrl: user.avatarUrl,
188+
online: false,
189+
);
190+
if (!mounted) return;
191+
Navigator.of(context).push(
192+
MaterialPageRoute(builder: (_) => ChatScreen(friend: friend)),
193+
);
194+
}
195+
165196
void _goTo(int page) {
166197
_closeSheetInstant();
167198
if (page == _pageConnector) {
@@ -380,6 +411,7 @@ class _AppShellState extends State<AppShell>
380411
SkinsScreen(key: _skinsKey),
381412
const WikiScreen(),
382413
ProfileScreen(
414+
key: _profileKey,
383415
onGoToHome: () => _goTo(_pageHome),
384416
onGoToConnector: () => _goTo(_pageConnector),
385417
onGoToSkins: () => _goTo(_pageSkins),

lib/screens/profile_screen.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class ProfileScreen extends StatefulWidget {
3333
});
3434

3535
@override
36-
State<ProfileScreen> createState() => _ProfileScreenState();
36+
State<ProfileScreen> createState() => ProfileScreenState();
3737
}
3838

39-
class _ProfileScreenState extends State<ProfileScreen>
39+
class ProfileScreenState extends State<ProfileScreen>
4040
with SingleTickerProviderStateMixin {
4141
late final TabController _tabs;
4242
StreamSubscription<AuthUser?>? _authSubscription;
@@ -67,6 +67,13 @@ class _ProfileScreenState extends State<ProfileScreen>
6767
super.dispose();
6868
}
6969

70+
/// Called externally (e.g. from a notification tap) to jump to a specific tab.
71+
/// Tab indices: 0 = Profile, 1 = Friends, 2 = Requests, 3 = Chats
72+
void switchToTab(int index) {
73+
if (!mounted) return;
74+
_tabs.animateTo(index);
75+
}
76+
7077
void _onPresence(({String uid, bool online}) event) {
7178
if (!mounted) return;
7279
final idx = _friends.indexWhere((f) => f.firebaseUid == event.uid);

0 commit comments

Comments
 (0)