track_controller.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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(Constants.traceNormalLineId, points,
  95. mapPadding: mapPadding);
  96. }
  97. //画标记点
  98. if (markers.isNotEmpty) {
  99. mapController.updateOrAddMarkers(markers);
  100. }
  101. }
  102. void showSelectMarker(List<LatLng> points, Marker selectMarker) {
  103. _clearSelectMapMarker();
  104. mapController.updateOrAddMarker(selectMarker);
  105. mapController.moveToSuitableLocation(points, mapPadding: selectPadding);
  106. }
  107. void back() {
  108. Get.back();
  109. }
  110. void showMovingTrack(List<LatLng> movingPoints) {
  111. _clearSelectMapMarker();
  112. mapController.addPolyline(Constants.traceSelectLineId, movingPoints,
  113. lineType: PolylineType.selected, mapPadding: mapPadding);
  114. }
  115. void showTrackError(List<LatLng> errorPoints, Marker errorMarker) {
  116. _clearSelectMapMarker();
  117. mapController.updateOrAddMarker(errorMarker);
  118. mapController.addPolyline(Constants.traceSelectLineId, errorPoints,
  119. lineType: PolylineType.error, mapPadding: mapPadding);
  120. }
  121. void _clearSelectMapMarker() {
  122. mapController.removeMarker(Constants.mineLocationId);
  123. mapController.removePolyline(Constants.traceSelectLineId);
  124. mapController.removeMarker(Constants.tracePopupId);
  125. mapController.removeMarker(Constants.traceErrorId);
  126. }
  127. setSheetProgress(double progress) {
  128. _sheetProgress.value = progress;
  129. }
  130. void onCurrentLocationClick() async {
  131. //权限检查
  132. bool isGranted = await PermissionUtil.checkLocationPermission();
  133. if (!isGranted) {
  134. LocationPermissionDialog.show(onNextStep: _requestLocationPermission);
  135. } else {
  136. _updateCurrentLocation();
  137. }
  138. }
  139. void _updateCurrentLocation() {
  140. var lastLocation = MapHelper.getLastLocation();
  141. if (lastLocation == null) {
  142. locationListener(MapLocation location) {
  143. MapHelper.removeLocationListener(locationListener);
  144. showMineLocationMarker(location);
  145. }
  146. MapHelper.addLocationListener(locationListener);
  147. } else {
  148. showMineLocationMarker(lastLocation);
  149. }
  150. }
  151. void showMineLocationMarker(MapLocation location) {
  152. _clearSelectMapMarker();
  153. Marker mineMarker = Marker(
  154. id: Constants.mineLocationId,
  155. latitude: location.latitude,
  156. longitude: location.longitude,
  157. isSelected: false,
  158. customAvatarUrl: accountRepository.mineUserInfo.value.avatar,
  159. markerName: StringName.locationMine,
  160. markerType: MarkerType.mine);
  161. mapController.updateOrAddMarker(mineMarker);
  162. animateCamera(latitude: location.latitude, longitude: location.longitude);
  163. }
  164. void animateCamera({required double? latitude, required double? longitude}) {
  165. mapController.animateCamera(
  166. CameraPosition(longitude: longitude, latitude: latitude, zoom: 18));
  167. }
  168. void _requestLocationPermission() async {
  169. bool isGranted = await PermissionUtil.requestLocationPermission();
  170. if (isGranted) {
  171. _showLocationAlways();
  172. _updateCurrentLocation();
  173. }
  174. }
  175. void _showLocationAlways() async {
  176. bool isGranted = await PermissionUtil.checkShowLocationAlways();
  177. if (!isGranted) {
  178. LocationAlwaysPermissionDialog.show(onNextStep: () async {
  179. isGranted = await PermissionUtil.requestShowLocationAlways();
  180. if (isGranted) {
  181. _updateCurrentLocation();
  182. }
  183. });
  184. }
  185. }
  186. @override
  187. void onClose() {
  188. super.onClose();
  189. tabController?.dispose();
  190. }
  191. }