Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/quest delete and decrease #147

Merged
merged 3 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 3 additions & 4 deletions DailyQuest/DailyQuest/Domain/Entities/Quest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ struct Quest: Equatable {
- Parameters:
- value: 0보닀 큰 μ •μˆ˜κ°’μž…λ‹ˆλ‹€. 기본값은 1μž…λ‹ˆλ‹€.
*/
mutating func decreaseCount(with value: Int=1) {
func decreaseCount(with value: Int=1) -> Self? {
guard currentCount - value >= 0 else {
self.currentCount = 0
return
return nil
}
self.currentCount -= value
return .init(groupId: groupId, uuid: uuid, date: date, title: title, currentCount: currentCount-value, totalCount: totalCount)
}

static func == (lhs: Self, rhs: Self) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class FriendViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

questViewDelegate = QuestViewDelegate(header: questViewHeader)
questViewDelegate = QuestViewDelegate(header: questViewHeader, type: .friend)
questView.delegate = questViewDelegate

configureUI()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ final class HomeViewController: UIViewController {
private var disposableBag = DisposeBag()
private var questViewDelegate: QuestViewDelegate?

private var itemDidLongClick = PublishSubject<Quest>()
private var itemDidDeleteClicked: PublishSubject<Quest>!
Comment on lines +26 to +27
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

itemDidDeleteClickedκ°€ μ˜΅μ…”λ„μΈ μ΄μœ λŠ” λ¬΄μ—‡μΈκ°€μš”??


// MARK: - Components
private lazy var scrollView: UIScrollView = {
return UIScrollView()
Expand All @@ -45,7 +48,6 @@ final class HomeViewController: UIViewController {

private lazy var questView: QuestView = {
let questView = QuestView()

return questView
}()

Expand All @@ -72,8 +74,8 @@ final class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

questViewDelegate = QuestViewDelegate(header: questViewHeader)

questViewDelegate = QuestViewDelegate(header: questViewHeader, type: .home)
self.itemDidDeleteClicked = questViewDelegate?.itemDidDeleteClicked
questView.delegate = questViewDelegate

view.backgroundColor = .white
Expand Down Expand Up @@ -155,6 +157,8 @@ final class HomeViewController: UIViewController {
input: HomeViewModel.Input(
viewDidLoad: viewDidLoad,
itemDidClicked: itemDidClick,
itemDidLongClicked: itemDidLongClick.asObservable(),
itemDidDeleteClicked: itemDidDeleteClicked,
profileButtonDidClicked: statusView.profileButtonDidClick,
dragEventInCalendar: dragEventInCalendar,
daySelected: daySelected
Expand Down Expand Up @@ -229,6 +233,12 @@ final class HomeViewController: UIViewController {
.data
.drive(questView.rx.items(cellIdentifier: QuestCell.reuseIdentifier, cellType: QuestCell.self)) { row, item, cell in
cell.setup(with: item)
cell.isUserInteractionEnabled = true

let recognizer = UILongPressGestureRecognizer(target: self,action: #selector(self.longPress))
recognizer.allowableMovement = 0
cell.layer.setValue(item, forKey: "item")
cell.addGestureRecognizer(recognizer)
}
.disposed(by: disposableBag)

Expand All @@ -239,6 +249,13 @@ final class HomeViewController: UIViewController {
.disposed(by: disposableBag)
}

@objc func longPress(sender: UILongPressGestureRecognizer) {
if sender.state == .ended {
guard let quest = sender.view?.layer.value(forKey: "item") as? Quest else { return }
itemDidLongClick.onNext(quest)
}
}

private func bindToStatusView(with output: HomeViewModel.Output) {
output
.profileTapResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class HomeViewModel {
struct Input {
let viewDidLoad: Observable<Date>
let itemDidClicked: Observable<Quest>
let itemDidLongClicked: Observable<Quest>
let itemDidDeleteClicked: Observable<Quest>
let profileButtonDidClicked: Observable<Void>
let dragEventInCalendar: Observable<CalendarView.ScrollDirection>
let daySelected: Observable<Date>
Expand All @@ -51,6 +53,24 @@ final class HomeViewModel {
.share()
.asObservable()


let updatedDown = input
.itemDidLongClicked
.compactMap { $0.decreaseCount() }
.flatMap(questUseCase.update(with:))
.filter({ $0 })
.compactMap { [weak self] _ in self?.currentDate }
.share()
.asObservable()

let updatedDelete = input
.itemDidDeleteClicked
.flatMap(questUseCase.delete(with:))
.filter({$0})
.compactMap { [weak self] _ in self?.currentDate }
.share()
.asObservable()

let questUpdateNotification = NotificationCenter
.default
.rx
Expand Down Expand Up @@ -87,6 +107,8 @@ final class HomeViewModel {
let data = Observable
.merge(
updated,
updatedDown,
updatedDelete,
input.viewDidLoad,
notification,
input.daySelected
Expand Down Expand Up @@ -114,6 +136,8 @@ final class HomeViewModel {
let questStatus = Observable
.merge(
updated,
updatedDown,
updatedDelete,
input.viewDidLoad,
notification)
.map { _ in Date() }
Expand Down