From 2dee330ec54629727f4b4b6c8341d3f0ea535776 Mon Sep 17 00:00:00 2001 From: Muhammad Umar Manzoor Chaudhary <124767245+umareko@users.noreply.github.com> Date: Fri, 22 Dec 2023 11:41:22 +0700 Subject: [PATCH] Live collection implementation on community post and user post (#207) --- .../community_feed/community_feed_screen.dart | 117 +++++++++++------- .../screen/login/login_screen.dart | 2 + .../screen/user_post/user_post_screen.dart | 74 +++++++---- 3 files changed, 122 insertions(+), 71 deletions(-) diff --git a/lib/presentation/screen/community_feed/community_feed_screen.dart b/lib/presentation/screen/community_feed/community_feed_screen.dart index 521a838..3b3f715 100644 --- a/lib/presentation/screen/community_feed/community_feed_screen.dart +++ b/lib/presentation/screen/community_feed/community_feed_screen.dart @@ -20,60 +20,85 @@ class CommunityFeedScreen extends StatefulWidget { } class _CommunityFeedScreenState extends State { - late PagingController _controller; - final amityPosts = []; - + // late PagingController _controller; + List amityPosts = []; + late PostLiveCollection postLiveCollection; final scrollcontroller = ScrollController(); bool loading = false; AmityPostSortOption _sortOption = AmityPostSortOption.LAST_CREATED; final List _dataType = []; List _tags = []; + @override void initState() { - _controller = PagingController( - pageFuture: (token) => AmitySocialClient.newPostRepository() - .getPosts() - .targetCommunity(widget.communityId) - .feedType(AmityFeedType.PUBLISHED) - .includeDeleted(false) - .types(_dataType) - .tags(_tags) - .sortBy(_sortOption) - .onlyParent(true) - .getPagingData(token: token, limit: GlobalConstant.pageSize), - pageSize: GlobalConstant.pageSize, - )..addListener( - () { - if (_controller.error == null) { - setState(() { - amityPosts.clear(); - amityPosts.addAll(_controller.loadedItems); - }); - } else { - //Error on pagination controller - setState(() {}); - ErrorDialog.show(context, - title: 'Error', message: _controller.error.toString()); - } - }, - ); + postLiveCollection = PostLiveCollection( + request: () => AmitySocialClient.newPostRepository() + .getPosts() + .targetCommunity(widget.communityId) + .feedType(AmityFeedType.PUBLISHED) + .includeDeleted(false) + .types(_dataType) + .tags(_tags) + .sortBy(_sortOption) + .onlyParent(true) + .build()); + + postLiveCollection.getStreamController().stream.listen((event) { + if (mounted) { + setState(() { + amityPosts = event; + }); + } + }); WidgetsBinding.instance.addPostFrameCallback((timeStamp) { - _controller.fetchNextPage(); + postLiveCollection.loadNext(); }); scrollcontroller.addListener(pagination); + // _controller = PagingController( + // pageFuture: (token) => AmitySocialClient.newPostRepository() + // .getPosts() + // .targetCommunity(widget.communityId) + // .feedType(AmityFeedType.PUBLISHED) + // .includeDeleted(false) + // .types(_dataType) + // .tags(_tags) + // .sortBy(_sortOption) + // .onlyParent(true) + // .getPagingData(token: token, limit: GlobalConstant.pageSize), + // pageSize: GlobalConstant.pageSize, + // )..addListener( + // () { + // if (_controller.error == null) { + // setState(() { + // amityPosts.clear(); + // amityPosts.addAll(_controller.loadedItems); + // }); + // } else { + // //Error on pagination controller + // setState(() {}); + // ErrorDialog.show(context, + // title: 'Error', message: _controller.error.toString()); + // } + // }, + // ); + + // WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + // _controller.fetchNextPage(); + // }); + + // scrollcontroller.addListener(pagination); + super.initState(); } void pagination() { if ((scrollcontroller.position.pixels == - scrollcontroller.position.maxScrollExtent) && - _controller.hasMoreItems) { - setState(() { - _controller.fetchNextPage(); - }); + (scrollcontroller.position.maxScrollExtent)) && + postLiveCollection.hasNextPage()) { + postLiveCollection.loadNext(); } } @@ -140,8 +165,8 @@ class _CommunityFeedScreenState extends State { } } - _controller.reset(); - _controller.fetchNextPage(); + postLiveCollection.reset(); + postLiveCollection.getFirstPageRequest(); }, ), ), @@ -174,8 +199,8 @@ class _CommunityFeedScreenState extends State { _sortOption = AmityPostSortOption.LAST_CREATED; } - _controller.reset(); - _controller.fetchNextPage(); + postLiveCollection.reset(); + postLiveCollection.getFirstPageRequest(); }, ), ), @@ -193,8 +218,8 @@ class _CommunityFeedScreenState extends State { if (value.isEmpty) { _tags = []; } - _controller.reset(); - _controller.fetchNextPage(); + postLiveCollection.reset(); + postLiveCollection.getFirstPageRequest(); }); }, ), @@ -206,8 +231,8 @@ class _CommunityFeedScreenState extends State { child: amityPosts.isNotEmpty ? RefreshIndicator( onRefresh: () async { - _controller.reset(); - _controller.fetchNextPage(); + postLiveCollection.reset(); + postLiveCollection.getFirstPageRequest(); }, child: ListView.builder( controller: scrollcontroller, @@ -224,12 +249,12 @@ class _CommunityFeedScreenState extends State { ) : Container( alignment: Alignment.center, - child: _controller.isFetching + child: postLiveCollection.isFetching ? const CircularProgressIndicator() : const Text('No Post'), ), ), - if (_controller.isFetching && amityPosts.isNotEmpty) + if (postLiveCollection.isFetching && amityPosts.isNotEmpty) Container( alignment: Alignment.center, child: const CircularProgressIndicator(), diff --git a/lib/presentation/screen/login/login_screen.dart b/lib/presentation/screen/login/login_screen.dart index bfb54ca..f312a4a 100644 --- a/lib/presentation/screen/login/login_screen.dart +++ b/lib/presentation/screen/login/login_screen.dart @@ -111,6 +111,8 @@ class _LoginScreenState extends State { key: const Key('Login_btn_id'), onPressed: () async { try { + // AmityCoreClient.newUserRepository(); + FocusManager.instance.primaryFocus!.unfocus(); // Setup the Amity Option First String apikey = _apiKeyTextController.text.trim(); diff --git a/lib/presentation/screen/user_post/user_post_screen.dart b/lib/presentation/screen/user_post/user_post_screen.dart index eded923..71126cc 100644 --- a/lib/presentation/screen/user_post/user_post_screen.dart +++ b/lib/presentation/screen/user_post/user_post_screen.dart @@ -12,42 +12,66 @@ class UserPostScreen extends StatefulWidget { } class _UserPostScreenState extends State { - late PagingController _controller; - final amityPosts = []; + List amityPosts = []; + late PostLiveCollection postLiveCollection; final scrollcontroller = ScrollController(); + bool loading = false; + + @override void initState() { - _controller = PagingController( - pageFuture: (token) => AmitySocialClient.newPostRepository() + + postLiveCollection = PostLiveCollection( + request: () => AmitySocialClient.newPostRepository() .getPosts() - .targetUser(widget.userId) - .getPagingData(token: token, limit: GlobalConstant.pageSize), - pageSize: GlobalConstant.pageSize, - )..addListener( - () { - setState(() { - amityPosts.clear(); - amityPosts.addAll(_controller.loadedItems); - }); - }, - ); + .targetUser(widget.userId).build(), + ); + + postLiveCollection.getStreamController().stream.listen((event) { + if (mounted) { + setState(() { + amityPosts = event; + }); + } + }); WidgetsBinding.instance.addPostFrameCallback((timeStamp) { - _controller.fetchNextPage(); + postLiveCollection.loadNext(); }); scrollcontroller.addListener(pagination); + // _controller = PagingController( + // pageFuture: (token) => AmitySocialClient.newPostRepository() + // .getPosts() + // .targetUser(widget.userId) + // .getPagingData(token: token, limit: GlobalConstant.pageSize), + // pageSize: GlobalConstant.pageSize, + // )..addListener( + // () { + // setState(() { + // amityPosts.clear(); + // amityPosts.addAll(_controller.loadedItems); + // }); + // }, + // ); + + // WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + // _controller.fetchNextPage(); + // }); + + // scrollcontroller.addListener(pagination); + super.initState(); } void pagination() { - if ((scrollcontroller.position.pixels == scrollcontroller.position.maxScrollExtent) && _controller.hasMoreItems) { - setState(() { - _controller.fetchNextPage(); - }); + if ((scrollcontroller.position.pixels == + (scrollcontroller.position.maxScrollExtent)) && + postLiveCollection.hasNextPage()) { + postLiveCollection.loadNext(); } } @@ -56,16 +80,16 @@ class _UserPostScreenState extends State { return Scaffold( appBar: widget.showAppBar ? AppBar(title: Text('User Post - ${widget.userId}')) : null, body: amityPosts.isEmpty - ? Center( - child: Text(_controller.error == null ? 'No Post Found' : _controller.error.toString()), + ? const Center( + child: Text('No Post Found' ), ) : Column( children: [ Expanded( child: RefreshIndicator( onRefresh: () async { - _controller.reset(); - _controller.fetchNextPage(); + postLiveCollection.reset(); + postLiveCollection.getFirstPageRequest(); }, child: ListView.builder( controller: scrollcontroller, @@ -79,7 +103,7 @@ class _UserPostScreenState extends State { ), ), ), - if (_controller.isFetching) + if (postLiveCollection.isFetching) Container( alignment: Alignment.center, child: const CircularProgressIndicator(),