Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live collection implementation on community post and user post #207

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading