Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DoctorListViewModel extends StateNotifier<DoctorPagingState> {
- final DefaultQueryParams _param;
- DoctorListViewModel(this._param)
- : super(
- DoctorPagingState(
- pagingState: PagingState(),
- ),
- );
- String? _searchTerm;
- bool _isDisposed = false;
- @override
- void dispose() {
- _isDisposed = true;
- super.dispose();
- }
- /// Fetches the next page of data
- Future<void> fetchNextPage() async {
- final paging = state.pagingState;
- if (paging.isLoading) return;
- final newPaging = paging.copyWith(isLoading: true, error: null);
- state = state.copyWith(pagingState: newPaging);
- try {
- final newKey = paging.keys?.last == null ? 0 : paging.keys!.last + 1;
- final doctorResponse = await DoctorRepository.instance.getDoctors(
- employeeId: _param.employeeId,
- year: _param.year,
- month: _param.month,
- levelCode: _param.levelCode,
- page: newKey,
- term: _searchTerm,
- selectedOnly: _param.selectedOnly,
- );
- final doctorData = doctorResponse.data;
- final updatedPaging = paging.copyWith(
- pages: [...?paging.pages, doctorData.doctors?.content ?? []],
- keys: [...?paging.keys, newKey],
- hasNextPage: doctorData.doctors?.hasNext,
- isLoading: false,
- );
- state = state.copyWith(
- pagingState: updatedPaging,
- totalDoctors: doctorData.doctors?.totalElements ?? 0,
- selectedDoctors: doctorData.selectedDoctor ?? 0,
- approved: doctorData.approved ?? true,
- editable: doctorData.editable ?? false,
- );
- // trigger the status message listener
- doctorPlanApprovedNotifier.value = state.approved;
- } catch (error) {
- if (_isDisposed) return;
- final failedPaging = paging.copyWith(
- error: error,
- isLoading: false,
- );
- state = state.copyWith(pagingState: failedPaging);
- }
- }
- /// Updates search term and refreshes the list
- void updateSearchTerm(String searchTerm) {
- _searchTerm = searchTerm;
- refreshList();
- }
- /// Refreshes the list and fetches the first page
- void refreshList() {
- state = state.copyWith(
- pagingState: PagingState(),
- totalDoctors: 0,
- selectedDoctors: 0,
- approved: false,
- );
- fetchNextPage();
- }
- /// Method to toggle the selection of a Doctor
- void toggleSelection(DoctorData content) {
- final oldPaging = state.pagingState;
- int updatedSelectedCount = state.selectedDoctors;
- final updatedPages = oldPaging.pages?.map((page) {
- return page.map((c) {
- if (c.doctor?.id == content.doctor?.id) {
- final wasSelected = c.selected;
- final nowSelected = !wasSelected;
- if (nowSelected) {
- updatedSelectedCount++;
- } else {
- updatedSelectedCount--;
- }
- return c.copyWith(selected: nowSelected);
- }
- return c;
- }).toList();
- }).toList();
- state = state.copyWith(
- pagingState: oldPaging.copyWith(pages: updatedPages),
- selectedDoctors: updatedSelectedCount,
- );
- }
- }
- /// **Provider for ViewModel**
- final doctorListViewModelProvider =
- StateNotifierProvider.family.autoDispose<DoctorListViewModel, DoctorPagingState, DefaultQueryParams>((ref, param) {
- return DoctorListViewModel(param);
- });
- // --------------------------------------------------
- final doctorListState = ref.watch(doctorListViewModelProvider(_params));
- final doctorListViewModel = ref.read(doctorListViewModelProvider(_params).notifier);
- PagedListView<int, DoctorData>.separated(
- state: doctorListState.pagingState,
- fetchNextPage: doctorListViewModel.fetchNextPage,
- builderDelegate: PagedChildBuilderDelegate(
- itemBuilder: (context, item, index) {
- return DoctorListItem(
- doctorData: item,
- clickable: doctorListState.editable,
- onClick: () => _toggleSelection(item, doctorListViewModel),
- );
- },
- ),
- separatorBuilder: (BuildContext context, int index) {
- return const Divider(color: AppColors.dividerColor, height: 1);
- },
- ),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement