Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / RouteSearchRequest.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20
21 namespace Tizen.Maps
22 {
23     /// <summary>
24     /// Route search request for Tizen map service requests
25     /// </summary>
26     /// <since_tizen>3</since_tizen>
27     public class RouteSearchRequest : MapServiceRequest<Route>
28     {
29         private Interop.SearchRouteCallback _routeCallback;
30         private List<Route> _routeList = new List<Route>();
31
32         private Geocoordinates _to;
33         private Geocoordinates _from;
34         private List<Geocoordinates> _waypoints = new List<Geocoordinates>();
35
36         internal RouteSearchRequest(MapService service, Geocoordinates from, Geocoordinates to) : this(service, ServiceRequestType.SearchRoute)
37         {
38             _to = to;
39             _from = from;
40             startExecutionAction = new Action(() =>
41             {
42                 int requestID;
43                 errMessage = $"Failed to get route list for given origin {_from} and destination {_to}";
44                 if (_waypoints?.Count == 0)
45                 {
46                     _type = ServiceRequestType.SearchRoute;
47                     errorCode = _service.handle.SearchRoute(_from.handle, _to.handle, _service.Preferences.handle, _routeCallback, IntPtr.Zero, out requestID);
48                     if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
49                     {
50                         _requestTask?.TrySetException(errorCode.GetException(errMessage));
51                     }
52                 }
53                 else
54                 {
55                     _type = ServiceRequestType.SearchRouteWithWaypoints;
56
57                     var waypoints = GetCoordinateListForWaypoints();
58                     errorCode = _service.handle.SearchRouteWaypoints(waypoints, waypoints.Length, _service.Preferences.handle, _routeCallback, IntPtr.Zero, out requestID);
59                     if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
60                     {
61                         _requestTask?.TrySetException(errorCode.GetException(errMessage));
62                     }
63                 }
64                 _requestID = requestID;
65             });
66         }
67
68
69         private RouteSearchRequest(MapService service, ServiceRequestType type) : base(service, type)
70         {
71             // The Maps Service invokes this callback while iterating through the set of obtained Routes.
72             _routeCallback = (result, id, index, total, route, userData) =>
73             {
74                 errorCode = result;
75                 if (result.IsSuccess())
76                 {
77                     // The parameter route must be released using maps_route_destroy().
78                     var routeHandle = new Interop.RouteHandle(route, needToRelease: true);
79                     _routeList.Add(new Route(routeHandle));
80                     if (_routeList.Count == total)
81                     {
82                         _requestTask?.TrySetResult(_routeList);
83                     }
84                     return true;
85                 }
86                 else
87                 {
88                     // If search is failed, the value of total is 0 and route is NULL.
89                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
90                     return false;
91                 }
92             };
93         }
94
95         /// <summary>
96         /// Gets or sets a list of way-points to cover between origin and destination.
97         /// </summary>
98         /// <since_tizen>3</since_tizen>
99         public IEnumerable<Geocoordinates> Waypoints
100         {
101             get
102             {
103                 return _waypoints;
104             }
105             set
106             {
107                 _waypoints = value.ToList();
108             }
109         }
110
111         private IntPtr[] GetCoordinateListForWaypoints()
112         {
113             var waypoints = new IntPtr[_waypoints.Count + 2];
114             waypoints[0] = _from.handle;
115             for (int i = 0; i < _waypoints.Count; ++i)
116             {
117                 waypoints[i + 1] = _waypoints[i].handle;
118             }
119             waypoints[waypoints.Length - 1] = _to.handle;
120             return waypoints;
121         }
122     }
123 }