[Maps] Modify diposing routines
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / Polyline.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 using Color = ElmSharp.Color;
22
23 namespace Tizen.Maps
24 {
25     /// <summary>
26     /// The polyline map object.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class Polyline : MapObject, IDisposable
30     {
31         internal Interop.PolylineHandle handle;
32         private List<Geocoordinates> _coordinateList;
33
34         /// <summary>
35         /// Creates polyline visual object.
36         /// </summary>
37         /// <since_tizen> 3 </since_tizen>
38         /// <param name="coordinates">List of geographical coordinates.</param>
39         /// <param name="color">Line color.</param>
40         /// <param name="width">The width of line [1 ~ 100] \(pixels).</param>
41         /// <exception cref="ArgumentException">Thrown when input values are invalid.</exception>
42         public Polyline(List<Geocoordinates> coordinates, Color color, int width) : base()
43         {
44             var err = Interop.ErrorCode.InvalidParameter;
45             if (coordinates == null || coordinates.Count() < 2)
46             {
47                 err.ThrowIfFailed("given coordinates list should contain at least 2 coordinates");
48             }
49             _coordinateList = coordinates.ToList();
50             var geocoordinateList = new GeocoordinatesList(_coordinateList);
51             handle = new Interop.PolylineHandle(geocoordinateList.handle, color, width);
52         }
53
54         /// <summary>
55         /// Destroy the Polyline object.
56         /// </summary>
57         ~Polyline()
58         {
59             Dispose(false);
60         }
61
62         /// <summary>
63         /// Adds or removes the clicked event handlers.
64         /// </summary>
65         /// <since_tizen> 3 </since_tizen>
66         public event EventHandler Clicked;
67
68         /// <summary>
69         /// Gets or sets the visibility for polyline.
70         /// </summary>
71         /// <since_tizen> 3 </since_tizen>
72         public override bool IsVisible
73         {
74             get { return handle.IsVisible; }
75             set { handle.IsVisible = value; }
76         }
77
78         /// <summary>
79         /// Gets or sets a list of geographical coordinates for polyline vertices.
80         /// </summary>
81         /// <since_tizen> 3 </since_tizen>
82         public IEnumerable<Geocoordinates> Coordinates
83         {
84             get
85             {
86                 return _coordinateList;
87             }
88             set
89             {
90                 var coordinates = value.ToList();
91                 var err = Interop.ErrorCode.InvalidParameter;
92                 if (coordinates == null || coordinates.Count() < 2)
93                 {
94                     err.ThrowIfFailed("given coordinates list should contain at least 2 coordinates");
95                 }
96
97                 var geocoordinateList = new GeocoordinatesList(coordinates, false);
98                 if (handle.SetPolyline(geocoordinateList.handle).IsSuccess())
99                 {
100                     _coordinateList = coordinates;
101                 }
102             }
103         }
104
105         /// <summary>
106         /// Gets or sets the line color.
107         /// </summary>
108         /// <since_tizen> 3 </since_tizen>
109         public Color LineColor
110         {
111             get
112             {
113                 return handle.LineColor;
114             }
115             set
116             {
117                 handle.LineColor = value;
118             }
119         }
120
121         /// <summary>
122         /// Gets or sets the line width from 1 to 100 pixels.
123         /// </summary>
124         /// <since_tizen> 3 </since_tizen>
125         public int Width
126         {
127             get
128             {
129                 return handle.LineWidth;
130             }
131             set
132             {
133                 handle.LineWidth = value;
134             }
135         }
136
137         internal override void HandleClickedEvent()
138         {
139             Clicked?.Invoke(this, EventArgs.Empty);
140         }
141
142         internal override void InvalidateMapObject()
143         {
144             handle = null;
145         }
146
147         internal override Interop.ViewObjectHandle GetHandle()
148         {
149             return handle;
150         }
151
152         #region IDisposable Support
153         private bool _disposedValue = false;
154
155         /// <summary>
156         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
157         /// </summary>
158         /// <param name="disposing">If true, managed and unmanaged resources can be disposed, otherwise only unmanaged resources can be disposed.</param>
159         /// <since_tizen> 3 </since_tizen>
160         protected virtual void Dispose(bool disposing)
161         {
162             if (!_disposedValue)
163             {
164                 if (disposing)
165                 {
166                     _coordinateList?.Clear();
167                 }
168                 handle?.Dispose();
169                 _disposedValue = true;
170             }
171         }
172
173         /// <summary>
174         /// Releases all the resources used by this object.
175         /// </summary>
176         /// <since_tizen> 3 </since_tizen>
177         public void Dispose()
178         {
179             Dispose(true);
180             GC.SuppressFinalize(this);
181         }
182         #endregion
183     }
184 }