Skip to content

Commit

Permalink
[#1240] Fix crash under Windows (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
jherkel authored Oct 25, 2023
1 parent 1c4be61 commit 322eaf6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion geolocator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies:
geolocator_android: ^4.3.0
geolocator_apple: ^2.3.0
geolocator_web: ^2.2.0
geolocator_windows: ^0.2.1
geolocator_windows: ^0.2.2

dev_dependencies:
flutter_test:
Expand Down
6 changes: 5 additions & 1 deletion geolocator_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.2

* Fixes crash under Windows when getCurrentPosition method is called. (#1240)

## 0.2.1

* Exposes altitude accuracy to the `Position` class.
Expand All @@ -24,4 +28,4 @@ if "Let apps access your location" toggle option is switched ON from OFF.

## 0.1.0

* Adds an initial implementation of Windows support for the geolocator plugin.
* Adds an initial implementation of Windows support for the geolocator plugin.
2 changes: 1 addition & 1 deletion geolocator_windows/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: geolocator_windows
description: Geolocation Windows plugin for Flutter. This plugin provides the Windows implementation for the geolocator.
repository: https://github.com/baseflow/flutter-geolocators
issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen
version: 0.2.1
version: 0.2.2

environment:
sdk: ">=2.15.0 <4.0.0"
Expand Down
6 changes: 4 additions & 2 deletions geolocator_windows/windows/geolocator_enums.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace geolocator_plugin {

enum ErrorCode {
PermissionDefinitionsNotFound
PermissionDefinitionsNotFound,
OperationCanceled,
UnknownError
};

enum LocationPermission {
Expand Down Expand Up @@ -31,4 +33,4 @@ enum ServiceStatus {
Enabled
};

} // namespace geolocator_plugin
} // namespace geolocator_plugin
42 changes: 35 additions & 7 deletions geolocator_windows/windows/geolocator_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ std::string ErrorCodeToString(ErrorCode errorCode) {
switch (errorCode) {
case ErrorCode::PermissionDefinitionsNotFound:
return "PERMISSION_DEFINITIONS_NOT_FOUND";
case ErrorCode::OperationCanceled:
return "OPERATION_CANCELED";
case ErrorCode::UnknownError:
return "UNKNOWN_ERROR";
default:
return "";
throw std::logic_error("unexcepted value" + static_cast<int>(errorCode));
}
}

Expand Down Expand Up @@ -146,8 +150,13 @@ void GeolocatorPlugin::OnCheckPermission(std::unique_ptr<MethodResult<>> result)
RequestAccessAsync(std::move(result));
}

bool isLocationStatusValid (const PositionStatus& status) {
return status != PositionStatus::Disabled
&& status != PositionStatus::NotAvailable;
}

void GeolocatorPlugin::OnIsLocationServiceEnabled(std::unique_ptr<MethodResult<>> result) {
result->Success(EncodableValue(geolocator.LocationStatus() != PositionStatus::NotAvailable));
result->Success(EncodableValue(isLocationStatusValid(geolocator.LocationStatus())));
}

void GeolocatorPlugin::OnRequestPermission(std::unique_ptr<MethodResult<>> result) {
Expand All @@ -156,8 +165,18 @@ void GeolocatorPlugin::OnRequestPermission(std::unique_ptr<MethodResult<>> resul

winrt::fire_and_forget GeolocatorPlugin::OnGetLastKnownPosition(const MethodCall<>& method_call,
std::unique_ptr<MethodResult<>> result) {
auto location = co_await geolocator.GetGeopositionAsync(std::chrono::hours(1), std::chrono::milliseconds::zero());
result->Success(LocationToEncodableMap(location));
try {
auto location = co_await geolocator.GetGeopositionAsync(std::chrono::hours(1), std::chrono::milliseconds::zero());
result->Success(LocationToEncodableMap(location));
}
catch (hresult_canceled const& error) {
result->Error(ErrorCodeToString(ErrorCode::OperationCanceled),
to_string(error.message()));
}
catch (hresult_error const& error) {
result->Error(ErrorCodeToString(ErrorCode::UnknownError),
to_string(error.message()));
}
}

void GeolocatorPlugin::GetLocationAccuracy(std::unique_ptr<MethodResult<>> result) {
Expand All @@ -169,9 +188,18 @@ void GeolocatorPlugin::GetLocationAccuracy(std::unique_ptr<MethodResult<>> resul

winrt::fire_and_forget GeolocatorPlugin::OnGetCurrentPosition(const MethodCall<>& method_call,
std::unique_ptr<MethodResult<>> result) {

auto location = co_await geolocator.GetGeopositionAsync();
result->Success(LocationToEncodableMap(location));
try {
auto location = co_await geolocator.GetGeopositionAsync();
result->Success(LocationToEncodableMap(location));
}
catch (hresult_canceled const& error) {
result->Error(ErrorCodeToString(ErrorCode::OperationCanceled),
to_string(error.message()));
}
catch (hresult_error const& error) {
result->Error(ErrorCodeToString(ErrorCode::UnknownError),
to_string(error.message()));
}
}

std::unique_ptr<StreamHandlerError<EncodableValue>> GeolocatorPlugin::OnListen(
Expand Down

0 comments on commit 322eaf6

Please sign in to comment.