Files
note/Note/ViewController.swift
T

159 lines
5.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// ViewController.swift
// Note
//
// Created by fish on 2026/7/12.
//
import UIKit
class ViewController: UIViewController {
private var tableView: UITableView!
private struct MenuItem {
let icon: String
let iconColor: UIColor
let title: String
}
private var menuItems: [[MenuItem]] = [
[
MenuItem(icon: "tray.full", iconColor: .systemIndigo, title: "收集箱"),
MenuItem(icon: "note.text", iconColor: .systemYellow, title: "打开备忘录"),
MenuItem(icon: "doc.text", iconColor: .systemBlue, title: "格式化日志"),
MenuItem(icon: "text.alignleft", iconColor: .systemGreen, title: "格式化笔记"),
MenuItem(icon: "folder", iconColor: .systemOrange, title: "打开文件")
]
]
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateInboxBadge()
}
private func setupUI() {
title = "首页"
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
view.backgroundColor = .systemGroupedBackground
tableView = UITableView(frame: .zero, style: .insetGrouped)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "menuCell")
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)
])
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return menuItems.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath)
let item = menuItems[indexPath.section][indexPath.row]
var config = cell.defaultContentConfiguration()
config.text = item.title
config.image = UIImage(systemName: item.icon)
config.imageProperties.tintColor = item.iconColor
config.imageToTextPadding = 12
cell.contentConfiguration = config
cell.accessoryType = .disclosureIndicator
return cell
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let item = menuItems[indexPath.section][indexPath.row]
switch item.title {
case "收集箱":
let viewController = ClipInboxViewController()
navigationController?.pushViewController(viewController, animated: true)
case "打开备忘录":
openSystemNotesApp()
case "格式化日志":
let viewController = FormatLogViewController()
navigationController?.pushViewController(viewController, animated: true)
case "格式化笔记":
let viewController = FormatNoteViewController()
navigationController?.pushViewController(viewController, animated: true)
case "打开文件":
openFilesApp()
default:
break
}
}
private func updateInboxBadge() {
Task {
guard let count = try? await ClipStore.shared.count(), count > 0 else {
await MainActor.run {
self.updateInboxTitle("收集箱")
}
return
}
await MainActor.run {
self.updateInboxTitle("收集箱(\(count)")
}
}
}
private func updateInboxTitle(_ title: String) {
for (sectionIndex, section) in menuItems.enumerated() {
for (rowIndex, item) in section.enumerated() {
if item.title.hasPrefix("收集箱") {
menuItems[sectionIndex][rowIndex] = MenuItem(
icon: item.icon,
iconColor: item.iconColor,
title: title
)
let indexPath = IndexPath(row: rowIndex, section: sectionIndex)
tableView.reloadRows(at: [indexPath], with: .none)
return
}
}
}
}
private func openSystemNotesApp() {
openURLIfPossible("mobilenotes://")
}
private func openFilesApp() {
openURLIfPossible("shareddocuments://")
}
private func openURLIfPossible(_ string: String) {
guard let url = URL(string: string) else { return }
guard UIApplication.shared.canOpenURL(url) else { return }
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}