Files
note/Note/ViewController.swift
T
2026-07-12 21:41:02 +08:00

145 lines
5.0 KiB
Swift

//
// ViewController.swift
// Note
//
// Created by fish on 2026/7/12.
//
import UIKit
import UniformTypeIdentifiers
class ViewController: UIViewController {
private var tableView: UITableView!
private struct MenuItem {
let icon: String
let iconColor: UIColor
let title: String
}
private let menuItems: [[MenuItem]] = [
[
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()
}
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 "打开备忘录":
openSystemNotesApp()
case "格式化日志":
let viewController = FormatLogViewController()
navigationController?.pushViewController(viewController, animated: true)
case "格式化笔记":
let viewController = FormatNoteViewController()
navigationController?.pushViewController(viewController, animated: true)
case "打开文件":
openFiles()
default:
break
}
}
private func openSystemNotesApp() {
guard let url = URL(string: "mobilenotes://") else { return }
guard UIApplication.shared.canOpenURL(url) else { return }
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
private func openFiles() {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: true)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
present(documentPicker, animated: true)
}
}
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else { return }
let fileName = url.lastPathComponent
var message: String?
if let data = try? Data(contentsOf: url),
let text = String(data: data, encoding: .utf8),
!text.isEmpty {
message = text
} else {
let attributes = try? FileManager.default.attributesOfItem(atPath: url.path)
let size = attributes?[.size] as? Int64 ?? 0
message = "无法以文本预览\n大小: \(size) 字节"
}
let alert = UIAlertController(title: fileName, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "确定", style: .default))
present(alert, animated: true)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
}
}