geo-smart-app/lib/service/position_service.dart

50 lines
1.2 KiB
Dart
Raw Permalink Normal View History

2019-12-16 00:18:09 +00:00
import 'package:dio/dio.dart';
import 'package:geosmart/model/position.dart';
import 'package:geosmart/model/response.dart';
import 'package:geosmart/service/setting_service.dart';
2020-07-28 18:11:47 +00:00
import 'package:meta/meta.dart';
2019-12-16 00:18:09 +00:00
class PositionService {
2020-07-28 18:11:47 +00:00
final Dio dio;
final SettingService _settingBloc = SettingService();
2019-12-16 00:18:09 +00:00
2020-07-28 18:11:47 +00:00
PositionService({
@required this.dio,
});
2019-12-16 00:18:09 +00:00
Future<ResponseModel> sendPosition(String lat, String lng) async {
var m = await _settingBloc.getSetting();
2019-12-16 00:18:09 +00:00
Position position = new Position(
id: m.id,
2019-12-16 00:18:09 +00:00
type: "user",
lat: lat,
lng: lng,
);
try {
2020-07-28 18:11:47 +00:00
Response response = await dio.post(
m.host + "/point/set",
2019-12-16 00:18:09 +00:00
data: position.toJson(),
);
return ResponseModel.fromJson(response.data);
} on DioError catch (e) {
2020-07-28 18:11:47 +00:00
throw (e);
2019-12-16 00:18:09 +00:00
}
}
Future<ResponseModel> stopTracking() async {
var m = await _settingBloc.getSetting();
2019-12-16 00:18:09 +00:00
try {
2020-07-28 18:11:47 +00:00
Response response = await dio.post(
m.host + "/point/unset",
2019-12-16 00:18:09 +00:00
data: {
"id": m.id,
2019-12-16 00:18:09 +00:00
"type": "user",
},
);
return ResponseModel.fromJson(response.data);
} on DioError catch (e) {
2020-07-28 18:11:47 +00:00
throw (e);
2019-12-16 00:18:09 +00:00
}
}
}