168 lines
6.7 KiB
Swift
168 lines
6.7 KiB
Swift
//
|
|
// ClipEditViewController.swift
|
|
// Note
|
|
//
|
|
// Created by Kimi Code CLI on 2026/7/15.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class ClipEditViewController: UIViewController {
|
|
|
|
private var textView: UITextView!
|
|
private var floatingDoneButton: UIButton!
|
|
private var floatingDoneButtonBottomConstraint: NSLayoutConstraint!
|
|
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.floatingDoneButtonBottomConstraint.constant = -(keyboardHeight + 16)
|
|
self.floatingDoneButton.alpha = 1
|
|
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.floatingDoneButtonBottomConstraint.constant = -16
|
|
self.floatingDoneButton.alpha = 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
|
|
view.addSubview(textView)
|
|
|
|
var doneConfig = UIButton.Configuration.glass()
|
|
doneConfig.title = "完成"
|
|
doneConfig.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
|
|
var outgoing = incoming
|
|
outgoing.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
|
|
return outgoing
|
|
}
|
|
doneConfig.baseForegroundColor = .label
|
|
doneConfig.cornerStyle = .capsule
|
|
doneConfig.contentInsets = NSDirectionalEdgeInsets(top: 15, leading: 22, bottom: 15, trailing: 22)
|
|
floatingDoneButton = UIButton(configuration: doneConfig)
|
|
floatingDoneButton.translatesAutoresizingMaskIntoConstraints = false
|
|
floatingDoneButton.alpha = 0
|
|
floatingDoneButton.addTarget(self, action: #selector(didTapFloatingDoneButton), for: .touchUpInside)
|
|
view.addSubview(floatingDoneButton)
|
|
|
|
let safeArea = view.safeAreaLayoutGuide
|
|
floatingDoneButtonBottomConstraint = floatingDoneButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16)
|
|
|
|
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),
|
|
|
|
floatingDoneButton.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -16),
|
|
floatingDoneButtonBottomConstraint
|
|
])
|
|
}
|
|
|
|
@objc private func didTapFloatingDoneButton() {
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|