-
Notifications
You must be signed in to change notification settings - Fork 4
/
charger_spots_repository.dart
71 lines (60 loc) · 1.96 KB
/
charger_spots_repository.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import 'dart:convert';
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map_app/charger_spot.dart';
/// 充電スポット取得のステータス
enum GetChargerSpotsStatus {
/// 成功
ok,
/// 失敗:指定範囲内に充電スポットが多すぎる
ngTooManySpots,
}
/// 充電スポット取得のレスポンス
@immutable
class GetChargerSpotsResponse {
const GetChargerSpotsResponse({
required this.status,
required this.spots,
});
/// ステータス
final GetChargerSpotsStatus status;
/// 取得した充電スポット
final List<ChargerSpot> spots;
}
/// 充電スポット・リポジトリ
class ChargerSpotsRepository {
ChargerSpotsRepository();
/// 最南西の座標(緯度[swLat]・経度[swLng])と
/// 最北東の座標(緯度[neLat]・経度[neLng])と
/// 範囲内にある充電スポットを取得します。
Future<GetChargerSpotsResponse> getChargerSpots({
required double swLat,
required double swLng,
required double neLat,
required double neLng,
}) async {
final List<ChargerSpot> spots = _spots ??
(jsonDecode(await rootBundle.loadString('assets/spots.json')) as List)
.map((e) => ChargerSpot.fromJson(e as Map<String, dynamic>))
.toList();
await Future.delayed(Duration(milliseconds: Random().nextInt(400)));
final spotsInRegion = spots
.where((spot) =>
spot.latitude >= swLat &&
spot.latitude <= neLat &&
spot.longitude >= swLng &&
spot.longitude <= neLng)
.toList();
return spotsInRegion.length > 100
? const GetChargerSpotsResponse(
status: GetChargerSpotsStatus.ngTooManySpots,
spots: [],
)
: GetChargerSpotsResponse(
status: GetChargerSpotsStatus.ok,
spots: spotsInRegion,
);
}
List<ChargerSpot>? _spots;
}