Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added google geocoding to get city/state/country #83

Open
wants to merge 12 commits into
base: designs_v1.1
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions lib/app_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class AppConstants {
static const NOT_TESTED_FEEL_SICK = 4;
static const NOT_TESTTED_NOT_SICK = 5;

static const API_KEY_GEOCODING = "put the api key here";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will move to .env I guess


static const DISTANCE_DISPLACEMENT_FACTOR = "DISTANCE_DISPLACEMENT_FACTOR";

static const String TESTED_URL =
Expand Down
3 changes: 2 additions & 1 deletion lib/location_updates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class LocationUpdates {
static Future<Location> currentLocation() async => bg.BackgroundGeolocation.getCurrentPosition();

static Future<bool> isWithinAvailableGeoLocation() async {
var success = await PushNotifications.updateLoggedInUser();
var location = await LocationUpdates.currentLocation();
var success = await ApiRepository.isExistsInLocationGate(location);
return success;
}

Expand Down
59 changes: 54 additions & 5 deletions lib/network/api_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import 'package:corona_trace/network/notification/response_notification.dart';
import 'package:dio/dio.dart';
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart';
import 'package:flutter/material.dart';
import 'package:google_geocoding/google_geocoding.dart' as GoogleGeo;
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';

import '../app_constants.dart';

class ApiRepository {
static final ApiRepository _instance = ApiRepository._internal();

Expand Down Expand Up @@ -43,8 +46,7 @@ class ApiRepository {
var deviceID = await AppConstants.getDeviceId();
var url = "$API_URL/users";
var body = tokenRequestBody(token, deviceID, currentLocation);
Response response =
await _dio.post(url, data: JSON.jsonEncode(body));
Response response = await _dio.post(url, data: JSON.jsonEncode(body));
var statusCode = response.statusCode;
debugPrint("$statusCode - $url");
if (response.statusCode == 200) {
Expand All @@ -54,9 +56,48 @@ class ApiRepository {
return false;
}

static Map<String, dynamic> tokenRequestBody(String token, String deviceID, Location location) =>
{
"token": token,
static Future<bool> isExistsInLocationGate(Location fromLocation) async {
try {
GoogleGeo.GoogleGeocoding googleGeocoding =
GoogleGeo.GoogleGeocoding(AppConstants.API_KEY_GEOCODING);
var geocodingResponse = await googleGeocoding.geocoding.getReverse(
GoogleGeo.LatLon(
fromLocation.coords.latitude, fromLocation.coords.longitude));
var address = geocodingResponse.results.first.addressComponents;
Triple csc = extractCSC(address);
return true;
} catch (ex) {
print(ex);
}
return false;
}

static Triple extractCSC(List<GoogleGeo.AddressComponent> address) {
var country;
var state;
var city;
if (address.isNotEmpty) {
address.forEach((addressComponent) {
addressComponent.types.forEach((type) {
if (type == "country") {
country = addressComponent.longName;
}
if (type == "administrative_area_level_1") {
state = addressComponent.longName;
}
if (type == "locality") {
city = addressComponent.longName;
}
});
});
}
return Triple(country, state, city);
}

static Map<String, dynamic> tokenRequestBody(String token, String deviceID,
Location location) =>
{
"token": token,
"userId": deviceID,
"location": {
"latitude": location.coords.latitude,
Expand Down Expand Up @@ -162,3 +203,11 @@ class ApiRepository {
await sharedPrefs.setBool(DID_ALLOW_NOTIFY_WHEN_AVAILABLE, shouldNotify);
}
}

class Triple<T extends String, S extends String, C extends String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice name 😅 How about LocationGateTriple

T a;
S b;
C c;

Triple(this.a, this.b, this.c);
}
1 change: 0 additions & 1 deletion lib/service/push_notifications/push_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class PushNotifications {
'Zero channel',
playSound: false,
enableVibration: false,
style: AndroidNotificationStyle.BigText,
importance: Importance.Max,
priority: Priority.High,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ class OnboardingLocationPermissionState
}

Future onPremissionAvailable(BuildContext context) async {
showLoadingDialog(tapDismiss: false);
hideLoadingDialog();
navigateNotificationPermission(context);
}

Expand Down
Loading