0

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!

NG_
  • 6,895
  • 7
  • 45
  • 67
Ldweller
  • 327
  • 2
  • 16
  • What is `AppSwitch`? – folibis Jun 29 '19 at 15:44
  • Hi @folibis I have updated my question with a link to Felgo's `AppSwitch` qml type, it's a toggle on/off switch used for simple bool functions most commonly used! – Ldweller Jun 29 '19 at 16:04
  • The property @checked@ should be true or false, I suppose. Why are you executing that code on that property? What that code returns? – perencia Jun 29 '19 at 20:38

1 Answers1

1

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
        }
    }
}
augre
  • 2,011
  • 12
  • 18