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
parent a6e15f9bf7
commit 5ec4cf2577
6 changed files with 42 additions and 90 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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