Remove unnecessaray parameter of TizenDeviceInfo
[platform/upstream/xamarin-forms.git] / Xamarin.Forms.Pages / JsonDataSource.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Linq;
5 using System.Threading.Tasks;
6 using Newtonsoft.Json.Linq;
7
8 namespace Xamarin.Forms.Pages
9 {
10         public class JsonDataSource : BaseDataSource
11         {
12                 readonly ObservableCollection<IDataItem> _dataItems = new ObservableCollection<IDataItem>();
13                 Task _currentParseTask;
14                 bool _initialized;
15                 JsonSource _source;
16
17                 public JsonDataSource()
18                 {
19                 }
20
21                 internal JsonDataSource(JToken rootToken)
22                 {
23                         ParseJsonToken(rootToken);
24                 }
25
26                 [TypeConverter(typeof(JsonSourceConverter))]
27                 public JsonSource Source
28                 {
29                         get { return _source; }
30                         set
31                         {
32                                 if (_source == value)
33                                         return;
34                                 _source = value;
35
36                                 _dataItems.Clear();
37                                 if (value != null && _initialized)
38                                 {
39                                         _currentParseTask = ParseJson();
40                                         _currentParseTask.ContinueWith(t => { throw t.Exception; }, TaskContinuationOptions.OnlyOnFaulted);
41                                 }
42                         }
43                 }
44
45                 protected override async Task<IList<IDataItem>> GetRawData()
46                 {
47                         if (!_initialized)
48                         {
49                                 Task task = _currentParseTask = ParseJson();
50                                 await task;
51                         }
52                         else if (_currentParseTask != null && _currentParseTask.IsCompleted == false)
53                                 await _currentParseTask;
54                         return _dataItems;
55                 }
56
57                 protected override object GetValue(string key)
58                 {
59                         IDataItem target = _dataItems.FirstOrDefault(d => d.Name == key);
60                         return target?.Value;
61                 }
62
63                 protected override bool SetValue(string key, object value)
64                 {
65                         IDataItem target = _dataItems.FirstOrDefault(d => d.Name == key);
66                         if (target == null)
67                         {
68                                 _dataItems.Add(new DataItem(key, value));
69                                 return true;
70                         }
71                         if (target.Value == value)
72                                 return false;
73                         target.Value = value;
74                         return true;
75                 }
76
77                 object GetValueForJToken(JToken token)
78                 {
79                         switch (token.Type)
80                         {
81                                 case JTokenType.Object:
82                                 case JTokenType.Array:
83                                         return new JsonDataSource(token);
84                                 case JTokenType.Constructor:
85                                 case JTokenType.Property:
86                                 case JTokenType.Comment:
87                                         throw new NotImplementedException();
88                                 case JTokenType.Integer:
89                                         return (int)token;
90                                 case JTokenType.Float:
91                                         return (float)token;
92                                 case JTokenType.Raw:
93                                 case JTokenType.String:
94                                         return (string)token;
95                                 case JTokenType.Boolean:
96                                         return (bool)token;
97                                 case JTokenType.Date:
98                                         return (DateTime)token;
99                                 case JTokenType.Bytes:
100                                         return (byte[])token;
101                                 case JTokenType.Guid:
102                                         return (Guid)token;
103                                 case JTokenType.Uri:
104                                         return (Uri)token;
105                                 case JTokenType.TimeSpan:
106                                         return (TimeSpan)token;
107                                 default:
108                                         return null;
109                         }
110                 }
111
112                 async Task ParseJson()
113                 {
114                         _initialized = true;
115
116                         if (Source == null)
117                                 return;
118
119                         IsLoading = true;
120                         string json = await Source.GetJson();
121                         JToken jToken = JToken.Parse(json);
122                         ParseJsonToken(jToken);
123                         IsLoading = false;
124                 }
125
126                 void ParseJsonToken(JToken token)
127                 {
128                         var jArray = token as JArray;
129                         var jObject = token as JObject;
130                         if (jArray != null)
131                         {
132                                 for (var i = 0; i < jArray.Count; i++)
133                                 {
134                                         JToken obj = jArray[i];
135                                         _dataItems.Add(new DataItem(i.ToString(), GetValueForJToken(obj)));
136                                 }
137                         }
138                         else if (jObject != null)
139                         {
140                                 foreach (KeyValuePair<string, JToken> kvp in jObject)
141                                 {
142                                         _dataItems.Add(new DataItem(kvp.Key, GetValueForJToken(kvp.Value)));
143                                 }
144                         }
145                 }
146         }
147 }