maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   SailfishOS (https://talk.maemo.org/forumdisplay.php?f=52)
-   -   Help me to code for Sailfish OS (https://talk.maemo.org/showthread.php?t=100817)

juiceme 2020-07-10 08:11

Re: Help me to code for Sailfish OS
 
Quote:

Originally Posted by levone1 (Post 1568243)
Oh - so if I change the file first, then install the patch, it should work... I'll check it out...

It depends on what the change is; patchmanager just applies patches with "standard" unix patch utility which can apply hunks if the changes done to the file are not touching the same lines and it can still detect the correct place to apply the hunk.

The way to learn is to read the file and see what changes the patch is making, just like @coderus said. :)

Markkyboy 2020-08-22 10:48

Re: Help me to code for Sailfish OS
 
Hi all,

I've made myself a basic weather application, it uses the OpenWeatherMap.org API for data.

I would like to know how to enter my Weather API URL to my `weather.js` file using Dialog, but I'm unsure how to do it. I have played around with the example from Jolla, but I don't know quite what I'm doing......help! :)

The weather.js file uses XmlHttpRequest to fetch the data.

Code:

function requestWeatherData(cntr) {
    var xhr = new XMLHttpRequest;
    xhr.open("GET", `MY API KEY AND ID HERE`);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            cntr.weatherData = JSON.parse(xhr.responseText)
        }
    }
    xhr.send();
}

thanks,

Thoke 2020-08-23 08:50

Re: Help me to code for Sailfish OS
 
is your API key embedded to url? Then you can just do this:

Code:

let url = 'https.//www.example.com?api_key=' + API_key;
the key might need to be converted to string, perhaps. Also need to check what name the API key has, it might not be api_key like in the url.

I would then use fetch api to get the response:

Code:

let response = await fetch(url);
let data = await response.json();

// parse data here


Fuzzillogic 2020-08-23 14:42

Re: Help me to code for Sailfish OS
 
Quote:

Originally Posted by Thoke (Post 1568874)
I would then use fetch api to get the response:

Is the fetch API present in SFOS's ancient Qt version? The async await-construct isn't, I found out the hard way a few months ago. Native Promises are neither, by the way.

Thoke 2020-08-23 17:24

Re: Help me to code for Sailfish OS
 
Ah, right. Didn't know that. I suggested fetch because it's so easy. Let me check how to do it with xmlhttprequest...

Thoke 2020-08-23 18:53

Re: Help me to code for Sailfish OS
 
Found from here: https://openweathermap.org/forecast5#call

Code:

api.openweathermap.org/data/2.5/forecast?zip={zip code},{country code}&appid={your api key}
so you can contstruct your url as per my previous suggestion.

EDIT: I had to check, but XMLHttpRequest doesn't allow cross-site requests, so it wont work without modifications. With a short look, it seems there's CORS and JSONP to circumvent the restriction, but I don't know if they're supported on SFOS, and this is also something I have no experience implementing.

I would try it anyways, though. Maybe there's no restrictions when using it when you are not actually doing this js stuff with a browser attached.

rob_kouw 2020-08-26 09:01

Re: Help me to code for Sailfish OS
 
1 Attachment(s)
Is there a way to force a ListModel to refresh, when the database records have been changed?

I have a page, where the ListModel is filled by a JS function getting a SELECT from a SQLite database. It is a list of letters, with a possible value, e.g. A="3", B="".
After some experiments with TextFields I chose for Buttons. Then a Dialog opens, where the value can be changed.

However, after the change, the Button does not show the new value of the letter. Can I force an update? Maybe by a signal from the PageStack that the current page has become visible again?

The Button is now configured like:
Code:

Button {
        height: Theme.itemSizeMedium
        preferredWidth: Theme.buttonWidthLarge
        anchors.horizontalCenter: parent.horizontalCenter
        text: letter + " = " + (lettervalue === "" ? "<?>" : lettervalue) + qsTr(". Click to change")
        onClicked: {
                generic.lettEdit  = letter
                pageStack.push(Qt.resolvedUrl("LetterPage.qml"),
                                          {"lettervalue": lettervalue})
                listModelLett.updateLett();
        }
}


coderus 2020-08-26 23:47

Re: Help me to code for Sailfish OS
 
how do you work with model?

rob_kouw 2020-08-28 09:27

Re: Help me to code for Sailfish OS
 
Thanks for helping, coderus!

The simplified code looks like this.

Code:

import QtQuick 2.0
import Sailfish.Silica 1.0
import "../scripts/Database.js" as Database

Page {
    id: thisWayptPage

    ListModel {
        id: listModelLett

        function updateLett()
        {
            listModelLett.clear();
            var lettrs = Database.getLettersWP(generic.wpId);
            for (var i = 0; i < lettrs.length; ++i) {
                listModelLett.append(lettrs[i]);
            }
        }
    }

    Component.onCompleted: listModelLett.updateLett();

    SilicaFlickable {

        PageHeader { ... }

        Column {

            TextArea { ... }

            Repeater {
                model: listModelLett

                ListItem {

                    Button {
                        text: letter + qsTr(". Click to change")
                        onClicked: {
                            pageStack.push(Qt.resolvedUrl("LetterPage.qml"),
                                          {"letterid": letterid})
                        }
                    }
                }
            }
        }
    }
}

I took the ListModel from the app BudgetBook. The database function just reads the records from the table, in order to fill the ListModel.

On clicking the button, the Dialog in "LetterPage.qml" provides the possibility to enter a value for the letter. Then onDone, the value is stored in the database. After the pop() I don't know how to update the ListModel from the updated database.

This morning I thought of a work-around, but a proper method would be nice.

rob_kouw 2020-08-28 13:03

Re: Help me to code for Sailfish OS
 
Hm. Work-around is not working.


All times are GMT. The time now is 09:50.

vBulletin® Version 3.8.8