@@ -4,6 +4,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
44import 'package:flutter_svg/svg.dart' ;
55import 'package:gap/gap.dart' ;
66import 'package:go_router/go_router.dart' ;
7+ import 'package:logging/logging.dart' ;
78import 'package:whitenoise/config/providers/active_account_provider.dart' ;
89import 'package:whitenoise/config/providers/group_provider.dart' ;
910import 'package:whitenoise/domain/models/contact_model.dart' ;
@@ -57,13 +58,11 @@ class _GroupChatDetailsSheetState extends ConsumerState<GroupChatDetailsSheet> w
5758 bool _hasGroupImage = false ;
5859 bool _isGroupNameValid = false ;
5960 bool _isCreatingGroup = false ;
60- bool _hasContactsWithKeyPackage = true ;
6161
6262 @override
6363 void initState () {
6464 super .initState ();
6565 _groupNameController.addListener (_onGroupNameChanged);
66- _checkContactsKeyPackages ();
6766 }
6867
6968 void _onGroupNameChanged () {
@@ -75,36 +74,12 @@ class _GroupChatDetailsSheetState extends ConsumerState<GroupChatDetailsSheet> w
7574 }
7675 }
7776
78- Future <void > _checkContactsKeyPackages () async {
79- try {
80- final filteredContacts = await _filterContactsByKeyPackage (widget.selectedContacts);
81- if (! mounted) return ;
82-
83- final contactsWithKeyPackage = filteredContacts['withKeyPackage' ]! ;
84-
85- if (mounted) {
86- setState (() {
87- _hasContactsWithKeyPackage = contactsWithKeyPackage.isNotEmpty;
88- });
89- }
90- } catch (e) {
91- // If there's an error checking keypackages, assume no contacts have keypackages
92- if (mounted) {
93- setState (() {
94- _hasContactsWithKeyPackage = false ;
95- });
96- }
97- }
98- }
99-
10077 void _createGroupChat () async {
101- if (! _isGroupNameValid) return ;
102- final groupName = _groupNameController.text.trim ();
78+ if (! _isGroupNameValid || ! mounted) return ;
10379
104- // Store the ref early to avoid accessing it after disposal
80+ final groupName = _groupNameController.text. trim ();
10581 final notifier = ref.read (groupsProvider.notifier);
10682
107- if (! mounted) return ;
10883 setState (() {
10984 _isCreatingGroup = true ;
11085 });
@@ -117,13 +92,25 @@ class _GroupChatDetailsSheetState extends ConsumerState<GroupChatDetailsSheet> w
11792 final contactsWithKeyPackage = filteredContacts['withKeyPackage' ]! ;
11893 final contactsWithoutKeyPackage = filteredContacts['withoutKeyPackage' ]! ;
11994
120- if (contactsWithKeyPackage.isEmpty) {
121- safeShowErrorToast ('No contacts have keypackages available for group creation' );
95+ // If less than 2 contacts have keypackages, only show invite sheet (no group creation)
96+ if (contactsWithKeyPackage.length < 2 ) {
97+ if (contactsWithoutKeyPackage.isNotEmpty && mounted) {
98+ await ShareInviteBottomSheet .show (
99+ context: context,
100+ contacts: contactsWithoutKeyPackage,
101+ );
102+ }
103+
104+ if (mounted) {
105+ context.pop ();
106+ }
122107 return ;
123108 }
124109
125- // Create group with contacts that have keypackages - use stored notifier
126- final groupData = await notifier.createNewGroup (
110+ // Create group with contacts that have keypackages
111+ if (! mounted) return ;
112+
113+ final createdGroupData = await notifier.createNewGroup (
127114 groupName: groupName,
128115 groupDescription: '' ,
129116 memberPublicKeyHexs: contactsWithKeyPackage.map ((c) => c.publicKey).toList (),
@@ -132,58 +119,50 @@ class _GroupChatDetailsSheetState extends ConsumerState<GroupChatDetailsSheet> w
132119
133120 if (! mounted) return ;
134121
135- GroupData ? successGroupData;
136- String ? errorMessage;
137-
138- if (groupData != null ) {
139- successGroupData = groupData;
122+ if (createdGroupData != null ) {
140123 // Show share invite bottom sheet for members without keypackages
141124 if (contactsWithoutKeyPackage.isNotEmpty && mounted) {
142- await ShareInviteBottomSheet .show (
143- context: context,
144- contacts: contactsWithoutKeyPackage,
145- );
125+ try {
126+ await ShareInviteBottomSheet .show (
127+ context: context,
128+ contacts: contactsWithoutKeyPackage,
129+ );
130+ } catch (e) {
131+ Logger (
132+ 'GroupChatDetailsSheet' ,
133+ ).severe ('Error showing share invite bottom sheet: $e ' );
134+ }
146135 }
147- } else {
148- errorMessage = 'Failed to create group chat. Please try again.' ;
149- }
150-
151- // Complete all local operations first
152- if (mounted) {
153- setState (() {
154- _isCreatingGroup = false ;
155- });
156- }
157136
158- // Show error if needed
159- if (errorMessage != null ) {
160- safeShowErrorToast (errorMessage);
161- }
137+ // Navigate to the created group
138+ if (mounted) {
139+ context.pop ();
162140
163- if (successGroupData != null && mounted) {
164- // Navigate to home first, then to the group chat
165- WidgetsBinding .instance.addPostFrameCallback ((_) {
166- if (mounted) {
167- context.pop ();
168- context.go (Routes .home);
169- WidgetsBinding .instance.addPostFrameCallback ((_) {
141+ WidgetsBinding .instance.addPostFrameCallback ((_) async {
142+ if (mounted) {
143+ context.go (Routes .home);
144+ // Small delay to ensure navigation completes
145+ await Future .delayed (const Duration (milliseconds: 150 ));
170146 if (mounted) {
171- Routes .goToChat (context, groupData ! .mlsGroupId);
147+ Routes .goToChat (context, createdGroupData .mlsGroupId);
172148 }
173- });
174- }
175- });
149+ }
150+ });
151+ }
152+ } else {
153+ safeShowErrorToast ('Failed to create group chat. Please try again.' );
176154 }
177-
178- return ;
179155 } catch (e) {
156+ if (mounted) {
157+ safeShowErrorToast ('Error creating group: ${e .toString ()}' );
158+ }
159+ } finally {
160+ // Always reset loading state
180161 if (mounted) {
181162 setState (() {
182163 _isCreatingGroup = false ;
183164 });
184165 }
185- safeShowErrorToast ('Error creating group: ${e .toString ()}' );
186- return ;
187166 }
188167 }
189168
@@ -319,10 +298,7 @@ class _GroupChatDetailsSheetState extends ConsumerState<GroupChatDetailsSheet> w
319298 ),
320299 ),
321300 WnFilledButton (
322- onPressed:
323- _isCreatingGroup || ! _isGroupNameValid || ! _hasContactsWithKeyPackage
324- ? null
325- : () => _createGroupChat (),
301+ onPressed: _isCreatingGroup || ! _isGroupNameValid ? null : _createGroupChat,
326302 loading: _isCreatingGroup,
327303 title: _isCreatingGroup ? 'Creating Group...' : 'Create Group' ,
328304 ),
0 commit comments