Modify documentation of APIs
[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     public class RouteSearchRequest : MapServiceRequest<Route>
27     {
28         private Interop.SearchRouteCallback _routeCallback;
29         private List<Route> _routeList = new List<Route>();
30
31         private Geocoordinates _to;
32         private Geocoordinates _from;
33         private List<Geocoordinates> _waypoints = new List<Geocoordinates>();
34
35         internal RouteSearchRequest(MapService service, Geocoordinates from, Geocoordinates to) : this(service, ServiceRequestType.SearchRoute)
36         {
37             _to = to;
38             _from = from;
39             startExecutionAction = new Action(() =>
40             {
41                 int requestID;
42                 errMessage = $"Failed to get route list for given origin {_from} and destination {_to}";
43                 if (_waypoints?.Count == 0)
44                 {
45                     _type = ServiceRequestType.SearchRoute;
46                     errorCode = _service.handle.SearchRoute(_from.handle, _to.handle, _service.Preferences.handle, _routeCallback, IntPtr.Zero, out requestID);
47                     if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
48                     {
49                         _requestTask?.TrySetException(errorCode.GetException(errMessage));
50                     }
51                 }
52                 else
53                 {
54                     _type = ServiceRequestType.SearchRouteWithWaypoints;
55
56                     var waypoints = GetCoordinateListForWaypoints();
57                     errorCode = _service.handle.SearchRouteWaypoints(waypoints, waypoints.Length, _service.Preferences.handle, _routeCallback, IntPtr.Zero, out requestID);
58                     if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
59                     {
60                         _requestTask?.TrySetException(errorCode.GetException(errMessage));
61                     }
62                 }
63                 _requestID = requestID;
64             });
65         }
66
67
68         private RouteSearchRequest(MapService service, ServiceRequestType type) : base(service, type)
69         {
70             // The Maps Service invokes this callback while iterating through the set of obtained Routes.
71             _routeCallback = (result, id, index, total, route, userData) =>
72             {
73                 errorCode = result;
74                 if (result.IsSuccess())
75                 {
76                     // The parameter route must be released using maps_route_destroy().
77                     var routeHandle = new Interop.RouteHandle(route, needToRelease: true);
78                     _routeList.Add(new Route(routeHandle));
79                     if (_routeList.Count == total)
80                     {
81                         _requestTask?.TrySetResult(_routeList);
82                     }
83                     return true;
84                 }
85                 else
86                 {
87                     // If search is failed, the value of total is 0 and route is NULL.
88                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
89                     return false;
90                 }
91             };
92         }
93
94         /// <summary>
95         /// Gets or sets a list of way-points to cover between origin and destination.
96         /// </summary>
97         public IEnumerable<Geocoordinates> Waypoints
98         {
99             get
100             {
101                 return _waypoints;
102             }
103             set
104             {
105                 _waypoints = value.ToList();
106             }
107         }
108
109         private IntPtr[] GetCoordinateListForWaypoints()
110         {
111             var waypoints = new IntPtr[_waypoints.Count + 2];
112             waypoints[0] = _from.handle;
113             for (int i = 0; i < _waypoints.Count; ++i)
114             {
115                 waypoints[i + 1] = _waypoints[i].handle;
116             }
117             waypoints[waypoints.Length - 1] = _to.handle;
118             return waypoints;
119         }
120     }
121 }