Commit 8dba1c08 authored by kempe's avatar kempe

Refactoring moved all remaining python calls to RadioAPI

parent f5479223
......@@ -36,6 +36,7 @@ OTHER_FILES += qml/harbour-received.qml \
qml/pages/Stations.qml \
qml/pages/BrowseByCategory.qml \
qml/components/SleepTimer.qml \
qml/components/RadioAPI.qml \
qml/pages/Settings.qml
# to disable building translations every time, comment out the
......
import QtQuick 2.0
import Sailfish.Silica 1.0
import io.thp.pyotherside 1.3
import "./js/Rad.js" as Rad
import "./js/Favorites.js" as FavoritesUtils
......@@ -20,39 +19,81 @@ Python {
function playStationById(id) {
loading = true;
call('api.radio.getStationById', [''+id], function(response) {
loading = false;
var stationData = Rad.getStationFromRadioJson(response);
console.log("Playing station:", JSON.stringify(stationData))
player.play(stationData);
loading = false;
});
}
function addRadIoFavorit(id) {
loading = true;
call('api.radio.getStationById', [''+id], function(response) {
loading = false
var stationData = Rad.getStationFromRadioJson(response);
FavoritesUtils.addFavorite(stationData)
loading = false
});
}
function updateFavorite(oldFavorite) {
loading = true;
call('api.radio.getStationById', [''+oldFavorite.radIoId], function(response) {
loading = false
var stationData = Rad.getStationFromRadioJson(response);
FavoritesUtils.removeFavorite(oldFavorite)
FavoritesUtils.addFavorite(stationData)
loading = false
});
}
function getCategories(cat, callback) {
loading = true;
call('api.radio.getCategories', [cat], function(response) {
callback(response);
function getCategories(cat, mList) {
window.loading = true;
call("api.radio.getCategories", [cat], function(response) {
console.log("Number of results:", response.length)
mList.clear()
response.forEach(function(category) {
mList.append({ title: category})
});
loading = false
});
}
function getTop100(mList) {
getStationsAndUpdateList("api.radio.getTopStations", [], mList);
}
function getRecomended(mList) {
getStationsAndUpdateList("api.radio.getRecomendedStations", [], mList);
}
function getLocal(mList) {
getStationsAndUpdateList("api.radio.getLocalStations", [], mList)
}
function getStationByCategory(category, value, mList) {
getStationsAndUpdateList("api.radio.getStationsByCategory", [category, value], mList)
}
function search(s, mList) {
getStationsAndUpdateList("api.radio.getSearchResults", [s], mList)
}
function getStationsAndUpdateList(method, params, mList) {
window.loading = true;
call(method, params, function(response) {
console.log("Number of results:", response.length)
mList.clear()
response.forEach(function(station) {
if(station.playable === "FREE") {
mList.append(station)
}
});
loading = false
});
}
......
......@@ -21,15 +21,15 @@ class Radio_API:
def getTopStations(self):
params = {'category': '_top'}
response = self.doRequest(top100URL, params)
pyotherside.send('updateStationList', response)
return response
def getRecomendedStations(self):
response = self.doRequest(recomendedURL)
pyotherside.send('updateStationList', response)
return response
def getLocalStations(self):
response = self.getMostWantedStations()
pyotherside.send('updateStationList', response['localBroadcasts'])
return response['localBroadcasts']
def getMostWantedStations(self):
# Available lists from most wanted
......@@ -51,14 +51,14 @@ class Radio_API:
params = {"category": "_{0}".format(category), "value": value}
pyotherside.send('log', "getStationsByCategory params: {0}".format(params))
response = self.doRequest(stationsByCategoriesURL, params)
pyotherside.send('updateStationList', response)
return response
def getSearchResults(self, s):
pyotherside.send('log', "Searching for: {}".format(s))
params = {"q": s, "start": "0", "rows": "100",
"streamcontentformats": "aac,mp3"}
response = self.doRequest(searchURL, params)
pyotherside.send('updateStationList', response)
return response
def getStationById(self, id):
pyotherside.send('log', "Getting station: {}".format(id))
......
......@@ -57,18 +57,7 @@ Page {
}
}
function populateList(cats) {
console.log("Number of results:", cats.length)
browseByModel.clear()
cats.forEach(function(cat) {
browseByModel.append({ title: cat})
});
}
Component.onCompleted: {
radioAPI.getCategories(category.toLocaleLowerCase(), function(cats) {
populateList(cats)
});
radioAPI.getCategories(category.toLocaleLowerCase(), browseByModel);
}
}
......@@ -43,7 +43,7 @@ Page {
EnterKey.iconSource: "image://theme/icon-m-enter-accept"
EnterKey.onClicked: {
if(text != "") {
python.search(text)
radioAPI.search(text, stationModel);
}
else {
focus = false;
......@@ -146,78 +146,20 @@ Page {
}
}
Python {
id: python
Component.onCompleted: {
addImportPath(Qt.resolvedUrl('../components/python'));
setHandler('updateStationList', function(stations) {
window.loading = false;
populateList(stations)
});
importModule('api', function () {});
}
function getTop100() {
window.loading = true;
call('api.radio.getTopStations', function() {});
}
function getRecomended() {
window.loading = true;
call('api.radio.getRecomendedStations', function() {});
}
function getLocal() {
window.loading = true;
call('api.radio.getLocalStations', function() {});
}
function getStationsByCategory() {
window.loading = true;
call('api.radio.getStationsByCategory', [category.toLowerCase(), value], function() {});
}
function search(s) {
window.loading = true;
call('api.radio.getSearchResults', [s], function() {});
}
onError: {
console.log('python error: ' + traceback);
}
onReceived: {
console.log('got message from python');
}
}
Component.onCompleted: {
if(page.listType === Utils.Top100) {
python.getTop100();
radioAPI.getTop100(stationModel);
}
else if(page.listType === Utils.Recommended) {
python.getRecomended();
radioAPI.getRecomended(stationModel);
}
else if(page.listType === Utils.Local) {
python.getLocal();
radioAPI.getLocal(stationModel)
}
else if(page.listType !== Utils.Search) {
python.getStationsByCategory();
radioAPI.getStationByCategory(category.toLocaleLowerCase(), value, stationModel);
}
}
function populateList(stations) {
console.log("Number of stations:", stations.length)
stationModel.clear()
stations.forEach(function(station) {
if(station.playable === "FREE") {
stationModel.append(station)
}
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment