Skip to content

Commit

Permalink
Add Double tap action handler
Browse files Browse the repository at this point in the history
  • Loading branch information
varkrishna committed Mar 16, 2024
1 parent 310f767 commit df1a57e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
62 changes: 40 additions & 22 deletions Sources/JSONViewer/JSONNodeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,29 @@

import SwiftUI

public enum JSONNodeActionEvents {
case onDoubleTap(node: JSONNode)
}

enum JSONNodeActionInternalEvents {
case onToggle(node: JSONNode)
case onDoubleTap(node: JSONNode)
}

public struct JSONNodeView: View {
let node: JSONNode
let level: Int
private var initialNodeExpandStategy: InitialNodeExpandStrategy = .root
@Binding var fontConfiguration: JSONViewerFontConfiguration
@State var expandedNodes: [String: Bool]
var actionHandler: ((JSONNodeActionEvents) -> Void)?

internal init(node: JSONNode, level: Int, fontConfiguration: Binding<JSONViewerFontConfiguration>, initialNodeExpandStategy: InitialNodeExpandStrategy) {
internal init(node: JSONNode, level: Int, fontConfiguration: Binding<JSONViewerFontConfiguration>, initialNodeExpandStategy: InitialNodeExpandStrategy, actionHandler: ((JSONNodeActionEvents) -> Void)? = nil) {
self.node = node
self.level = level
self._fontConfiguration = fontConfiguration
self.initialNodeExpandStategy = initialNodeExpandStategy

self.actionHandler = actionHandler
if initialNodeExpandStategy == .root && level == 0 {
_expandedNodes = State(initialValue: ["Root": true])
} else if initialNodeExpandStategy == .all {
Expand All @@ -32,11 +42,14 @@ public struct JSONNodeView: View {
public var body: some View {
VStack {
if node.isExpandable {
ExpandableJSONNodeView(fontConfiguration: $fontConfiguration,
node: node,
level: level,
isExpanded: isExpandedNode(),
toggleActionHandler: toggleNodeState)
ExpandableJSONNodeView(fontConfiguration: $fontConfiguration, node: node, level: level, isExpanded: isExpandedNode()) { event in
switch event {
case .onToggle(let _):
toggleNodeState()
case .onDoubleTap(let node):
actionHandler?(.onDoubleTap(node: node))
}
}
} else {
NonExpandableJSONNodeView(node: node,
fontConfiguration: fontConfiguration,
Expand Down Expand Up @@ -68,30 +81,37 @@ private struct ExpandableJSONNodeView: View {
let node: JSONNode
let level: Int
let isExpanded: Bool
let toggleActionHandler: () -> Void
let actionHandler: (JSONNodeActionInternalEvents) -> Void

var body: some View {
VStack(alignment: .trailing) {
HStack {
Spacer()
.frame(width: 32 * CGFloat(level))
Button {
toggleActionHandler()
} label: {
HStack {
nodeToggleButtonIcon()
expandableNodeTypeLabel()
Text(node.key)
.font(fontConfiguration.keyFont)
}
HStack {
nodeToggleButtonIcon()
.onTapGesture {
actionHandler(.onToggle(node: node))
}
expandableNodeTypeLabel()
Text(node.key)
.font(fontConfiguration.keyFont)
.onTapGesture(count: 2, perform: {
actionHandler(.onDoubleTap(node: node))
})
}
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.buttonStyle(PlainButtonStyle())

if isExpanded {
JSONNodeSuccessorView(fontConfiguration: $fontConfiguration, node: node, level: level)
JSONNodeSuccessorView(fontConfiguration: $fontConfiguration, node: node, level: level) { event in
switch event {
case .onDoubleTap(let node):
actionHandler(.onDoubleTap(node: node))
}
}
}
}
}
Expand Down Expand Up @@ -135,6 +155,7 @@ private struct JSONNodeSuccessorView: View {
let node: JSONNode
let level: Int
let initialNodeExpandStategy: InitialNodeExpandStrategy = .root
let actionHandler: (JSONNodeActionEvents) -> Void

var body: some View {
VStack(alignment: .trailing, spacing: 8) {
Expand All @@ -143,7 +164,7 @@ private struct JSONNodeSuccessorView: View {
JSONNodeView(node: childNode,
level: level + 1,
fontConfiguration: $fontConfiguration,
initialNodeExpandStategy: self.initialNodeExpandStategy)
initialNodeExpandStategy: self.initialNodeExpandStategy, actionHandler: self.actionHandler)
}
}
}
Expand Down Expand Up @@ -179,9 +200,6 @@ private struct JSONNodeViewDot: View {
.font(fontConfiguration.keyFont)
.frame(minWidth: 8, minHeight: 8)
}
.onAppear(perform: {

})
}
}

Expand Down
9 changes: 6 additions & 3 deletions Sources/JSONViewer/JSONViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ public struct JSONViewer: View {
@Binding var fontConfiguration: JSONViewerFontConfiguration
private let rootNode: JSONNode
private var initialNodeExpandStategy: InitialNodeExpandStrategy = .root
private var actionHandler: ((JSONNodeActionEvents) -> Void)? = nil

public init(rootNode: JSONNode, initialNodeExpandStategy: InitialNodeExpandStrategy = .root) {
public init(rootNode: JSONNode, initialNodeExpandStategy: InitialNodeExpandStrategy = .root, actionHandler: ((JSONNodeActionEvents) -> Void)? = nil) {
self.rootNode = rootNode
self.initialNodeExpandStategy = initialNodeExpandStategy
self.actionHandler = actionHandler
self._fontConfiguration = Binding.constant(JSONViewerFontConfiguration())
}

public init(rootNode: JSONNode, fontConfiguration: Binding<JSONViewerFontConfiguration>, initialNodeExpandStategy: InitialNodeExpandStrategy = .root) {
public init(rootNode: JSONNode, fontConfiguration: Binding<JSONViewerFontConfiguration>, initialNodeExpandStategy: InitialNodeExpandStrategy = .root, actionHandler: ((JSONNodeActionEvents) -> Void)? = nil) {
self.rootNode = rootNode
self.initialNodeExpandStategy = initialNodeExpandStategy
self._fontConfiguration = fontConfiguration
self.actionHandler = actionHandler
}

public var body: some View {
Expand All @@ -24,7 +27,7 @@ public struct JSONViewer: View {
JSONNodeView(node: rootNode,
level: 0,
fontConfiguration: $fontConfiguration,
initialNodeExpandStategy: self.initialNodeExpandStategy)
initialNodeExpandStategy: self.initialNodeExpandStategy, actionHandler: self.actionHandler)
}
.scrollIndicators(.hidden)
Spacer()
Expand Down

0 comments on commit df1a57e

Please sign in to comment.