首页菜单支持自定义排序
This commit is contained in:
@@ -10,6 +10,9 @@ import UIKit
|
|||||||
class ViewController: UIViewController {
|
class ViewController: UIViewController {
|
||||||
|
|
||||||
private var tableView: UITableView!
|
private var tableView: UITableView!
|
||||||
|
private var editButton: UIBarButtonItem!
|
||||||
|
|
||||||
|
private let menuOrderKey = "homeMenuOrder"
|
||||||
|
|
||||||
private struct MenuItem {
|
private struct MenuItem {
|
||||||
let icon: String
|
let icon: String
|
||||||
@@ -18,18 +21,19 @@ class ViewController: UIViewController {
|
|||||||
let identifier: String
|
let identifier: String
|
||||||
}
|
}
|
||||||
|
|
||||||
private var menuItems: [[MenuItem]] = [
|
private let defaultMenuItems: [MenuItem] = [
|
||||||
[
|
|
||||||
MenuItem(icon: "tray.full", iconColor: .systemIndigo, title: "收集箱", identifier: "inbox"),
|
MenuItem(icon: "tray.full", iconColor: .systemIndigo, title: "收集箱", identifier: "inbox"),
|
||||||
MenuItem(icon: "note.text", iconColor: .systemYellow, title: "打开备忘录", identifier: "notes"),
|
MenuItem(icon: "note.text", iconColor: .systemYellow, title: "打开备忘录", identifier: "notes"),
|
||||||
MenuItem(icon: "doc.text", iconColor: .systemBlue, title: "格式化日志", identifier: "formatLog"),
|
MenuItem(icon: "doc.text", iconColor: .systemBlue, title: "格式化日志", identifier: "formatLog"),
|
||||||
MenuItem(icon: "text.alignleft", iconColor: .systemGreen, title: "格式化笔记", identifier: "formatNote"),
|
MenuItem(icon: "text.alignleft", iconColor: .systemGreen, title: "格式化笔记", identifier: "formatNote"),
|
||||||
MenuItem(icon: "folder", iconColor: .systemOrange, title: "打开文件", identifier: "files")
|
MenuItem(icon: "folder", iconColor: .systemOrange, title: "打开文件", identifier: "files")
|
||||||
]
|
]
|
||||||
]
|
|
||||||
|
private var menuItems: [[MenuItem]] = []
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
menuItems = [loadMenuOrder()]
|
||||||
setupUI()
|
setupUI()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +48,9 @@ class ViewController: UIViewController {
|
|||||||
navigationItem.largeTitleDisplayMode = .always
|
navigationItem.largeTitleDisplayMode = .always
|
||||||
view.backgroundColor = .systemGroupedBackground
|
view.backgroundColor = .systemGroupedBackground
|
||||||
|
|
||||||
|
editButton = UIBarButtonItem(title: "编辑", style: .plain, target: self, action: #selector(didTapEditButton))
|
||||||
|
navigationItem.rightBarButtonItem = editButton
|
||||||
|
|
||||||
tableView = UITableView(frame: .zero, style: .insetGrouped)
|
tableView = UITableView(frame: .zero, style: .insetGrouped)
|
||||||
tableView.translatesAutoresizingMaskIntoConstraints = false
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
tableView.delegate = self
|
tableView.delegate = self
|
||||||
@@ -59,6 +66,34 @@ class ViewController: UIViewController {
|
|||||||
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc private func didTapEditButton() {
|
||||||
|
tableView.setEditing(!tableView.isEditing, animated: true)
|
||||||
|
editButton.title = tableView.isEditing ? "完成" : "编辑"
|
||||||
|
}
|
||||||
|
|
||||||
|
private func loadMenuOrder() -> [MenuItem] {
|
||||||
|
guard let savedOrder = UserDefaults.standard.array(forKey: menuOrderKey) as? [String] else {
|
||||||
|
return defaultMenuItems
|
||||||
|
}
|
||||||
|
|
||||||
|
var orderedItems: [MenuItem] = []
|
||||||
|
for identifier in savedOrder {
|
||||||
|
if let item = defaultMenuItems.first(where: { $0.identifier == identifier }) {
|
||||||
|
orderedItems.append(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let remaining = defaultMenuItems.filter { item in
|
||||||
|
!orderedItems.contains { $0.identifier == item.identifier }
|
||||||
|
}
|
||||||
|
return orderedItems + remaining
|
||||||
|
}
|
||||||
|
|
||||||
|
private func saveMenuOrder() {
|
||||||
|
let identifiers = menuItems.flatMap { $0.map(\.identifier) }
|
||||||
|
UserDefaults.standard.set(identifiers, forKey: menuOrderKey)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ViewController: UITableViewDataSource {
|
extension ViewController: UITableViewDataSource {
|
||||||
@@ -81,16 +116,30 @@ extension ViewController: UITableViewDataSource {
|
|||||||
config.imageProperties.tintColor = item.iconColor
|
config.imageProperties.tintColor = item.iconColor
|
||||||
config.imageToTextPadding = 12
|
config.imageToTextPadding = 12
|
||||||
cell.contentConfiguration = config
|
cell.contentConfiguration = config
|
||||||
cell.accessoryType = .disclosureIndicator
|
cell.accessoryType = tableView.isEditing ? .none : .disclosureIndicator
|
||||||
|
cell.selectionStyle = tableView.isEditing ? .none : .default
|
||||||
|
|
||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
|
||||||
|
guard sourceIndexPath != destinationIndexPath else { return }
|
||||||
|
|
||||||
|
let item = menuItems[sourceIndexPath.section].remove(at: sourceIndexPath.row)
|
||||||
|
menuItems[destinationIndexPath.section].insert(item, at: destinationIndexPath.row)
|
||||||
|
saveMenuOrder()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ViewController: UITableViewDelegate {
|
extension ViewController: UITableViewDelegate {
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||||
tableView.deselectRow(at: indexPath, animated: true)
|
tableView.deselectRow(at: indexPath, animated: true)
|
||||||
|
guard !tableView.isEditing else { return }
|
||||||
|
|
||||||
let item = menuItems[indexPath.section][indexPath.row]
|
let item = menuItems[indexPath.section][indexPath.row]
|
||||||
switch item.identifier {
|
switch item.identifier {
|
||||||
@@ -112,6 +161,14 @@ extension ViewController: UITableViewDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
private func updateInboxBadge() {
|
private func updateInboxBadge() {
|
||||||
Task {
|
Task {
|
||||||
guard let count = try? await ClipStore.shared.count(), count > 0 else {
|
guard let count = try? await ClipStore.shared.count(), count > 0 else {
|
||||||
|
|||||||
Reference in New Issue
Block a user