添加收集箱与快捷指令保存功能
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
//
|
||||
// ClipInboxViewController.swift
|
||||
// Note
|
||||
//
|
||||
// Created by Kimi Code CLI on 2026/7/15.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
class ClipInboxViewController: UIViewController {
|
||||
|
||||
private var tableView: UITableView!
|
||||
private var emptyLabel: UILabel!
|
||||
private var clips: [Clip] = []
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
title = "收集箱"
|
||||
navigationItem.largeTitleDisplayMode = .never
|
||||
view.backgroundColor = .systemGroupedBackground
|
||||
|
||||
setupNavigationBar()
|
||||
setupTableView()
|
||||
setupEmptyLabel()
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
loadClips()
|
||||
}
|
||||
|
||||
private func setupNavigationBar() {
|
||||
let clearButton = UIBarButtonItem(
|
||||
title: "清空",
|
||||
style: .plain,
|
||||
target: self,
|
||||
action: #selector(didTapClearButton)
|
||||
)
|
||||
clearButton.tintColor = .systemRed
|
||||
navigationItem.rightBarButtonItem = clearButton
|
||||
}
|
||||
|
||||
private func setupTableView() {
|
||||
tableView = UITableView(frame: .zero, style: .insetGrouped)
|
||||
tableView.translatesAutoresizingMaskIntoConstraints = false
|
||||
tableView.delegate = self
|
||||
tableView.dataSource = self
|
||||
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "clipCell")
|
||||
tableView.backgroundColor = .systemGroupedBackground
|
||||
view.addSubview(tableView)
|
||||
|
||||
NSLayoutConstraint.activate([
|
||||
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
||||
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
||||
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
||||
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
||||
])
|
||||
}
|
||||
|
||||
private func setupEmptyLabel() {
|
||||
emptyLabel = UILabel()
|
||||
emptyLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
emptyLabel.text = "还没有保存的文案\n复制内容后运行快捷指令即可收藏"
|
||||
emptyLabel.numberOfLines = 0
|
||||
emptyLabel.textAlignment = .center
|
||||
emptyLabel.textColor = .secondaryLabel
|
||||
emptyLabel.font = UIFont.preferredFont(forTextStyle: .body)
|
||||
emptyLabel.isHidden = true
|
||||
view.addSubview(emptyLabel)
|
||||
|
||||
NSLayoutConstraint.activate([
|
||||
emptyLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
||||
emptyLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
|
||||
emptyLabel.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor),
|
||||
emptyLabel.trailingAnchor.constraint(equalTo: view.readableContentGuide.trailingAnchor)
|
||||
])
|
||||
}
|
||||
|
||||
private func loadClips() {
|
||||
Task {
|
||||
do {
|
||||
clips = try await ClipStore.shared.loadAll()
|
||||
await MainActor.run {
|
||||
tableView.reloadData()
|
||||
updateEmptyState()
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
showError(message: "加载失败: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func updateEmptyState() {
|
||||
let isEmpty = clips.isEmpty
|
||||
emptyLabel.isHidden = !isEmpty
|
||||
tableView.isHidden = isEmpty
|
||||
navigationItem.rightBarButtonItem?.isEnabled = !isEmpty
|
||||
}
|
||||
|
||||
@objc private func didTapClearButton() {
|
||||
let alert = UIAlertController(
|
||||
title: "清空收集箱",
|
||||
message: "确定要删除所有保存的文案吗?此操作不可撤销。",
|
||||
preferredStyle: .alert
|
||||
)
|
||||
alert.addAction(UIAlertAction(title: "取消", style: .cancel))
|
||||
alert.addAction(UIAlertAction(title: "清空", style: .destructive) { [weak self] _ in
|
||||
self?.clearAllClips()
|
||||
})
|
||||
present(alert, animated: true)
|
||||
}
|
||||
|
||||
private func clearAllClips() {
|
||||
Task {
|
||||
do {
|
||||
try await ClipStore.shared.clearAll()
|
||||
clips = []
|
||||
await MainActor.run {
|
||||
tableView.reloadData()
|
||||
updateEmptyState()
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
showError(message: "清空失败: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteClip(at indexPath: IndexPath) {
|
||||
let clip = clips[indexPath.row]
|
||||
Task {
|
||||
do {
|
||||
try await ClipStore.shared.delete(id: clip.id)
|
||||
clips.remove(at: indexPath.row)
|
||||
await MainActor.run {
|
||||
tableView.deleteRows(at: [indexPath], with: .automatic)
|
||||
updateEmptyState()
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
showError(message: "删除失败: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func openClip(_ clip: Clip) {
|
||||
let viewController = FormatNoteViewController()
|
||||
viewController.initialText = clip.text
|
||||
navigationController?.pushViewController(viewController, animated: true)
|
||||
}
|
||||
|
||||
private func showError(message: String) {
|
||||
let alert = UIAlertController(title: "提示", message: message, preferredStyle: .alert)
|
||||
alert.addAction(UIAlertAction(title: "确定", style: .default))
|
||||
present(alert, animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
extension ClipInboxViewController: UITableViewDataSource {
|
||||
|
||||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return clips.count
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "clipCell", for: indexPath)
|
||||
let clip = clips[indexPath.row]
|
||||
|
||||
var config = cell.defaultContentConfiguration()
|
||||
config.text = previewText(for: clip.text)
|
||||
config.secondaryText = formattedDate(clip.createdAt)
|
||||
config.secondaryTextProperties.color = .secondaryLabel
|
||||
cell.contentConfiguration = config
|
||||
cell.accessoryType = .disclosureIndicator
|
||||
|
||||
return cell
|
||||
}
|
||||
|
||||
private func previewText(for text: String) -> String {
|
||||
let lines = text.components(separatedBy: .newlines)
|
||||
let firstTwoLines = Array(lines.prefix(2))
|
||||
let preview = firstTwoLines.joined(separator: " ").trimmingCharacters(in: .whitespaces)
|
||||
if preview.isEmpty {
|
||||
return "(无内容)"
|
||||
}
|
||||
return preview
|
||||
}
|
||||
|
||||
private func formattedDate(_ date: Date) -> String {
|
||||
let formatter = RelativeDateTimeFormatter()
|
||||
formatter.unitsStyle = .short
|
||||
return formatter.localizedString(for: date, relativeTo: Date())
|
||||
}
|
||||
}
|
||||
|
||||
extension ClipInboxViewController: UITableViewDelegate {
|
||||
|
||||
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
tableView.deselectRow(at: indexPath, animated: true)
|
||||
let clip = clips[indexPath.row]
|
||||
openClip(clip)
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView,
|
||||
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
||||
let deleteAction = UIContextualAction(style: .destructive, title: "删除") { [weak self] _, _, completion in
|
||||
self?.deleteClip(at: indexPath)
|
||||
completion(true)
|
||||
}
|
||||
return UISwipeActionsConfiguration(actions: [deleteAction])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user