Skip to content

Commit

Permalink
Merge pull request #376 from rakuyoMo/feature/tap-to-create-new-event
Browse files Browse the repository at this point in the history
Allows creation of event to be triggered by tapping instead of long pressing
  • Loading branch information
kvyatkovskys authored Sep 5, 2024
2 parents ccabe5c + 44da545 commit d7f83cc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
12 changes: 6 additions & 6 deletions Sources/KVKCalendar/EventViewGeneral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ open class EventViewGeneral: UIView, CalendarTimer {
}
}

@objc public func editEvent(gesture: UILongPressGestureRecognizer) {
@objc public func editEvent(gesture: UIGestureRecognizer) {
let location = gesture.location(in: self)

switch gesture.state {
Expand Down Expand Up @@ -197,11 +197,11 @@ extension EventViewGeneral {

protocol EventDelegate: AnyObject {

func didStartResizeEvent(_ event: Event, gesture: UILongPressGestureRecognizer, view: UIView)
func didEndResizeEvent(_ event: Event, gesture: UILongPressGestureRecognizer)
func didStartMovingEvent(_ event: Event, gesture: UILongPressGestureRecognizer, view: UIView)
func didEndMovingEvent(_ event: Event, gesture: UILongPressGestureRecognizer)
func didChangeMovingEvent(_ event: Event, gesture: UILongPressGestureRecognizer)
func didStartResizeEvent(_ event: Event, gesture: UIGestureRecognizer, view: UIView)
func didEndResizeEvent(_ event: Event, gesture: UIGestureRecognizer)
func didStartMovingEvent(_ event: Event, gesture: UIGestureRecognizer, view: UIView)
func didEndMovingEvent(_ event: Event, gesture: UIGestureRecognizer)
func didChangeMovingEvent(_ event: Event, gesture: UIGestureRecognizer)
func didSelectEvent(_ event: Event, gesture: UITapGestureRecognizer)
func deselectEvent(_ event: Event)

Expand Down
8 changes: 8 additions & 0 deletions Sources/KVKCalendar/Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ public struct TimelineStyle {
public var minimumPressDuration: TimeInterval = 0.5
public var isHiddenStubEvent: Bool = true
public var isEnabledCreateNewEvent: Bool = true
public var isEnabledForceDeselectEvent: Bool = true
public var isEnabledDefaultTapGestureRecognizer: Bool = true
public var createNewEventMethod: CreateNewEventMethod = .longTap
public var maxLimitCachedPages: UInt = 10
public var scrollDirections: Set<ScrollDirectionType> = Set(ScrollDirectionType.allCases)
public var dividerType: DividerType? = nil
Expand Down Expand Up @@ -275,6 +277,10 @@ public struct TimelineStyle {
}
}
}

public enum CreateNewEventMethod: Equatable {
case tap, longTap
}
}

// MARK: Week style
Expand Down Expand Up @@ -897,6 +903,8 @@ extension TimelineStyle: Equatable {
&& compare(\.timeDividerFont)
&& compare(\.scale)
&& compare(\.createEventAtTouch)
&& compare(\.createNewEventMethod)
&& compare(\.isEnabledForceDeselectEvent)
}

}
Expand Down
29 changes: 21 additions & 8 deletions Sources/KVKCalendar/Timeline+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,20 @@ extension TimelineView {
}
}

@objc func forceDeselectEvent() {
@objc func handleDefaultTapGesture(gesture: UITapGestureRecognizer) {
// Record before unchecking
let hasCreateEvent = events.contains { $0.isNew }

if style.timeline.isEnabledForceDeselectEvent {
forceDeselectEvent()
}

if style.timeline.isEnabledCreateNewEvent && style.timeline.createNewEventMethod == .tap && !hasCreateEvent {
addNewEvent(gesture: gesture)
}
}

func forceDeselectEvent() {
removeEventResizeView()

guard let eventViewGeneral = scrollView.subviews.first(where: { ($0 as? EventViewGeneral)?.isSelected == true }) as? EventViewGeneral else { return }
Expand Down Expand Up @@ -408,7 +421,7 @@ extension TimelineView {
}
}

@objc func addNewEvent(gesture: UILongPressGestureRecognizer) {
@objc func addNewEvent(gesture: UIGestureRecognizer) {
var point = gesture.location(in: scrollView)
if style.timeline.createEventAtTouch && !style.event.states.contains(.move) {
let offset = eventPreviewYOffset - style.timeline.offsetEvent - 6
Expand Down Expand Up @@ -576,7 +589,7 @@ extension TimelineView: EventDelegate {
delegate?.didSelectEvent(event, frame: gesture.view?.frame)
}

func didStartResizeEvent(_ event: Event, gesture: UILongPressGestureRecognizer, view: UIView) {
func didStartResizeEvent(_ event: Event, gesture: UIGestureRecognizer, view: UIView) {
forceDeselectEvent()
isResizableEventEnable = true

Expand Down Expand Up @@ -610,11 +623,11 @@ extension TimelineView: EventDelegate {
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}

func didEndResizeEvent(_ event: Event, gesture: UILongPressGestureRecognizer) {
func didEndResizeEvent(_ event: Event, gesture: UIGestureRecognizer) {
removeEventResizeView()
}

func didStartMovingEvent(_ event: Event, gesture: UILongPressGestureRecognizer, view: UIView) {
func didStartMovingEvent(_ event: Event, gesture: UIGestureRecognizer, view: UIView) {
removeEventResizeView()
let location = gesture.location(in: scrollView)

Expand Down Expand Up @@ -659,7 +672,7 @@ extension TimelineView: EventDelegate {
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}

func didEndMovingEvent(_ event: Event, gesture: UILongPressGestureRecognizer) {
func didEndMovingEvent(_ event: Event, gesture: UIGestureRecognizer) {
eventPreview?.removeFromSuperview()
eventPreview = nil
movingMinuteLabel.removeFromSuperview()
Expand Down Expand Up @@ -692,7 +705,7 @@ extension TimelineView: EventDelegate {
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}

func didChangeMovingEvent(_ event: Event, gesture: UILongPressGestureRecognizer) {
func didChangeMovingEvent(_ event: Event, gesture: UIGestureRecognizer) {
let location = gesture.location(in: scrollView)
guard scrollView.frame.width >= (location.x + 20) &&
(location.x - 20) >= style.timeline.allLeftOffset else { return }
Expand Down Expand Up @@ -783,7 +796,7 @@ extension TimelineView: CalendarSettingProtocol {
scrollView.isScrollEnabled = style.timeline.scrollDirections.contains(.vertical)

tapGestureRecognizer.isEnabled = style.timeline.isEnabledDefaultTapGestureRecognizer
longTapGestureRecognizer.isEnabled = style.timeline.isEnabledCreateNewEvent
longTapGestureRecognizer.isEnabled = style.timeline.isEnabledCreateNewEvent && style.timeline.createNewEventMethod == .longTap
longTapGestureRecognizer.minimumPressDuration = style.timeline.minimumPressDuration
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/KVKCalendar/TimelineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public final class TimelineView: UIView, EventDateProtocol, CalendarTimer {
return scroll
}()

private(set) lazy var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(forceDeselectEvent))
private(set) lazy var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDefaultTapGesture(gesture:)))

private(set) lazy var longTapGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(addNewEvent))

init(parameters: Parameters, frame: CGRect) {
Expand Down

0 comments on commit d7f83cc

Please sign in to comment.