diff --git a/lib/api/offers_and_discounts_api_client.dart b/lib/api/offers_and_discounts_api_client.dart index 5744ac6..5d24824 100644 --- a/lib/api/offers_and_discounts_api_client.dart +++ b/lib/api/offers_and_discounts_api_client.dart @@ -253,6 +253,7 @@ class OffersAndDiscountsApiClient { String? offerType, BuildContext? context, String? mobileNumber, + String? query, }) async { String url = ApiConsts.walaPlusRetailers; String pUserName = _resolveUserName(); @@ -275,6 +276,9 @@ class OffersAndDiscountsApiClient { } postParams["WP_Page"] = page; postParams["WP_Per_page"] = perPage; + if (query != null && query.trim().isNotEmpty) { + postParams["WP_Query"] = query.trim(); + } if (geoLocation != null && geoLocation.isNotEmpty) { postParams["WP_Geo_Location"] = geoLocation; diff --git a/lib/ui/landing/dashboard_screen_for_dependet.dart b/lib/ui/landing/dashboard_screen_for_dependet.dart index a80a894..cba99e2 100644 --- a/lib/ui/landing/dashboard_screen_for_dependet.dart +++ b/lib/ui/landing/dashboard_screen_for_dependet.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -41,6 +42,7 @@ class _DashboardScreenForDependetState extends State int totalPages = 1; final int pageSize = 10; String searchQuery = ""; + Timer? _debounceTimer; final TextEditingController _searchController = TextEditingController(); final ScrollController _scrollController = ScrollController(); bool _hasRegisteredMobileLoginInfo = false; @@ -143,6 +145,7 @@ class _DashboardScreenForDependetState extends State @override void dispose() { + _debounceTimer?.cancel(); _scrollController.removeListener(_scrollListener); _scrollController.dispose(); _searchController.dispose(); @@ -229,8 +232,11 @@ class _DashboardScreenForDependetState extends State } } - Future fetchOffers({String? categoryId}) async { + Future fetchOffers({String? categoryId, String? query}) async { String targetCategoryId = categoryId ?? (currentCategoryId.isEmpty ? "all" : currentCategoryId); + if (query != null) { + searchQuery = query; + } setState(() { isLoading = true; @@ -241,25 +247,26 @@ class _DashboardScreenForDependetState extends State try { String catParam = (targetCategoryId == "all" || targetCategoryId == "0") ? "" : targetCategoryId; String? offerTypeParam = OffersAndDiscountsApiClient.mapOfferTypeFilter(selectedTypeFilter); - print("Fetching WalaPlus retailers: category='$catParam', offerType='$offerTypeParam', page=1, per_page=$pageSize"); + String? queryParam = searchQuery.trim().isNotEmpty ? searchQuery.trim() : null; + print("Fetching WalaPlus retailers: category='$catParam', offerType='$offerTypeParam', query='$queryParam', page=1, per_page=$pageSize"); WalaPlusRetailersResponse? response = await OffersAndDiscountsApiClient().getWalaPlusRetailers( categoryId: catParam, page: 1, perPage: pageSize, offerType: offerTypeParam, + query: queryParam, context: isNearestOffers ? context : null, ); - retailersList = response?.retailers ?? []; + retailersList = response?.retailers ?? []; + _filteredRetailersList = retailersList; currentPage = 1; totalPages = response?.pagesCount ?? 1; hasMore = currentPage < totalPages && (response?.retailers?.isNotEmpty ?? false); - - _runFilter(_searchController.text); } catch (e) { print("Error fetching WalaPlus retailers: $e"); - retailersList = []; - _runFilter(_searchController.text); + retailersList = []; + _filteredRetailersList = []; } finally { if (mounted) { setState(() { @@ -281,14 +288,16 @@ class _DashboardScreenForDependetState extends State int nextPage = currentPage + 1; String catParam = (currentCategoryId == "all" || currentCategoryId == "0") ? "" : currentCategoryId; String? offerTypeParam = OffersAndDiscountsApiClient.mapOfferTypeFilter(selectedTypeFilter); + String? queryParam = searchQuery.trim().isNotEmpty ? searchQuery.trim() : null; - print("Fetching WalaPlus retailers next page: $nextPage of $totalPages, category: '$catParam', offerType: '$offerTypeParam'"); + print("Fetching WalaPlus retailers next page: $nextPage of $totalPages, category: '$catParam', offerType: '$offerTypeParam', query: '$queryParam'"); WalaPlusRetailersResponse? response = await OffersAndDiscountsApiClient().getWalaPlusRetailers( categoryId: catParam, page: nextPage, perPage: pageSize, offerType: offerTypeParam, + query: queryParam, context: isNearestOffers ? context : null, ); @@ -298,7 +307,7 @@ class _DashboardScreenForDependetState extends State totalPages = response?.pagesCount ?? totalPages; hasMore = currentPage < totalPages; retailersList.addAll(newRetailers); - _runFilter(_searchController.text); + _filteredRetailersList = retailersList; } else { hasMore = false; } @@ -315,30 +324,19 @@ class _DashboardScreenForDependetState extends State } Future _onRefresh() async { - await Future.wait([fetchCategories(), fetchOffers(categoryId: currentCategoryId.isEmpty ? "all" : currentCategoryId)]); + await Future.wait(>[fetchCategories(), fetchOffers(categoryId: currentCategoryId.isEmpty ? "all" : currentCategoryId, query: _searchController.text)]); } - void _runFilter([String? query]) { - if (query != null) { - searchQuery = query; - } - // Type filtering (online/in store) is done server-side via WP_Offer_Type (see fetchOffers), - // so retailersList already reflects the selected type - only search is applied locally here. - List temp = retailersList; - - if (searchQuery.trim().isNotEmpty) { - String q = searchQuery.trim().toLowerCase(); - temp = - temp.where((WalaPlusRetailerModel item) { - String name = (item.name ?? "").toLowerCase(); - String title = (item.offerTitle ?? "").toLowerCase(); - String desc = (item.description ?? "").toLowerCase(); - String city = item.displayCity.toLowerCase(); - return name.contains(q) || title.contains(q) || desc.contains(q) || city.contains(q); - }).toList(); + void _onSearchChanged(String query) { + _debounceTimer?.cancel(); + searchQuery = query; + if (query.trim().isEmpty) { + fetchOffers(query: ""); + } else { + _debounceTimer = Timer(const Duration(milliseconds: 1000), () { + fetchOffers(query: query.trim()); + }); } - _filteredRetailersList = temp; - setState(() {}); } void _openRetailer(WalaPlusRetailerModel item) { @@ -502,7 +500,12 @@ class _DashboardScreenForDependetState extends State const SizedBox(height: 2), TextField( controller: _searchController, - onChanged: _runFilter, + onChanged: _onSearchChanged, + onSubmitted: (String val) { + _debounceTimer?.cancel(); + fetchOffers(query: val.trim()); + }, + textInputAction: TextInputAction.search, decoration: InputDecoration( isDense: true, contentPadding: EdgeInsets.zero,