import UIKit import GeneralUtils class VCNhapMucChup: UIViewController, IVCLoadDataTableViewUIThread { @IBOutlet weak var edtTitle: UITextField! @IBOutlet weak var topMargin: NSLayoutConstraint! @IBOutlet weak var tableView: UITableViewLoadDataFromUIThread! @IBOutlet weak var vTopLogo: VTopLogo! @IBOutlet weak var btnNext: UIButtonCustomGradientBg! var cTruong: CongTruong? static func getInstance() -> VCNhapMucChup { let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let vcOpen = storyboard.instantiateViewController(withIdentifier: "VCNhapMucChup") as! VCNhapMucChup return vcOpen } static func openControllerFromEdit(_ viewController: UIViewController, cTruong: CongTruong?) { let vcOpen = getInstance() vcOpen.cTruong = cTruong; viewController.present(vcOpen, animated: true, completion: nil) } override func viewDidLoad() { super.viewDidLoad() vTopLogo.leftBtnClick = { self.btnLeftClick(self.vTopLogo) } tableView.emptyText = LocalizedString("list_empty") CellMucChupWithDelete.registerClass(tableView: tableView, forCellReuseIdentifier: "CellMucChupWithDelete") self.tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.estimatedRowHeight = 150 if self.cTruong != nil { btnNext.isHidden = true; } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) tableView.initAndLoadData(self) } @IBAction func btnAddClick(_ sender: Any) { if let title = edtTitle.text?.trimAndReturn(), title.length > 0 { for item in tableView.itemsData { if let mc = item as? ShootItem, mc.name == title { CommonUtils.showToastLong(text: LocalizedString("muc_chup_da_ton_tai")) return } } tableView.itemsData.insert(ShootItem(name: title), at: 0) tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic) tableView.removeAllLoaddingEmpty() edtTitle.text = "" } else { CommonUtils.showToastLong(text: LocalizedString("khong_de_trong")) } } @IBAction func btnRightClick(_ sender: Any) { saveMucChup() _ = getVcRoot()?.changeCurrentController(VCChonMucChup.getInstance()) } @IBAction func btnLeftClick(_ sender: Any) { saveMucChup() if self.cTruong != nil { dismiss(animated: true) } else { _ = getVcRoot()?.changeCurrentController(VCNhapTenCtruong.getInstance()) } } private func saveMucChup() { var mucChups = [ShootItem]() for item in tableView.itemsData { if let mucChup = item as? ShootItem { mucChups.append(mucChup) } } if let cTruong = self.cTruong { cTruong.shootItems = mucChups; } else { getVcRoot()?.mucChups = mucChups } } //region ============== TableView Data ========= func loadDataOnUI(complete: @escaping ([Any]?) -> ()) { if let cTruong = self.cTruong { complete(cTruong.shootItems); } else { complete(getVcRoot()?.mucChups) } } func getAllCell() -> [BaseCell] { var baseCells: [BaseCell] = [BaseCell](); baseCells.append(BaseCell(type: 0, identifier: "CellMucChupWithDelete")) return baseCells } func getTypeOfData(baseobj: Any) -> Int { return 0 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let mucChup = self.tableView.getItem(indexPath) as? ShootItem { edtTitle.text = mucChup.name } } func deleteMucChup(_ mucChupParam: ShootItem) { var indexFind = -1, index = -1 for item in self.tableView.itemsData { index += 1 if let mucChup = item as? ShootItem, mucChup.name == mucChupParam.name { indexFind = index break } } if indexFind >= 0 { tableView.itemsData.remove(at: indexFind) tableView.deleteRows(at: [IndexPath(row: indexFind, section: 0)], with: .automatic) } } //endregion @IBAction func edtActionTrigerClick(_ sender: Any) { edtTitle.endEditing(true) } func getVcRoot() -> VCRootCreateNew? { return self.parent as? VCRootCreateNew } override var prefersStatusBarHidden: Bool { return true } override open var supportedInterfaceOrientations: UIInterfaceOrientationMask { return UIInterfaceOrientationMask.portrait } }