track_controller.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_map/flutter_map.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:get/get.dart';
  5. import 'package:get/get_core/src/get_main.dart';
  6. import 'package:injectable/injectable.dart';
  7. import 'package:location/base/base_controller.dart';
  8. import 'package:location/data/bean/track_days.dart';
  9. import 'package:location/data/consts/constants.dart';
  10. import 'package:location/data/repositories/account_repository.dart';
  11. import 'package:location/data/repositories/friends_repository.dart';
  12. import 'package:location/data/repositories/track_repository.dart';
  13. import 'package:location/handler/error_handler.dart';
  14. import 'package:location/resource/string.gen.dart';
  15. import 'package:permission_handler/permission_handler.dart';
  16. import 'package:sliding_sheet2/sliding_sheet2.dart';
  17. import '../../data/bean/user_info.dart';
  18. import '../../dialog/common_confirm_dialog_impl.dart';
  19. import '../../dialog/location_permission_dialog.dart';
  20. import '../../sdk/map/map_helper.dart';
  21. import '../../utils/permission_util.dart';
  22. import '../../utils/toast_util.dart';
  23. @injectable
  24. class TrackController extends BaseController
  25. with GetSingleTickerProviderStateMixin {
  26. final Rxn<UserInfo> _userInfo = Rxn<UserInfo>();
  27. UserInfo? get userInfo => _userInfo.value;
  28. final MapController mapController = MapController();
  29. SheetController sheetController = SheetController();
  30. final RxList<TrackDays> daysList = RxList<TrackDays>();
  31. final Rxn<TabController> _tabController = Rxn<TabController>();
  32. TabController? get tabController => _tabController.value;
  33. final Rxn<TrackDays> currentTrackDay = Rxn();
  34. final RxDouble _sheetProgress = 0.0.obs;
  35. double get sheetProgress => _sheetProgress.value;
  36. final mapPadding =
  37. MapPadding(left: 50.w, top: 100.w, right: 50.w, bottom: Get.height / 2);
  38. final selectPadding =
  39. MapPadding(left: 80.w, top: 150.w, right: 80.w, bottom: Get.height / 2);
  40. final RxDouble trackBottomHeight = RxDouble(330.w);
  41. final TrackRepository trackRepository;
  42. final FriendsRepository friendsRepository;
  43. final AccountRepository accountRepository;
  44. TrackController(
  45. this.trackRepository, this.friendsRepository, this.accountRepository);
  46. @override
  47. void onInit() {
  48. final param = Get.arguments;
  49. if (param is UserInfo) {
  50. _userInfo.value = param;
  51. }
  52. _onRequestTrackDateList();
  53. }
  54. @override
  55. void onReady() {
  56. super.onReady();
  57. sheetController.expand();
  58. _recordNumberTrajectoryViewed();
  59. }
  60. void _onRequestTrackDateList() {
  61. trackRepository.getLocationTrackDays().then((list) {
  62. daysList.assignAll(list);
  63. _createTabController();
  64. }).catchError((error) {
  65. ErrorHandler.toastError(error);
  66. });
  67. }
  68. void _createTabController() {
  69. tabController?.dispose();
  70. final tab = TabController(
  71. length: daysList.length,
  72. vsync: this,
  73. );
  74. _tabController.value = tab;
  75. tab.addListener(() {
  76. if (tab.indexIsChanging == false) {
  77. //控制请求当前页数据或者数据已有时跟换数据
  78. mapController.clear();
  79. currentTrackDay.value = daysList[tab.index];
  80. }
  81. });
  82. Future.delayed(Duration(milliseconds: 100), () {
  83. currentTrackDay.value = daysList.isNotEmpty ? daysList[0] : null;
  84. });
  85. }
  86. ///记录查看轨迹的次数
  87. void _recordNumberTrajectoryViewed() {
  88. if (accountRepository.memberStatusInfo.value?.trialed == true &&
  89. accountRepository.memberStatusInfo.value?.level == 20) {
  90. trackRepository.refreshMemberTrailTrack();
  91. }
  92. }
  93. ///显示轨迹以及标记点
  94. void showMapTrack(List<LatLng> points, List<Marker> markers) {
  95. mapController.clear();
  96. //画折线
  97. if (points.length > 1) {
  98. mapController.addPolyline(
  99. Polyline(lineId: Constants.traceNormalLineId, points: points),
  100. mapPadding: mapPadding);
  101. }
  102. //画标记点
  103. if (markers.isNotEmpty) {
  104. mapController.updateOrAddMarkers(markers);
  105. }
  106. }
  107. void showSelectMarker(List<LatLng> points, Marker selectMarker) {
  108. _clearSelectMapMarker();
  109. mapController.updateOrAddMarker(selectMarker);
  110. mapController.moveToSuitableLocation(points, mapPadding: selectPadding);
  111. }
  112. void back() {
  113. Get.back();
  114. }
  115. void showMovingTrack(List<LatLng> movingPoints) {
  116. _clearSelectMapMarker();
  117. mapController.addPolyline(
  118. Polyline(
  119. lineId: Constants.traceSelectLineId,
  120. points: movingPoints,
  121. lineType: PolylineType.selected),
  122. mapPadding: mapPadding);
  123. }
  124. void showTrackError(List<LatLng> errorPoints, Marker errorMarker) {
  125. _clearSelectMapMarker();
  126. mapController.updateOrAddMarker(errorMarker);
  127. mapController.addPolyline(
  128. Polyline(
  129. lineId: Constants.traceSelectLineId,
  130. points: errorPoints,
  131. lineType: PolylineType.error),
  132. mapPadding: mapPadding);
  133. }
  134. void _clearSelectMapMarker() {
  135. mapController.removeMarker(Constants.mineLocationId);
  136. mapController.removePolyline(Constants.traceSelectLineId);
  137. mapController.removeMarker(Constants.tracePopupId);
  138. mapController.removeMarker(Constants.traceErrorId);
  139. }
  140. setSheetProgress(double progress) {
  141. _sheetProgress.value = progress;
  142. if (progress >= 1) {
  143. trackBottomHeight.value = 0;
  144. } else if (progress <= 0.45) {
  145. trackBottomHeight.value = 330.w;
  146. } else {
  147. double percent = (1 - progress) / (1 - 0.45);
  148. trackBottomHeight.value = 330.w * percent;
  149. }
  150. }
  151. void onCurrentLocationClick() async {
  152. //权限检查
  153. bool isGranted = await PermissionUtil.checkLocationPermission();
  154. if (!isGranted) {
  155. LocationPermissionDialog.show(onNextStep: _requestLocationPermission);
  156. } else {
  157. _updateCurrentLocation();
  158. }
  159. }
  160. void _updateCurrentLocation() {
  161. var lastLocation = MapHelper.getLastLocation();
  162. if (lastLocation == null) {
  163. locationListener(MapLocation location) {
  164. MapHelper.removeLocationListener(locationListener);
  165. showMineLocationMarker(location);
  166. }
  167. MapHelper.addLocationListener(locationListener);
  168. } else {
  169. showMineLocationMarker(lastLocation);
  170. }
  171. }
  172. void showMineLocationMarker(MapLocation location) {
  173. _clearSelectMapMarker();
  174. Marker mineMarker = Marker(
  175. id: Constants.mineLocationId,
  176. latitude: location.latitude,
  177. longitude: location.longitude,
  178. isSelected: false,
  179. customAvatarUrl: accountRepository.mineUserInfo.value.avatar,
  180. markerName: StringName.locationMine,
  181. markerType: MarkerType.mine);
  182. mapController.updateOrAddMarker(mineMarker);
  183. animateCamera(latitude: location.latitude, longitude: location.longitude);
  184. }
  185. void animateCamera({required double? latitude, required double? longitude}) {
  186. mapController.animateCamera(
  187. CameraPosition(longitude: longitude, latitude: latitude, zoom: 18));
  188. }
  189. void _requestLocationPermission() async {
  190. bool isGranted = await PermissionUtil.requestLocationPermission();
  191. if (isGranted) {
  192. _showLocationAlways();
  193. _updateCurrentLocation();
  194. } else {
  195. permissionRefuseDialog(settingClick: () {
  196. openAppSettings();
  197. });
  198. ToastUtil.show(StringName.permissionRequestFail);
  199. }
  200. }
  201. void _showLocationAlways() async {
  202. bool isGranted = await PermissionUtil.checkShowLocationAlways();
  203. if (!isGranted) {
  204. LocationAlwaysPermissionDialog.show(onNextStep: () async {
  205. isGranted = await PermissionUtil.requestShowLocationAlways();
  206. if (isGranted) {
  207. _updateCurrentLocation();
  208. }
  209. });
  210. }
  211. }
  212. @override
  213. void onClose() {
  214. super.onClose();
  215. tabController?.dispose();
  216. }
  217. }