-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#15: Setup http and json serialization and parse api from network
- Loading branch information
1 parent
e20dcc7
commit 2106d26
Showing
6 changed files
with
255 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,53 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter_app/bloc/bloc_provider.dart'; | ||
import 'package:flutter_app/pages/places/models.dart'; | ||
import 'package:flutter_app/pages/places/places_api_service.dart'; | ||
import 'package:google_maps_flutter/google_maps_flutter.dart'; | ||
|
||
class MapBloc extends BlocBase { | ||
MapBloc(this.placesApiService) { | ||
_fetchLocations(); | ||
} | ||
|
||
final PlacesApiService placesApiService; | ||
|
||
StreamController<MapType> _mapTypeController = | ||
StreamController<MapType>.broadcast(); | ||
|
||
Stream<MapType> get mayType => _mapTypeController.stream; | ||
|
||
StreamController<List<Office>> _officesController = | ||
StreamController<List<Office>>.broadcast(); | ||
|
||
Stream<List<Office>> get offices => _officesController.stream; | ||
|
||
@override | ||
void dispose() { | ||
_mapTypeController.close(); | ||
_officesController.close(); | ||
} | ||
|
||
void setMayType(MapType mayType) { | ||
_mapTypeController.sink.add(mayType); | ||
} | ||
|
||
void _fetchLocations() { | ||
placesApiService.getGoogleOffices().then((value) { | ||
_officesController.sink.add(value.offices); | ||
}); | ||
} | ||
} | ||
|
||
extension MyOffices on Office { | ||
Marker toMarker() { | ||
return Marker( | ||
markerId: MarkerId(name), | ||
position: LatLng(lat, lng), | ||
infoWindow: InfoWindow( | ||
title: name, | ||
snippet: address, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
@JsonSerializable() | ||
class Locations { | ||
Locations({this.offices, this.regions}); | ||
|
||
factory Locations.fromJson(Map<String, dynamic> json) { | ||
return Locations( | ||
offices: json['offices'] != null | ||
? (json['offices'] as List).map((i) => Office.fromJson(i)).toList() | ||
: null, | ||
regions: json['regions'] != null | ||
? (json['regions'] as List).map((i) => Region.fromJson(i)).toList() | ||
: null, | ||
); | ||
} | ||
|
||
List<Office> offices; | ||
|
||
List<Region> regions; | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = Map<String, dynamic>(); | ||
if (this.offices != null) { | ||
data['offices'] = this.offices.map((v) => v.toJson()).toList(); | ||
} | ||
if (this.regions != null) { | ||
data['regions'] = this.regions.map((v) => v.toJson()).toList(); | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
@JsonSerializable() | ||
class Region { | ||
Region({this.coords, this.id, this.name, this.zoom}); | ||
|
||
factory Region.fromJson(Map<String, dynamic> json) { | ||
return Region( | ||
coords: json['coords'] != null ? Coords.fromJson(json['coords']) : null, | ||
id: json['id'], | ||
name: json['name'], | ||
zoom: json['zoom'], | ||
); | ||
} | ||
|
||
Coords coords; | ||
String id; | ||
|
||
String name; | ||
|
||
double zoom; | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = Map<String, dynamic>(); | ||
data['id'] = this.id; | ||
data['name'] = this.name; | ||
data['zoom'] = this.zoom; | ||
if (this.coords != null) { | ||
data['coords'] = this.coords.toJson(); | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
@JsonSerializable() | ||
class Coords { | ||
Coords({this.lat, this.lng}); | ||
|
||
factory Coords.fromJson(Map<String, dynamic> json) { | ||
return Coords( | ||
lat: json['lat'], | ||
lng: json['lng'], | ||
); | ||
} | ||
|
||
double lat; | ||
|
||
double lng; | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = Map<String, dynamic>(); | ||
data['lat'] = this.lat; | ||
data['lng'] = this.lng; | ||
return data; | ||
} | ||
} | ||
|
||
@JsonSerializable() | ||
class Office { | ||
Office( | ||
{this.address, | ||
this.id, | ||
this.image, | ||
this.lat, | ||
this.lng, | ||
this.name, | ||
this.phone, | ||
this.region}); | ||
|
||
factory Office.fromJson(Map<String, dynamic> json) { | ||
return Office( | ||
address: json['address'], | ||
id: json['id'], | ||
image: json['image'], | ||
lat: json['lat'], | ||
lng: json['lng'], | ||
name: json['name'], | ||
phone: json['phone'], | ||
region: json['region'], | ||
); | ||
} | ||
|
||
String address; | ||
String id; | ||
String image; | ||
double lat; | ||
double lng; | ||
String name; | ||
String phone; | ||
|
||
String region; | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = Map<String, dynamic>(); | ||
data['address'] = this.address; | ||
data['id'] = this.id; | ||
data['image'] = this.image; | ||
data['lat'] = this.lat; | ||
data['lng'] = this.lng; | ||
data['name'] = this.name; | ||
data['phone'] = this.phone; | ||
data['region'] = this.region; | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'package:http/http.dart' as http; | ||
|
||
import 'package:flutter_app/pages/places/models.dart'; | ||
|
||
class PlacesApiService { | ||
Future<Locations> getGoogleOffices() async { | ||
const googleLocationsURL = | ||
'https://about.google/static/data/locations.json'; | ||
|
||
// Retrieve the locations of Google offices | ||
final response = await http.get(googleLocationsURL); | ||
if (response.statusCode == 200) { | ||
return Locations.fromJson(json.decode(response.body)); | ||
} else { | ||
throw HttpException( | ||
'Unexpected status code ${response.statusCode}:' | ||
' ${response.reasonPhrase}', | ||
uri: Uri.parse(googleLocationsURL)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters