track_controller.dart 7.0 KB

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