1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/ui/screens/user_page_screen.dart

197 lines
5.3 KiB
Dart
Raw Normal View History

2019-07-05 09:40:43 +00:00
import 'package:comunic/enums/post_target.dart';
2019-06-10 12:47:27 +00:00
import 'package:comunic/helpers/posts_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-04-16 11:13:31 +00:00
import 'package:comunic/ui/routes/home_route.dart';
2020-04-16 12:20:24 +00:00
import 'package:comunic/ui/widgets/account_image_widget.dart';
2019-07-05 09:40:43 +00:00
import 'package:comunic/ui/widgets/post_create_form_widget.dart';
2019-06-10 12:47:27 +00:00
import 'package:comunic/ui/widgets/posts_list_widget.dart';
2020-04-16 07:17:10 +00:00
import 'package:comunic/ui/widgets/scroll_watcher.dart';
2019-06-15 06:16:47 +00:00
import 'package:comunic/utils/conversations_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 }
2019-06-15 06:16:47 +00:00
enum _MenuOptions { FRIENDS_LIST }
2020-04-16 11:26:04 +00:00
class UserPageScreen extends StatefulWidget {
2019-06-10 12:24:34 +00:00
final int userID;
2020-04-16 11:26:04 +00:00
const UserPageScreen({Key key, @required this.userID})
2019-06-10 12:24:34 +00:00
: assert(userID != null),
super(key: key);
@override
2020-04-16 11:26:04 +00:00
_UserPageScreenState createState() => _UserPageScreenState();
2019-06-10 12:24:34 +00:00
}
2020-04-16 11:26:04 +00:00
class _UserPageScreenState extends State<UserPageScreen> {
2019-06-10 12:24:34 +00:00
// Helpers
final usersHelper = UsersHelper();
2019-06-10 12:47:27 +00:00
final PostsHelper _postsHelper = PostsHelper();
2019-06-10 12:24:34 +00:00
// Objects members
_PageStatus _status = _PageStatus.LOADING;
AdvancedUserInfo _userInfo;
2019-07-05 09:40:43 +00:00
GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
2019-06-10 12:24:34 +00:00
2020-04-16 07:17:10 +00:00
// Scroll detection (to load more user posts automatically)
final _postListKey = GlobalKey<PostsListWidgetState>();
ScrollWatcher _scrollWatcher;
2019-06-10 12:24:34 +00:00
_setStatus(_PageStatus s) => setState(() => _status = s);
2020-04-16 07:17:10 +00:00
@override
void initState() {
_scrollWatcher = ScrollWatcher(
onReachBottom: () => _postListKey.currentState.reachedPostsBottom());
super.initState();
}
2019-06-10 12:24:34 +00:00
@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);
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 = HomeController.of(context);
controller.popPage();
controller.openUserAccessDeniedPage(widget.userID);
}
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-04-16 12:20:24 +00:00
child: ListView(
children: <Widget>[_buildHeader(), _buildBody()],
2019-07-05 09:40:43 +00:00
physics: AlwaysScrollableScrollPhysics(),
2020-04-16 07:17:10 +00:00
controller: _scrollWatcher,
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")),
),
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() {
2020-04-16 12:20:24 +00:00
return Container(
color: Colors.black26,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
2019-06-10 12:24:34 +00:00
children: <Widget>[
2020-04-16 12:20:24 +00:00
AccountImageWidget(user: _userInfo),
Text(" ${_userInfo.displayName}"),
Spacer(),
IconButton(
icon: Icon(
Icons.chat,
2019-06-10 12:24:34 +00:00
),
2020-04-16 12:20:24 +00:00
onPressed: () {
openPrivateConversation(context, widget.userID);
}),
PopupMenuButton<_MenuOptions>(
itemBuilder: (c) => [
PopupMenuItem(
child: Text(tr("Friends")),
enabled: _userInfo != null,
value: _MenuOptions.FRIENDS_LIST,
)
],
onSelected: _selectedMenuOption,
2019-06-10 12:24:34 +00:00
),
],
),
),
);
}
Widget _buildBody() {
2020-04-16 12:20:24 +00:00
return Column(
children: <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,
),
],
2019-06-10 12:24:34 +00:00
);
}
2019-06-15 06:16:47 +00:00
/// Method called each time a menu option is selected
void _selectedMenuOption(_MenuOptions value) {
switch (value) {
case _MenuOptions.FRIENDS_LIST:
2020-04-16 11:13:31 +00:00
HomeController.of(context).openUserFriendsList(_userInfo.id);
2019-06-15 06:16:47 +00:00
break;
}
}
2019-07-05 09:40:43 +00:00
/// Method called once a post has been created
void _postCreated() {
_refreshIndicatorKey.currentState.show();
}
2019-06-10 12:24:34 +00:00
}