Skip to content

Commit

Permalink
Live collection implementation on community post and user post (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
umareko authored Dec 22, 2023
1 parent 99917fe commit 2dee330
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 71 deletions.
117 changes: 71 additions & 46 deletions lib/presentation/screen/community_feed/community_feed_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,85 @@ class CommunityFeedScreen extends StatefulWidget {
}

class _CommunityFeedScreenState extends State<CommunityFeedScreen> {
late PagingController<AmityPost> _controller;
final amityPosts = <AmityPost>[];

// late PagingController<AmityPost> _controller;
List<AmityPost> amityPosts = <AmityPost>[];
late PostLiveCollection postLiveCollection;
final scrollcontroller = ScrollController();
bool loading = false;
AmityPostSortOption _sortOption = AmityPostSortOption.LAST_CREATED;
final List<AmityDataType> _dataType = [];
List<String> _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();
}
}

Expand Down Expand Up @@ -140,8 +165,8 @@ class _CommunityFeedScreenState extends State<CommunityFeedScreen> {
}
}

_controller.reset();
_controller.fetchNextPage();
postLiveCollection.reset();
postLiveCollection.getFirstPageRequest();
},
),
),
Expand Down Expand Up @@ -174,8 +199,8 @@ class _CommunityFeedScreenState extends State<CommunityFeedScreen> {
_sortOption = AmityPostSortOption.LAST_CREATED;
}

_controller.reset();
_controller.fetchNextPage();
postLiveCollection.reset();
postLiveCollection.getFirstPageRequest();
},
),
),
Expand All @@ -193,8 +218,8 @@ class _CommunityFeedScreenState extends State<CommunityFeedScreen> {
if (value.isEmpty) {
_tags = [];
}
_controller.reset();
_controller.fetchNextPage();
postLiveCollection.reset();
postLiveCollection.getFirstPageRequest();
});
},
),
Expand All @@ -206,8 +231,8 @@ class _CommunityFeedScreenState extends State<CommunityFeedScreen> {
child: amityPosts.isNotEmpty
? RefreshIndicator(
onRefresh: () async {
_controller.reset();
_controller.fetchNextPage();
postLiveCollection.reset();
postLiveCollection.getFirstPageRequest();
},
child: ListView.builder(
controller: scrollcontroller,
Expand All @@ -224,12 +249,12 @@ class _CommunityFeedScreenState extends State<CommunityFeedScreen> {
)
: 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(),
Expand Down
2 changes: 2 additions & 0 deletions lib/presentation/screen/login/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class _LoginScreenState extends State<LoginScreen> {
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();
Expand Down
74 changes: 49 additions & 25 deletions lib/presentation/screen/user_post/user_post_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,66 @@ class UserPostScreen extends StatefulWidget {
}

class _UserPostScreenState extends State<UserPostScreen> {
late PagingController<AmityPost> _controller;
final amityPosts = <AmityPost>[];
List<AmityPost> amityPosts = <AmityPost>[];

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();
}
}

Expand All @@ -56,16 +80,16 @@ class _UserPostScreenState extends State<UserPostScreen> {
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,
Expand All @@ -79,7 +103,7 @@ class _UserPostScreenState extends State<UserPostScreen> {
),
),
),
if (_controller.isFetching)
if (postLiveCollection.isFetching)
Container(
alignment: Alignment.center,
child: const CircularProgressIndicator(),
Expand Down

0 comments on commit 2dee330

Please sign in to comment.