Skip to content

Commit

Permalink
Update organization_search_list.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
ARYPROGRAMMER authored Nov 17, 2024
1 parent ba8bfe3 commit 54f63d5
Showing 1 changed file with 22 additions and 91 deletions.
113 changes: 22 additions & 91 deletions lib/widgets/organization_search_list.dart
Original file line number Diff line number Diff line change
@@ -1,90 +1,43 @@
// 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
_OrganizationSearchListState createState() => _OrganizationSearchListState();
}

class _OrganizationSearchListState extends State<OrganizationSearchList> {
static const int maxRefetch = 10;
final ValueNotifier<int> _refetchCount = ValueNotifier<int>(0);

// Added a stream to simulate organization data for testing.
late StreamController<List<OrgInfo>> _orgDataController;

@override
void initState() {
super.initState();
_orgDataController = StreamController<List<OrgInfo>>();
}

@override
void dispose() {
_refetchCount.dispose();
_orgDataController.close();
super.dispose();
}

@override
Widget build(BuildContext context) {
return GraphQLProvider(
client: ValueNotifier<GraphQLClient>(graphqlConfig.authClient()),
child: Query(
options: QueryOptions(
document: gql(Queries().fetchJoinInOrgByName),
variables: {
'nameStartsWith': widget.model.searchController.text,
'first': 30,
'skip': 0,
},
),
builder: (
QueryResult result, {
Future<QueryResult> Function(FetchMoreOptions)? fetchMore,
Future<QueryResult?> 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<QueryResult> Function(FetchMoreOptions)? fetchMore,
) {
return Scrollbar(
thumbVisibility: true,
interactive: true,
Expand All @@ -93,42 +46,20 @@ class _OrganizationSearchListState extends State<OrganizationSearchList> {
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);
},
),
);
Expand Down

0 comments on commit 54f63d5

Please sign in to comment.