     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
         textField.resignFirstResponder()
         if let newName = stageNameField.text {
+            if let profile = userProfile, let record = userRecord {
+                profile.stageName = newName
+                updateUserBasicProfile(record, with: profile)
+            }
             UserDefaults.standard.set(newName, forKey: SettingsKeys.performerKey)
             print("USVC: Set stage name to: ", newName)
         }
         return true
     }
-    
 }
 

                    do {
                        try FileManager.default.removeItem(at: avatarURL)
                    } catch {
                        print("Error deleting temporary avatar file: \(error)")
                    }
                }
            }
            
            guard error == nil else {
                // top-notch error handling
                DispatchQueue.main.async {
                    self.avatarImageView.image = fallbackImage
                }
                return
            }
            
            print("Successfully updated user record with new avatar")
        }
    }
    
}

/// Stage Name Chooser extension

    
    /// Triggered when user changes the jam colour slider
    @IBAction func jamSliderMoved(_ sender: UISlider) {
        let colour = PerformerProfile.colourFromHue(hue: sender.value)
        sender.tintColor = colour
        sender.thumbTintColor = colour
        if let profile = userProfile {
            profile.jamColour = colour
        }
    }
    
    /// Triggered when user changes the background colour
    @IBAction func backgroundSliderMoved(_ sender: UISlider) {
        let colour = PerformerProfile.colourFromHue(hue: sender.value)
        sender.tintColor = colour
        sender.thumbTintColor = colour
        if let profile = userProfile {
            profile.backgroundColour = colour
        }
    }
    
    /// Update the User's stageName in CloudKit
    private func updateUserStageName(_ userRecord: CKRecord, with stageName: String) {
        userRecord[UserCloudKeys.stagename] = stageName as CKRecordValue
        container.publicCloudDatabase.save(userRecord) { _, error in
            if (error != nil) {
                print("USVC: Error saving to cloudkit")
                print(error ?? "")
            } else {
                print("USVC: updated user in cloudkit", stageName)
            }
        }
    }
    
    /// Update basic profile info in CloudKit (stagename, colours, and soundscheme)
    /// Does not update avatar image.
    internal func updateUserBasicProfile(_ userRecord: CKRecord, with profile: PerformerProfile) {
        userRecord[UserCloudKeys.stagename] = profile.stageName as CKRecordValue
        userRecord[UserCloudKeys.jamColour] = profile.jamColour.hexString() as CKRecordValue
        userRecord[UserCloudKeys.backgroundColour] = profile.backgroundColour.hexString() as CKRecordValue
        userRecord[UserCloudKeys.soundScheme] = profile.soundScheme as CKRecordValue
        
        container.publicCloudDatabase.save(userRecord) { _, error in
            if (error != nil) {
                print("USVC: Error saving to cloudkit")
                print(error ?? "")
            } else {
                print("USVC: updated user profile in cloudkit.")
            }
        }
        
        // update user defaults
        // FIXME: use of userdefaults should be deprecated.
        UserDefaults.standard.set(profile.stageName, forKey: SettingsKeys.performerKey)
        UserDefaults.standard.set(PerformerProfile.hueFrom(colour: profile.jamColour), forKey: SettingsKeys.performerColourKey)
        UserDefaults.standard.set(PerformerProfile.hueFrom(colour: profile.backgroundColour), forKey: SettingsKeys.backgroundColourKey)
        UserDefaults.standard.set(profile.soundScheme, forKey: SettingsKeys.soundSchemeKey)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        print("USVC: view will disappear")
        if let profile = userProfile, let record = userRecord {
            updateUserBasicProfile(record, with: profile)
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // When segue occurs, request to upload basic profile info to CloudKit
        print("USVC: preparing to segue")
    }
}

