Skip to content

Commit

Permalink
#15: Build Place API and models using json_serlization
Browse files Browse the repository at this point in the history
  • Loading branch information
burhanrashid52 committed Nov 11, 2019
1 parent 2106d26 commit 5b9bfed
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 163 deletions.
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include: package:pedantic/analysis_options.yaml

analyzer:
exclude:
- lib/src/locations.g.dart
- lib/src/places_models.g.dart

linter:
rules:
Expand Down
20 changes: 10 additions & 10 deletions lib/pages/map/map_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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:flutter_app/pages/places/places_models.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapBloc extends BlocBase {
MapBloc(this.placesApiService) {
_fetchLocations();
_fetchLocations("Mumbai");
}

final PlacesApiService placesApiService;
Expand All @@ -17,10 +17,10 @@ class MapBloc extends BlocBase {

Stream<MapType> get mayType => _mapTypeController.stream;

StreamController<List<Office>> _officesController =
StreamController<List<Office>>.broadcast();
StreamController<List<Candidate>> _officesController =
StreamController<List<Candidate>>.broadcast();

Stream<List<Office>> get offices => _officesController.stream;
Stream<List<Candidate>> get offices => _officesController.stream;

@override
void dispose() {
Expand All @@ -32,14 +32,14 @@ class MapBloc extends BlocBase {
_mapTypeController.sink.add(mayType);
}

void _fetchLocations() {
placesApiService.getGoogleOffices().then((value) {
_officesController.sink.add(value.offices);
void _fetchLocations(String placeQuery) {
placesApiService.searchPlaces(placeQuery).then((value) {
_officesController.sink.add(value.candidates);
});
}
}

extension MyOffices on Office {
/*extension MyOffices on Office {
Marker toMarker() {
return Marker(
markerId: MarkerId(name),
Expand All @@ -50,4 +50,4 @@ extension MyOffices on Office {
),
);
}
}
}*/
17 changes: 10 additions & 7 deletions lib/pages/map/map_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_app/bloc/bloc_provider.dart';
import 'package:flutter_app/pages/map/map_bloc.dart';
import 'package:flutter_app/pages/places/models.dart';
import 'package:flutter_app/pages/places/places_models.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapPage extends StatelessWidget {
Expand Down Expand Up @@ -48,23 +48,26 @@ class MapPage extends StatelessWidget {
},
),
//TODO: Fix the scroll and item are not visible
StreamBuilder<List<Office>>(
StreamBuilder<List<Candidate>>(
stream: mapBloc.offices,
builder: (context, snapshot) {
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
var item = snapshot.data[index];
return ListTile(
/*return ListTile(
leading: CircleAvatar(
backgroundColor: Colors.red,
child: Text("P"),
),
title: Text(item.name),
subtitle: Text(item.address),
subtitle: Text(item.name),
);*/
return Text(
item.name,
style: Theme.of(context).textTheme.headline,
);
//return Text(item.name);
},
);
},
Expand All @@ -74,13 +77,13 @@ class MapPage extends StatelessWidget {
);
}

Set<Marker> buildMarkerSet(List<Office> offices) {
/*Set<Marker> buildMarkerSet(List<Office> offices) {
Set<Marker> markers = Set<Marker>();
for (var office in offices) {
markers.add(office.toMarker());
}
return markers;
}
}*/

CameraPosition buildInitialCamera() {
return CameraPosition(
Expand Down
136 changes: 0 additions & 136 deletions lib/pages/places/models.dart

This file was deleted.

23 changes: 15 additions & 8 deletions lib/pages/places/places_api_service.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter_app/pages/places/places_models.dart';
import 'package:http/http.dart' as http;

import 'package:flutter_app/pages/places/models.dart';
// ignore: constant_identifier_names
const BASE_URL = "https://maps.googleapis.com/maps/api/place";
// ignore: constant_identifier_names
const API_KEY = "AIzaSyBex56icjz1uvDbOn80NDCMM6a7GqRiOBo";
// ignore: constant_identifier_names
const PLACE_PARAMS =
"/findplacefromtext/json?key=$API_KEY&inputtype=textquery&fields=photos,formatted_address,name,geometry&&input=";
// ignore: constant_identifier_names
const SEARCH_PLACE_ENDPOINT = BASE_URL + PLACE_PARAMS;

class PlacesApiService {
Future<Locations> getGoogleOffices() async {
const googleLocationsURL =
'https://about.google/static/data/locations.json';

Future<PlaceResponse> searchPlaces(String placeQuery) async {
final googlePlacesURL = SEARCH_PLACE_ENDPOINT + placeQuery;
// Retrieve the locations of Google offices
final response = await http.get(googleLocationsURL);
final response = await http.get(googlePlacesURL);
if (response.statusCode == 200) {
return Locations.fromJson(json.decode(response.body));
return PlaceResponse.fromJson(json.decode(response.body));
} else {
throw HttpException(
'Unexpected status code ${response.statusCode}:'
' ${response.reasonPhrase}',
uri: Uri.parse(googleLocationsURL));
uri: Uri.parse(googlePlacesURL));
}
}
}
Loading

0 comments on commit 5b9bfed

Please sign in to comment.