File size: 5,584 Bytes
b3787d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*!
 * RxShortages
 * http://mickschroeder.com/rxshortages
 *
 * Copyright 2011, Michael Schroeder
 * License: GPL Version 3 or later
 * http://www.gnu.org/licenses/gpl.html
 */

//
// Show Feed
//

// Set properties
var win = Ti.UI.currentWindow;
win.backgroundColor = '#fff';

// Set variables
var data, newRow, query, siteUrl, yqlMatches;


// Create search bar
var search = Titanium.UI.createSearchBar({
	showCancel:true,
	height:43,
	top:0,
});

// Create Tableview
var tableView = Ti.UI.createTableView({
	search: search,
	filterAttribute: 'theTitle'
});

// Populate a tableview with the titles
Ti.UI.currentWindow.add(tableView);

// Refresh buttons
if (Ti.Platform.name === 'iPhone OS') {
	var refresh = Titanium.UI.createButton({
		systemButton: Titanium.UI.iPhone.SystemButton.REFRESH
	});
		win.rightNavButton = refresh;

	refresh.addEventListener('click', function () {
		Ti.App.fireEvent("show_indicator");
		tableView.setData(null);
		setTableData();
	});
} 
else if (Titanium.Platform.name === 'android')
{
var activity = Ti.Android.currentActivity;
activity.onCreateOptionsMenu = function(e) {
    var menu = e.menu;
    var menuItem = menu.add({ title: "Refresh" });
    menuItem.addEventListener("click", function(e) {
		Ti.App.fireEvent("show_indicator");
		tableView.setData(null);
		setTableData();
		});    
};
}


function setTableData() {

	// YQL query to get feed.
	YQLMatches = Ti.App.Properties.getString('websiteName');

	siteUrl = "www.fda.gov/downloads/Drugs/DrugSafety/DrugShortages/UCM163172.xml";
		
	query = "Select title, description, link from rss where url='" + siteUrl + "' and title MATCHES '.*"+ YQLMatches +".*'  | unique(field='link') | sort(field='title')";

	Ti.Yahoo.yql(query, function (e) {
		data = e.data;
		
		//Ti.API.info(data);
		//Ti.API.info(data.item.length);

		// if (data === null)
		// {
			// Ti.App.fireEvent("hide_indicator");
			// Titanium.UI.createAlertDialog({
				// title: 'ERROR',
				// message: 'No data could be retrieved. Please Check your internet connection.' }).show();
				// return;
			// }
		if (data === null) {
					Ti.API.info('data.item.length == null');

					newRow = Ti.UI.createTableViewRow({
						hasChild: false,
					});
					
					// Label
					var articleTitleLabel = Ti.UI.createLabel({
						text: 'Nothing to report',
						left: 10,
						right: 30,
						color: '#000',
						font: {
							fontSize: '15dp',
							fontWeight: 'bold'
						}
					});
					newRow.add(articleTitleLabel);
					tableView.appendRow(newRow);
					Ti.App.fireEvent("hide_indicator");					

		}
		
		else if (data.item.length == null) {
		//Remove titles
					data.item.title = data.item.title.replace("Current Drug Shortages: ", "");
					data.item.title = data.item.title.replace("Resolved Drug Shortages: ", "");
					data.item.title = data.item.title.replace("Drugs to be Discontinued: ", "");
					data.item.title = data.item.title.replace("(updated)", "");

					newRow = Ti.UI.createTableViewRow({
						theTitle: data.item.title,
						pubDate: data.item.pubDate,
						desc : data.item.description,
						url: data.item.link,
						hasChild: true,
						className: 'FDA_row',
						height: Ti.UI.FILL,
					});
					// Label
					var articleTitleLabel = Ti.UI.createLabel({
						text: data.item.title,
						left: 10,
						right: 30,
						color: '#000',
						height: Ti.UI.FILL,
						font: {
							fontSize: '15dp',
							fontWeight: 'bold'
						}
					});
					newRow.add(articleTitleLabel);
					tableView.appendRow(newRow);
					Ti.App.fireEvent("hide_indicator");	
		}
			
	 		else {
				var tableData = [];
				// For each item from the total number of postings returned from the query...
				for (var i = 0, j = data.item.length; i < j; i++) {
					
					//Remove titles
					data.item[i].title = data.item[i].title.replace("Current Drug Shortages: ", "");
					data.item[i].title = data.item[i].title.replace("Resolved Drug Shortages: ", "");
					data.item[i].title = data.item[i].title.replace("Drugs to be Discontinued: ", "");
					data.item[i].title = data.item[i].title.replace("(updated)", "");

					newRow = Ti.UI.createTableViewRow({
						theTitle: data.item[i].title,
						pubDate: data.item[i].pubDate,
						desc : data.item[i].description,
						url: data.item[i].link,
						hasChild: true,
						height: Ti.UI.FILL,
						className: 'FDA_row'
					});
					// Label
					var articleTitleLabel = Ti.UI.createLabel({
						text: data.item[i].title,
						left: 10,
						right: 30,
						color: '#000',
						height: Ti.UI.FILL,
						font: {
							fontSize: '15dp',
							fontWeight: 'bold'
						}
					});
					newRow.add(articleTitleLabel);
					tableData.push(newRow);
					} // end YQL
					tableView.setData(tableData);
					

					// Data has been loaded/added, so remove the loading icon.
					Ti.App.fireEvent("hide_indicator");					
				}
			});
		}
		// When a title is clicked, open a new window and pass the details of the selected posting.
	tableView.addEventListener('click', function (e) {

		Ti.App.fireEvent('detailLoadFDA', {theUrl:e.rowData.url,desc:e.rowData.desc,theTitle:e.rowData.theTitle});
	
	});


		// Make sure device is connection to the internet

		if (Ti.Network.online) {

			// Show loading indicator 
			Ti.App.fireEvent("show_indicator");

			// Load table
			setTableData();

		} 

		// If not connected, alert the user
		else {
			var alertDialog = Titanium.UI.createAlertDialog({
				title: 'Internet Connection Required',
				message: 'Your device is not online',
				buttonNames: ['OK']
			});
			alertDialog.show();
		}