diff --git a/lib/presentation/screen/community_list/community_list_screen.dart b/lib/presentation/screen/community_list/community_list_screen.dart index 7b6c21c..a49aae4 100644 --- a/lib/presentation/screen/community_list/community_list_screen.dart +++ b/lib/presentation/screen/community_list/community_list_screen.dart @@ -1,5 +1,3 @@ -import 'dart:developer'; - import 'package:amity_sdk/amity_sdk.dart'; import 'package:flutter/material.dart'; import 'package:flutter_social_sample_app/core/route/app_route.dart'; @@ -16,9 +14,7 @@ class CommunityListScreen extends StatefulWidget { } class _CommunityListScreenState extends State { - // var amityCommunities = []; - final List _amityCommunities = []; - final List _amityCommunitiesForFeed = []; + var amityCommunities = []; final scrollcontroller = ScrollController(); bool loading = false; @@ -37,14 +33,11 @@ class _CommunityListScreenState extends State { @override void initState() { - communityLiveCollectionInit(); - initMyCommunity(); super.initState(); } - void communityLiveCollectionInit() { - + void communityLiveCollectionInit() { communityLiveCollection = CommunityLiveCollection( request: () => AmitySocialClient.newCommunityRepository() .getCommunities() @@ -55,61 +48,24 @@ class _CommunityListScreenState extends State { .includeDeleted(_includeDelete) .build()); - - // communityLiveCollection.getStreamController().stream.listen((event) { - // if (mounted) { - // setState(() { - // amityCommunities = event; - // }); - // } - // }); - - WidgetsBinding.instance.addPostFrameCallback((timeStamp) { - communityLiveCollection.loadNext(); - }); - - scrollcontroller.addListener(pagination); - } - - Future initMyCommunity([String? keyword]) async { - final repository = AmitySocialClient.newCommunityRepository() - .getCommunities() - .filter(AmityCommunityFilter.MEMBER) - .includeDeleted(false); - if (keyword != null && keyword.isNotEmpty) { - repository.withKeyword( - keyword); // Add keyword filtering only if keyword is provided and not empty - } - communityLiveCollection = repository.getLiveCollection(pageSize: 20); communityLiveCollection.getStreamController().stream.listen((event) { - print("getStreamController"); - _amityCommunitiesForFeed.clear(); - _amityCommunitiesForFeed.addAll(event); - - _amityCommunities.clear(); - _amityCommunities.addAll(event); - - setState(() { - - }); - - }).onError((error, stackTrace) { - log("error:${error.error.toString()}"); - // await AmityDialog().showAlertErrorDialog( - // title: "Error!", - // message: _communityController.error.toString()); + if (mounted) { + setState(() { + amityCommunities = event; + }); + } }); - scrollcontroller.removeListener(() {}); scrollcontroller.addListener(pagination); } void pagination() { if ((scrollcontroller.position.pixels == - scrollcontroller.position.maxScrollExtent) && - communityLiveCollection.hasNextPage()) { - communityLiveCollection.loadNext(); - } + scrollcontroller.position.maxScrollExtent) && + communityLiveCollection.hasNextPage()) { + print("TriggerPagination"); + communityLiveCollection.loadNext(); + } } @override @@ -124,147 +80,155 @@ class _CommunityListScreenState extends State { onChanged: (value) { _debouncer.run(() { _keyboard = value; - communityLiveCollection.reset(); - communityLiveCollectionInit(); }); }, decoration: const InputDecoration(hintText: 'Enter Keybaord'), ), ), - Container( - child: Row( - children: [ - Container( - padding: const EdgeInsets.all(8), - child: PopupMenuButton( - itemBuilder: (context) { - return [ - PopupMenuItem( - value: 1, - child: Text(AmityCommunityFilter.ALL.name), - ), - PopupMenuItem( - value: 2, - child: Text(AmityCommunityFilter.MEMBER.name), - ), - PopupMenuItem( - value: 3, - child: Text(AmityCommunityFilter.NOT_MEMBER.name), - ) - ]; - }, - child: const Icon( - Icons.filter_alt_rounded, - size: 18, - ), - onSelected: (index) { - if (index == 1) { - _filter = AmityCommunityFilter.ALL; - } - if (index == 2) { - _filter = AmityCommunityFilter.MEMBER; - } - if (index == 3) { - _filter = AmityCommunityFilter.NOT_MEMBER; - } - - communityLiveCollection.reset(); - communityLiveCollectionInit(); - }, + Row( + children: [ + Container( + padding: const EdgeInsets.all(8), + child: PopupMenuButton( + itemBuilder: (context) { + return [ + PopupMenuItem( + value: 1, + child: Text(AmityCommunityFilter.ALL.name), + ), + PopupMenuItem( + value: 2, + child: Text(AmityCommunityFilter.MEMBER.name), + ), + PopupMenuItem( + value: 3, + child: Text(AmityCommunityFilter.NOT_MEMBER.name), + ) + ]; + }, + child: const Icon( + Icons.filter_alt_rounded, + size: 18, ), + onSelected: (index) { + if (index == 1) { + _filter = AmityCommunityFilter.ALL; + } + if (index == 2) { + _filter = AmityCommunityFilter.MEMBER; + } + if (index == 3) { + _filter = AmityCommunityFilter.NOT_MEMBER; + } + }, ), - Container( - padding: const EdgeInsets.all(8), - child: PopupMenuButton( - itemBuilder: (context) { - return [ - PopupMenuItem( - - child: Text(AmityCommunitySortOption.DISPLAY_NAME.name), - value: 1, - ), - PopupMenuItem( - child: Text(AmityCommunitySortOption.FIRST_CREATED.name), - value: 2, - ), - PopupMenuItem( - child: Text(AmityCommunitySortOption.LAST_CREATED.name), - value: 3, - ) - ]; - }, - child: const Icon( - Icons.sort_rounded, - size: 18, - ), - onSelected: (index) { - if (index == 1) { - _sort = AmityCommunitySortOption.DISPLAY_NAME; - } - if (index == 2) { - _sort = AmityCommunitySortOption.FIRST_CREATED; - } - if (index == 3) { - _sort = AmityCommunitySortOption.LAST_CREATED; - } - communityLiveCollection.reset(); - communityLiveCollectionInit(); - }, + ), + Container( + padding: const EdgeInsets.all(8), + child: PopupMenuButton( + itemBuilder: (context) { + return [ + PopupMenuItem( + value: 1, + child: + Text(AmityCommunitySortOption.DISPLAY_NAME.name), + ), + PopupMenuItem( + value: 2, + child: + Text(AmityCommunitySortOption.FIRST_CREATED.name), + ), + PopupMenuItem( + value: 3, + child: + Text(AmityCommunitySortOption.LAST_CREATED.name), + ) + ]; + }, + child: const Icon( + Icons.sort_rounded, + size: 18, ), + onSelected: (index) { + if (index == 1) { + _sort = AmityCommunitySortOption.DISPLAY_NAME; + } + if (index == 2) { + _sort = AmityCommunitySortOption.FIRST_CREATED; + } + if (index == 3) { + _sort = AmityCommunitySortOption.LAST_CREATED; + } + }, ), - Container( - padding: const EdgeInsets.all(8), - child: InkWell( - child: const Icon(Icons.tag, size: 18), - onTap: () { - EditTextDialog.show(context, title: 'Enter tags, separate by comma', hintText: 'type tags here', - onPress: (value) { - if (value.isNotEmpty) { - _tags = value.trim().split(','); - } else { - _tags = null; - } + ), + Container( + padding: const EdgeInsets.all(8), + child: InkWell( + child: const Icon(Icons.tag, size: 18), + onTap: () { + EditTextDialog.show(context, + title: 'Enter tags, separate by comma', + hintText: 'type tags here', onPress: (value) { + if (value.isNotEmpty) { + _tags = value.trim().split(','); + } else { + _tags = null; + } + }); + }, + ), + ), + Container( + padding: const EdgeInsets.all(8), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Checkbox( + value: _includeDelete, + onChanged: (value) { + setState(() { + _includeDelete = (value ?? false); + }); + }, + ), + const Text('Include Delete') + ], + ), + ), + Container( + padding: const EdgeInsets.all(8), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + ElevatedButton( + onPressed: () { communityLiveCollection.reset(); communityLiveCollectionInit(); - }); - }, - ), - ), - Container( - padding: const EdgeInsets.all(8), - child: Row( - mainAxisSize: MainAxisSize.min, - children: [ - Checkbox( - value: _includeDelete, - onChanged: (value) { - setState(() { - _includeDelete = (value ?? false); - communityLiveCollection.reset(); - - communityLiveCollectionInit(); - }); - }, + communityLiveCollection.loadNext(); + }, + child: const Text( + "Query", ), - const Text('Include Delete') - ], - ), - ) - ], - ), + ) + ], + ), + ) + ], ), Expanded( - child: _amityCommunities.isNotEmpty + child: amityCommunities.isNotEmpty ? RefreshIndicator( onRefresh: () async { communityLiveCollection.reset(); communityLiveCollectionInit(); + communityLiveCollection.loadNext(); }, child: ListView.builder( controller: scrollcontroller, - itemCount: _amityCommunities.length, + itemCount: amityCommunities.length, itemBuilder: (context, index) { - final amityCommunity = _amityCommunities[index]; + final amityCommunity = amityCommunities[index]; return Container( margin: const EdgeInsets.all(12), child: CommunityWidget( @@ -286,7 +250,7 @@ class _CommunityListScreenState extends State { : const Text('No Post'), ), ), - if (_amityCommunities.isNotEmpty && communityLiveCollection.isFetching) + if (amityCommunities.isNotEmpty && communityLiveCollection.isFetching) Container( alignment: Alignment.center, child: const CircularProgressIndicator(),