From adf8ff33bfe3dbcea18602221e9b4417592aef96 Mon Sep 17 00:00:00 2001 From: Benny Samir Hierl Date: Thu, 22 Oct 2020 20:39:31 +0200 Subject: [PATCH] Option to search Station IDs Signed-off-by: Benny Samir Hierl --- VRR-Monitor/VRR-Monitor.js | 52 +++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/VRR-Monitor/VRR-Monitor.js b/VRR-Monitor/VRR-Monitor.js index 338a6f0..78cef0a 100644 --- a/VRR-Monitor/VRR-Monitor.js +++ b/VRR-Monitor/VRR-Monitor.js @@ -79,12 +79,60 @@ async function fetchStationdata(Id, linesFilter, distance, transportCodes) { } }; +async function findNextStations(latitude, longitude) { + let req = new Request(`https://abfahrtsmonitor.vrr.de/backend/api/stations/coord?long=${longitude}&lat=${latitude}`) + + let res = await req.loadJSON() + return res['suggestions'] +}; + +function populateTable(table, stations) { + table.removeAllRows() + for (i = 0; i < stations.length; i++) { + let station = stations[i] + let row = new UITableRow() + + let nameCell = row.addText(station.value) + nameCell.leftAligned() + nameCell.widthWeight = 1 + + let buttonCell = row.addButton('Copy ID to clipboard'); + buttonCell.rightAligned() + buttonCell.widthWeight = 1 + buttonCell.onTap = () => { + Pasteboard.copy(station.data); + + let alert = new Alert(); + alert.message = `The Station ID '${station.data}' was copied to the clipboard`; + alert.presentSheet() + } + + table.addRow(row) + } +} + let widgetInputRAW = args.widgetParameter; let widgetInput = null; if (widgetInputRAW !== null) { widgetInput = widgetInputRAW.toString().split(";"); } else { + if(!config.runsInWidget) { + let prompt = new Alert() + prompt.message = 'Do you like to find Station IDs from Stations next to you?' + let okAction = prompt.addAction('Yes') + + let cancelAction = prompt.addCancelAction('No') + let decision = await prompt.presentAlert() + if (decision === 0) { + let l = await Location.current() + let stations = await findNextStations(l.latitude, l.longitude) + let table = new UITable() + table.showSeparators = true + populateTable(table, stations) + await QuickLook.present(table) + } + } throw new Error('No Station Code set!') } @@ -116,7 +164,6 @@ if (data) { ] widget.backgroundGradient = gradient - let firstLineStack = widget.addStack() let provider = firstLineStack.addText("🚏 " + stationName) @@ -180,5 +227,4 @@ if(!config.runsInWidget) { // Tell the system to show the widget. Script.setWidget(widget) Script.complete() -} - +} \ No newline at end of file