1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 23:09:21 +00:00
comunicmobile/lib/ui/screens/user_access_denied_screen.dart

118 lines
3.0 KiB
Dart
Raw Normal View History

2019-06-15 14:01:58 +00:00
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';
2020-05-05 11:21:37 +00:00
import 'package:comunic/ui/routes/main_route/main_route.dart';
2019-06-15 14:01:58 +00:00
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';
2020-04-16 11:26:04 +00:00
/// User access denied screen
2019-06-15 14:01:58 +00:00
///
/// @author Pierre HUBERT
2020-04-16 11:26:04 +00:00
class UserAccessDeniedScreen extends StatefulWidget {
2019-06-15 14:01:58 +00:00
final int userID;
2020-04-16 11:26:04 +00:00
const UserAccessDeniedScreen({Key key, @required this.userID})
2019-06-15 14:01:58 +00:00
: assert(userID != null),
super(key: key);
@override
2020-04-16 11:26:04 +00:00
_UserAccessDeniedScreenState createState() => _UserAccessDeniedScreenState();
2019-06-15 14:01:58 +00:00
}
2020-04-16 11:26:04 +00:00
class _UserAccessDeniedScreenState extends State<UserAccessDeniedScreen> {
2019-06-15 14:01:58 +00:00
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
2020-04-16 11:26:04 +00:00
if (status.areFriend) {
2020-04-17 13:26:37 +00:00
final controller = MainController.of(context);
2020-04-16 11:26:04 +00:00
controller.popPage();
controller.openUserPage(widget.userID);
}
2019-06-15 14:01:58 +00:00
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(),
)
],
),
);
}
}