Skip to content

Commit

Permalink
[#1240] Fix crash under Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jherkel committed Oct 24, 2023
1 parent 452fe98 commit 76bd4b0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
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 76bd4b0

Please sign in to comment.