Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LocationViewModel extends StateNotifier<AsyncValue<List<Places>>> {
- final List<Places> initialLocations;
- final String employeeId;
- final int year;
- final int month;
- final String shift;
- LocationViewModel({
- required this.initialLocations,
- required this.employeeId,
- required this.year,
- required this.month,
- required this.shift,
- }) : super(const AsyncValue.loading()) {
- _fetchLocations();
- }
- String _searchTerm = '';
- List<Places> _locations = [];
- // Getter for the selected locations
- List<Places> get selectedLocations => initialLocations;
- // Method to fetch locations
- Future<void> _fetchLocations() async {
- try {
- state = const AsyncValue.loading();
- final response = await TourPlanRepository.instance.getTourPlanLocation(employeeId, year, month, shift);
- final locations = response.data ?? [];
- _locations = locations
- .map((location) => location.copyWith(
- selected: initialLocations.any((loc) => loc.uniqueKey == location.uniqueKey),
- )).toList();
- state = AsyncValue.data(_locations);
- } catch (e, stackTrace) {
- state = AsyncValue.error(e, stackTrace);
- }
- }
- // Method to update the search term
- void updateSearchTerm(String term) {
- _searchTerm = term.trim();
- state = AsyncValue.data(_filterLocations());
- }
- // Method to toggle the selection of a location
- void toggleSelection(Places location) {
- final index = _locations.indexWhere((l) => l.uniqueKey == location.uniqueKey);
- if (index != -1) {
- final isSelected = _locations[index].selected;
- // Update the main list
- _locations[index] = _locations[index].copyWith(selected: !isSelected);
- // Update the selected list
- if (isSelected) {
- initialLocations.removeWhere((l) => l.uniqueKey == location.uniqueKey);
- } else {
- initialLocations.add(_locations[index]);
- }
- // Update state without resetting the filtered list
- state = AsyncValue.data(_filterLocations());
- }
- }
- // Method to filter locations based on search term
- List<Places> _filterLocations() {
- return _locations.where((location) {
- return location.locationName?.toLowerCase().contains(_searchTerm.toLowerCase()) ?? false;
- }).toList();
- }
- }
- final locationViewModelProvider =
- StateNotifierProvider.family.autoDispose<LocationViewModel, AsyncValue<List<Places>>, LocationQueryParams>(
- (ref, params) => LocationViewModel(
- initialLocations: params.initialLocations,
- employeeId: params.employeeId,
- year: params.year,
- month: params.month,
- shift: params.shift,
- ),
- );
- // --------------------------------------------------
- class LocationViewModel extends StateNotifier<PagingState<int, Places>> {
- final List<Places> initialLocations;
- final int year;
- final int month;
- final Debouncer _debounce = Debouncer();
- String? _searchTerm;
- LocationViewModel({
- required this.initialLocations,
- required this.year,
- required this.month,
- }) : super(PagingState());
- List<Places> get selectedLocations => initialLocations;
- /// Fetches the next page of data
- Future<void> fetchNextPage() async {
- if (state.isLoading) return;
- state = state.copyWith(isLoading: true, error: null);
- try {
- final newKey = state.keys?.isNotEmpty == true ? state.keys!.last + 1 : 0;
- final response = await TourPlanRepository.instance.getTourPlanLocation(year, month, newKey, _searchTerm);
- final locations = response.data.content ?? [];
- final newItems = locations.map((location) {
- return location.copyWith(
- selected: initialLocations.any((loc) => loc.id == location.id),
- );
- }).toList();
- state = state.copyWith(
- pages: [...?state.pages, newItems],
- keys: [...?state.keys, newKey],
- hasNextPage: response.data.hasNext,
- isLoading: false,
- );
- } catch (error) {
- state = state.copyWith(
- error: error,
- isLoading: false,
- );
- }
- }
- /// Updates search term and refreshes the list
- void updateSearchTerm(String searchTerm) {
- _searchTerm = searchTerm;
- _debounce.run(() {
- refreshList();
- });
- }
- /// Clear search and refreshes the list
- void clearSearchTerm() {
- _searchTerm = null;
- _debounce.run(() {
- refreshList();
- });
- }
- /// Refreshes the list and fetches the first page
- void refreshList() {
- state = PagingState();
- fetchNextPage();
- }
- void toggleSelection(Places location) {
- final updatedPages = state.pages?.map((page) {
- return page.map((loc) {
- if (loc.id == location.id) {
- return loc.copyWith(selected: !(loc.selected));
- }
- return loc;
- }).toList();
- }).toList();
- state = state.copyWith(pages: updatedPages);
- if (location.selected == true) {
- initialLocations.remove(location);
- } else {
- initialLocations.add(location);
- }
- }
- }
- final locationViewModelProvider =
- StateNotifierProvider.family.autoDispose<LocationViewModel, PagingState<int, Places>, LocationQueryParams>(
- (ref, params) => LocationViewModel(
- initialLocations: params.initialLocations,
- year: params.year,
- month: params.month,
- ),
- );
- // --------------------------------------------------
- class LocationViewModel extends StateNotifier<AsyncValue<List<Places>>> {
- final List<Places> initialLocations;
- final String employeeId;
- final int year;
- final int month;
- final Debouncer _debounce = Debouncer();
- String _searchTerm = '';
- LocationViewModel({
- required this.initialLocations,
- required this.employeeId,
- required this.year,
- required this.month,
- }) : super(const AsyncValue.loading()) {
- _fetchLocations();
- }
- String get searchTerm => _searchTerm;
- // Method to fetch locations from the API based on the search term
- Future<void> _fetchLocations() async {
- try {
- state = const AsyncValue.loading();
- final response = await TourPlanRepository.instance.getTourPlanLocation(employeeId, year, month);
- final locations = response.data ?? [];
- // Update the state with the new list of places
- state = AsyncValue.data(
- locations.map((location) {
- return location.copyWith(
- selected: initialLocations.any((loc) => loc.id == location.id),
- );
- }).toList(),
- );
- } catch (e, stackTrace) {
- state = AsyncValue.error(e, stackTrace);
- }
- }
- // Method to update the search term with debounce
- void updateSearchTerm(String term) {
- _searchTerm = term.trim();
- _debounce.run(() {
- _fetchLocations();
- });
- }
- // Method to toggle the selection of a location
- void toggleSelection(Places location) {
- final locations = state.value ?? [];
- final index = locations.indexWhere((l) => l.id == location.id);
- if (index == -1) return;
- final isSelected = locations[index].selected;
- final updatedLocation = locations[index].copyWith(selected: !isSelected);
- locations[index] = updatedLocation;
- state = AsyncValue.data(locations);
- if (isSelected) {
- initialLocations.removeWhere((l) => l.id == location.id);
- } else {
- initialLocations.add(updatedLocation);
- }
- }
- // Getter for the selected locations
- List<Places> get selectedLocations => initialLocations;
- }
- final locationViewModelProvider =
- StateNotifierProvider.family.autoDispose<LocationViewModel, AsyncValue<List<Places>>, LocationQueryParams>(
- (ref, params) => LocationViewModel(
- initialLocations: params.initialLocations,
- employeeId: params.employeeId,
- year: params.year,
- month: params.month,
- ),
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement