Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 385
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
My Carrier: O2 UK

Creating "Favourites" & "History" databases

Hi, 

 

I have three tabs in my application: "Home", "Favourite" & "History". 

 

The "Home" tab uses a listView with context actions to add to the "Favourite" tab and the "History" tab should log each time an item is clicked is on the "Home" tab and create its own listView with the date as a header, the item's title as the item and the time as the item's status - it's a very similar structure to what is being used in Soluto's Wikipedia app for BlackBerry 10. 

 

The only problem is, I do not know where to begin on how to create a database to store the favourites and log the items in the history tab. 

 

Here's the code I'm currently using for the "Home" tab:

 

ListView {
                            id: listView1
                            dataModel: dataModel

                            leadingVisual: [
                                Container {
                                    id: dropDownContainer
                                    topPadding: 20
                                    leftPadding: 20
                                    rightPadding: 20
                                    bottomPadding: 20
                                    background: Color.create("#212121")
                                    DropDown {
                                        id: dropDown
                                        title: qsTr("Continent:")
                                        Option {
                                            text: qsTr("All")
                                            id: all
                                            value: "All"
                                            selected: true
                                        }
                                        Option {
                                            text: qsTr("Africa")
                                            value: "Africa"
                                        }
                                        Option {
                                            text: qsTr("Antarctica")
                                            value: "Antarctica"
                                        }
                                        Option {
                                            text: qsTr("Asia")
                                            value: "Asia"
                                        }
                                        Option {
                                            text: qsTr("Australia")
                                            value: "Australia"
                                        }
                                        Option {
                                            text: qsTr("Europe")
                                            value: "Europe"
                                        }
                                        Option {
                                            text: qsTr("North America")
                                            value: "North America"
                                        }
                                        Option {
                                            text: qsTr("South America")
                                            value: "South America"
                                        }
                                        onSelectedIndexChanged: {
                                            if (selectedOption == all) {
                                                dropDownDataSource.sQuery = ""
                                            } else
                                                dropDownDataSource.sQuery = dropDown.at(dropDown.selectedIndex).value;
                                        }
                                    }
                                }
                            ]

                            contextActions: [
                                ActionSet {
                                    id: itemActionSet
                                    subtitle: qsTr("Actions")
                                    actions: [
                                        ActionItem {
                                            title: qsTr("Add To Saved")
                                            imageSource: "asset:///IMG/ic_add_favorite.png"

                                            onTriggered: {
                                                //Add to saved tab which can then be accessed by the user
                                            }
                                        }
                                    ]
                                }
                            ]
                            listItemComponents: [
                                ListItemComponent {
                                    type: "item"
                                    StandardListItem {
                                        title: ListItemData.country
                                    }
                                }
                            ]

                            onTriggered: {
                                var selectedItem = dataModel.data(indexPath);
                                var detail = country.createObject();

                                ... // Custom QML Parameters

                                navigationPane1.push(detail)
                            }
                        }

 Whilst the other two tabs currently remain empty.

 

Therefore, if anyone could help me by showing me how to create these functions then that would be great! 

 

Thanks in advance Smiley Happy 

Developer
Posts: 358
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: Creating "Favourites" & "History" databases

where are you getting your app's data from? An existing XML, JSON or SQL or from an online resource?
Highlighted
Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: Creating "Favourites" & "History" databases

take a look at the "Quotes" tutorial, it shows simple usage of databases
https://developer.blackberry.com/native/documentation/cascades/ui/lists/lists_set_up_project.html
-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Developer
Posts: 385
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
My Carrier: O2 UK

Re: Creating "Favourites" & "History" databases

I'm using a local JSON file currently on a GroupDataModel & DataSource,

 

Main.qml:

 

attachedObjects: [
                // Definition of the second Page, used to dynamically create the Page above.
                GroupDataModel {
                    id: dataModel1
                    sortingKeys: [ "continent" ]
                    grouping: ItemGrouping.ByFullValue
                },
                DataSource {
                    id: dataSource1
                    property string sQuery: ""
                    onSQueryChanged: {
                        dataModel1.clear()
                        load()
                    }
                    source: "asset:///JSON/Countries.json"
                    type: DataSourceType.Json

                    onDataLoaded: {
                        //create a temporary array tohold the data
                        var tempdata = new Array();
                        for (var i = 0; i < data.length; i ++) {

                            tempdata[i] = data[i]

                            //this is where we handle the search query
                            if (sQuery == "") {
                                //if no query is made, we load all the data
                                dataModel1.insert(tempdata[i])
                            } else {
                                //if the query matches any part of the country TITLE, we insert that into the list
                                //we use a regExp to compare the search query to the COUNTRY TITLE (case insenstive)
                                if (data[i].country.search(new RegExp(sQuery, "i")) != -1) {
                                    dataModel1.insert(tempdata[i])

                                    //Otherwise, we do nothingand donot insert the item
                                }

                            }

                        }

                        // this if statement below does the same as above,but handles the output if there is only one search result
                        if (tempdata[0] == undefined) {
                            tempdata = data

                            if (sQuery == "") {
                                dataModel1.insert(tempdata)
                            } else {
                                if (data.country.search(new RegExp(sQuery, "i")) != -1) {
                                    dataModel1.insert(tempdata)
                                }
                            }
                        }
                    }
                    onError: {
                        console.log(errorMessage)
                    }
                },

                ComponentDefinition {
                    id: country
                    source: "CountryDetail.qml"
                },
                ComponentDefinition {
                    id: selector
                    source: "CategorySelector.qml"
                },
                ComponentDefinition {
                    id: ca
                    source: "asset:///Countries/Canada.qml"
                }
            ]

 

Countries.JSON:

 

[
  {
    "type":"item",
    "id":"1",
    "continent":"Africa",
    "country":"Nigeria ",
    //Custom parameters from QML
  },
  
  
  
  {
    "type":"item",
    "id":"1",
    "continent":"Antarctica",
    "country":"Country 2 ",
    //Custom parameters from QML
  },
  
  
  
  {
    "type":"item",
    "id":"1",
    "continent":"Asia",
    "country":"China ",
    //Custom parameters from QML
  },
  
  
  
  {
    "type":"item",
    "id":"1",
    "continent":"Australia",
    "country":"Country ",
    //Custom parameters from QML
  },
  
  
  
  {
    "type":"item",
    "id":"1",
    "continent":"Europe",
    "country":"United Kingdom ",
    //Custom parameters from QML
  },
  
  
  
  {
    "type":"item",
    "id":"1",
    "continent":"North America",
    "country":"Canada ",
    //Custom parameters from QML
  },
  
  
  
  {
    "type":"item",
    "id":"1",
    "continent":"South America",
    "country":"Brazil ",
    //Custom parameters from QML
  }
]

 

Developer
Posts: 358
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: Creating "Favourites" & "History" databases

Add two more columns or children to your datamodel. One for favourites and another for history. Have the favourite fill with a value anytime someone chooses it and then create a listview that only shows records with favourite value.

For history have a history column get populated with an increasing number everytime someone opens a record. So it would be 1, 2, 3 etc you'd need a qsetting to help with this for counting upwards. You'd then fill a listview with records that have a number and order them in descending order.
Developer
Posts: 385
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
My Carrier: O2 UK

Re: Creating "Favourites" & "History" databases

This is exactly what I'm trying to do Smiley Very Happy

 

Would you be able to provide me with some code as a strarting point for this?

 

Thanks 

Developer
Posts: 358
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: Creating "Favourites" & "History" databases

I personally use SQL and have never used JSON but the Bucket list sample app does exactly this with the exception of the history
Developer
Posts: 385
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
My Carrier: O2 UK

Re: Creating "Favourites" & "History" databases

Developer
Posts: 358
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: Creating "Favourites" & "History" databases

Yes, that one.
Developer
Posts: 385
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
My Carrier: O2 UK

Re: Creating "Favourites" & "History" databases

Okay, so I figured that I would need this code:

 

ActionItem {

                title: qsTr("Finished") + Retranslate.onLanguageChanged

                enabled: (bucketList.hasSelection &&  bucketModel.filter != "finished")
                imageSource: "asset:///images/finished.png"

                onTriggered: {
                    if (enabled) {
                        // Change the status of the selected items to "finished". Clear selection before items are manipulated to avoid blink.
                        var selectionList = bucketList.selectionList();
                        bucketList.clearSelection();
                        bucketModel.setStatus(selectionList, "finished");
                        updateBBMStatus("Finished some items to my bucket list", "images/finished.png");
                    }
                }
            }

 & I have altered it to match my code:

 

ActionItem {
                                            title: qsTr("Add To Saved")
                                            imageSource: "asset:///IMG/ic_add_favorite.png"

                                            onTriggered: {
                                                //Add to saved tab which can then be accessed by the user
                                                var selectionList = listView1.selectionList();
                                                listView1.clearSelection();
                                                dataModel1.setStatus(selectionList, "finished");
                                            }
                                        }

 The only issue is that I don't know what setStatus is - everything else is recognized