diff --git a/Note/ClipEditViewController.swift b/Note/ClipEditViewController.swift new file mode 100644 index 0000000..4b74086 --- /dev/null +++ b/Note/ClipEditViewController.swift @@ -0,0 +1,100 @@ +// +// 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() + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + textView.becomeFirstResponder() + } + + private func setupNavigationBar() { + let saveButton = UIBarButtonItem( + title: "保存", + style: .done, + 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) + + 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) + ]) + } + + @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) + } + } catch { + await MainActor.run { + let alert = UIAlertController(title: "提示", message: "保存失败: \(error.localizedDescription)", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "确定", style: .default)) + self.present(alert, animated: true) + } + } + } + } +} diff --git a/Note/ClipInboxViewController.swift b/Note/ClipInboxViewController.swift index b55a0d4..deec5dc 100644 --- a/Note/ClipInboxViewController.swift +++ b/Note/ClipInboxViewController.swift @@ -154,8 +154,7 @@ class ClipInboxViewController: UIViewController { } private func openClip(_ clip: Clip) { - let viewController = FormatNoteViewController() - viewController.initialText = clip.text + let viewController = ClipEditViewController(clip: clip) navigationController?.pushViewController(viewController, animated: true) } diff --git a/Note/ClipStore.swift b/Note/ClipStore.swift index 1dcc617..4af3f45 100644 --- a/Note/ClipStore.swift +++ b/Note/ClipStore.swift @@ -68,6 +68,15 @@ actor ClipStore { return clips } + func update(_ clip: Clip) async throws { + var clips = try await loadAll() + guard let index = clips.firstIndex(where: { $0.id == clip.id }) else { + return + } + clips[index] = clip + try await persist(clips) + } + func delete(id: UUID) async throws { var clips = try await loadAll() clips.removeAll { $0.id == id }