2019-10-31 15:51:22 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2020-07-08 13:13:24 +00:00
|
|
|
import 'package:geosmart/config.dart';
|
|
|
|
import 'package:geosmart/model/response.dart';
|
|
|
|
import 'package:geosmart/model/unique_id_model.dart';
|
2019-10-31 15:51:22 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
class Position {
|
2024-12-25 05:37:16 +00:00
|
|
|
late String id;
|
|
|
|
late String type;
|
|
|
|
late String lat;
|
|
|
|
late String lng;
|
2019-10-31 15:51:22 +00:00
|
|
|
|
2024-12-25 05:37:16 +00:00
|
|
|
Position({
|
|
|
|
required this.id,
|
|
|
|
required this.type,
|
|
|
|
required this.lat,
|
|
|
|
required this.lng,
|
|
|
|
});
|
2019-10-31 15:51:22 +00:00
|
|
|
|
|
|
|
Position.fromJson(Map<String, dynamic> json) {
|
|
|
|
id = json['id'];
|
|
|
|
type = json['type'];
|
|
|
|
lat = json['lat'];
|
|
|
|
lng = json['lng'];
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
final Map<String, dynamic> data = new Map<String, dynamic>();
|
|
|
|
data['id'] = this.id;
|
|
|
|
data['type'] = this.type;
|
|
|
|
data['lat'] = this.lat;
|
|
|
|
data['lng'] = this.lng;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<Position> getPosition() async {
|
|
|
|
SharedPreferences sp = await SharedPreferences.getInstance();
|
|
|
|
String keyName = "unique_id";
|
2024-12-25 05:37:16 +00:00
|
|
|
String? spId = (sp.getString(keyName));
|
2019-10-31 15:51:22 +00:00
|
|
|
Position position = new Position(
|
2024-12-25 05:37:16 +00:00
|
|
|
id: spId ?? "",
|
2019-10-31 15:51:22 +00:00
|
|
|
type: "user",
|
|
|
|
lat: "0.0",
|
|
|
|
lng: "0.0",
|
|
|
|
);
|
|
|
|
if (spId == null) {
|
2024-12-25 05:37:16 +00:00
|
|
|
final response = await http.get(("${Config.api}/id/get/unique") as Uri);
|
2019-10-31 15:51:22 +00:00
|
|
|
if (response.statusCode == 200) {
|
2019-12-16 00:18:09 +00:00
|
|
|
position.id = UniqueIDModel.fromJson(json.decode(response.body)).id;
|
2019-10-31 15:51:22 +00:00
|
|
|
} else {
|
|
|
|
throw Exception("Something Error");
|
|
|
|
}
|
|
|
|
sp.setString(keyName, position.id);
|
|
|
|
}
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid() {
|
2024-12-25 05:37:16 +00:00
|
|
|
// ignore: unnecessary_null_comparison
|
2019-10-31 15:51:22 +00:00
|
|
|
return id != null;
|
|
|
|
}
|
|
|
|
|
2019-12-16 00:18:09 +00:00
|
|
|
Future<ResponseModel> sendPosition() async {
|
|
|
|
ResponseModel result;
|
2019-10-31 15:51:22 +00:00
|
|
|
final response = await http.post(
|
2024-12-25 05:37:16 +00:00
|
|
|
("${Config.api}/point/set") as Uri,
|
|
|
|
body: json.encode(toJson()),
|
2019-10-31 15:51:22 +00:00
|
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
2019-12-16 00:18:09 +00:00
|
|
|
result = ResponseModel.fromJson(json.decode(response.body));
|
|
|
|
} else {
|
|
|
|
throw Exception("Something Error");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<ResponseModel> stopPosition() async {
|
|
|
|
ResponseModel result;
|
|
|
|
final response = await http.post(
|
2024-12-25 05:37:16 +00:00
|
|
|
("${Config.api}/point/unset") as Uri,
|
|
|
|
body: json.encode(toJson()),
|
2019-12-16 00:18:09 +00:00
|
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
result = ResponseModel.fromJson(json.decode(response.body));
|
2019-10-31 15:51:22 +00:00
|
|
|
} else {
|
|
|
|
throw Exception("Something Error");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|