This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathFromFieldView.swift
51 lines (43 loc) · 1.76 KB
/
FromFieldView.swift
1
2
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
//
// FromFieldView.swift
// reddift
//
// Created by sonson on 2016/12/12.
// Copyright © 2016年 sonson. All rights reserved.
//
import Foundation
protocol FromFieldViewDelegate {
func didTapFromFieldView(sender: FromFieldView)
}
class FromFieldView: PostFieldView {
var delegate: FromFieldViewDelegate?
let button = UIButton(type: .custom)
override func setupSubviews() {
super.setupSubviews()
field.isEnabled = false
let views = ["button": button]
button.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(button)
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[button]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[button]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views))
button.addTarget(self, action: #selector(FromFieldView.didTapButton(sender:)), for: .touchUpInside)
if let name = UIApplication.appDelegate()?.session?.token?.name {
field.text = name
} else {
field.text = "anonymous"
}
NotificationCenter.default.addObserver(self, selector: #selector(FromFieldView.didUpdateToken(notification:)), name: OAuth2TokenRepositoryDidUpdateTokenName, object: nil)
}
@objc func didUpdateToken(notification: NSNotification) {
if let name = UIApplication.appDelegate()?.session?.token?.name {
field.text = name
} else {
field.text = "anonymous"
}
}
@objc func didTapButton(sender: Any) {
if let delegate = delegate {
delegate.didTapFromFieldView(sender: self)
}
}
}