mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Fix user access denied page
This commit is contained in:
		
							
								
								
									
										117
									
								
								lib/ui/screens/user_access_denied_screen.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								lib/ui/screens/user_access_denied_screen.dart
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,117 @@
 | 
			
		||||
import 'package:comunic/helpers/friends_helper.dart';
 | 
			
		||||
import 'package:comunic/helpers/users_helper.dart';
 | 
			
		||||
import 'package:comunic/models/friend_status.dart';
 | 
			
		||||
import 'package:comunic/models/user.dart';
 | 
			
		||||
import 'package:comunic/ui/routes/home_route.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/FrienshipStatusWidget.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/account_image_widget.dart';
 | 
			
		||||
import 'package:comunic/utils/intl_utils.dart';
 | 
			
		||||
import 'package:comunic/utils/ui_utils.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
 | 
			
		||||
/// User access denied screen
 | 
			
		||||
///
 | 
			
		||||
/// @author Pierre HUBERT
 | 
			
		||||
 | 
			
		||||
class UserAccessDeniedScreen extends StatefulWidget {
 | 
			
		||||
  final int userID;
 | 
			
		||||
 | 
			
		||||
  const UserAccessDeniedScreen({Key key, @required this.userID})
 | 
			
		||||
      : assert(userID != null),
 | 
			
		||||
        super(key: key);
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  _UserAccessDeniedScreenState createState() => _UserAccessDeniedScreenState();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class _UserAccessDeniedScreenState extends State<UserAccessDeniedScreen> {
 | 
			
		||||
  final UsersHelper usersHelper = UsersHelper();
 | 
			
		||||
  final FriendsHelper friendsHelper = FriendsHelper();
 | 
			
		||||
 | 
			
		||||
  GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
 | 
			
		||||
      GlobalKey<RefreshIndicatorState>();
 | 
			
		||||
 | 
			
		||||
  FriendStatus _status;
 | 
			
		||||
  User _user;
 | 
			
		||||
  bool _error = false;
 | 
			
		||||
 | 
			
		||||
  void setError(bool e) => setState(() => _error = e);
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  void didChangeDependencies() {
 | 
			
		||||
    super.didChangeDependencies();
 | 
			
		||||
    refresh();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> refresh() async {
 | 
			
		||||
    try {
 | 
			
		||||
      final status = await friendsHelper.getFriendshipStatus(widget.userID);
 | 
			
		||||
      final user = await usersHelper.getSingleWithThrow(widget.userID);
 | 
			
		||||
 | 
			
		||||
      // Check if the two users are friend now
 | 
			
		||||
      if (status.areFriend) {
 | 
			
		||||
        final controller = HomeController.of(context);
 | 
			
		||||
        controller.popPage();
 | 
			
		||||
        controller.openUserPage(widget.userID);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      setState(() {
 | 
			
		||||
        _status = status;
 | 
			
		||||
        _user = user;
 | 
			
		||||
      });
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      setError(true);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    if (!_error && _status == null) return buildLoadingPage(showAppBar: true);
 | 
			
		||||
 | 
			
		||||
    return Scaffold(
 | 
			
		||||
      appBar: AppBar(
 | 
			
		||||
        title: Text(_error ? "Error" : _user.displayName),
 | 
			
		||||
      ),
 | 
			
		||||
      body: RefreshIndicator(
 | 
			
		||||
        key: _refreshIndicatorKey,
 | 
			
		||||
        onRefresh: refresh,
 | 
			
		||||
        child: ListView.builder(
 | 
			
		||||
          itemBuilder: (c, i) => _scrollBuild(),
 | 
			
		||||
          itemCount: 1,
 | 
			
		||||
        ),
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _scrollBuild() {
 | 
			
		||||
    if (_error) return _buildError();
 | 
			
		||||
 | 
			
		||||
    return _buildPage();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildError() {
 | 
			
		||||
    return buildErrorCard("Could get information about your friendship!");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildPage() {
 | 
			
		||||
    return Center(
 | 
			
		||||
      child: Column(
 | 
			
		||||
        children: <Widget>[
 | 
			
		||||
          AccountImageWidget(
 | 
			
		||||
            user: _user,
 | 
			
		||||
            width: 75,
 | 
			
		||||
          ),
 | 
			
		||||
          Text(
 | 
			
		||||
            _user.displayName,
 | 
			
		||||
            style: TextStyle(fontSize: 25.0),
 | 
			
		||||
          ),
 | 
			
		||||
          Text(tr("This account is private.")),
 | 
			
		||||
          FriendshipStatusWidget(
 | 
			
		||||
            status: _status,
 | 
			
		||||
            onFriendshipUpdated: () => _refreshIndicatorKey.currentState.show(),
 | 
			
		||||
          )
 | 
			
		||||
        ],
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										229
									
								
								lib/ui/screens/user_page_screen.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										229
									
								
								lib/ui/screens/user_page_screen.dart
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,229 @@
 | 
			
		||||
import 'package:comunic/enums/post_target.dart';
 | 
			
		||||
import 'package:comunic/helpers/posts_helper.dart';
 | 
			
		||||
import 'package:comunic/helpers/users_helper.dart';
 | 
			
		||||
import 'package:comunic/models/advanced_user_info.dart';
 | 
			
		||||
import 'package:comunic/ui/routes/home_route.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/network_image_widget.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/post_create_form_widget.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/posts_list_widget.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/scroll_watcher.dart';
 | 
			
		||||
import 'package:comunic/utils/conversations_utils.dart';
 | 
			
		||||
import 'package:comunic/utils/intl_utils.dart';
 | 
			
		||||
import 'package:comunic/utils/ui_utils.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
 | 
			
		||||
/// User page route
 | 
			
		||||
///
 | 
			
		||||
/// @author Pierre HUBERT
 | 
			
		||||
 | 
			
		||||
enum _PageStatus { LOADING, ERROR, READY }
 | 
			
		||||
 | 
			
		||||
enum _MenuOptions { FRIENDS_LIST }
 | 
			
		||||
 | 
			
		||||
class UserPageScreen extends StatefulWidget {
 | 
			
		||||
  final int userID;
 | 
			
		||||
 | 
			
		||||
  const UserPageScreen({Key key, @required this.userID})
 | 
			
		||||
      : assert(userID != null),
 | 
			
		||||
        super(key: key);
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  _UserPageScreenState createState() => _UserPageScreenState();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class _UserPageScreenState extends State<UserPageScreen> {
 | 
			
		||||
  // Helpers
 | 
			
		||||
  final usersHelper = UsersHelper();
 | 
			
		||||
  final PostsHelper _postsHelper = PostsHelper();
 | 
			
		||||
 | 
			
		||||
  // Objects members
 | 
			
		||||
  final double _appBarHeight = 256.0;
 | 
			
		||||
  _PageStatus _status = _PageStatus.LOADING;
 | 
			
		||||
  AdvancedUserInfo _userInfo;
 | 
			
		||||
  GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
 | 
			
		||||
      GlobalKey<RefreshIndicatorState>();
 | 
			
		||||
 | 
			
		||||
  // Scroll detection (to load more user posts automatically)
 | 
			
		||||
  final _postListKey = GlobalKey<PostsListWidgetState>();
 | 
			
		||||
  ScrollWatcher _scrollWatcher;
 | 
			
		||||
 | 
			
		||||
  _setStatus(_PageStatus s) => setState(() => _status = s);
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  void initState() {
 | 
			
		||||
    _scrollWatcher = ScrollWatcher(
 | 
			
		||||
        onReachBottom: () => _postListKey.currentState.reachedPostsBottom());
 | 
			
		||||
    super.initState();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  void didChangeDependencies() {
 | 
			
		||||
    super.didChangeDependencies();
 | 
			
		||||
    _getUserInfo();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> _getUserInfo() async {
 | 
			
		||||
    _setStatus(_PageStatus.LOADING);
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      final user = await usersHelper.getAdvancedInfo(widget.userID);
 | 
			
		||||
 | 
			
		||||
      setState(() {
 | 
			
		||||
        _userInfo = user;
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      _setStatus(_PageStatus.READY);
 | 
			
		||||
    } on GetUserAdvancedUserError catch (e) {
 | 
			
		||||
      _setStatus(_PageStatus.ERROR);
 | 
			
		||||
 | 
			
		||||
      if (e.cause == GetUserAdvancedInformationErrorCause.NOT_AUTHORIZED) {
 | 
			
		||||
        final controller = HomeController.of(context);
 | 
			
		||||
        controller.popPage();
 | 
			
		||||
        controller.openUserAccessDeniedPage(widget.userID);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    if (_status == _PageStatus.LOADING) return buildLoadingPage();
 | 
			
		||||
 | 
			
		||||
    if (_status == _PageStatus.ERROR) return _buildError();
 | 
			
		||||
 | 
			
		||||
    return Scaffold(
 | 
			
		||||
      body: RefreshIndicator(
 | 
			
		||||
        key: _refreshIndicatorKey,
 | 
			
		||||
        child: CustomScrollView(
 | 
			
		||||
          slivers: <Widget>[_buildHeader(), _buildBody()],
 | 
			
		||||
          physics: AlwaysScrollableScrollPhysics(),
 | 
			
		||||
          controller: _scrollWatcher,
 | 
			
		||||
        ),
 | 
			
		||||
        onRefresh: _getUserInfo,
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Error card
 | 
			
		||||
  Widget _buildError() {
 | 
			
		||||
    return Scaffold(
 | 
			
		||||
      appBar: AppBar(
 | 
			
		||||
        title: Text(tr("Error")),
 | 
			
		||||
      ),
 | 
			
		||||
      body: Center(
 | 
			
		||||
          child:
 | 
			
		||||
              buildErrorCard(tr("Could not get user information!"), actions: [
 | 
			
		||||
        FlatButton(
 | 
			
		||||
          onPressed: _getUserInfo,
 | 
			
		||||
          child: Text(
 | 
			
		||||
            tr("Retry").toUpperCase(),
 | 
			
		||||
            style: TextStyle(color: Colors.white),
 | 
			
		||||
          ),
 | 
			
		||||
        )
 | 
			
		||||
      ])),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildHeader() {
 | 
			
		||||
    return SliverAppBar(
 | 
			
		||||
      expandedHeight: _appBarHeight,
 | 
			
		||||
      floating: false,
 | 
			
		||||
      pinned: true,
 | 
			
		||||
      snap: false,
 | 
			
		||||
      flexibleSpace: FlexibleSpaceBar(
 | 
			
		||||
        title: Text(_userInfo.displayName),
 | 
			
		||||
        background: Stack(
 | 
			
		||||
          fit: StackFit.expand,
 | 
			
		||||
          children: <Widget>[
 | 
			
		||||
            NetworkImageWidget(
 | 
			
		||||
              url: _userInfo.accountImageURL,
 | 
			
		||||
              height: _appBarHeight,
 | 
			
		||||
              roundedEdges: false,
 | 
			
		||||
            ),
 | 
			
		||||
            // This gradient ensures that the toolbar icons are distinct
 | 
			
		||||
            // against the background image.
 | 
			
		||||
            const DecoratedBox(
 | 
			
		||||
              decoration: BoxDecoration(
 | 
			
		||||
                gradient: LinearGradient(
 | 
			
		||||
                  begin: Alignment(0.0, -1.0),
 | 
			
		||||
                  end: Alignment(0.0, -0.4),
 | 
			
		||||
                  colors: <Color>[Color(0x60000000), Color(0x00000000)],
 | 
			
		||||
                ),
 | 
			
		||||
              ),
 | 
			
		||||
            ),
 | 
			
		||||
 | 
			
		||||
            const DecoratedBox(
 | 
			
		||||
              decoration: BoxDecoration(
 | 
			
		||||
                gradient: LinearGradient(
 | 
			
		||||
                  begin: Alignment(0.0, 0.4),
 | 
			
		||||
                  end: Alignment(0.0, 0.9),
 | 
			
		||||
                  colors: <Color>[Color(0x00000000), Color(0x60000000)],
 | 
			
		||||
                ),
 | 
			
		||||
              ),
 | 
			
		||||
            ),
 | 
			
		||||
          ],
 | 
			
		||||
        ),
 | 
			
		||||
      ),
 | 
			
		||||
      actions: <Widget>[
 | 
			
		||||
        IconButton(
 | 
			
		||||
          icon: Icon(
 | 
			
		||||
            Icons.chat,
 | 
			
		||||
          ),
 | 
			
		||||
          onPressed: () {
 | 
			
		||||
            openPrivateConversation(context, widget.userID);
 | 
			
		||||
          },
 | 
			
		||||
        ),
 | 
			
		||||
        PopupMenuButton<_MenuOptions>(
 | 
			
		||||
          itemBuilder: (c) => [
 | 
			
		||||
            PopupMenuItem(
 | 
			
		||||
              child: Text(tr("Friends")),
 | 
			
		||||
              enabled: _userInfo != null,
 | 
			
		||||
              value: _MenuOptions.FRIENDS_LIST,
 | 
			
		||||
            )
 | 
			
		||||
          ],
 | 
			
		||||
          onSelected: _selectedMenuOption,
 | 
			
		||||
        ),
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildBody() {
 | 
			
		||||
    return SliverList(
 | 
			
		||||
      delegate: SliverChildListDelegate(
 | 
			
		||||
        <Widget>[
 | 
			
		||||
          // Posts create form
 | 
			
		||||
          _userInfo.canPostTexts
 | 
			
		||||
              ? PostCreateFormWidget(
 | 
			
		||||
                  postTarget: PostTarget.USER_PAGE,
 | 
			
		||||
                  targetID: _userInfo.id,
 | 
			
		||||
                  onCreated: _postCreated,
 | 
			
		||||
                )
 | 
			
		||||
              : Container(),
 | 
			
		||||
 | 
			
		||||
          // Posts list
 | 
			
		||||
          PostsListWidget(
 | 
			
		||||
            key: _postListKey,
 | 
			
		||||
            getPostsList: () => _postsHelper.getUserPosts(widget.userID),
 | 
			
		||||
            getOlder: (from) =>
 | 
			
		||||
                _postsHelper.getUserPosts(widget.userID, from: from),
 | 
			
		||||
            showPostsTarget: false,
 | 
			
		||||
            buildListView: false,
 | 
			
		||||
          ),
 | 
			
		||||
        ],
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Method called each time a menu option is selected
 | 
			
		||||
  void _selectedMenuOption(_MenuOptions value) {
 | 
			
		||||
    switch (value) {
 | 
			
		||||
      case _MenuOptions.FRIENDS_LIST:
 | 
			
		||||
        HomeController.of(context).openUserFriendsList(_userInfo.id);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Method called once a post has been created
 | 
			
		||||
  void _postCreated() {
 | 
			
		||||
    _refreshIndicatorKey.currentState.show();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user