|
|
|
@@ -11,6 +11,7 @@ class ClipInboxViewController: UIViewController {
|
|
|
|
|
|
|
|
|
|
private var tableView: UITableView!
|
|
|
|
|
private var emptyLabel: UILabel!
|
|
|
|
|
private var archiveButton: UIButton!
|
|
|
|
|
private var clips: [Clip] = []
|
|
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
@@ -20,8 +21,10 @@ class ClipInboxViewController: UIViewController {
|
|
|
|
|
view.backgroundColor = .systemGroupedBackground
|
|
|
|
|
|
|
|
|
|
setupNavigationBar()
|
|
|
|
|
setupArchiveButton()
|
|
|
|
|
setupTableView()
|
|
|
|
|
setupEmptyLabel()
|
|
|
|
|
setupAppStateObserver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
@@ -29,6 +32,23 @@ class ClipInboxViewController: UIViewController {
|
|
|
|
|
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() {
|
|
|
|
|
let clearButton = UIBarButtonItem(
|
|
|
|
|
title: "清空",
|
|
|
|
@@ -53,10 +73,58 @@ class ClipInboxViewController: UIViewController {
|
|
|
|
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
|
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
|
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() {
|
|
|
|
|
emptyLabel = UILabel()
|
|
|
|
|
emptyLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
@@ -96,6 +164,7 @@ class ClipInboxViewController: UIViewController {
|
|
|
|
|
let isEmpty = clips.isEmpty
|
|
|
|
|
emptyLabel.isHidden = !isEmpty
|
|
|
|
|
tableView.isHidden = isEmpty
|
|
|
|
|
archiveButton.isHidden = isEmpty
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc private func didTapClearButton() {
|
|
|
|
|