VenuInfo improved #49

Merged
shas6395 merged 1 commits from shas6395 into master 2022-05-30 12:33:16 +02:00
2 changed files with 156 additions and 36 deletions
Showing only changes of commit fc045a6f87 - Show all commits

View File

@ -1,26 +1,72 @@
import 'dart:collection';
import 'dart:convert';
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:http/http.dart' as http;
import 'package:html/dom.dart' as dom;
import 'dart:io';
import 'Venue.dart';
class VenueInfo {
static const photoSize = '600x400'; //Can be replaced with custom size (Example Format: '300x400')
late LinkedHashMap<String, dynamic> _photos;
late List<dynamic> _tastes;
late int _priceClass;
late double _rating;
late int _totalRatings;
late bool _openNow;
late String _openHoursToday;
static const photoSize = 'original'; //Can be replaced with custom size (Example Format: '300x400')
late String _photo = 'https://www.bing.com/th?id=OIP.ZSXrQuieNC-hoPC4kIv_vgAAAA&w=212&h=212&c=8&rs=1&qlt=90&o=6&dpr=1.35&pid=3.1&rm=2';
late int _priceClass = -1;
late double _rating = -1;
late int _totalRatings = -1;
late bool _openNow = false;
late String _openHoursToday = 'N/A';
late String _fsqLink;
late double _popularity = -1;
VenueInfo();
VenueInfo(/*this._photos, this._tastes, this._priceClass, this._rating, this._totalRatings, this._openNow, this._openHoursToday*/);
Future getVenueInfo(Venue venue) async {
String venueName = venue.venueName;
double lat = venue.position.latitude;
double lng = venue.position.longitude;
Future getVenueInfo(String venueName) async {
final response1 = await http.get(
Uri.parse('https://api.foursquare.com/v3/autocomplete?query=$venueName&ll=$lat%2C$lng&types=place'),
headers: {
HttpHeaders.authorizationHeader: 'fsq3LBbeZ8imQK8X1hov7DTb9F64Xs1fs2bojHQ99QNm4TE=',
});
if(response1.statusCode == 200) {
Map data = jsonDecode(response1.body);
List results = data['results'];
if(results.isNotEmpty && data['results'][0]['link'] != null) {
_fsqLink = data['results'][0]['link'];
final response2 = await http.get(
Uri.parse('https://api.foursquare.com$_fsqLink?fields=price%2Crating%2Cphotos%2Chours%2Cstats%2Cpopularity'),
headers: {
HttpHeaders.authorizationHeader: 'fsq3LBbeZ8imQK8X1hov7DTb9F64Xs1fs2bojHQ99QNm4TE=',
}
);
if(response2.statusCode == 200) {
Map data = jsonDecode(response2.body);
data['price'] != null ? _priceClass = data['price']: null;
data['rating'] != null ? _rating = data['rating']: null;
data['hours']['open_now'] != null ? _openNow = data['hours']['open_now']: null;
data['stats']['total_ratings'] != null ? _totalRatings = data['stats']['total_ratings']: null;
data['hours']['display'] != null ? _openHoursToday = data['hours']['display']: null;
data['popularity'] != null ? _popularity = data['popularity']: null;
data['photos'][0]['prefix'] != null && data['photos'][0]['suffix'] != null ?
_photo = data['photos'][0]['prefix'] + photoSize + data['photos'][0]['suffix']: null;
}
else {
throw const HttpException("No connection to api.foursquare.com");
}
}
}
else {
throw const HttpException("No connection to api.foursquare.com");
}
}
/*Future getVenueInfo(String venueName) async {
final fourSquareURL =
Uri.parse('https://foursquare.com/explore?mode=url&near=Stockholm%2C%20Sweden&nearGeoId=72057594040601666&q=$venueName');
@ -40,12 +86,21 @@ class VenueInfo {
if(response2.statusCode == 200) {
Map data = jsonDecode(response2.body);
if(data['price'] != null) {
_priceClass = data['price'];
}
if(data['rating'] != null) {
_rating = data['rating'];
}
if(data['photos'][0] != null) {
_photos = data['photos'][0];
}
if(data['hours']['open now'] != null) {
_openNow = data['hours']['open_now'];
}
if(data['stats']['total_ratings'] != null) {
_totalRatings = data['stats']['total_ratings'];
_tastes = data['tastes'];
}
if(data['hours']['display'] != null) {
_openHoursToday = data['hours']['display'];
}
@ -57,44 +112,41 @@ class VenueInfo {
else {
throw const HttpException("No connection to foursquare.com");
}
}*/
String getRating() {
return _rating != -1 ? _rating.toString() + '/10': 'N/A';
}
double getRating() {
return _rating;
}
int getTotalRatings() { //Number of people who contributed to rating score
return _totalRatings;
String getTotalRatings() {
return _totalRatings != -1 ? _totalRatings.toString(): 'N/A';
}
String getOpenStatus() {
if(_openNow)
return 'Open now!';
else
return 'Closed';
return _openNow ? 'Open now!': 'Closed';
}
String getOpeningHours() {
String splitString;
if(_openHoursToday.isNotEmpty) {
return _openHoursToday;
splitString = _openHoursToday.replaceAll(';', '\n');
return splitString;
}
return 'N/A';
}
String getPriceClass () {
return '\$'*_priceClass;
return _priceClass != -1 ? '\$'*_priceClass : 'N/A';
}
String getPhotoURL () {
return _photos['prefix'] + photoSize + _photos['suffix'];
return _photo;
}
String getPopularity() {
return _popularity != -1 ? (_popularity*100).truncate().toString() + '%': 'N/A';
}
bool isOpenNow() {
return _openNow;
}
String getVenueURL () {
return 'https://api.foursquare.com$_fsqLink';
}
}

68
test/venue_info_test.dart Normal file
View File

@ -0,0 +1,68 @@
import 'package:flutter_applicationdemo/Venue.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_applicationdemo/VenueInfo.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
main() {
group('On successful connection', () {
late VenueInfo venueInfo;
test('Call async function', () async {
Venue testVenue = Venue(1, 'mälarpaviljongen', 'bla', 'bla', LatLng(59.327942, 18.034170));
VenueInfo vi = VenueInfo();
venueInfo = vi;
await venueInfo.getVenueInfo(testVenue);
});
test('_priceClass is not null', () {
expect(venueInfo.getPriceClass(), isNotNull);
});
test('_rating is not null', () {
expect(venueInfo.getRating(), isNotNull);
});
test('_totalRatings is not null', () {
expect(venueInfo.getTotalRatings(), isNotNull);
});
test('_openHoursToday is not null', () {
expect(venueInfo.getOpeningHours(), isNotNull);
});
test('_popularity is not null', () {
expect(venueInfo.getPopularity(), isNotNull);
});
test('_photo is not null', () {
expect(venueInfo.getPhotoURL(), isNotNull);
});
test('_openNow is not null', () {
expect(venueInfo.getOpenStatus(), isNotNull);
});
});
group('On unsuccessful connection', () {
late VenueInfo venueInfo;
test('Call constructor', () async {
Venue testVenue = Venue(1, '', 'bla', 'bla', LatLng(0, 0));
VenueInfo vi = VenueInfo();
venueInfo = vi;
await venueInfo.getVenueInfo(testVenue);
});
test('_priceClass is default', () {
expect(venueInfo.getPriceClass(), 'N/A');
});
test('_rating is default', () {
expect(venueInfo.getRating(), 'N/A');
});
test('_totalRatings is default', () {
expect(venueInfo.getTotalRatings(), 'N/A');
});
test('_openHoursToday is default', () {
expect(venueInfo.getOpeningHours(), 'N/A');
});
test('_popularity is default', () {
expect(venueInfo.getPopularity(), 'N/A');
});
test('_photo is default', () {
expect(venueInfo.getPhotoURL(), 'https://www.bing.com/th?id=OIP.ZSXrQuieNC-hoPC4kIv_vgAAAA&w=212&h=212&c=8&rs=1&qlt=90&o=6&dpr=1.35&pid=3.1&rm=2');
});
test('_openNow is default', () {
expect(venueInfo.getOpenStatus(), 'Closed');
});
});
}