feat: use ChatScope for userstory screens to get service, options and userId

This commit is contained in:
Freek van de Ven 2025-02-12 11:53:46 +01:00 committed by Bart Ribbers
parent d610a0bb7d
commit c2f4d60bae
6 changed files with 42 additions and 90 deletions

View file

@ -38,8 +38,6 @@ class NavigatorWrapper extends StatelessWidget {
/// The chat overview screen
Widget chatScreen(BuildContext context) => ChatScreen(
chatService: chatService,
chatOptions: chatOptions,
onExit: onExit,
onPressChat: (chat) async =>
_routeToScreen(context, chatDetailScreen(context, chat)),
@ -105,9 +103,6 @@ class NavigatorWrapper extends StatelessWidget {
ChatModel? chat,
) =>
ChatProfileScreen(
service: chatService,
options: chatOptions,
userId: userId,
userModel: user,
chatModel: chat,
onExit: () => Navigator.of(context).pop(),
@ -127,9 +122,6 @@ class NavigatorWrapper extends StatelessWidget {
/// The new chat screen
Widget newChatScreen(BuildContext context) => NewChatScreen(
userId: userId,
chatService: chatService,
chatOptions: chatOptions,
onExit: () => Navigator.of(context).pop(),
onPressCreateGroupChat: () async =>
_routeToScreen(context, newGroupChatScreen(context)),
@ -146,9 +138,6 @@ class NavigatorWrapper extends StatelessWidget {
/// The new group chat screen
Widget newGroupChatScreen(BuildContext context) => NewGroupChatScreen(
userId: userId,
chatService: chatService,
chatOptions: chatOptions,
onExit: () => Navigator.of(context).pop(),
onContinue: (users) async => _replaceCurrentScreen(
context,
@ -159,7 +148,6 @@ class NavigatorWrapper extends StatelessWidget {
/// The new group chat overview screen
Widget newGroupChatOverview(BuildContext context, List<UserModel> users) =>
NewGroupChatOverview(
options: chatOptions,
users: users,
onExit: () => Navigator.of(context).pop(),
onComplete: (users, title, description, image) async {

View file

@ -12,23 +12,14 @@ import "package:flutter_profile/flutter_profile.dart";
class ChatProfileScreen extends HookWidget {
/// Constructs a [ChatProfileScreen]
const ChatProfileScreen({
required this.options,
required this.onExit,
required this.userId,
required this.userModel,
required this.service,
required this.chatModel,
required this.onTapUser,
required this.onPressStartChat,
super.key,
});
/// The chat options
final ChatOptions options;
/// The user ID of the person currently looking at the chat
final String userId;
/// The user model of the persons profile to be viewed
final UserModel? userModel;
@ -38,9 +29,6 @@ class ChatProfileScreen extends HookWidget {
/// Callback function triggered when a user is tapped
final Function(String)? onTapUser;
/// instance of a chat service
final ChatService service;
/// Callback function triggered when the start chat button is pressed
final Function(String)? onPressStartChat;
@ -50,6 +38,9 @@ class ChatProfileScreen extends HookWidget {
@override
Widget build(BuildContext context) {
var chatScope = ChatScope.of(context);
var options = chatScope.options;
var service = chatScope.service;
var userId = chatScope.userId;
useEffect(() {
chatScope.popHandler.add(onExit);

View file

@ -13,8 +13,6 @@ import "package:flutter_profile/flutter_profile.dart";
class ChatScreen extends HookWidget {
/// Constructs a [ChatScreen]
const ChatScreen({
required this.chatService,
required this.chatOptions,
required this.onPressChat,
required this.onDeleteChat,
required this.onExit,
@ -22,12 +20,6 @@ class ChatScreen extends HookWidget {
super.key,
});
/// The chat service
final ChatService chatService;
/// The chat options
final ChatOptions chatOptions;
/// Callback function for starting a chat.
final Function()? onPressStartChat;
@ -43,6 +35,8 @@ class ChatScreen extends HookWidget {
@override
Widget build(BuildContext context) {
var chatScope = ChatScope.of(context);
var options = chatScope.options;
var service = chatScope.service;
useEffect(() {
if (onExit == null) return null;
@ -50,15 +44,15 @@ class ChatScreen extends HookWidget {
return () => chatScope.popHandler.remove(onExit!);
});
if (chatOptions.builders.baseScreenBuilder == null) {
if (options.builders.baseScreenBuilder == null) {
return Scaffold(
appBar: _AppBar(
chatOptions: chatOptions,
chatService: chatService,
chatOptions: options,
chatService: service,
),
body: _Body(
chatOptions: chatOptions,
chatService: chatService,
chatOptions: options,
chatService: service,
onPressChat: onPressChat,
onPressStartChat: onPressStartChat,
onDeleteChat: onDeleteChat,
@ -66,16 +60,16 @@ class ChatScreen extends HookWidget {
);
}
return chatOptions.builders.baseScreenBuilder!.call(
return options.builders.baseScreenBuilder!.call(
context,
mapScreenType,
_AppBar(
chatOptions: chatOptions,
chatService: chatService,
chatOptions: options,
chatService: service,
),
_Body(
chatOptions: chatOptions,
chatService: chatService,
chatOptions: options,
chatService: service,
onPressChat: onPressChat,
onPressStartChat: onPressStartChat,
onDeleteChat: onDeleteChat,

View file

@ -13,24 +13,12 @@ import "package:flutter_hooks/flutter_hooks.dart";
class NewChatScreen extends StatefulHookWidget {
/// Constructs a [NewChatScreen]
const NewChatScreen({
required this.userId,
required this.onExit,
required this.chatService,
required this.chatOptions,
required this.onPressCreateGroupChat,
required this.onPressCreateChat,
super.key,
});
/// The user ID of the person currently looking at the chat
final String userId;
/// The chat service associated with the widget.
final ChatService chatService;
/// The chat options
final ChatOptions chatOptions;
/// Callback function triggered when the create group chat button is pressed
final VoidCallback onPressCreateGroupChat;
@ -52,16 +40,19 @@ class _NewChatScreenState extends State<NewChatScreen> {
@override
Widget build(BuildContext context) {
var chatScope = ChatScope.of(context);
var options = chatScope.options;
var service = chatScope.service;
var userId = chatScope.userId;
useEffect(() {
chatScope.popHandler.add(widget.onExit);
return () => chatScope.popHandler.remove(widget.onExit);
});
if (widget.chatOptions.builders.baseScreenBuilder == null) {
if (options.builders.baseScreenBuilder == null) {
return Scaffold(
appBar: _AppBar(
chatOptions: widget.chatOptions,
chatOptions: options,
isSearching: _isSearching,
onSearch: (query) {
setState(() {
@ -82,22 +73,22 @@ class _NewChatScreenState extends State<NewChatScreen> {
focusNode: _textFieldFocusNode,
),
body: _Body(
chatOptions: widget.chatOptions,
chatService: widget.chatService,
chatOptions: options,
chatService: service,
isSearching: _isSearching,
onPressCreateGroupChat: widget.onPressCreateGroupChat,
onPressCreateChat: widget.onPressCreateChat,
userId: widget.userId,
userId: userId,
query: query,
),
);
}
return widget.chatOptions.builders.baseScreenBuilder!.call(
return options.builders.baseScreenBuilder!.call(
context,
widget.mapScreenType,
_AppBar(
chatOptions: widget.chatOptions,
chatOptions: options,
isSearching: _isSearching,
onSearch: (query) {
setState(() {
@ -118,12 +109,12 @@ class _NewChatScreenState extends State<NewChatScreen> {
focusNode: _textFieldFocusNode,
),
_Body(
chatOptions: widget.chatOptions,
chatService: widget.chatService,
chatOptions: options,
chatService: service,
isSearching: _isSearching,
onPressCreateGroupChat: widget.onPressCreateGroupChat,
onPressCreateChat: widget.onPressCreateChat,
userId: widget.userId,
userId: userId,
query: query,
),
);

View file

@ -15,16 +15,12 @@ import "package:flutter_profile/flutter_profile.dart";
class NewGroupChatOverview extends HookWidget {
/// Constructs a [NewGroupChatOverview]
const NewGroupChatOverview({
required this.options,
required this.onExit,
required this.users,
required this.onComplete,
super.key,
});
/// The chat options
final ChatOptions options;
/// The users to be added to the group chat
final List<UserModel> users;
@ -42,6 +38,7 @@ class NewGroupChatOverview extends HookWidget {
@override
Widget build(BuildContext context) {
var chatScope = ChatScope.of(context);
var options = chatScope.options;
useEffect(() {
chatScope.popHandler.add(onExit);

View file

@ -13,23 +13,11 @@ import "package:flutter_hooks/flutter_hooks.dart";
class NewGroupChatScreen extends StatefulHookWidget {
/// Constructs a [NewGroupChatScreen]
const NewGroupChatScreen({
required this.userId,
required this.onExit,
required this.chatService,
required this.chatOptions,
required this.onContinue,
super.key,
});
/// The user ID of the person currently looking at the chat
final String userId;
/// The chat service associated with the widget.
final ChatService chatService;
/// The chat options
final ChatOptions chatOptions;
/// Callback for when the user wants to navigate back
final VoidCallback onExit;
@ -50,15 +38,18 @@ class _NewGroupChatScreenState extends State<NewGroupChatScreen> {
@override
Widget build(BuildContext context) {
var chatScope = ChatScope.of(context);
var chatOptions = chatScope.options;
var chatService = chatScope.service;
var userId = chatScope.userId;
useEffect(() {
chatScope.popHandler.add(widget.onExit);
return () => chatScope.popHandler.remove(widget.onExit);
});
if (widget.chatOptions.builders.baseScreenBuilder == null) {
if (chatOptions.builders.baseScreenBuilder == null) {
return Scaffold(
appBar: _AppBar(
chatOptions: widget.chatOptions,
chatOptions: chatOptions,
isSearching: _isSearching,
onSearch: (query) {
setState(() {
@ -82,20 +73,20 @@ class _NewGroupChatScreenState extends State<NewGroupChatScreen> {
onSelectedUser: handleUserTap,
selectedUsers: selectedUsers,
onPressGroupChatOverview: widget.onContinue,
chatOptions: widget.chatOptions,
chatService: widget.chatService,
chatOptions: chatOptions,
chatService: chatService,
isSearching: _isSearching,
userId: widget.userId,
userId: userId,
query: query,
),
);
}
return widget.chatOptions.builders.baseScreenBuilder!.call(
return chatOptions.builders.baseScreenBuilder!.call(
context,
widget.mapScreenType,
_AppBar(
chatOptions: widget.chatOptions,
chatOptions: chatOptions,
isSearching: _isSearching,
onSearch: (query) {
setState(() {
@ -119,10 +110,10 @@ class _NewGroupChatScreenState extends State<NewGroupChatScreen> {
onSelectedUser: handleUserTap,
selectedUsers: selectedUsers,
onPressGroupChatOverview: widget.onContinue,
chatOptions: widget.chatOptions,
chatService: widget.chatService,
chatOptions: chatOptions,
chatService: chatService,
isSearching: _isSearching,
userId: widget.userId,
userId: userId,
query: query,
),
);