ThimraTV/MoviaBox/Class/Mine/Controller/SPLanguageViewController.swift
2025-05-10 15:28:05 +08:00

126 lines
3.7 KiB
Swift

//
// SPLanguageViewController.swift
// MoviaBox
//
// Created by on 2025/5/10.
//
import UIKit
class SPLanguageViewController: SPViewController {
private lazy var dataArr: [SPLanguageModel] = []
private lazy var currentLocalizedKey = SPLocalizedManager.shared.currentLocalizedKey
private lazy var tableView: SPTableView = {
let tableView = SPTableView(frame: .zero, style: .insetGrouped)
tableView.delegate = self
tableView.dataSource = self
tableView.sectionFooterHeight = 0
tableView.tableHeaderView = UIView()
tableView.rowHeight = 60
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: kSPTabbarSafeBottomMargin + 10, right: 0)
SPLanguageCell.registerCell(tableView: tableView)
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.edgesForExtendedLayout = .top
self.title = "movia_profile_Language".localized
requestDataList()
_setupUI()
// SPSettingAPI.requestLocalizedData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
setNavigationNormalStyle(backgroundColor: .clear, isTranslucent: true)
}
}
extension SPLanguageViewController {
private func _setupUI() {
view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
make.top.equalToSuperview().offset(kSPNavBarHeight)
}
}
}
//MARK: -------------- UITableViewDelegate & UITableViewDataSource --------------
extension SPLanguageViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = dataArr[indexPath.section]
let cell = SPLanguageCell.dequeueReusableCell(tableView: tableView, indexPath: indexPath)
cell.model = model
cell.sp_isSelected = model.lang_key == currentLocalizedKey
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.dataArr.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 12
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let model = dataArr[indexPath.section]
if model.lang_key == currentLocalizedKey { return }
if let key = model.lang_key {
SPHUD.show()
SPLocalizedManager.shared.updateLocalizedData(key: key) { (finish) in
if finish {
SPLocalizedManager.shared.currentLocalizedKey = key
NotificationCenter.default.post(name: SPLocalizedManager.localizedDidChange, object: nil)
}
SPHUD.dismiss()
}
}
}
}
extension SPLanguageViewController {
private func requestDataList() {
SPSettingAPI.requestLanguageList { [weak self] list in
guard let self = self else { return }
if let list = list {
self.dataArr = list
self.tableView.reloadData()
}
}
}
}