Search code examples
qmlsignalstogglev-playfelgo

QML AppSwitch sending onCheckedChanged signal each time page is opened?


I have an AppSwitch, which is being used to 'activate/deactivate' user accounts in my app, It's checked status is read from a database, which works as intended, changing both the switch and text depending on the bool per user.

My problem is
Each time I open the page with the AppSwitch on, it sends the onCheckedChanged signal - in turn sending a notification to the user their account has been activated? I had planned to notify users that their account was active with a push notification, but don't want this sending every time the admin moves to their page!

I am using Felgo(formerly V-Play) for development, a link to the AppSwitch documentation is:
Felgo AppSwitch

My code is:

            Rectangle {               
                width: parent.width / 2
                height: parent.height
                Column {
                    id: column
                    anchors.fill: parent
                    AppButton {
                        id: groupStatusText
                        width: parent.width
                        height: parent.height / 2
                    }
                    AppSwitch {
                        id: editStatus
                        knobColorOn: "#b0ced9"
                        backgroundColorOn: "#81b1c2"
                        checked: {
//userListItem is the current profile details of the user page
                            if(userListItem.groupStatus === 1) {
                                groupStatusText.text = "Deactivate this user"
                            }
                            else if(userListItem.groupStatus === 0) {
                                groupStatusText.text = "Activate this user"
                            }
                        }
                        onCheckedChanged: {
                            if(editStatus.checked === true) {
//the signal which sends to my database on updating the value,
//works as intended but sends signal each time page is created?
                                detailPage.adminEditGroupStatus(1)
                            } else {
                                detailPage.adminEditGroupStatus(0)
                            }
                        }
                    }
                }

Is it normal behaviour for the onCheckedChanged signal to be firing on each instance the page is loaded? or how could I amend this that it only fires when the user changes it!?

Thanks for any help!


Solution

  • Add a condition to be sure the signal is sent after full init of the component (including after creation of the binding on the checked property).

    You can add a boolean property on the switch :

    AppSwitch {
        property bool loaded: false    
    
        Component.onCompleted: loaded = true
    
        onCheckedChanged: {
            if (loaded) {
                // notify user
            }
        }
    }
    

    or simply use the state:

    AppSwitch {    
        Component.onCompleted: state = "loaded"
    
        onCheckedChanged: {
            if (state === "loaded") {
                // notify user
            }
        }
    }