1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00
comunicmobile/lib/ui/screens/user_page_screen.dart

125 lines
3.5 KiB
Dart
Raw Normal View History

2020-05-16 15:21:33 +00:00
import 'package:comunic/helpers/friends_helper.dart';
2019-06-10 12:24:34 +00:00
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/models/advanced_user_info.dart';
2020-05-16 15:21:33 +00:00
import 'package:comunic/models/friend_status.dart';
2020-05-05 11:21:37 +00:00
import 'package:comunic/ui/routes/main_route/main_route.dart';
2020-05-16 08:20:27 +00:00
import 'package:comunic/ui/widgets/mobile_mode/user_page_mobile.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
2020-05-16 09:04:58 +00:00
import 'package:comunic/ui/widgets/tablet_mode/user_page_tablet.dart';
2020-05-16 15:21:33 +00:00
import 'package:comunic/utils/account_utils.dart';
2019-06-10 12:24:34 +00:00
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 }
2020-04-16 11:26:04 +00:00
class UserPageScreen extends StatefulWidget {
2019-06-10 12:24:34 +00:00
final int userID;
const UserPageScreen({Key? key, required this.userID})
2022-03-11 15:41:29 +00:00
: super(key: key);
2019-06-10 12:24:34 +00:00
@override
2020-04-16 11:26:04 +00:00
_UserPageScreenState createState() => _UserPageScreenState();
2019-06-10 12:24:34 +00:00
}
2020-05-16 08:20:27 +00:00
class _UserPageScreenState extends SafeState<UserPageScreen> {
2019-06-10 12:24:34 +00:00
// Helpers
final usersHelper = UsersHelper();
// Objects members
_PageStatus _status = _PageStatus.LOADING;
late AdvancedUserInfo _userInfo;
FriendStatus? _frienshipStatus;
2020-05-16 08:20:27 +00:00
final _refreshIndicatorKey = GlobalKey<RefreshIndicatorState>();
2019-06-10 12:24:34 +00:00
_setStatus(_PageStatus s) => setState(() => _status = s);
@override
void didChangeDependencies() {
super.didChangeDependencies();
_getUserInfo();
}
Future<void> _getUserInfo() async {
_setStatus(_PageStatus.LOADING);
try {
final user = await usersHelper.getAdvancedInfo(widget.userID);
2020-05-16 15:21:33 +00:00
final status = widget.userID == userID()
? null
: await FriendsHelper().getFriendshipStatus(widget.userID);
2019-06-10 12:24:34 +00:00
setState(() {
_userInfo = user;
2020-05-16 15:21:33 +00:00
_frienshipStatus = status;
2019-06-10 12:24:34 +00:00
});
_setStatus(_PageStatus.READY);
2019-06-15 14:01:58 +00:00
} on GetUserAdvancedUserError catch (e) {
2019-06-10 12:24:34 +00:00
_setStatus(_PageStatus.ERROR);
2019-06-15 14:01:58 +00:00
2020-04-16 11:26:04 +00:00
if (e.cause == GetUserAdvancedInformationErrorCause.NOT_AUTHORIZED) {
final controller = MainController.of(context)!;
2020-04-16 11:26:04 +00:00
controller.popPage();
controller.openUserAccessDeniedPage(widget.userID);
}
2020-05-16 15:21:33 +00:00
} catch (e, s) {
print("Could not refresh user information! $e\n$s");
_setStatus(_PageStatus.ERROR);
2019-06-10 12:24:34 +00:00
}
}
@override
Widget build(BuildContext context) {
if (_status == _PageStatus.LOADING) return buildLoadingPage();
if (_status == _PageStatus.ERROR) return _buildError();
return Scaffold(
2019-06-15 14:06:20 +00:00
body: RefreshIndicator(
2019-07-05 09:40:43 +00:00
key: _refreshIndicatorKey,
2020-05-08 07:23:02 +00:00
child: _buildBody(),
2019-06-15 14:06:20 +00:00
onRefresh: _getUserInfo,
2019-06-10 12:24:34 +00:00
),
);
}
/// Error card
Widget _buildError() {
return Scaffold(
appBar: AppBar(
title: Text(tr("Error")!),
2019-06-10 12:24:34 +00:00
),
body: Center(
child:
buildErrorCard(tr("Could not get user information!"), actions: [
2021-03-13 14:28:34 +00:00
TextButton(
2019-06-10 12:24:34 +00:00
onPressed: _getUserInfo,
child: Text(
tr("Retry")!.toUpperCase(),
2019-06-10 12:24:34 +00:00
style: TextStyle(color: Colors.white),
),
)
])),
);
}
2020-05-08 07:23:02 +00:00
Widget _buildBody() {
2020-05-16 09:04:58 +00:00
return isTablet(context)
? UserPageTablet(
userInfo: _userInfo,
onNeedRefresh: () => _refreshIndicatorKey.currentState!.show(),
2020-05-16 15:21:33 +00:00
friendshipStatus: _frienshipStatus,
2020-05-16 09:04:58 +00:00
)
: UserMobilePage(
userInfo: _userInfo,
onNeedRefresh: () => _refreshIndicatorKey.currentState!.show(),
2020-05-16 09:04:58 +00:00
);
2019-06-10 12:24:34 +00:00
}
}