mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Add RefreshIndicator on friends screen
This commit is contained in:
		@@ -33,28 +33,31 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
 | 
				
			|||||||
  _ErrorsLevel _error = _ErrorsLevel.NONE;
 | 
					  _ErrorsLevel _error = _ErrorsLevel.NONE;
 | 
				
			||||||
  FriendsList _friendsList;
 | 
					  FriendsList _friendsList;
 | 
				
			||||||
  UsersList _usersInfo;
 | 
					  UsersList _usersInfo;
 | 
				
			||||||
  bool _loading = true;
 | 
					  GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
 | 
				
			||||||
 | 
					      GlobalKey<RefreshIndicatorState>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Useful setters
 | 
					  /// Useful setters
 | 
				
			||||||
  set error(_ErrorsLevel err) => setState(() => _error = err);
 | 
					  set error(_ErrorsLevel err) => setState(() => _error = err);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  set loading(bool loading) => setState(() => _loading = loading);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  void _gotError() {
 | 
					  void _gotError() {
 | 
				
			||||||
    loading = false;
 | 
					 | 
				
			||||||
    error = _friendsList == null ? _ErrorsLevel.MAJOR : _ErrorsLevel.MINOR;
 | 
					    error = _friendsList == null ? _ErrorsLevel.MAJOR : _ErrorsLevel.MINOR;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  void didChangeDependencies() {
 | 
					  void didChangeDependencies() {
 | 
				
			||||||
    super.didChangeDependencies();
 | 
					    super.didChangeDependencies();
 | 
				
			||||||
    _loadList();
 | 
					    _loadList();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Refresh the list of friends
 | 
				
			||||||
 | 
					  Future<void> _refreshList() async {
 | 
				
			||||||
 | 
					    await _refreshIndicatorKey.currentState.show();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Load the list of friends
 | 
					  /// Load the list of friends
 | 
				
			||||||
  Future<void> _loadList() async {
 | 
					  Future<void> _loadList() async {
 | 
				
			||||||
    error = _ErrorsLevel.NONE;
 | 
					    error = _ErrorsLevel.NONE;
 | 
				
			||||||
    loading = true;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Get the list of friends
 | 
					    // Get the list of friends
 | 
				
			||||||
    final list = await _friendsHelper.downloadList();
 | 
					    final list = await _friendsHelper.downloadList();
 | 
				
			||||||
@@ -73,7 +76,6 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
 | 
				
			|||||||
      _friendsList = list;
 | 
					      _friendsList = list;
 | 
				
			||||||
      _usersInfo = users;
 | 
					      _usersInfo = users;
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    loading = false;
 | 
					 | 
				
			||||||
    error = _ErrorsLevel.NONE;
 | 
					    error = _ErrorsLevel.NONE;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -82,7 +84,7 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
 | 
				
			|||||||
        tr("Could not load your list of friends!"),
 | 
					        tr("Could not load your list of friends!"),
 | 
				
			||||||
        actions: [
 | 
					        actions: [
 | 
				
			||||||
          FlatButton(
 | 
					          FlatButton(
 | 
				
			||||||
            onPressed: _loadList,
 | 
					            onPressed: _refreshList,
 | 
				
			||||||
            child: Text(
 | 
					            child: Text(
 | 
				
			||||||
              tr("Retry").toUpperCase(),
 | 
					              tr("Retry").toUpperCase(),
 | 
				
			||||||
              style: TextStyle(color: Colors.white),
 | 
					              style: TextStyle(color: Colors.white),
 | 
				
			||||||
@@ -96,43 +98,33 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
 | 
				
			|||||||
    if (_error == _ErrorsLevel.MAJOR) return _buildError();
 | 
					    if (_error == _ErrorsLevel.MAJOR) return _buildError();
 | 
				
			||||||
    if (_friendsList == null) return buildCenteredProgressBar();
 | 
					    if (_friendsList == null) return buildCenteredProgressBar();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return Stack(
 | 
					    return Column(
 | 
				
			||||||
      children: <Widget>[
 | 
					      children: <Widget>[
 | 
				
			||||||
        Column(
 | 
					        // Check for errors
 | 
				
			||||||
          children: <Widget>[
 | 
					        Container(child: _error != _ErrorsLevel.NONE ? _buildError() : null),
 | 
				
			||||||
            // Check for errors
 | 
					 | 
				
			||||||
            Container(
 | 
					 | 
				
			||||||
                child: _error != _ErrorsLevel.NONE ? _buildError() : null),
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // List of friends
 | 
					        // List of friends
 | 
				
			||||||
            Expanded(
 | 
					        Expanded(
 | 
				
			||||||
              child: ListView.builder(
 | 
					          child: RefreshIndicator(
 | 
				
			||||||
                  itemCount: _friendsList.length,
 | 
					            key: _refreshIndicatorKey,
 | 
				
			||||||
                  itemBuilder: (c, i) => _friendsList[i].accepted
 | 
					            onRefresh: _loadList,
 | 
				
			||||||
                      ? AcceptedFriendTile(
 | 
					            child: ListView.builder(
 | 
				
			||||||
                          friend: _friendsList[i],
 | 
					                physics: AlwaysScrollableScrollPhysics(),
 | 
				
			||||||
                          user: _usersInfo.getUser(_friendsList[i].id),
 | 
					                itemCount: _friendsList.length,
 | 
				
			||||||
                          onOpenPrivateConversation: _openPrivateConversation,
 | 
					                itemBuilder: (c, i) => _friendsList[i].accepted
 | 
				
			||||||
                          onSetFollowing: _setFollowingFriend,
 | 
					                    ? AcceptedFriendTile(
 | 
				
			||||||
                          onRequestDelete: _deleteFriend,
 | 
					                        friend: _friendsList[i],
 | 
				
			||||||
                        )
 | 
					                        user: _usersInfo.getUser(_friendsList[i].id),
 | 
				
			||||||
                      : PendingFriendTile(
 | 
					                        onOpenPrivateConversation: _openPrivateConversation,
 | 
				
			||||||
                          friend: _friendsList[i],
 | 
					                        onSetFollowing: _setFollowingFriend,
 | 
				
			||||||
                          user: _usersInfo.getUser(_friendsList[i].id),
 | 
					                        onRequestDelete: _deleteFriend,
 | 
				
			||||||
                          onRespond: _respondRequest,
 | 
					                      )
 | 
				
			||||||
                        )),
 | 
					                    : PendingFriendTile(
 | 
				
			||||||
            ),
 | 
					                        friend: _friendsList[i],
 | 
				
			||||||
          ],
 | 
					                        user: _usersInfo.getUser(_friendsList[i].id),
 | 
				
			||||||
        ),
 | 
					                        onRespond: _respondRequest,
 | 
				
			||||||
 | 
					                      )),
 | 
				
			||||||
        // Check if loading
 | 
					          ),
 | 
				
			||||||
        Positioned(
 | 
					 | 
				
			||||||
          child: Container(
 | 
					 | 
				
			||||||
              child:
 | 
					 | 
				
			||||||
                  _loading ? Center(child: CircularProgressIndicator()) : null),
 | 
					 | 
				
			||||||
          top: 16.0,
 | 
					 | 
				
			||||||
          left: 0.0,
 | 
					 | 
				
			||||||
          right: 0.0,
 | 
					 | 
				
			||||||
        ),
 | 
					        ),
 | 
				
			||||||
      ],
 | 
					      ],
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
@@ -140,23 +132,19 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  /// Respond to friendship request
 | 
					  /// Respond to friendship request
 | 
				
			||||||
  Future<void> _respondRequest(Friend f, bool accept) async {
 | 
					  Future<void> _respondRequest(Friend f, bool accept) async {
 | 
				
			||||||
    loading = true;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (!await _friendsHelper.respondRequest(f.id, accept))
 | 
					    if (!await _friendsHelper.respondRequest(f.id, accept))
 | 
				
			||||||
      showSimpleSnack(context, tr("Could not respond to friendship request!"));
 | 
					      showSimpleSnack(context, tr("Could not respond to friendship request!"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Load the list of friends again
 | 
					    // Load the list of friends again
 | 
				
			||||||
    _loadList();
 | 
					    _refreshList();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Update following status of a friend
 | 
					  /// Update following status of a friend
 | 
				
			||||||
  Future<void> _setFollowingFriend(Friend friend, bool follow) async {
 | 
					  Future<void> _setFollowingFriend(Friend friend, bool follow) async {
 | 
				
			||||||
    loading = true;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (!await _friendsHelper.setFollowing(friend.id, follow))
 | 
					    if (!await _friendsHelper.setFollowing(friend.id, follow))
 | 
				
			||||||
      showSimpleSnack(context, tr("Could not update following status!"));
 | 
					      showSimpleSnack(context, tr("Could not update following status!"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _loadList();
 | 
					    _refreshList();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Handles deletion request of a friend
 | 
					  /// Handles deletion request of a friend
 | 
				
			||||||
@@ -186,19 +174,16 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
 | 
				
			|||||||
    if (choice == null || !choice) return;
 | 
					    if (choice == null || !choice) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Forward the request to the server
 | 
					    // Forward the request to the server
 | 
				
			||||||
    loading = true;
 | 
					 | 
				
			||||||
    if (!await _friendsHelper.removeFriend(f.id))
 | 
					    if (!await _friendsHelper.removeFriend(f.id))
 | 
				
			||||||
      showSimpleSnack(
 | 
					      showSimpleSnack(
 | 
				
			||||||
          context, tr("Could not delete this person from your friends list!"));
 | 
					          context, tr("Could not delete this person from your friends list!"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Refresh list
 | 
					    // Refresh list
 | 
				
			||||||
    _loadList();
 | 
					    _refreshList();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Open a private conversation for a given [friend]
 | 
					  /// Open a private conversation for a given [friend]
 | 
				
			||||||
  Future<void> _openPrivateConversation(Friend friend) async {
 | 
					  Future<void> _openPrivateConversation(Friend friend) async {
 | 
				
			||||||
    loading = true;
 | 
					 | 
				
			||||||
    await openPrivateConversation(context, friend.id);
 | 
					    await openPrivateConversation(context, friend.id);
 | 
				
			||||||
    loading = false;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user