Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ac2eac5fcd | |||
| a386802a55 | |||
| 1f94b9884c | |||
| 58a8d27315 | |||
| 7f4fc34dbf |
@@ -11,6 +11,7 @@ class ClipInboxViewController: UIViewController {
|
|||||||
|
|
||||||
private var tableView: UITableView!
|
private var tableView: UITableView!
|
||||||
private var emptyLabel: UILabel!
|
private var emptyLabel: UILabel!
|
||||||
|
private var archiveButton: UIButton!
|
||||||
private var clips: [Clip] = []
|
private var clips: [Clip] = []
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
@@ -20,8 +21,10 @@ class ClipInboxViewController: UIViewController {
|
|||||||
view.backgroundColor = .systemGroupedBackground
|
view.backgroundColor = .systemGroupedBackground
|
||||||
|
|
||||||
setupNavigationBar()
|
setupNavigationBar()
|
||||||
|
setupArchiveButton()
|
||||||
setupTableView()
|
setupTableView()
|
||||||
setupEmptyLabel()
|
setupEmptyLabel()
|
||||||
|
setupAppStateObserver()
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewWillAppear(_ animated: Bool) {
|
override func viewWillAppear(_ animated: Bool) {
|
||||||
@@ -29,6 +32,23 @@ class ClipInboxViewController: UIViewController {
|
|||||||
loadClips()
|
loadClips()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
NotificationCenter.default.removeObserver(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func setupAppStateObserver() {
|
||||||
|
NotificationCenter.default.addObserver(
|
||||||
|
self,
|
||||||
|
selector: #selector(appDidBecomeActive),
|
||||||
|
name: UIApplication.didBecomeActiveNotification,
|
||||||
|
object: nil
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func appDidBecomeActive() {
|
||||||
|
loadClips()
|
||||||
|
}
|
||||||
|
|
||||||
private func setupNavigationBar() {
|
private func setupNavigationBar() {
|
||||||
let clearButton = UIBarButtonItem(
|
let clearButton = UIBarButtonItem(
|
||||||
title: "清空",
|
title: "清空",
|
||||||
@@ -53,10 +73,58 @@ class ClipInboxViewController: UIViewController {
|
|||||||
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
||||||
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
||||||
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
||||||
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
tableView.bottomAnchor.constraint(equalTo: archiveButton.topAnchor, constant: -16)
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func setupArchiveButton() {
|
||||||
|
var config = UIButton.Configuration.filled()
|
||||||
|
config.title = "归档"
|
||||||
|
config.baseBackgroundColor = .systemBlue
|
||||||
|
config.baseForegroundColor = .white
|
||||||
|
config.cornerStyle = .medium
|
||||||
|
config.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)
|
||||||
|
|
||||||
|
archiveButton = UIButton(configuration: config)
|
||||||
|
archiveButton.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
archiveButton.addTarget(self, action: #selector(didTapArchiveButton), for: .touchUpInside)
|
||||||
|
view.addSubview(archiveButton)
|
||||||
|
|
||||||
|
NSLayoutConstraint.activate([
|
||||||
|
archiveButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16),
|
||||||
|
archiveButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16),
|
||||||
|
archiveButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16),
|
||||||
|
archiveButton.heightAnchor.constraint(equalToConstant: 52)
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func didTapArchiveButton() {
|
||||||
|
guard clips.count >= 2 else {
|
||||||
|
let alert = UIAlertController(title: "提示", message: "至少需要两条文案才能归档。", preferredStyle: .alert)
|
||||||
|
alert.addAction(UIAlertAction(title: "确定", style: .default))
|
||||||
|
present(alert, animated: true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let title = self.archiveTitle()
|
||||||
|
let itemsText = clips.map { "\($0.text)\n\n---" }.joined(separator: "\n\n")
|
||||||
|
let archivedText = "\(title)\n\n\(itemsText)"
|
||||||
|
let viewController = FormatNoteViewController()
|
||||||
|
viewController.initialText = archivedText
|
||||||
|
navigationController?.pushViewController(viewController, animated: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func archiveTitle() -> String {
|
||||||
|
let dateFormatter = DateFormatter()
|
||||||
|
dateFormatter.locale = Locale(identifier: "zh_CN")
|
||||||
|
dateFormatter.dateFormat = "yyyy年MM月dd日"
|
||||||
|
let dateString = dateFormatter.string(from: Date())
|
||||||
|
let weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
|
||||||
|
let weekdayIndex = Calendar.current.component(.weekday, from: Date()) - 1
|
||||||
|
let weekday = (weekdayIndex >= 0 && weekdayIndex < weekdays.count) ? weekdays[weekdayIndex] : ""
|
||||||
|
return "【读书摘抄】\(dateString) \(weekday)"
|
||||||
|
}
|
||||||
|
|
||||||
private func setupEmptyLabel() {
|
private func setupEmptyLabel() {
|
||||||
emptyLabel = UILabel()
|
emptyLabel = UILabel()
|
||||||
emptyLabel.translatesAutoresizingMaskIntoConstraints = false
|
emptyLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||||
@@ -96,6 +164,7 @@ class ClipInboxViewController: UIViewController {
|
|||||||
let isEmpty = clips.isEmpty
|
let isEmpty = clips.isEmpty
|
||||||
emptyLabel.isHidden = !isEmpty
|
emptyLabel.isHidden = !isEmpty
|
||||||
tableView.isHidden = isEmpty
|
tableView.isHidden = isEmpty
|
||||||
|
archiveButton.isHidden = isEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc private func didTapClearButton() {
|
@objc private func didTapClearButton() {
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ struct SaveClipIntent: AppIntent {
|
|||||||
return .result(value: "剪贴板没有有效文本,未保存。")
|
return .result(value: "剪贴板没有有效文本,未保存。")
|
||||||
}
|
}
|
||||||
|
|
||||||
try await ClipStore.shared.save(text: trimmed, source: source)
|
let formattedText = TextFormatter.formatForShortcut(trimmed)
|
||||||
|
try await ClipStore.shared.save(text: formattedText, source: source)
|
||||||
return .result(value: "已保存到收集箱。")
|
return .result(value: "已保存到收集箱。")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,13 @@ enum TextFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static func formatForCloud(_ text: String) -> String {
|
static func formatForCloud(_ text: String) -> String {
|
||||||
|
var content = formatForShortcut(text)
|
||||||
|
content = "# " + content
|
||||||
|
content = insertEmptyLineAfterFirstLine(content)
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
static func formatForShortcut(_ text: String) -> String {
|
||||||
var content = text
|
var content = text
|
||||||
content = content.replacingOccurrences(of: " ", with: "")
|
content = content.replacingOccurrences(of: " ", with: "")
|
||||||
content = content.replacingOccurrences(of: " ", with: "")
|
content = content.replacingOccurrences(of: " ", with: "")
|
||||||
@@ -71,8 +78,6 @@ enum TextFormatter {
|
|||||||
content = content.replacingOccurrences(of: " ", with: "")
|
content = content.replacingOccurrences(of: " ", with: "")
|
||||||
content = content.replacingOccurrences(of: "---", with: "---\n")
|
content = content.replacingOccurrences(of: "---", with: "---\n")
|
||||||
content = content.replacingOccurrences(of: "日星期", with: "日 星期")
|
content = content.replacingOccurrences(of: "日星期", with: "日 星期")
|
||||||
content = "# " + content
|
|
||||||
content = insertEmptyLineAfterFirstLine(content)
|
|
||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user