2021-04-23 17:32:34 +00:00
import ' package:comunic/forez/helpers/forez_group_helper.dart ' ;
2021-04-23 16:46:35 +00:00
import ' package:comunic/helpers/forez_groups_helper.dart ' ;
2021-04-23 17:32:34 +00:00
import ' package:comunic/helpers/groups_helper.dart ' ;
2021-04-23 16:46:35 +00:00
import ' package:comunic/models/group.dart ' ;
2021-04-23 17:32:34 +00:00
import ' package:comunic/ui/dialogs/alert_dialog.dart ' ;
2021-04-23 16:46:35 +00:00
import ' package:comunic/ui/widgets/async_screen_widget.dart ' ;
import ' package:comunic/ui/widgets/tour/presentation_pane.dart ' ;
import ' package:comunic/utils/intl_utils.dart ' ;
2021-04-23 17:32:34 +00:00
import ' package:comunic/utils/log_utils.dart ' ;
import ' package:comunic/utils/ui_utils.dart ' ;
2021-04-23 16:46:35 +00:00
import ' package:flutter/material.dart ' ;
/// Ask the user to join a Forez group
///
/// @author Pierre Hubert
2021-04-24 06:51:56 +00:00
class JoinForezGroupPane extends PresentationPane {
JoinForezGroupPane ( {
2022-03-10 18:39:57 +00:00
required Function ( ) onUpdated ,
required GlobalKey < JoinGroupPaneBodyState > ? key ,
2021-04-23 16:46:35 +00:00
} ) : super (
icon: Icons . login ,
2022-03-10 18:39:57 +00:00
title: tr ( " Join a Forez group " ) ! ,
2021-04-23 16:46:35 +00:00
child: ( c ) = > _JoinGroupPaneBody (
key: key ,
onUpdated: onUpdated ,
) ,
2021-04-23 17:32:34 +00:00
canGoNext: key ? . currentState ? . canGoNext ? ? false ,
2022-03-10 18:39:57 +00:00
onTapNext: ( c ) = > key ! . currentState ! . validateChoice ( ) ,
2021-04-23 16:46:35 +00:00
) ;
}
class _JoinGroupPaneBody extends StatefulWidget {
final Function ( ) onUpdated ;
const _JoinGroupPaneBody ( {
2022-03-10 18:39:57 +00:00
Key ? key ,
required this . onUpdated ,
2022-03-11 15:27:01 +00:00
} ) : super ( key: key ) ;
2021-04-23 16:46:35 +00:00
@ override
2021-04-23 17:32:34 +00:00
JoinGroupPaneBodyState createState ( ) = > JoinGroupPaneBodyState ( ) ;
2021-04-23 16:46:35 +00:00
}
2021-04-23 17:32:34 +00:00
class JoinGroupPaneBodyState extends State < _JoinGroupPaneBody > {
2021-04-23 17:41:14 +00:00
final _key = GlobalKey < AsyncScreenWidgetState > ( ) ;
2022-03-10 18:39:57 +00:00
late List < Group > _groups ;
int ? _currChoice ;
2021-04-23 17:32:34 +00:00
2022-03-10 18:39:57 +00:00
bool get canGoNext = > _currChoice ! = null & & _currChoice ! > 0 ;
2021-04-23 17:32:34 +00:00
Group get _currGroup = > _groups . firstWhere ( ( e ) = > e . id = = _currChoice ) ;
2021-04-23 16:46:35 +00:00
Future < void > _load ( ) async {
_groups = await ForezGroupsHelper . getForezGroups ( ) ;
2021-04-23 17:32:34 +00:00
_currChoice = _groups [ 0 ] . id ;
2021-04-23 17:35:34 +00:00
widget . onUpdated ( ) ;
2021-04-23 16:46:35 +00:00
}
@ override
Widget build ( BuildContext context ) = > AsyncScreenWidget (
2021-04-23 17:41:14 +00:00
key: _key ,
2021-04-23 16:46:35 +00:00
onReload: _load ,
onBuild: onBuild ,
2022-03-10 18:39:57 +00:00
errorMessage: tr ( " Failed to load the list of Forez groups! " ) ! ) ;
2021-04-23 16:46:35 +00:00
2021-04-23 17:32:34 +00:00
Widget onBuild ( ) = > ConstrainedBox (
constraints: BoxConstraints ( maxWidth: 300 ) ,
child: Column (
children: [
2022-03-10 18:39:57 +00:00
Text ( tr ( " Please choose now the Forez group you want to join... " ) ! ) ,
2021-04-23 17:32:34 +00:00
] . . addAll ( _groups . map ( ( e ) = > RadioListTile (
value: e . id ,
groupValue: _currChoice ,
2022-03-10 18:39:57 +00:00
onChanged: ( dynamic v ) = > setState ( ( ) = > _currChoice = e . id ) ,
2021-04-23 17:32:34 +00:00
title: Text ( e . name ) ,
subtitle: Text ( e . membershipText ) ,
) ) ) ,
) ,
) ;
Future < bool > validateChoice ( ) async {
try {
switch ( _currGroup . membershipLevel ) {
case GroupMembershipLevel . VISITOR:
if ( ! await GroupsHelper . sendRequest ( _currGroup . id ) )
throw Exception ( " Failed to send group membership request! " ) ;
break ;
case GroupMembershipLevel . INVITED:
if ( ! await GroupsHelper . respondInvitation ( _currGroup . id , true ) )
throw Exception (
" Failed to respond to group membership invitation! " ) ;
break ;
2021-04-23 16:46:35 +00:00
2021-04-23 17:32:34 +00:00
case GroupMembershipLevel . PENDING:
break ;
2021-04-23 16:46:35 +00:00
2021-04-23 17:32:34 +00:00
case GroupMembershipLevel . ADMINISTRATOR:
case GroupMembershipLevel . MODERATOR:
case GroupMembershipLevel . MEMBER:
break ;
}
// Check if the user can not access yet the group
if ( ( _currGroup . membershipLevel = = GroupMembershipLevel . VISITOR & &
_currGroup . registrationLevel ! = GroupRegistrationLevel . OPEN ) | |
_currGroup . membershipLevel = = GroupMembershipLevel . PENDING ) {
await alert ( context ,
2021-04-26 08:22:23 +00:00
" ${ tr ( " You can not access this group yet, please wait for a member of the group to accept your request. " ) } \n ${ tr ( " Hopefully this will not be too long. " ) } \n ${ tr ( " Please check back soon! " ) } " ) ;
2021-04-23 17:41:14 +00:00
2022-03-10 18:39:57 +00:00
_key . currentState ! . refresh ( ) ;
2021-04-23 17:32:34 +00:00
return false ;
}
await ForezGroupHelper . setId ( _currGroup . id ) ;
return true ;
} catch ( e , s ) {
logError ( e , s ) ;
2022-03-10 18:39:57 +00:00
snack ( context , tr ( " Failed to register to group! " ) ! ) ;
_key . currentState ! . refresh ( ) ;
2021-04-23 17:32:34 +00:00
return false ;
}
}
2021-04-23 16:46:35 +00:00
}