Skip to content

Commit

Permalink
fix pr comment
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaCoduriV committed Sep 5, 2022
1 parent 646f870 commit 63e85e7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
8 changes: 1 addition & 7 deletions lib/router/chat_guard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ class ChatGuard extends AutoRouteGuard {
if (isAdmin) {
resolver.next(true);
} else {
router.push(ChatScreenRoute(
otherUser: router.navigatorKey.currentContext!
.read<ChatProvider>()
.messages
.keys
.first,
));
router.push(ChatScreenRoute());
}
}
}
15 changes: 14 additions & 1 deletion lib/router/chat_router_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ class ChatRouterPage extends StatelessWidget {
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => ChatProvider(GetIt.I.get<AuthProvider>()),
child: const AutoRouter(),
builder: (context, child) {
return FutureBuilder(
future: context.read<ChatProvider>().fetchAllMessages(),
builder: (context, snapshot) =>
snapshot.connectionState == ConnectionState.done
? const AutoRouter()
: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [CircularProgressIndicator()]),
),
);
},
);
}
}
9 changes: 5 additions & 4 deletions lib/router/router.gr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class AppRouter extends _i18.RootStackRouter {
routeData: routeData, child: const _i6.ProfileScreen());
},
ChatScreenRoute.name: (routeData) {
final args = routeData.argsAs<ChatScreenRouteArgs>();
final args = routeData.argsAs<ChatScreenRouteArgs>(
orElse: () => const ChatScreenRouteArgs());
return _i18.MaterialPageX<dynamic>(
routeData: routeData,
child: _i7.ChatScreen(key: args.key, otherUser: args.otherUser));
Expand Down Expand Up @@ -283,7 +284,7 @@ class ProfileScreenRoute extends _i18.PageRouteInfo<void> {
/// generated route for
/// [_i7.ChatScreen]
class ChatScreenRoute extends _i18.PageRouteInfo<ChatScreenRouteArgs> {
ChatScreenRoute({_i19.Key? key, required _i25.User otherUser})
ChatScreenRoute({_i19.Key? key, _i25.User? otherUser})
: super(ChatScreenRoute.name,
path: 'onechat',
args: ChatScreenRouteArgs(key: key, otherUser: otherUser));
Expand All @@ -292,11 +293,11 @@ class ChatScreenRoute extends _i18.PageRouteInfo<ChatScreenRouteArgs> {
}

class ChatScreenRouteArgs {
const ChatScreenRouteArgs({this.key, required this.otherUser});
const ChatScreenRouteArgs({this.key, this.otherUser});

final _i19.Key? key;

final _i25.User otherUser;
final _i25.User? otherUser;

@override
String toString() {
Expand Down
12 changes: 8 additions & 4 deletions lib/screens/chat.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:math' as math;

import 'package:auto_route/auto_route.dart';
Expand All @@ -21,10 +22,10 @@ String randomString() {
}

class ChatScreen extends StatefulWidget {
final User _otherUser;
final User? _otherUser;
const ChatScreen({
Key? key,
required User otherUser,
User? otherUser,
}) : _otherUser = otherUser,
super(key: key);

Expand All @@ -34,6 +35,7 @@ class ChatScreen extends StatefulWidget {

class _ChatScreenState extends State<ChatScreen> {
final _mainUser = types.User(id: GetIt.I.get<AuthProvider>().userUid);
late User _otherUser;

@override
void initState() {
Expand All @@ -60,10 +62,12 @@ class _ChatScreenState extends State<ChatScreen> {
@override
Widget build(BuildContext context) {
final ChatProvider chatProvider = context.watch<ChatProvider>();
_otherUser = widget._otherUser ?? chatProvider.messages.keys.first;
log(chatProvider.messages.toString());
return ChatInterface(
name: '${widget._otherUser.firstName} ${widget._otherUser.lastName}',
name: '${_otherUser.firstName} ${_otherUser.lastName}',
currentUser: _mainUser,
messages: chatProvider.messages[widget._otherUser]!
messages: chatProvider.messages[_otherUser]!
.map((m) => types.TextMessage(
id: m.uid,
author: types.User(id: m.fromId),
Expand Down
38 changes: 15 additions & 23 deletions lib/screens/discussion_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,21 @@ class DiscussionListScreen extends StatelessWidget {

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: context.read<ChatProvider>().fetchAllMessages(),
builder: (context, snapshot) =>
snapshot.connectionState == ConnectionState.done
? CustomList(
title: 'Discussions',
conversationsTileData: context
.watch<ChatProvider>()
.getLastMessageOfEachUser()
.map((e) => CustomListTileData(
title: '${e.key.firstName} ${e.key.lastName}',
subtitle: e.value.content,
date: e.value.time,
onTap: () {
AutoRouter.of(context)
.push(ChatScreenRoute(otherUser: e.key));
},
))
.toList(),
)
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [CircularProgressIndicator()]),
return CustomList(
title: 'Discussions',
conversationsTileData: context
.watch<ChatProvider>()
.getLastMessageOfEachUser()
.map((e) => CustomListTileData(
title: '${e.key.firstName} ${e.key.lastName}',
subtitle: e.value.content,
date: e.value.time,
onTap: () {
AutoRouter.of(context)
.push(ChatScreenRoute(otherUser: e.key));
},
))
.toList(),
);
}
}

0 comments on commit 63e85e7

Please sign in to comment.