From bbaaea98739c0debc570a452bc1b7229ecaab4ac Mon Sep 17 00:00:00 2001 From: JH713 Date: Tue, 16 Apr 2024 14:55:23 +0900 Subject: [PATCH 1/2] feat: folder menu in boxList --- iBox/Sources/BoxList/BoxListView.swift | 12 +++++ .../BoxList/BoxListViewController.swift | 46 +++++++++++++++++++ iBox/Sources/BoxList/BoxListViewModel.swift | 21 +++++++++ 3 files changed, 79 insertions(+) diff --git a/iBox/Sources/BoxList/BoxListView.swift b/iBox/Sources/BoxList/BoxListView.swift index 316c5bf..b718866 100644 --- a/iBox/Sources/BoxList/BoxListView.swift +++ b/iBox/Sources/BoxList/BoxListView.swift @@ -15,6 +15,8 @@ protocol BoxListViewDelegate: AnyObject { func pushViewController(type: EditType) func pushViewController(url: URL?) func presentEditBookmarkController(at indexPath: IndexPath) + func deleteFolderinBoxList(at section: Int) + func editFolderNameinBoxList(at section: Int, currentName: String) } class BoxListView: UIView { @@ -187,6 +189,16 @@ extension BoxListView: UITableViewDelegate { button.addTarget(self, action: #selector(handleOpenClose), for: .touchUpInside) + let edit = UIAction(title: "폴더 편집", image: UIImage(systemName: "pencil")) { [weak self] _ in + guard let folderName = self?.viewModel?.boxList[section].name else { return } + self?.delegate?.editFolderNameinBoxList(at: section, currentName: folderName) + } + let delete = UIAction(title: "폴더 삭제", image: UIImage(systemName: "trash"), attributes: .destructive) { [weak self] _ in + self?.delegate?.deleteFolderinBoxList(at: section) + } + + button.menu = UIMenu(options: .displayInline, children: [edit, delete]) + return button } diff --git a/iBox/Sources/BoxList/BoxListViewController.swift b/iBox/Sources/BoxList/BoxListViewController.swift index 54ed918..553dde3 100644 --- a/iBox/Sources/BoxList/BoxListViewController.swift +++ b/iBox/Sources/BoxList/BoxListViewController.swift @@ -96,6 +96,52 @@ extension BoxListViewController: AddBookmarkViewControllerProtocol { } extension BoxListViewController: BoxListViewDelegate { + func deleteFolderinBoxList(at section: Int) { + recheckDeleteFolder(at: section) + } + + private func recheckDeleteFolder(at section: Int) { + let actionSheetController = UIAlertController(title: nil, message: "모든 북마크가 삭제됩니다.", preferredStyle: .actionSheet) + let firstAction = UIAlertAction(title: "폴더 삭제", style: .destructive) {[weak self] _ in + guard let contentView = self?.contentView as? BoxListView else { return } + contentView.viewModel?.deleteFolderDirect(section) + } + let cancelAction = UIAlertAction(title: "취소", style: .cancel) + actionSheetController.addAction(firstAction) + actionSheetController.addAction(cancelAction) + present(actionSheetController, animated: true) + } + + func editFolderNameinBoxList(at section: Int, currentName: String) { + let controller = UIAlertController(title: "폴더 이름 변경", message: nil, preferredStyle: .alert) + + let cancelAction = UIAlertAction(title: "취소", style: .default) { _ in return } + let okAction = UIAlertAction(title: "확인", style: .default) { [weak self] action in + guard let newName = controller.textFields?.first?.text else { return } + guard let contentView = self?.contentView as? BoxListView else { return } + contentView.viewModel?.editFolderDirect(section, name: newName) + } + controller.addAction(cancelAction) + controller.addAction(okAction) + okAction.isEnabled = true + + controller.addTextField() { textField in + NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main, using: + {_ in + let textCount = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines).count ?? 0 + let textIsNotEmpty = textCount > 0 + + okAction.isEnabled = textIsNotEmpty + + }) + } + controller.textFields?.first?.text = currentName + controller.textFields?.first?.autocorrectionType = .no + controller.textFields?.first?.spellCheckingType = .no + + self.present(controller, animated: true) + } + func presentEditBookmarkController(at indexPath: IndexPath) { guard let contentView = contentView as? BoxListView else { return } diff --git a/iBox/Sources/BoxList/BoxListViewModel.swift b/iBox/Sources/BoxList/BoxListViewModel.swift index a4ba8e9..3fbcf25 100644 --- a/iBox/Sources/BoxList/BoxListViewModel.swift +++ b/iBox/Sources/BoxList/BoxListViewModel.swift @@ -179,4 +179,25 @@ class BoxListViewModel { output.send(.sendBoxList(boxList: boxList)) } + func deleteFolderDirect(_ section: Int) { + let folderId = boxList[section].id + CoreDataManager.shared.deleteFolder(id: folderId) + boxList.remove(at: section) + for box in boxList { + sectionsToReload.update(with: box.id) + } + output.send(.sendBoxList(boxList: boxList)) + output.send(.reloadSections(idArray: Array(sectionsToReload))) + sectionsToReload.removeAll() + } + + func editFolderDirect(_ section: Int, name: String) { + let folderId = boxList[section].id + CoreDataManager.shared.updateFolder(id: folderId, name: name) + boxList[section].name = name + sectionsToReload.update(with: boxList[section].id) + output.send(.reloadSections(idArray: Array(sectionsToReload))) + sectionsToReload.removeAll() + } + } From 7239e7389afdbf02401b54d6086dd83707d648c6 Mon Sep 17 00:00:00 2001 From: JH713 Date: Tue, 16 Apr 2024 15:26:11 +0900 Subject: [PATCH 2/2] fix: initial button rotate --- iBox/Sources/BoxList/FolderButton.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/iBox/Sources/BoxList/FolderButton.swift b/iBox/Sources/BoxList/FolderButton.swift index 23d6cbd..66cfbd4 100644 --- a/iBox/Sources/BoxList/FolderButton.swift +++ b/iBox/Sources/BoxList/FolderButton.swift @@ -43,6 +43,9 @@ class FolderButton: UIButton { private func setupProperty() { backgroundColor = .tableViewBackgroundColor openCloseImageView.image = UIImage(systemName: "chevron.right") + if isOpen { + openCloseImageView.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2) + } } private func setupHierarchy() {