|
@@ -9,10 +9,18 @@
|
|
|
import UIKit
|
|
|
|
|
|
class PushNotificationSettingsTableViewCell: UITableViewCell {
|
|
|
+
|
|
|
+ deinit {
|
|
|
+ if observe != nil {
|
|
|
+ NotificationCenter.default.removeObserver(observe!)
|
|
|
+ }
|
|
|
+ NXLLog("deinit")
|
|
|
+ }
|
|
|
+ weak var observe : NSObjectProtocol?
|
|
|
|
|
|
- class func cellWith(tableView:UITableView,indexPath:IndexPath) -> PushNotificationSettingsTableViewCell {
|
|
|
+ class func cellWith(tableView:UITableView,indexPath:IndexPath) -> PushNotificationSettingsTableViewCell {
|
|
|
let ID = "PushNotificationSettingsTableViewCell"
|
|
|
- tableView.register(SetTableViewCell.self, forCellReuseIdentifier: ID)
|
|
|
+ tableView.register(PushNotificationSettingsTableViewCell.self, forCellReuseIdentifier: ID)
|
|
|
let cell : PushNotificationSettingsTableViewCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! PushNotificationSettingsTableViewCell
|
|
|
cell.indexPath = indexPath
|
|
|
return cell
|
|
@@ -22,6 +30,7 @@ class PushNotificationSettingsTableViewCell: UITableViewCell {
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
setupViews()
|
|
|
setupLayouts()
|
|
|
+ setupData()
|
|
|
}
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
@@ -37,6 +46,7 @@ class PushNotificationSettingsTableViewCell: UITableViewCell {
|
|
|
private func setupViews() {
|
|
|
self.selectionStyle = .none
|
|
|
addSubview(titleLabel)
|
|
|
+ addSubview(onSwitch)
|
|
|
addSubview(lineLabel)
|
|
|
}
|
|
|
|
|
@@ -45,6 +55,10 @@ class PushNotificationSettingsTableViewCell: UITableViewCell {
|
|
|
make.centerY.equalToSuperview()
|
|
|
make.left.equalTo(14)
|
|
|
}
|
|
|
+ onSwitch.snp.makeConstraints { (make) in
|
|
|
+ make.centerY.equalToSuperview()
|
|
|
+ make.right.equalTo(-14)
|
|
|
+ }
|
|
|
lineLabel.snp.makeConstraints { (make) in
|
|
|
make.bottom.right.equalToSuperview()
|
|
|
make.left.equalTo(14)
|
|
@@ -52,6 +66,17 @@ class PushNotificationSettingsTableViewCell: UITableViewCell {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ func setupData() {
|
|
|
+ onSwitch.addTarget(self, action: #selector(onSwitchAction(onSwitch:)), for: UIControl.Event.touchUpInside)
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func onSwitchAction(onSwitch:UISwitch) {
|
|
|
+ self.onSwitch.isOn = !onSwitch.isOn
|
|
|
+ if indexPath?.section == 0 {
|
|
|
+ NXLPermission.openSettingsURL()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private lazy var titleLabel: UILabel = {
|
|
|
let titleLabel = UILabel()
|
|
|
titleLabel.textColor = k333333Color
|
|
@@ -66,9 +91,38 @@ class PushNotificationSettingsTableViewCell: UITableViewCell {
|
|
|
return lineLabel
|
|
|
}()
|
|
|
|
|
|
+ lazy var onSwitch: UISwitch = {
|
|
|
+ let onSwitch = UISwitch()
|
|
|
+ onSwitch.isOn = true
|
|
|
+ onSwitch.backgroundColor = kf1f1f1Color
|
|
|
+ onSwitch.onTintColor = kThemeColor
|
|
|
+ onSwitch.tintColor = kf1f1f1Color
|
|
|
+ onSwitch.thumbTintColor = .white
|
|
|
+ onSwitch.cornerRadius = 15.5
|
|
|
+ onSwitch.masksToBounds = true
|
|
|
+ return onSwitch
|
|
|
+ }()
|
|
|
+
|
|
|
var title : String? {
|
|
|
didSet {
|
|
|
titleLabel.text = title
|
|
|
+ if indexPath?.section == 0 {
|
|
|
+ isAllowedNotification()
|
|
|
+ observe = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main, using: {
|
|
|
+ [weak self] (notification) in
|
|
|
+ self?.isAllowedNotification()
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 推送通知的调试
|
|
|
+ func isAllowedNotification() {
|
|
|
+ if NXLPermission.isAllowed(.notification) {
|
|
|
+ onSwitch.setOn(true, animated: true)
|
|
|
+ }else {
|
|
|
+ onSwitch.setOn(false, animated: true)
|
|
|
}
|
|
|
}
|
|
|
}
|