ソースを参照

[new]上报当前定位

zk 10 ヶ月 前
コミット
bc9d9c107c

+ 8 - 0
lib/data/repositories/contact_repository.dart

@@ -1,9 +1,17 @@
 import 'package:injectable/injectable.dart';
 import 'package:location/data/api/atmob_api.dart';
 
+import '../../di/get_it.dart';
+
 @lazySingleton
 class ContactRepository {
   final AtmobApi atmobApi;
 
   ContactRepository(this.atmobApi);
+
+  void refreshContactList() {}
+
+  static ContactRepository getInstance() {
+    return getIt.get<ContactRepository>();
+  }
 }

+ 1 - 1
lib/data/repositories/friends_repository.dart

@@ -28,10 +28,10 @@ class FriendsRepository {
   }
 
   void updateFriendsLocation(List<LocationInfo> location) {
-    AtmobLog.d('zk', 'updateFriendsLocation');
     if (friendsList.isEmpty || location.isEmpty) {
       return;
     }
+    AtmobLog.d('zk', 'updateFriendsLocation');
     final idUserMap = <String, UserInfo>{
       for (final user in friendsList) user.id: user
     };

+ 4 - 1
lib/data/repositories/message_repository.dart

@@ -1,5 +1,6 @@
 import 'package:injectable/injectable.dart';
 import 'package:location/data/api/atmob_api.dart';
+import 'package:location/di/get_it.dart';
 
 @lazySingleton
 class MessageRepository {
@@ -9,5 +10,7 @@ class MessageRepository {
 
   void requestMessageList() {}
 
-  void refreshContactList() {}
+  static MessageRepository getInstance() {
+    return getIt.get<MessageRepository>();
+  }
 }

+ 2 - 6
lib/di/get_it.config.dart

@@ -43,6 +43,8 @@ extension GetItInjectableX on _i174.GetIt {
     gh.factory<_i923.BrowserController>(() => _i923.BrowserController());
     gh.factory<_i973.SplashController>(() => _i973.SplashController());
     gh.singleton<_i361.Dio>(() => networkModule.createDefaultDio());
+    gh.lazySingleton<_i220.AtmobLocationClient>(
+        () => _i220.AtmobLocationClient());
     gh.singleton<_i243.AtmobApi>(
         () => networkModule.provideAtmobApi(gh<_i361.Dio>()));
     gh.lazySingleton<_i20.AccountRepository>(
@@ -53,12 +55,6 @@ extension GetItInjectableX on _i174.GetIt {
         () => _i791.MessageRepository(gh<_i243.AtmobApi>()));
     gh.lazySingleton<_i850.ContactRepository>(
         () => _i850.ContactRepository(gh<_i243.AtmobApi>()));
-    gh.lazySingleton<_i220.AtmobLocationClient>(() => _i220.AtmobLocationClient(
-          gh<_i1053.FriendsRepository>(),
-          gh<_i791.MessageRepository>(),
-          gh<_i850.ContactRepository>(),
-          gh<_i20.AccountRepository>(),
-        ));
     gh.factory<_i731.MainController>(() => _i731.MainController(
           gh<_i1053.FriendsRepository>(),
           gh<_i20.AccountRepository>(),

+ 4 - 1
lib/sdk/map/map_helper.dart

@@ -1,4 +1,5 @@
 import 'package:flutter_map/flutter_map.dart';
+import 'package:location/socket/atmob_location_client.dart';
 import '../../utils/atmob_log.dart';
 
 class MapHelper {
@@ -14,14 +15,16 @@ class MapHelper {
   static Future<void> init() async {
     await FlutterMap.init();
     _mapPlatform = FlutterMap.getMapPlatform();
-    initLocationClient();
   }
 
   static void initLocationClient() {
+    AtmobLocationClient atmobLocationClient =
+        AtmobLocationClient.getAtmobLocationClient();
     _mapPlatform.receiveLocationStream(listener: (location) {
       AtmobLog.d(tag, "onLocationChanged: $location");
       if (location.errorCode == 0) {
         _lastLocation = location;
+        atmobLocationClient.uploadLocation(location);
         try {
           for (MapLocationListener listener in _locationListeners) {
             listener.call(location);

+ 24 - 12
lib/socket/atmob_location_client.dart

@@ -1,6 +1,7 @@
 import 'dart:async';
 import 'dart:convert';
 
+import 'package:flutter_map/src/entity/map_location.dart';
 import 'package:injectable/injectable.dart';
 import 'package:location/data/repositories/friends_repository.dart';
 import 'package:location/di/get_it.dart';
@@ -14,6 +15,7 @@ import '../data/bean/location_info.dart';
 import '../data/repositories/account_repository.dart';
 import '../data/repositories/contact_repository.dart';
 import '../data/repositories/message_repository.dart';
+import 'location_message.dart';
 
 typedef OnLocationChangeListener = void Function(List<LocationInfo> data);
 
@@ -28,13 +30,7 @@ class AtmobLocationClient {
   bool _isConnecting = false;
   CancelableFuture? cancelableFuture;
 
-  FriendsRepository friendsRepository;
-  MessageRepository messageRepository;
-  ContactRepository contactRepository;
-  AccountRepository accountRepository;
-
-  AtmobLocationClient(this.friendsRepository, this.messageRepository,
-      this.contactRepository, this.accountRepository) {
+  AtmobLocationClient() {
     startWebsocketInternal();
   }
 
@@ -123,19 +119,19 @@ class AtmobLocationClient {
           }
           switch (message.cmd) {
             case SocketConstants.refreshFriendList:
-              friendsRepository.refreshFriends();
+              FriendsRepository.getInstance().refreshFriends();
               break;
             case SocketConstants.refreshFriendRequest:
-              friendsRepository.refreshFriendRequestList();
+              FriendsRepository.getInstance().refreshFriendRequestList();
               break;
             case SocketConstants.refreshFriendMessage:
-              messageRepository.requestMessageList();
+              MessageRepository.getInstance().requestMessageList();
               break;
             case SocketConstants.refreshContact:
-              messageRepository.refreshContactList();
+              ContactRepository.getInstance().refreshContactList();
               break;
             case SocketConstants.refreshMember:
-              accountRepository.refreshMemberStatus();
+              AccountRepository.getInstance().refreshMemberStatus();
               break;
           }
           return SocketConstants.receiveFriendBatchLocation == message.cmd;
@@ -203,4 +199,20 @@ class AtmobLocationClient {
   static void removeLocationListener(OnLocationChangeListener listener) {
     _locationListeners.remove(listener);
   }
+
+  void _sendMessage(String msg) {
+    if (_webSocket == null) {
+      return;
+    }
+    _webSocket!.sink.add(msg);
+    AtmobLog.d(tag, 'send location: $msg');
+  }
+
+  void uploadLocation(MapLocation location) {
+    if (_webSocket == null ||
+        location.latitude == 0 && location.longitude == 0) {
+      return;
+    }
+    _sendMessage(LocationMessage.obtainMessage(location));
+  }
 }

+ 3 - 3
lib/socket/base_message.dart

@@ -5,12 +5,12 @@ part 'base_message.g.dart';
 @JsonSerializable()
 class BaseMessage {
   @JsonKey(name: 'cmd')
-  final String? cmd;
+  String? cmd;
 
   @JsonKey(name: 'body')
-  final String? data;
+  String? data;
 
-  BaseMessage(this.cmd, this.data);
+  BaseMessage({this.cmd, this.data});
 
   factory BaseMessage.fromJson(Map<String, dynamic> json) =>
       _$BaseMessageFromJson(json);

+ 2 - 2
lib/socket/base_message.g.dart

@@ -7,8 +7,8 @@ part of 'base_message.dart';
 // **************************************************************************
 
 BaseMessage _$BaseMessageFromJson(Map<String, dynamic> json) => BaseMessage(
-      json['cmd'] as String?,
-      json['body'] as String?,
+      cmd: json['cmd'] as String?,
+      data: json['body'] as String?,
     );
 
 Map<String, dynamic> _$BaseMessageToJson(BaseMessage instance) =>

+ 23 - 0
lib/socket/location_message.dart

@@ -0,0 +1,23 @@
+import 'dart:convert';
+
+import 'package:flutter_map/flutter_map.dart';
+import 'package:location/socket/base_message.dart';
+
+import 'message_data.dart';
+
+class LocationMessage extends BaseMessage {
+  static LocationMessage? message;
+
+  static String obtainMessage(MapLocation location) {
+    message ??= LocationMessage();
+    message!.cmd = "u.location";
+    message!.data = jsonEncode(MessageData(
+            longitude: location.longitude,
+            latitude: location.latitude,
+            speed: location.speed,
+            bearing: location.bearing,
+            address: location.address)
+        .toJson());
+    return jsonEncode(message!.toJson());
+  }
+}

+ 29 - 0
lib/socket/message_data.dart

@@ -0,0 +1,29 @@
+import 'package:json_annotation/json_annotation.dart';
+
+part 'message_data.g.dart';
+
+@JsonSerializable()
+class MessageData {
+  @JsonKey(name: "lat")
+  final double? latitude;
+
+  @JsonKey(name: "lng")
+  final double? longitude;
+
+  @JsonKey(name: "speed")
+  final double? speed;
+
+  @JsonKey(name: "bearing")
+  final double? bearing;
+
+  @JsonKey(name: "addr")
+  final String? address;
+
+  MessageData(
+      {this.latitude, this.longitude, this.speed, this.bearing, this.address});
+
+  factory MessageData.fromJson(Map<String, dynamic> json) =>
+      _$MessageDataFromJson(json);
+
+  Map<String, dynamic> toJson() => _$MessageDataToJson(this);
+}

+ 24 - 0
lib/socket/message_data.g.dart

@@ -0,0 +1,24 @@
+// GENERATED CODE - DO NOT MODIFY BY HAND
+
+part of 'message_data.dart';
+
+// **************************************************************************
+// JsonSerializableGenerator
+// **************************************************************************
+
+MessageData _$MessageDataFromJson(Map<String, dynamic> json) => MessageData(
+      latitude: (json['lat'] as num?)?.toDouble(),
+      longitude: (json['lng'] as num?)?.toDouble(),
+      speed: (json['speed'] as num?)?.toDouble(),
+      bearing: (json['bearing'] as num?)?.toDouble(),
+      address: json['addr'] as String?,
+    );
+
+Map<String, dynamic> _$MessageDataToJson(MessageData instance) =>
+    <String, dynamic>{
+      'lat': instance.latitude,
+      'lng': instance.longitude,
+      'speed': instance.speed,
+      'bearing': instance.bearing,
+      'addr': instance.address,
+    };

+ 1 - 0
lib/widget/marquee_text.dart

@@ -40,6 +40,7 @@ class MarqueeText extends StatelessWidget {
             text: text,
             style: textStyle,
             blankSpace: blankSpace,
+            pauseAfterRound: Duration(seconds: 2),
             velocity: velocity);
       },
     );