Skip to content

Commit 938ecc0

Browse files
access control improvements, cleanup, show how to check selected index
1 parent 102d207 commit 938ecc0

File tree

5 files changed

+26
-46
lines changed

5 files changed

+26
-46
lines changed

BlobMenu.xcodeproj/project.pbxproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@
125125
393AAE86246053B50059752A /* Views */ = {
126126
isa = PBXGroup;
127127
children = (
128-
393AAE87246053B50059752A /* StickyEffectView.swift */,
129-
393AAE88246053B50059752A /* HamburgerView.swift */,
130-
393AAE89246053B50059752A /* MenuItemView.swift */,
131128
393AAE8A246053B50059752A /* BlobMenuView.swift */,
132129
393AAE8B246053B50059752A /* BackgroundView.swift */,
130+
393AAE87246053B50059752A /* StickyEffectView.swift */,
133131
393AAE8C246053B50059752A /* StickyPathGenerator.swift */,
132+
393AAE88246053B50059752A /* HamburgerView.swift */,
133+
393AAE89246053B50059752A /* MenuItemView.swift */,
134134
393AAE8D246053B50059752A /* Theme.swift */,
135135
);
136136
path = Views;

Example/Example/Screens/ContentView.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ import SwiftUI
1010
import BlobMenu
1111

1212
struct ContentView: View {
13+
14+
@Environment(\.blobMenuEnvironment) var menuEnvironment: BlobMenuEnvironment
15+
16+
1317
var body: some View {
14-
BlobMenuView.createMenu(items: MenuItem.all)
18+
VStack {
19+
BlobMenuView.createMenu(items: MenuItem.all)
20+
}.onReceive(menuEnvironment.$selectedIndex) { index in
21+
print("index: \(index)")
22+
}
1523
}
1624
}
1725

Sources/Models/BlobMenuEnvironment.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99
import Foundation
1010
import SwiftUI
1111

12-
extension EnvironmentValues {
12+
public extension EnvironmentValues {
1313
var blobMenuEnvironment: BlobMenuEnvironment {
1414
get { return self[BlobMenuEnvironmentKey.self] }
1515
set { self[BlobMenuEnvironmentKey.self] = newValue }
1616
}
1717
}
1818

19-
public struct BlobMenuEnvironmentKey: EnvironmentKey {
20-
public static let defaultValue: BlobMenuEnvironment = BlobMenuEnvironment()
19+
struct BlobMenuEnvironmentKey: EnvironmentKey {
20+
static let defaultValue: BlobMenuEnvironment = BlobMenuEnvironment()
2121
}
2222

2323
public final class BlobMenuEnvironment: ObservableObject {
24-
@Published var isOpened: Bool = false
25-
@Published var isMenuItemsVisible: Bool = false
24+
@Published public internal(set) var isOpened: Bool = false
25+
@Published public internal(set) var isMenuItemsVisible: Bool = false
26+
@Published public internal(set) var selectedIndex: Int = 0
2627

2728
fileprivate init() {}
2829
}

Sources/Utilities/AnimationCompletion.swift

-33
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,6 @@
99
import Foundation
1010
import SwiftUI
1111

12-
/*
13-
struct AnimationCompletion: GeometryEffect {
14-
15-
/// Ratio should be in range [0...1]
16-
var progress: CGFloat = 0
17-
18-
var onCompletion: () -> Void
19-
20-
var animatableData: CGFloat {
21-
get { return progress }
22-
set { progress = newValue }
23-
}
24-
25-
func effectValue(size: CGSize) -> ProjectionTransform {
26-
if self.progress == 1 {
27-
DispatchQueue.main.async {
28-
self.onCompletion()
29-
}
30-
}
31-
32-
return ProjectionTransform()
33-
}
34-
}
35-
36-
extension View {
37-
func onAnimationCompleted(condition: Bool, callback: @escaping () -> Void) -> some View {
38-
let m = AnimationCompletion(progress: condition ? 1 : 0, onCompletion: callback)
39-
return AnyView(self.modifier(m))
40-
}
41-
}
42-
*/
43-
44-
4512
private struct CompletionPreferenceKey: PreferenceKey {
4613
typealias Value = CGFloat
4714

Sources/Views/BlobMenuView.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import SwiftUI
1010

1111
public struct BlobMenuView: View {
1212

13-
@EnvironmentObject var environment: BlobMenuEnvironment
14-
@State var selectedIndex: Int = 0
13+
@State public var selectedIndex: Int
1514

15+
@EnvironmentObject private var environment: BlobMenuEnvironment
1616
private let items: [MenuItem]
1717

1818
public static func createMenu(items: [MenuItem], selectedIndex: Int = 0) -> some View {
@@ -21,7 +21,11 @@ public struct BlobMenuView: View {
2121

2222
private init(items: [MenuItem], selectedIndex: Int = 0) {
2323
self.items = items
24-
self.selectedIndex = selectedIndex.limited(0, items.count - 1)
24+
25+
let limitedIndex = selectedIndex.limited(0, items.count - 1)
26+
_selectedIndex = State<Int>.init(initialValue: limitedIndex)
27+
28+
BlobMenuEnvironmentKey.defaultValue.selectedIndex = limitedIndex
2529

2630
UIWindow.current?.addGesture(type: .tap) { _ in
2731
BlobMenuEnvironmentKey.defaultValue.isMenuItemsVisible = false
@@ -69,7 +73,6 @@ public struct BlobMenuView: View {
6973

7074
return AnyView(effectView.fill(Color.backgroundColor)
7175
.frame(size: CGSize(width: w, height: f.height)))
72-
//.position(CGPoint(x: b.maxX + w / 2 - r, y: f.height / 2))
7376
}
7477

7578
private var background: some View {
@@ -107,6 +110,7 @@ public struct BlobMenuView: View {
107110
MenuItemView(item: item, isSelected: self.selectedIndex == index, isOpened: self.$environment.isMenuItemsVisible).onTapGesture {
108111

109112
self.selectedIndex = index
113+
BlobMenuEnvironmentKey.defaultValue.selectedIndex = index
110114
}
111115
}
112116
}

0 commit comments

Comments
 (0)