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 ( {
2021-04-23 16:46:35 +00:00
@ required Function ( ) onUpdated ,
2021-04-23 17:32:34 +00:00
@ required GlobalKey < JoinGroupPaneBodyState > key ,
2021-04-23 16:46:35 +00:00
} ) : super (
icon: Icons . login ,
2021-04-23 17:32:34 +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 ,
onTapNext: ( c ) = > key . currentState . validateChoice ( ) ,
2021-04-23 16:46:35 +00:00
) ;
}
class _JoinGroupPaneBody extends StatefulWidget {
final Function ( ) onUpdated ;
const _JoinGroupPaneBody ( {
Key key ,
@ required this . onUpdated ,
} ) : assert ( onUpdated ! = null ) ,
super ( key: key ) ;
@ 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 > ( ) ;
2021-04-23 16:46:35 +00:00
List < Group > _groups ;
2021-04-23 17:32:34 +00:00
int _currChoice ;
bool get canGoNext = > _currChoice ! = null & & _currChoice > 0 ;
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 ,
errorMessage: tr ( " Failed to load the list of Forez groups! " ) ) ;
2021-04-23 17:32:34 +00:00
Widget onBuild ( ) = > ConstrainedBox (
constraints: BoxConstraints ( maxWidth: 300 ) ,
child: Column (
children: [
Text ( tr ( " Please choose now the Forez group you want to join... " ) ) ,
] . . addAll ( _groups . map ( ( e ) = > RadioListTile (
value: e . id ,
groupValue: _currChoice ,
onChanged: ( v ) = > setState ( ( ) = > _currChoice = _currChoice ) ,
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-23 17:41:14 +00:00
tr ( " You can not access this group yet, please wait for a member of the group to accept your request. \n Hopefully this will not be too long. \n Please check back soon! " ) ) ;
_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 ) ;
snack ( context , tr ( " Failed to register to group! " ) ) ;
2021-04-23 17:41:14 +00:00
_key . currentState . refresh ( ) ;
2021-04-23 17:32:34 +00:00
return false ;
}
}
2021-04-23 16:46:35 +00:00
}