From 54f63d5c0f0db471f00fdbebc47006ba24140f91 Mon Sep 17 00:00:00 2001 From: Arya Pratap Singh Date: Mon, 18 Nov 2024 05:19:58 +0530 Subject: [PATCH] Update organization_search_list.dart --- lib/widgets/organization_search_list.dart | 113 +++++----------------- 1 file changed, 22 insertions(+), 91 deletions(-) diff --git a/lib/widgets/organization_search_list.dart b/lib/widgets/organization_search_list.dart index e7ad5e4bb..0591fd7a2 100644 --- a/lib/widgets/organization_search_list.dart +++ b/lib/widgets/organization_search_list.dart @@ -1,18 +1,16 @@ // ignore_for_file: talawa_good_doc_comments, talawa_api_doc -import 'package:flutter/cupertino.dart'; +import 'dart:async'; import 'package:flutter/material.dart'; -import 'package:graphql_flutter/graphql_flutter.dart'; import 'package:talawa/enums/enums.dart'; -import 'package:talawa/locator.dart'; import 'package:talawa/models/organization/org_info.dart'; -import 'package:talawa/utils/queries.dart'; import 'package:talawa/view_model/pre_auth_view_models/select_organization_view_model.dart'; import 'package:talawa/widgets/custom_list_tile.dart'; -import 'package:visibility_detector/visibility_detector.dart'; +/// This class returns the widget that shows all the matching orgs searched in the search bar. class OrganizationSearchList extends StatefulWidget { const OrganizationSearchList({required this.model, super.key}); + /// model constructor for the selectOrganisation widget. final SelectOrganizationViewModel model; @override @@ -20,71 +18,26 @@ class OrganizationSearchList extends StatefulWidget { } class _OrganizationSearchListState extends State { - static const int maxRefetch = 10; final ValueNotifier _refetchCount = ValueNotifier(0); + // Added a stream to simulate organization data for testing. + late StreamController> _orgDataController; + + @override + void initState() { + super.initState(); + _orgDataController = StreamController>(); + } + @override void dispose() { _refetchCount.dispose(); + _orgDataController.close(); super.dispose(); } @override Widget build(BuildContext context) { - return GraphQLProvider( - client: ValueNotifier(graphqlConfig.authClient()), - child: Query( - options: QueryOptions( - document: gql(Queries().fetchJoinInOrgByName), - variables: { - 'nameStartsWith': widget.model.searchController.text, - 'first': 30, - 'skip': 0, - }, - ), - builder: ( - QueryResult result, { - Future Function(FetchMoreOptions)? fetchMore, - Future Function()? refetch, - }) { - if (result.hasException) { - if (_refetchCount.value < maxRefetch) { - _refetchCount.value++; - refetch?.call(); - } else { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar( - content: Text( - 'Failed to load organizations. Please try again later.', - ), - ), - ); - } - } else if (!result.isLoading) { - final data = result.data; - if (data != null && data['organizationsConnection'] != null) { - try { - widget.model.organizations = OrgInfo().fromJsonToList( - data['organizationsConnection'], - ); - } catch (e) { - debugPrint('Error parsing organization data: $e'); - widget.model.organizations = []; - } - } - } - - return _buildListView(context, result, fetchMore); - }, - ), - ); - } - - Widget _buildListView( - BuildContext context, - QueryResult result, - Future Function(FetchMoreOptions)? fetchMore, - ) { return Scrollbar( thumbVisibility: true, interactive: true, @@ -93,42 +46,20 @@ class _OrganizationSearchListState extends State { controller: widget.model.controller, padding: EdgeInsets.zero, shrinkWrap: true, - itemCount: result.isLoading - ? widget.model.organizations.length + 1 - : widget.model.organizations.length, + // Use the length of widget.model.organizations directly. + itemCount: widget.model.organizations.length, itemBuilder: (BuildContext context, int index) { - if (index == widget.model.organizations.length) { - return const ListTile( - title: Center( - child: CupertinoActivityIndicator(), - ), - ); - } - - return VisibilityDetector( + // Render CustomListTile for each organization. + return CustomListTile( + index: index, + type: TileType.org, + orgInfo: widget.model.organizations[index], + onTapOrgInfo: widget.model.selectOrg, key: Key('OrgSelItem$index'), - onVisibilityChanged: (VisibilityInfo info) { - if (info.visibleFraction > 0 && - index == widget.model.organizations.length - 3) { - if (fetchMore != null) { - widget.model - .fetchMoreHelper(fetchMore, widget.model.organizations); - } - } - }, - child: CustomListTile( - index: index, - type: TileType.org, - orgInfo: widget.model.organizations[index], - onTapOrgInfo: widget.model.selectOrg, - key: Key('orgTile_${widget.model.organizations[index].id}'), - ), ); }, separatorBuilder: (BuildContext context, int index) { - return const Divider( - thickness: 0.5, - ); + return const Divider(thickness: 0.5); }, ), );