aboutsummaryrefslogblamecommitdiffstats
path: root/WireGuard/WireGuard/UI/iOS/View/SwitchCell.swift
blob: 68939a7a78096fe73b87ceea1f5c3f5b16501fe3 (plain) (tree)
1
2
3
4
5
6
7
8
9




                                                        
                                   

                                            
                                              








                                              
                                                         

         
 
                                          
 
                               
 
                                                                               
                                                                     
 


                                                                                        



                                                           
 


                                         
 

                                     
                        



                    
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.

import UIKit

class SwitchCell: UITableViewCell {
    var message: String {
        get { return textLabel?.text ?? "" }
        set(value) { textLabel?.text = value }
    }
    var isOn: Bool {
        get { return switchView.isOn }
        set(value) { switchView.isOn = value }
    }
    var isEnabled: Bool {
        get { return switchView.isEnabled }
        set(value) {
            switchView.isEnabled = value
            textLabel?.textColor = value ? .black : .gray
        }
    }

    var onSwitchToggled: ((Bool) -> Void)?

    let switchView = UISwitch()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: reuseIdentifier)

        accessoryView = switchView
        switchView.addTarget(self, action: #selector(switchToggled), for: .valueChanged)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func switchToggled() {
        onSwitchToggled?(switchView.isOn)
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        isEnabled = true
        message = ""
        isOn = false
    }
}