145 lines
4.1 KiB
Dart
145 lines
4.1 KiB
Dart
import 'package:collection/collection.dart';
|
|
import 'package:didvan/constants/assets.dart';
|
|
import 'package:didvan/models/enums.dart';
|
|
import 'package:didvan/models/view/radar_category.dart';
|
|
import 'package:didvan/models/radar_overview.dart';
|
|
import 'package:didvan/providers/core_provider.dart';
|
|
import 'package:didvan/services/network/request.dart';
|
|
import 'package:didvan/services/network/request_helper.dart';
|
|
|
|
class RadarState extends CoreProvier {
|
|
String search = '';
|
|
String lastSearch = '';
|
|
String? startDate;
|
|
String? endDate;
|
|
bool isScrolled = false;
|
|
bool shouldColapse = false;
|
|
final List<MapEntry> _markQueue = [];
|
|
final List<RadarCategory> selectedCats = [];
|
|
List<RadarCategory> categories = [];
|
|
final List<RadarOverview> radars = [];
|
|
|
|
bool get filtering =>
|
|
selectedCats.length > 1 || startDate != null || endDate != null;
|
|
|
|
bool get searching => search.isNotEmpty;
|
|
|
|
bool get isColapsed =>
|
|
(selectedCats.length == 1 && !filtering && isScrolled) || isScrolled;
|
|
|
|
bool get isCategorySelected => selectedCats.length == 1 && !filtering;
|
|
|
|
void resetFilters(bool isInit) {
|
|
startDate = null;
|
|
endDate = null;
|
|
selectedCats.clear();
|
|
search = '';
|
|
lastSearch = '';
|
|
isScrolled = false;
|
|
if (!isInit) {
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> getRadarOverviews({
|
|
required int page,
|
|
}) async {
|
|
radars.clear();
|
|
lastSearch = search;
|
|
appState = AppState.busy;
|
|
final RequestService service = RequestService(
|
|
RequestHelper.radarOverviews(
|
|
page: page,
|
|
startDate: startDate?.split(' ').first,
|
|
endDate: endDate?.split(' ').first,
|
|
search: search == '' ? null : search,
|
|
categories: selectedCats.map((e) => e.id).toList(),
|
|
),
|
|
);
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
for (var i = 0; i < service.result['radars'].length; i++) {
|
|
radars.add(RadarOverview.fromJson(service.result['radars'][i]));
|
|
}
|
|
if (searching || filtering || isColapsed || isCategorySelected) {
|
|
shouldColapse = true;
|
|
}
|
|
appState = AppState.idle;
|
|
return;
|
|
}
|
|
|
|
appState = AppState.failed;
|
|
}
|
|
|
|
Future<void> markRadar(int id) async {
|
|
radars.firstWhere((element) => element.id == id).marked = true;
|
|
notifyListeners();
|
|
_markQueue.add(MapEntry(id, true));
|
|
Future.delayed(const Duration(milliseconds: 500), () async {
|
|
final MapEntry? lastChange =
|
|
_markQueue.lastWhereOrNull((item) => item.key == id);
|
|
if (lastChange == null) return;
|
|
if (lastChange.value) {
|
|
final service = RequestService(RequestHelper.markRadar(id));
|
|
await service.post();
|
|
_markQueue.removeWhere((element) => element.key == id);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> unMarkRadar(int id) async {
|
|
radars.firstWhere((element) => element.id == id).marked = false;
|
|
notifyListeners();
|
|
_markQueue.add(MapEntry(id, false));
|
|
Future.delayed(const Duration(milliseconds: 500), () async {
|
|
final MapEntry? lastChange =
|
|
_markQueue.lastWhereOrNull((item) => item.key == id);
|
|
if (lastChange == null) return;
|
|
if (!lastChange.value) {
|
|
final service = RequestService(RequestHelper.markRadar(id));
|
|
await service.delete();
|
|
_markQueue.removeWhere((element) => element.key == id);
|
|
}
|
|
});
|
|
}
|
|
|
|
void init() {
|
|
resetFilters(true);
|
|
Future.delayed(Duration.zero, () {
|
|
getRadarOverviews(page: 1);
|
|
});
|
|
categories = [
|
|
RadarCategory(
|
|
id: 1,
|
|
title: 'اقتصادی',
|
|
asset: Assets.economicCategoryIcon,
|
|
),
|
|
RadarCategory(
|
|
id: 2,
|
|
title: 'سیاسی',
|
|
asset: Assets.politicalCategoryIcon,
|
|
),
|
|
RadarCategory(
|
|
id: 3,
|
|
title: 'فناوری',
|
|
asset: Assets.techCategoryIcon,
|
|
),
|
|
RadarCategory(
|
|
id: 4,
|
|
title: 'کسب و کار',
|
|
asset: Assets.businessCategoryIcon,
|
|
),
|
|
RadarCategory(
|
|
id: 5,
|
|
title: 'زیست محیطی',
|
|
asset: Assets.enviromentalCategoryIcon,
|
|
),
|
|
RadarCategory(
|
|
id: 6,
|
|
title: 'اجتماعی',
|
|
asset: Assets.socialCategoryIcon,
|
|
),
|
|
];
|
|
}
|
|
}
|