SBTLanguageVCtr.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // SBTLanguageVCtr.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/4/22.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import Localize_Swift
  10. class SBTlangueCell: UITableViewCell {
  11. var isCheck:Bool? = true{
  12. didSet{
  13. checkImageView.isHidden = !self.isCheck!
  14. }
  15. }
  16. lazy var checkImageView: UIImageView = {
  17. let checkImageView = UIImageView(image: UIImage(named: "勾选"))
  18. return checkImageView
  19. }()
  20. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  21. super.init(style: style, reuseIdentifier: reuseIdentifier)
  22. addSubview(checkImageView)
  23. checkImageView.snp.makeConstraints { (make) in
  24. make.centerY.equalToSuperview()
  25. make.right.equalToSuperview().offset(-16)
  26. make.width.height.equalTo(16)
  27. }
  28. }
  29. required init?(coder aDecoder: NSCoder) {
  30. fatalError("init(coder:) has not been implemented")
  31. }
  32. }
  33. let NOTIFICTIONRELOADLANGUAGE = "NOTIFICTIONRELOADLANGUAGE"
  34. class SBTLanguageVCtr: UIViewController {
  35. let selectedDefaultKey = "selectedDefaultKey"
  36. var selectedIndexPath : IndexPath? = IndexPath(row: 0, section: 0)
  37. var dataSource = ["Follow_system".da_localizedStr(),"Simplified_Chinese".da_localizedStr(),"English"]
  38. lazy var tableView: UITableView = {
  39. let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight), style: .plain)
  40. tableView.delegate = self
  41. tableView.dataSource = self
  42. tableView.register(SBTlangueCell.self, forCellReuseIdentifier: "cell")
  43. return tableView
  44. }()
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. checkTheBlueToothWasActivity()
  48. title = "Client_language".da_localizedStr()
  49. LocalizationManager.shareInstance().callback = {
  50. self.title = "Client_language".da_localizedStr()
  51. }
  52. view.addSubview(tableView)
  53. let defaults = UserDefaults.standard
  54. let row = defaults.integer(forKey: selectedDefaultKey)
  55. log.debug("userdefaults - \(row)")
  56. if row == 0 {
  57. selectedIndexPath = IndexPath(row: 0, section: 0)
  58. }else if row == 1 {
  59. selectedIndexPath = IndexPath(row: 1, section: 0)
  60. }else if row == 2 {
  61. selectedIndexPath = IndexPath(row: 2, section: 0)
  62. }else {
  63. selectedIndexPath = IndexPath(row: 0, section: 0)
  64. }
  65. NotificationCenter.default.addObserver(self, selector: #selector(reloadLanguage), name: NSNotification.Name(NOTIFICTIONRELOADLANGUAGE), object: nil)
  66. }
  67. @objc func reloadLanguage(){
  68. dataSource = ["Follow_system".da_localizedStr(),"Simplified_Chinese".da_localizedStr(),"English"]
  69. title = "Client_language".da_localizedStr()
  70. self.tableView.reloadData()
  71. }
  72. }
  73. extension SBTLanguageVCtr:UITableViewDelegate,UITableViewDataSource{
  74. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  75. return dataSource.count
  76. }
  77. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  78. var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? SBTlangueCell
  79. if cell == nil {
  80. cell = SBTlangueCell(style: .default, reuseIdentifier: "cell")
  81. }
  82. cell?.textLabel?.text = dataSource[indexPath.row].da_localizedStr()
  83. LocalizationManager.shareInstance().callback = {
  84. cell?.textLabel?.text = self.dataSource[indexPath.row].da_localizedStr()
  85. }
  86. if selectedIndexPath == indexPath {
  87. cell?.isCheck = true
  88. }else{
  89. cell?.isCheck = false
  90. }
  91. return cell!
  92. }
  93. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  94. // tableView.deselectRow(at: indexPath, animated: true)
  95. //取消之前的选择
  96. let celled = tableView.cellForRow(at: selectedIndexPath!) as! SBTlangueCell
  97. celled.isCheck = false
  98. //记录当前选择位置
  99. selectedIndexPath = indexPath
  100. let defaults = UserDefaults.standard
  101. defaults.set(indexPath.row, forKey: selectedDefaultKey)
  102. //选择当前的打勾
  103. let cell = tableView.cellForRow(at: indexPath) as! SBTlangueCell
  104. cell.isCheck = true
  105. let userDef = UserDefaults.standard
  106. if indexPath.row == 0 {
  107. DAConfig.userLanguage = nil
  108. userDef.set("0", forKey: "SOLARLANGUAGE")
  109. }else if indexPath.row == 1 {
  110. userDef.set("1", forKey: "SOLARLANGUAGE")
  111. DAConfig.userLanguage = "zh-Hans"
  112. }else if indexPath.row == 2 {
  113. userDef.set("2", forKey: "SOLARLANGUAGE")
  114. DAConfig.userLanguage = "en"
  115. }
  116. userDef.synchronize()
  117. //通知改变文字
  118. NotificationCenter.default.post(name: NSNotification.Name("updateEmptyViewLanguage"), object: nil)
  119. NotificationCenter.default.post(name: NSNotification.Name(NOTIFICTIONRELOADLANGUAGE), object: nil)
  120. NotificationCenter.default.post(name: NSNotification.Name(NOTIFICTIONCHANGEFEEDBACKURL), object: nil)
  121. LocalizationManager.shareInstance().notificationToChangeLanguage()
  122. g_showloading()
  123. }
  124. }