Use variable for json answer in QML BlackBerry - qml

I have a project in QML, MomenticsID (BlackBerry10).
I need to use the variable for json answer.
I can not find some manual for this problem.
When I use var result = data.item_01[0] everything is OK and program results in correct value.
But when I use var result = data.variable I won't get an answer.
main.qml
import bb.cascades 1.4
import bb.data 1.0
Page {
id: page
property string variable: "item_01[0]"
Container {
Label {
id: label
}
}
attachedObjects: [
DataSource {
id: dataSource
type: DataSourceType.Json
source: "asset:///data.json"
onDataLoaded: {
var result = data.item_01[0] // it's OK, result is "a"
//var result = data.variable // it's NOK, nothing result
label.text = result.answer_01
}
}
]
onCreationCompleted: {
dataSource.load()
}
}
data.json
{
"item_01": [
{
"answer_01": "a"
}
]
}

Related

Load more functionality using SwiftUI

i have used ScrollView with HStack, now i need to load more data when user reached scrolling at last.
var items: [Landmark]
i have used array of items which i am appeding in HStack using ForEach
ScrollView(showsHorizontalIndicator: false) {
HStack(alignment: .top, spacing: 0) {
ForEach(self.items) { landmark in
CategoryItem(landmark: landmark)
}
}
}
What is the best possible solution to manage load more in SwiftUI without using custom action like loadmore button.
It's better to use ForEach and List for this purpose
struct ContentView : View {
#State var textfieldText: String = "String "
private let chunkSize = 10
#State var range: Range<Int> = 0..<1
var body: some View {
List {
ForEach(range) { number in
Text("\(self.textfieldText) \(number)")
}
Button(action: loadMore) {
Text("Load more")
}
}
}
func loadMore() {
print("Load more...")
self.range = 0..<self.range.upperBound + self.chunkSize
}
}
In this example each time you press load more it increases range of State property. The same you can do for BindableObject.
If you want to do it automatically probably you should read about PullDownButton(I'm not sure if it works for PullUp)
UPD:
As an option you can download new items by using onAppear modifier on the last cell(it is a static button in this example)
var body: some View {
List {
ForEach(range) { number in
Text("\(self.textfieldText) \(number)")
}
Button(action: loadMore) {
Text("")
}
.onAppear {
DispatchQueue.global(qos: .background).asyncAfter(deadline: DispatchTime(uptimeNanoseconds: 10)) {
self.loadMore()
}
}
}
}
Keep in mind, that dispatch is necessary, because without it you will have an error saying "Updating table view while table view is updating). Possible you may using another async way to update the data
If you want to keep using List with Data instead of Range, you could implement the next script:
struct ContentView: View {
#State var items: [Landmark]
var body: some View {
List {
ForEach(self.items) { landmark in
CategoryItem(landmark: landmark)
.onAppear {
checkForMore(landmark)
}
}
}
}
func checkForMore(_ item: LandMark) {
guard let item = item else { return }
let thresholdIndex = items.index(items.endIndex, offsetBy: -5)
if items.firstIndex(where: { $0.id == item.id }) == thresholdIndex {
// function to request more data
getMoreLandMarks()
}
}
}
Probably you should work in a ViewModel and separate the logic from the UI.
Credits to Donny Wals: Complete example

BB Cascade- How to change a variable of an attached object?

so I'm new to the world of BB developement, QML, and C++ so please bear with me as I try to explain my issue.
I'm in the process of making an application that will seach for people from some far away API that returns an XML file that I will then parse and show the information in a list.
So in the first page I have two textfields that take in first name and last name. I also have a button that goes to the results page and is supposed to also send along the URL for contacting the API. Here's the code for the first page:
import bb.cascades 1.0
NavigationPane {
id: nav
Page {
content: Container {
TextField {
id: firstName
hintText: "First Name"
inputMode: TextFieldInputMode.Text
input {
flags: TextInputFlag.PredictionOff | TextInputFlag.AutoCorrectionOff
}
validator: Validator {
id: firstNameValidator
mode: ValidationMode.Immediate
errorMessage: "You must enter at least three characters."
onValidate: {
if (firstName.text.length >= 3 || firstName.text.length == 0)
state = ValidationState.Valid;
else
state = ValidationState.Invalid;
}
}
}
TextField {
id: lastName
hintText: "Last Name"
inputMode: TextAreaInputMode.Text
input {
flags: TextInputFlag.PredictionOff | TextInputFlag.AutoCorrectionOff
onSubmitted: {
if (firstNameValidator.state == ValidationState.Valid
&& lastNameValidator.state == ValidationState.Valid) {
theButton.text = "Valid enter!"
} else {
theButton.text = "Invalid enter!!!!"
}
}
}
validator: Validator {
id: lastNameValidator
mode: ValidationMode.Immediate
errorMessage: "You must enter at least three characters."
onValidate: {
if (lastName.text.length >= 3 || lastName.text.length == 0)
state = ValidationState.Valid;
else
state = ValidationState.Invalid;
}
}
}
Button {
id: theButton
text: "Search"
onClicked: {
if (firstNameValidator.state == ValidationState.Valid
&& lastNameValidator.state == ValidationState.Valid) {
text = "Valid button press!"
pushed.theUrl = link to url
pushed.dataSource.source = link to url
nav.push(pushed)
} else {
text = "Invalid button press!!!"
}
}
}
}
}
attachedObjects: [
AdvancedResult {
id: pushed
}
]
}
And here's the code for the second page:
import bb.cascades 1.0
import bb.data 1.0
Page {
property string theUrl
property alias dataSource: dataSource
content: ListView {
id: myListView
// Associate the list view with the data model that's defined in the
// attachedObjects list
dataModel: dataModel
listItemComponents: [
ListItemComponent {
type: "item"
// Use a standard list item to display the data in the data
// model
StandardListItem {
imageSpaceReserved: false
title: ListItemData.FIRST + " " + ListItemData.LAST
description: ListItemData.JOB
status: ListItemData.PHONE
}
}
]
onTriggered: {
var selectedItem = dataModel.data(indexPath);
// Do something with the item that was tapped
}
}
attachedObjects: [
GroupDataModel {
id: dataModel
grouping: ItemGrouping.None
},
DataSource {
id: dataSource
// Load the XML data from a remote data source, specifying that the
// "item" data items should be loaded
source: theUrl
query: some query
type: DataSourceType.Xml
onDataLoaded: {
// After the data is loaded, clear any existing items in the data
// model and populate it with the new data
dataModel.clear();
if (data[0] == undefined) {
dataModel.insert(data);
} else {
dataModel.insertList(data);
}
}
}
]
onCreationCompleted: {
// When the top-level Page is created, direct the data source to start
// loading data
dataSource.load();
}
}
As you can see in the first page, I try attaching the source for the data source object in two different ways: by pushing it directly to the global string theURL and by pushing it directly to the source variable of the datasource object but both ways don't seem to be working. What am I doing wrong? This is driving me crazy as I haven't been able to figure it out yet it seems like such an easy answer.
EDIT: Okay this is seriously so unintuitive! So I was able to fix it by simply adding theUrl: "link to url" under the "pushed" object in the main page and for the second page on the top adding "property string theUrl: gibberish". But now that I'm trying to make it so that the url is different depending on the user input, it just doesn't work.
So here's the current code I have so far:
import bb.cascades 1.0
NavigationPane {
id: nav
property string firstName: ""
property string lastName: ""
property string jobTitle: ""
property string test: url1
Page {
content: Container {
TextField {
id: firstNameField
hintText: "First Name"
inputMode: TextFieldInputMode.Text
input {
flags: TextInputFlag.PredictionOff | TextInputFlag.AutoCorrectionOff
}
validator: Validator {
id: firstNameValidator
mode: ValidationMode.Immediate
errorMessage: "You must enter at least three characters."
onValidate: {
if (firstNameField.text.length >= 3 || firstNameField.text.length == 0) {
state = ValidationState.Valid;
firstName = firstNameField.text;
} else {
state = ValidationState.Invalid;
}
}
}
}
TextField {
id: lastNameField
hintText: "Last Name"
inputMode: TextAreaInputMode.Text
input {
flags: TextInputFlag.PredictionOff | TextInputFlag.AutoCorrectionOff
onSubmitted: {
if (firstNameValidator.state == ValidationState.Valid
&& lastNameValidator.state == ValidationState.Valid) {
theButton.text = "Valid enter!"
} else {
theButton.text = "Invalid enter!!!!"
}
}
}
validator: Validator {
id: lastNameValidator
mode: ValidationMode.Immediate
errorMessage: "You must enter at least three characters."
onValidate: {
if (lastNameField.text.length >= 3 || lastNameField.text.length == 0) {
state = ValidationState.Valid;
lastName = lastNameField.text;
} else {
state = ValidationState.Invalid;
}
}
}
}
Button {
id: theButton
text: "Search"
onClicked: {
if (firstNameValidator.state == ValidationState.Valid
&& lastNameValidator.state == ValidationState.Valid) {
//text = "Valid button press!"
pushed.theUrl = url2
text = pushed.theUrl;
nav.push(pushed)
} else {
text = "Invalid button press!!!"
}
}
}
}
}
attachedObjects: [
AdvancedResult {
id: pushed
theUrl: test
}
]
}
So I'm currently using two different urls for testing: url1 and url2. url1 is set to test and test is set to theUrl under pushed. That works fine but when i add the line pushed.theUrl = url2; the page still returns the result for url1. So what I did is add the line text = pushed.theUrl; for testing purposes where "text" is the text shown on the button and when I run the app, even though it doesn't return to me a page, "text" is in fact set to url2 meaning that pushed.theUrl is url2. This has all been very unintuitive and not an enjoyable experience at all so far. What I'm trying to do here (get the input from a textfield, add it to a link and send it off to another page) would have taken me an hour at most on Android to code.

Invocation from function in Blackberrry 10 qml?

I have invoked invite to BBM while clicking a button in qml,but i need to send the invitation for the contacts how to do this?my code
Button {
text: "Invite"
onClicked: {
invokeQuery.uri = "pin:210000A"
invokeQuery.updateQuery();
}
attachedObjects: [
Invocation {
id: invokeShare
query: InvokeQuery {
id: invokeQuery
}
onArmed: {
trigger("bb.action.INVITEBBM");
}
}
]
}
Can anyone send me some solutions to solve this.?
Thanks
Invocation query is immutable object which means that values in the query are not dynamic. If you want to update query in dynamic you need to do it via control signals or variables.
For example, you have a component called Inviter with the pin property exposed:
import bb.cascades 1.0
Container {
property string pin
Button {
text: "Invite to BBM"
onClicked: {
query.uri = "pin:" + pin
invoke.trigger("bb.action.INVITEBBM")
}
}
attachedObjects: [
Invocation {
id: invoke
query: InvokeQuery {
id: query
invokeTargetId: "sys.bbm.sharehandler"
onQueryChanged: {
invoke.query.updateQuery()
}
}
}
]
}
Then you can use it this way:
import bb.cascades 1.0
Page {
Container {
layout: DockLayout {
}
TextArea {
id: pinEditor
hintText: "Enter PIN to invite"
onTextChanged: {
inviter.pin = text
}
input.submitKey: SubmitKey.Send
}
Inviter {
id: inviter
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Center
}
}
}
Also, don't forget to enable "Blackberry Messenger" permission in your bar-descriptor.xml and add following libraries to your .pro file:
LIBS += -lbbplatformbbm
LIBS += -lbbsystem

How to access the outer components from listitemcomponents in listview bb10 qml?

I am not able to access the Datasource id from inside of the ListItemComponent. Can anyone help me in regards to this?
ListItemComponent {
type: "item"
Container {
id: listviewcontainer
Container {
preferredWidth: 768
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
CustomImageView {
leftPadding: 10
rightPadding: 10
url: ListItemData.from_image
horizontalAlignment: HorizontalAlignment.Left
verticalAlignment: VerticalAlignment.Center
}
Container {
preferredWidth: 538
layout: StackLayout {
orientation: LayoutOrientation.TopToBottom
}
Container {
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
Label {
text: ListItemData.from
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: Color.create("#2db6ff")
}
}
ImageView {
imageSource: "asset:///Home/img.png"
verticalAlignment: VerticalAlignment.Center
}
}//Container
Label {
text: ListItemData.message
multiline: true
textStyle {
base: SystemDefaults.TextStyles.SubtitleText
}
content {
flags: TextContentFlag.Emoticons
}
}
Label {
id: time
text: ListItemData.time
textStyle {
base: SystemDefaults.TextStyles.SmallText
color: Color.create("#666666")
}
}
}//Container
ImageButton {
id: delete_btn
defaultImageSource: "asset:///Icon/delete.png"
pressedImageSource: "asset:///Icon/delete.png"
verticalAlignment: VerticalAlignment.Center
horizontalAlignment: HorizontalAlignment.Right
onClicked: {
deleteMessage(ListItemData.tid, ListItemData.uid);
}
function deleteMessage(tid, uid) {
var request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState == 4) {
var mResponse = request.responseText
mResponse = JSON.parse(mResponse)
var mResponseStatus = mResponse.response[0].receive.status;
var mMsg = mResponse.response[0].receive.message;
if (mResponseStatus == 1) {
msg_DataSource.source = "newurl.com" // This line not works here..
msg_DataSource.load(); // This line not works here..
} else if (mResponseStatus == 0) {
}
}
}// end function
request.open("GET", "myurl.com", true);
request.send();
}// deleteMessage
}//ImageButton
}//Container
}//Container
}//ListItemComponent
Here am not able to work out the following two lines
msg_DataSource.source = "newurl.com"
msg_DataSource.load();
I have tried like below but this also not working
listviewcontainer.ListItem.view.dataModel.message_DataSource.source = "myurl.com";
listviewcontainer.ListItem.view.dataModel.message_DataSource.load();
or this
listviewcontainer.ListItem.view.dataModel.source = "myurl.com";
listviewcontainer.ListItem.view.dataModel.load();
Another simplest way to store the object to global variable using the following code which works fine with me.
onCreationCompleted: {
Qt.tabbedPane = tabbedPane;
Qt.homeTab = homeTab;
}
Here I stored tabbedPane in global variable Qt.tabbedPane on page creation Completed.Now I able to access it from ListItemComponent using Qt.tabbedPane.
Hope it helps.
The simplest way to make the data model accessible would be to declare a property alias to your data model wherever it is defined, for example in your ListView QML file. This would make your data model accessible to the top level component in QML from this property alias. In effect, it gives you a global reference to your data model from anywhere else in QML.
For example, if you data model is called msg_DataSource, then in the QML file where it is defined you can create the property alias like this:
property alias myDataModel: msg_DataSource
Then in your ListItemComponent deleteMessage function, you can use myDataModel like this:
myDataModel.source = "newurl.com"
myDataModel.load();
Note: I am sure you could also do this in a more elegant way using signals and slots, but this way should be quicker and easier to understand.

Invocation from function in QML?

I can share an item easily using an InvokeActionItem in a Page but I need to be able to call it in a listview item. I've managed to trigger an invoke, but I cannot figure out how to add data when triggering it. I keep getting an error message of
InvocationPrivate::setQuery: you are not allowed to change InvokeQuery object
Note: I am trying to do this in purely QML, I will do it via c++ if necessary but QML would be preferable.
Code that works inside a Page object:
actions: [
InvokeActionItem {
ActionBar.placement: ActionBarPlacement.OnBar
title: "Share"
query {
mimeType: "text/plain"
invokeActionId: "bb.action.SHARE"
}
onTriggered: {
//myTextProperty is a string variable property for the page.
data = myTextProperty;
}
}
]
The code I've tried to use in the other item is as follows, but does NOT work:
Container {
gestureHandlers: [
TapHandler {
LongPressHandler {
onLongPressed: {
console.log("Longpress");
invokeQuery.setData("test");
invokeShare.trigger("bb.action.SHARE");
}
}
]
attachedObjects: [
Invocation {
id: invokeShare
query: InvokeQuery {
id:invokeQuery
mimeType: "text/plain"
}
}
]
}
Is there a way to change the data for an invoke purely with QML or do I need to just run it through c++ instead?
After a fair amount of browsing forums and testing various methods, I have finally found one that works.
Add the following in your attachedObjects:
attachedObjects: [
Invocation {
id: invokeShare
query: InvokeQuery {
id:invokeQuery
mimeType: "text/plain"
}
onArmed: {
if (invokeQuery.data != "") {
trigger("bb.action.SHARE");
}
}
}
]
Then wherever you need to call the invocation do the following:
invokeQuery.mimeType = "text/plain"
invokeQuery.data = "mytext";
invokeQuery.updateQuery();
Note that if you do not do a check in the onArmed for data it will automatically call the invocation on creation - in the case of a listview this can result in 20+ screens asking you to share on bbm... ;)
You can actually use the InvokeActionItem, you just have to call updateQuery to retrigger the invokeQuery.
When the ListItemData changes, the binding will cause the values to update.
InvokeActionItem {
enabled: recordItem.ListItem.data.videoId != undefined
id: invokeAction
query{
uri: "http://www.youtube.com/watch?v=" + recordItem.ListItem.data.videoId
onQueryChanged: {
updateQuery()
}
}
}
For remove "InvocationPrivate::setQuery: you are not allowed to change InvokeQuery object" message I use this:
attachedObjects: [
Invocation {
id: invoke
query {
mimeType: "text/plain"
invokeTargetId: "sys.bbm.sharehandler"
onDataChanged: {
console.log("change data")
}
}
onArmed: {
if (invoke.query.data != "") {
invoke.trigger("bb.action.SHARE");
}
}
}
]
function shareBBM(){
invoke.query.setData("TEXT TO SHARE");
invoke.query.updateQuery();
}