mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 08:15:16 +00:00
Create user access denied route
This commit is contained in:
120
lib/ui/routes/user_access_denied_route.dart
Normal file
120
lib/ui/routes/user_access_denied_route.dart
Normal file
@ -0,0 +1,120 @@
|
||||
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/user_page_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 route
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
class UserAccessDeniedRoute extends StatefulWidget {
|
||||
final int userID;
|
||||
|
||||
const UserAccessDeniedRoute({Key key, @required this.userID})
|
||||
: assert(userID != null),
|
||||
super(key: key);
|
||||
|
||||
@override
|
||||
_UserAccessDeniedRouteState createState() => _UserAccessDeniedRouteState();
|
||||
}
|
||||
|
||||
class _UserAccessDeniedRouteState extends State<UserAccessDeniedRoute> {
|
||||
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)
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(
|
||||
builder: (c) => UserPageRoute(
|
||||
userID: 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(),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ 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/other_friends_lists_route.dart';
|
||||
import 'package:comunic/ui/routes/user_access_denied_route.dart';
|
||||
import 'package:comunic/ui/widgets/network_image_widget.dart';
|
||||
import 'package:comunic/ui/widgets/posts_list_widget.dart';
|
||||
import 'package:comunic/utils/conversations_utils.dart';
|
||||
@ -57,8 +58,17 @@ class _UserPageRouteState extends State<UserPageRoute> {
|
||||
});
|
||||
|
||||
_setStatus(_PageStatus.READY);
|
||||
} on GetUserAdvancedUserError {
|
||||
} on GetUserAdvancedUserError catch (e) {
|
||||
_setStatus(_PageStatus.ERROR);
|
||||
|
||||
if (e.cause == GetUserAdvancedInformationErrorCause.NOT_AUTHORIZED)
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(
|
||||
builder: (c) => UserAccessDeniedRoute(
|
||||
userID: widget.userID,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user