Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions iBox/Sources/Base/BaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NavigationBar: UIView {
var titleLabel = UILabel()
var addButton = UIButton()
var moreButton = UIButton()
var doneButton = UIButton()
}

protocol BaseViewControllerProtocol {
Expand All @@ -40,6 +41,9 @@ class BaseViewController<View: UIView>: UIViewController {
$0.moreButton.configuration = .plain()
$0.moreButton.configuration?.image = UIImage(systemName: "ellipsis.circle")
$0.moreButton.configuration?.preferredSymbolConfigurationForImage = .init(weight: .bold)
$0.doneButton.configuration = .plain()
$0.doneButton.configuration?.baseForegroundColor = .label
$0.doneButton.configuration?.attributedTitle = AttributedString("완료", attributes: AttributeContainer([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .semibold)]))
}

let contentView: UIView = View()
Expand All @@ -63,6 +67,7 @@ class BaseViewController<View: UIView>: UIViewController {
setNavigationBarTitleLabelFont(titleFont)
setNavigationBarBackButtonHidden(true)
setNavigationBarMenuButtonHidden(true)
setNavigationBarDoneButtonHidden(true)

navigationBar.backButton.addTarget(self, action: #selector(backButtonTapped), for: .touchUpInside)
}
Expand All @@ -74,6 +79,7 @@ class BaseViewController<View: UIView>: UIViewController {
navigationBar.addSubview(navigationBar.titleLabel)
navigationBar.addSubview(navigationBar.addButton)
navigationBar.addSubview(navigationBar.moreButton)
navigationBar.addSubview(navigationBar.doneButton)
view.addSubview(contentView)
}

Expand Down Expand Up @@ -111,6 +117,11 @@ class BaseViewController<View: UIView>: UIViewController {
make.width.height.equalTo(24)
}

navigationBar.doneButton.snp.makeConstraints { make in
make.trailing.equalToSuperview().inset(20)
make.centerY.equalToSuperview()
}

contentView.snp.makeConstraints { make in
make.top.equalTo(statusBar.snp.bottom).offset(60)
make.leading.trailing.equalToSuperview()
Expand Down Expand Up @@ -181,6 +192,10 @@ class BaseViewController<View: UIView>: UIViewController {
}
}

func setNavigationBarDoneButtonHidden(_ hidden: Bool) {
navigationBar.doneButton.isHidden = hidden
}

func setNavigationBarAddButtonAction(_ selector: Selector) {
navigationBar.addButton.addTarget(self, action: selector, for: .touchUpInside)
}
Expand All @@ -189,6 +204,10 @@ class BaseViewController<View: UIView>: UIViewController {
navigationBar.moreButton.addTarget(self, action: selector, for: .touchUpInside)
}

func setNavigationBarDoneButtonAction(_ selector: Selector) {
navigationBar.doneButton.addTarget(self, action: selector, for: .touchUpInside)
}

func setNavigationBarTitleLabelText(_ text: String?) {
navigationBar.titleLabel.text = text
}
Expand Down
39 changes: 38 additions & 1 deletion iBox/Sources/BoxList/BoxListCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import UIKit
import SnapKit

class BoxListCell: UITableViewCell {

var viewModel: BoxListCellViewModel?
static let reuseIdentifier = "boxListCell"

var onDelete: (() -> Void)?
var onEdit: (() -> Void)?

// MARK: - UI Components

private let cellImageView = UIImageView().then {
Expand All @@ -24,19 +26,42 @@ class BoxListCell: UITableViewCell {

private let label = UILabel()

private let editButton = UIButton().then{
$0.configuration = .plain()
$0.configuration?.image = UIImage(systemName: "ellipsis.circle")?.withTintColor(.box, renderingMode: .alwaysOriginal)

$0.showsMenuAsPrimaryAction = true
$0.isHidden = true
}

private lazy var editAction = UIAction(title: "북마크 편집", image: UIImage(systemName: "pencil")) {[weak self] _ in
self?.onEdit?()
}

private lazy var deleteAction = UIAction(title: "삭제", image: UIImage(systemName: "trash"), attributes: .destructive) { [weak self] _ in
self?.onDelete?()
}

// MARK: - Initializer

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupProperty()
setupHierarchy()
setupLayout()
configureEditMenu()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func prepareForReuse() {
viewModel = nil
onDelete = nil
onEdit = nil
}

// MARK: - Setup Methods

private func setupProperty() {
Expand All @@ -47,6 +72,7 @@ class BoxListCell: UITableViewCell {
private func setupHierarchy() {
contentView.addSubview(cellImageView)
contentView.addSubview(label)
contentView.addSubview(editButton)
}

private func setupLayout() {
Expand All @@ -60,6 +86,13 @@ class BoxListCell: UITableViewCell {
make.top.trailing.bottom.equalToSuperview()
make.leading.equalTo(cellImageView.snp.trailing).offset(10)
}
editButton.snp.makeConstraints { make in
make.top.bottom.trailing.equalToSuperview()
}
}

private func configureEditMenu() {
editButton.menu = UIMenu(options: .displayInline, children: [editAction, deleteAction])
}

// MARK: - Bind ViewModel
Expand All @@ -68,5 +101,9 @@ class BoxListCell: UITableViewCell {
self.viewModel = viewModel
label.text = viewModel.name
}

func setEditButtonHidden(_ isHidden: Bool) {
editButton.isHidden = isHidden
}

}
12 changes: 7 additions & 5 deletions iBox/Sources/BoxList/BoxListCellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
import Foundation

class BoxListCellViewModel: Identifiable {
private let bookmark: Bookmark
var bookmark: Bookmark

init(bookmark: Bookmark) {
self.bookmark = bookmark
self.name = bookmark.name
self.url = bookmark.url
}

var id: UUID {
bookmark.id
}

var name: String
var name: String {
bookmark.name
}

var url: URL
var url: URL {
bookmark.url
}

}
58 changes: 46 additions & 12 deletions iBox/Sources/BoxList/BoxListSectionViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,66 @@ import Foundation

class BoxListSectionViewModel: Identifiable {
var folder: Folder
private var boxListCellViewModels: [BoxListCellViewModel]!

var boxListCellViewModels: [BoxListCellViewModel] {
didSet {
folder.bookmarks = boxListCellViewModels.map {
Bookmark(id: $0.id, name: $0.name, url: $0.url)
}
}
}

init(folder: Folder) {
self.folder = folder
boxListCellViewModels = folder.bookmarks.map { BoxListCellViewModel(bookmark: $0) }
}

var boxListCellViewModelsWithStatus: [BoxListCellViewModel] {
return isOpened ? boxListCellViewModels : []
}

var id: UUID {
folder.id
}

var name: String {
folder.name
}

var isOpened: Bool {
get {
folder.isOpened
folder.name
}

set {
folder.isOpened = newValue
folder.name = newValue
}
}

var isOpened: Bool = false


var boxListCellViewModelsWithStatus: [BoxListCellViewModel] {
return isOpened ? boxListCellViewModels : []
}

func viewModel(at index: Int) -> BoxListCellViewModel {
return boxListCellViewModels[index]
}

@discardableResult
func deleteCell(at index: Int) -> BoxListCellViewModel {
let cell = boxListCellViewModels[index]
boxListCellViewModels.remove(at: index)
return cell
}

func updateCell(at index: Int, bookmark: Bookmark) {
boxListCellViewModels[index].bookmark = bookmark
}

func insertCell(_ cell: BoxListCellViewModel, at index: Int) {
boxListCellViewModels.insert(cell, at: index)
}

@discardableResult
func openSectionIfNeeded() -> Bool {
if !isOpened {
isOpened = true
return true
}
return false
}
}

Loading