summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Pages.Azure/AzureDataSource.cs
blob: 907421cb47234909f9bc44a3dfe472db5fa4f881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
´╗┐using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace Xamarin.Forms.Pages.Azure
{
	public class AzureDataSource : BaseDataSource
	{
		readonly ObservableCollection<IDataItem> _dataItems = new ObservableCollection<IDataItem>();
		Task _currentParseTask;
		bool _initialized;
		AzureSource _source;

		public AzureDataSource()
		{
		}

		internal AzureDataSource(JToken rootToken)
		{
			ParseJsonToken(rootToken);
		}
		
		public AzureSource Source
		{
			get
			{
				return _source;
			}
			set
			{
				if (_source == value)
					return;
				_source = value;

				_dataItems.Clear();
				if (value != null && _initialized)
				{
					_currentParseTask = ParseJson();
					_currentParseTask.ContinueWith(t => { throw t.Exception; }, TaskContinuationOptions.OnlyOnFaulted);
				}
			}
		}

		protected override async Task<IList<IDataItem>> GetRawData()
		{
			if (!_initialized)
			{
				Task task = _currentParseTask = ParseJson();
				await task;
			}
			else if (_currentParseTask != null && _currentParseTask.IsCompleted == false)
				await _currentParseTask;
			return _dataItems;
		}

		protected override object GetValue(string key)
		{
			IDataItem target = _dataItems.FirstOrDefault(d => d.Name == key);
			return target?.Value;
		}

		protected override bool SetValue(string key, object value)
		{
			IDataItem target = _dataItems.FirstOrDefault(d => d.Name == key);
			if (target == null)
			{
				_dataItems.Add(new DataItem(key, value));
				return true;
			}
			if (target.Value == value)
				return false;
			target.Value = value;
			return true;
		}

		object GetValueForJToken(JToken token)
		{
			switch (token.Type)
			{
				case JTokenType.Object:
				case JTokenType.Array:
					return new AzureDataSource(token);
				case JTokenType.Constructor:
				case JTokenType.Property:
				case JTokenType.Comment:
					throw new NotImplementedException();
				case JTokenType.Integer:
					return (int)token;
				case JTokenType.Float:
					return (float)token;
				case JTokenType.Raw:
				case JTokenType.String:
					return (string)token;
				case JTokenType.Boolean:
					return (bool)token;
				case JTokenType.Date:
					return (DateTime)token;
				case JTokenType.Bytes:
					return (byte[])token;
				case JTokenType.Guid:
					return (Guid)token;
				case JTokenType.Uri:
					return (Uri)token;
				case JTokenType.TimeSpan:
					return (TimeSpan)token;
				default:
					return null;
			}
		}

		async Task ParseJson()
		{
			_initialized = true;

			if (Source == null)
				return;

			IsLoading = true;
			var jtoken = await Source.GetJson();
			ParseJsonToken(jtoken);
			IsLoading = false;
		}

		void ParseJsonToken(JToken token)
		{
			var jArray = token as JArray;
			var jObject = token as JObject;
			if (jArray != null)
			{
				for (var i = 0; i < jArray.Count; i++)
				{
					JToken obj = jArray[i];
					_dataItems.Add(new DataItem(i.ToString(), GetValueForJToken(obj)));
				}
			}
			else if (jObject != null)
			{
				foreach (KeyValuePair<string, JToken> kvp in jObject)
				{
					_dataItems.Add(new DataItem(kvp.Key, GetValueForJToken(kvp.Value)));
				}
			}
		}
	}
}