185 lines
5.9 KiB
Dart
185 lines
5.9 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:lamiter/Class/Diagnosis/diagnosis.dart';
|
|
import 'package:lamiter/Class/Entity/entity.dart';
|
|
|
|
import 'package:lamiter/Class/Service/service_item.dart';
|
|
import 'package:lamiter/Class/Service/service_item_filt_type.dart';
|
|
import 'package:lamiter/Mixin/filter.dart';
|
|
import 'package:lamiter/Provider/Diagnosis/Diagnosis_Item/constitution_provider.dart';
|
|
import 'package:lamiter/Provider/Diagnosis/Diagnosis_Item/posture_issue_provider.dart';
|
|
import 'package:lamiter/Provider/Diagnosis/Diagnosis_Item/urban_disease_provider.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
abstract class ServiceItemProvider extends ChangeNotifier with Filter {
|
|
// 品牌與服務項目
|
|
late List<ServiceItem> serviceItems;
|
|
late List<Entity> companyPairs;
|
|
late List<Entity> productCategoryPairs;
|
|
late List<Entity> urbanDiseasePairs;
|
|
late List<Entity> constitutionPairs;
|
|
late List<Entity> postureIssuePairs;
|
|
|
|
// 篩選條件
|
|
late String keyword;
|
|
late List<String> selectedCompanyIds;
|
|
late List<String> selectedProductCategoryIds;
|
|
late List<String> selectedUrbanDiseaseIds;
|
|
late List<String> selectedConstitutionIds;
|
|
late List<String> selectedPostureIssueIds;
|
|
|
|
ServiceItemProvider() {
|
|
init();
|
|
}
|
|
|
|
List<ServiceItemFiltType> filtTypes(); // 選擇開放的篩選條件
|
|
|
|
void init() {
|
|
// 配對商品
|
|
serviceItems = [];
|
|
filtedList = []; // 主要呈現列表
|
|
// 標籤
|
|
companyPairs = [];
|
|
productCategoryPairs = [];
|
|
urbanDiseasePairs = [];
|
|
constitutionPairs = [];
|
|
postureIssuePairs = [];
|
|
// 篩選標籤
|
|
keyword = '';
|
|
selectedCompanyIds = [];
|
|
selectedProductCategoryIds = [];
|
|
selectedUrbanDiseaseIds = [];
|
|
selectedConstitutionIds = [];
|
|
selectedPostureIssueIds = [];
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> start(BuildContext context, Diagnosis? diagnosis) async {
|
|
init();
|
|
// 固定標籤
|
|
urbanDiseasePairs = context
|
|
.read<UrbanDiseaseProvider>()
|
|
.elements
|
|
.map((urbanDisease) =>
|
|
Entity(id: urbanDisease.id, name: urbanDisease.name))
|
|
.toList();
|
|
constitutionPairs = context
|
|
.read<ConstitutionProvider>()
|
|
.elements
|
|
.map((constitution) =>
|
|
Entity(id: constitution.id, name: constitution.name))
|
|
.toList();
|
|
postureIssuePairs = context
|
|
.read<PostureIssueProvider>()
|
|
.elements
|
|
.map((postureIssue) =>
|
|
Entity(id: postureIssue.id, name: postureIssue.name))
|
|
.toList();
|
|
// 依據診斷報告調整篩選標籤
|
|
keyword = '';
|
|
selectedCompanyIds = [];
|
|
selectedProductCategoryIds = [];
|
|
selectedUrbanDiseaseIds = diagnosis?.urbanDiseaseResult?.diseaseIds ?? [];
|
|
selectedConstitutionIds = diagnosis?.constitutionResult
|
|
?.risk_constitutions(context)
|
|
.map((constitution) => constitution.id)
|
|
.toList() ??
|
|
[];
|
|
selectedPostureIssueIds = diagnosis?.postureIssueResult
|
|
?.risk_posture_issues(context)
|
|
.map((postureIssue) => postureIssue.id)
|
|
.toList() ??
|
|
[];
|
|
await refresh(context);
|
|
}
|
|
|
|
Future<void> refresh(BuildContext context);
|
|
|
|
int filtCount() {
|
|
int count = 0;
|
|
if (selectedCompanyIds.isNotEmpty) count++;
|
|
if (selectedProductCategoryIds.isNotEmpty) count++;
|
|
if (selectedUrbanDiseaseIds.isNotEmpty) count++;
|
|
if (selectedConstitutionIds.isNotEmpty) count++;
|
|
if (selectedPostureIssueIds.isNotEmpty) count++;
|
|
return count;
|
|
}
|
|
|
|
void updateKeyword(String value) {
|
|
keyword = value;
|
|
_filt();
|
|
}
|
|
|
|
void updateServiceItemFiltType(ServiceItemFiltType type, String id) {
|
|
int index = findServiceItemFiltTypeWithId(type, id);
|
|
switch (type) {
|
|
case ServiceItemFiltType.supportCompany:
|
|
if (index == -1) {
|
|
selectedCompanyIds.add(id);
|
|
} else {
|
|
selectedCompanyIds.removeAt(index);
|
|
}
|
|
break;
|
|
case ServiceItemFiltType.productCategory:
|
|
if (index == -1) {
|
|
selectedProductCategoryIds.add(id);
|
|
} else {
|
|
selectedProductCategoryIds.removeAt(index);
|
|
}
|
|
break;
|
|
case ServiceItemFiltType.urbanDisease:
|
|
if (index == -1) {
|
|
selectedUrbanDiseaseIds.add(id);
|
|
} else {
|
|
selectedUrbanDiseaseIds.removeAt(index);
|
|
}
|
|
break;
|
|
case ServiceItemFiltType.constitution:
|
|
if (index == -1) {
|
|
selectedConstitutionIds.add(id);
|
|
} else {
|
|
selectedConstitutionIds.removeAt(index);
|
|
}
|
|
break;
|
|
case ServiceItemFiltType.postureIssue:
|
|
if (index == -1) {
|
|
selectedPostureIssueIds.add(id);
|
|
} else {
|
|
selectedPostureIssueIds.removeAt(index);
|
|
}
|
|
break;
|
|
}
|
|
_filt();
|
|
}
|
|
|
|
int findServiceItemFiltTypeWithId(ServiceItemFiltType type, String id) {
|
|
switch (type) {
|
|
case ServiceItemFiltType.supportCompany:
|
|
return selectedCompanyIds.indexWhere((_id) => _id == id);
|
|
case ServiceItemFiltType.productCategory:
|
|
return selectedProductCategoryIds.indexWhere((_id) => _id == id);
|
|
case ServiceItemFiltType.urbanDisease:
|
|
return selectedUrbanDiseaseIds.indexWhere((_id) => _id == id);
|
|
case ServiceItemFiltType.constitution:
|
|
return selectedConstitutionIds.indexWhere((_id) => _id == id);
|
|
case ServiceItemFiltType.postureIssue:
|
|
return selectedPostureIssueIds.indexWhere((_id) => _id == id);
|
|
}
|
|
}
|
|
|
|
void _filt() {
|
|
super.filtedList = serviceItems;
|
|
super.filtedList = filtWithKeyword(super.filtedList!, keyword);
|
|
super.filtedList = filtWithCompany(super.filtedList!, selectedCompanyIds);
|
|
super.filtedList =
|
|
filtWithProductCategory(super.filtedList!, selectedProductCategoryIds);
|
|
super.filtedList = filtWithHealthConditions(
|
|
super.filtedList!,
|
|
selectedUrbanDiseaseIds,
|
|
selectedConstitutionIds,
|
|
selectedPostureIssueIds,
|
|
);
|
|
super.filtedList = sortWithLastUpdateTime(super.filtedList!);
|
|
notifyListeners();
|
|
}
|
|
}
|