// // ClipEditViewController.swift // Note // // Created by Kimi Code CLI on 2026/7/15. // import UIKit class ClipEditViewController: UIViewController { private var textView: UITextView! private var clip: Clip init(clip: Clip) { self.clip = clip super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() title = "编辑文案" navigationItem.largeTitleDisplayMode = .never view.backgroundColor = .systemGroupedBackground setupNavigationBar() setupTextView() setupKeyboardObservers() } deinit { NotificationCenter.default.removeObserver(self) } private func setupKeyboardObservers() { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil) } @objc private func keyboardWillShow(_ notification: Notification) { guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return } let keyboardFrameInView = view.convert(keyboardFrame, from: nil) let intersection = keyboardFrameInView.intersection(view.bounds) let keyboardHeight = intersection.height let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25 UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) { self.textView.contentInset.bottom = keyboardHeight self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight self.view.layoutIfNeeded() } } @objc private func keyboardWillHide(_ notification: Notification) { let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25 UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) { self.textView.contentInset.bottom = 0 self.textView.verticalScrollIndicatorInsets.bottom = 0 self.view.layoutIfNeeded() } } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) textView.becomeFirstResponder() } private func setupNavigationBar() { let saveButton = UIBarButtonItem( title: "保存", style: .prominent, target: self, action: #selector(didTapSaveButton) ) navigationItem.rightBarButtonItem = saveButton } private func setupTextView() { textView = UITextView() textView.translatesAutoresizingMaskIntoConstraints = false textView.backgroundColor = UIColor { traitCollection in traitCollection.userInterfaceStyle == .dark ? .secondarySystemBackground : .white } textView.layer.cornerRadius = 12 textView.layer.borderWidth = 0.5 textView.layer.borderColor = UIColor.separator.cgColor textView.layer.masksToBounds = true textView.font = UIFont.preferredFont(forTextStyle: .body) textView.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12) textView.text = clip.text textView.inputAccessoryView = createInputAccessoryView() view.addSubview(textView) let safeArea = view.safeAreaLayoutGuide NSLayoutConstraint.activate([ textView.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: 16), textView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: 16), textView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -16), textView.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -16) ]) } private func createInputAccessoryView() -> UIToolbar { let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)) toolbar.barStyle = .default toolbar.isTranslucent = true let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let doneButton = UIBarButtonItem(title: "完成", style: .done, target: self, action: #selector(didTapKeyboardDoneButton)) toolbar.items = [flexSpace, doneButton] toolbar.sizeToFit() return toolbar } @objc private func didTapKeyboardDoneButton() { textView.resignFirstResponder() } @objc private func didTapSaveButton() { let updatedText = textView.text ?? "" guard !updatedText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { let alert = UIAlertController(title: "提示", message: "文案不能为空。", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "确定", style: .default)) present(alert, animated: true) return } var updatedClip = clip updatedClip.text = updatedText Task { do { try await ClipStore.shared.update(updatedClip) _ = await MainActor.run { self.navigationController?.popViewController(animated: true) return () } } catch { await MainActor.run { let alert = UIAlertController(title: "提示", message: "保存失败: \(error.localizedDescription)", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "确定", style: .default)) self.present(alert, animated: true) } } } } }