VLCFileServerSectionTableHeaderView.swift 2.35 KB
Newer Older
1
/*****************************************************************************
2
 * VLCFileServerSectionTableHeaderView.swift
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2018 VideoLAN. All rights reserved.
 * $Id$
 *
 * Author: Carola Nitz <caro # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

import Foundation

@objc protocol VLCFileServerSectionTableHeaderViewDelegate: NSObjectProtocol {

    func connectToServer()
}

class VLCFileServerSectionTableHeaderView: VLCSectionTableHeaderView {

    @objc static let identifier = "VLCFileServerSectionTableHeaderView"
    @objc weak var delegate: VLCFileServerSectionTableHeaderViewDelegate?
    var layoutConstraints: [NSLayoutConstraint]?
    lazy var connectButton: UIButton = {
        let connectButton = UIButton(type: .system)
        connectButton.setTitle(NSLocalizedString("BUTTON_CONNECT", comment: ""), for: .normal)
        connectButton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        connectButton.titleLabel?.textColor = PresentationTheme.current.colors.orangeUI
        connectButton.translatesAutoresizingMaskIntoConstraints = false
        connectButton.addTarget(self, action: #selector(connectButtonDidPress), for: .touchUpInside)
        contentView.addSubview(connectButton)
        return connectButton
    }()

    override func setupUI() {
        super.setupUI()
        textLabel?.text = NSLocalizedString("FILE_SERVER", comment: "")
    }

    //Before layoutSubviews textlabel doesn't have a superview
    override func layoutSubviews() {
        super.layoutSubviews()
        if layoutConstraints == nil {
            layoutConstraints = [
                connectButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
                connectButton.firstBaselineAnchor.constraint(equalTo: textLabel!.firstBaselineAnchor)
                ]
            NSLayoutConstraint.activate(layoutConstraints!)
        }
    }
    @objc func connectButtonDidPress() {
        delegate?.connectToServer()
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        //Text gets set to nil in prepareForReuse so we set it again
        textLabel?.text = NSLocalizedString("FILE_SERVER", comment: "")
    }
}