Skip to content

Commit

Permalink
implement search field
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Jun Kit committed Jun 19, 2021
1 parent c3f7bd3 commit 14cd2c7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Spottie.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
475798D5266F1B9B00AADF2F /* WebAPIPlaylistTracksRefObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 475798D4266F1B9B00AADF2F /* WebAPIPlaylistTracksRefObject.swift */; };
475EE242267C9306007BEBDC /* ShortcutGrid.swift in Sources */ = {isa = PBXBuildFile; fileRef = 475EE241267C9306007BEBDC /* ShortcutGrid.swift */; };
475EE244267D6659007BEBDC /* ShortcutItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 475EE243267D6659007BEBDC /* ShortcutItem.swift */; };
475EE248267D7784007BEBDC /* SearchField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 475EE247267D7784007BEBDC /* SearchField.swift */; };
47C56F602679A789003EA20A /* PlayerCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C56F5F2679A789003EA20A /* PlayerCommands.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -139,6 +140,7 @@
475798D4266F1B9B00AADF2F /* WebAPIPlaylistTracksRefObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebAPIPlaylistTracksRefObject.swift; sourceTree = "<group>"; };
475EE241267C9306007BEBDC /* ShortcutGrid.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutGrid.swift; sourceTree = "<group>"; };
475EE243267D6659007BEBDC /* ShortcutItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutItem.swift; sourceTree = "<group>"; };
475EE247267D7784007BEBDC /* SearchField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchField.swift; sourceTree = "<group>"; };
47C56F5F2679A789003EA20A /* PlayerCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerCommands.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -318,6 +320,7 @@
474BAFC2266B85440006EB16 /* CarouselRowItem.swift */,
475EE241267C9306007BEBDC /* ShortcutGrid.swift */,
475EE243267D6659007BEBDC /* ShortcutItem.swift */,
475EE247267D7784007BEBDC /* SearchField.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -465,6 +468,7 @@
470201CA265CF9380030ECA9 /* ShuffleButton.swift in Sources */,
4730619326591EE1001E3A1F /* TrackChangedEvent.swift in Sources */,
475798CD266F0F7F00AADF2F /* WebAPIPlaylistObject.swift in Sources */,
475EE248267D7784007BEBDC /* SearchField.swift in Sources */,
4730619526591EF9001E3A1F /* PlaybackResumedEvent.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
44 changes: 44 additions & 0 deletions Spottie/Views/Components/SearchField.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// SearchField.swift
// Spottie
//
// Created by Lee Jun Kit on 19/6/21.
//

import SwiftUI

// https://stackoverflow.com/questions/64376948/swiftui-how-to-use-nssearchtoolbaritem-on-macos-11
struct SearchField: NSViewRepresentable {
class Coordinator: NSObject, NSSearchFieldDelegate {
var parent: SearchField

init(_ parent: SearchField) {
self.parent = parent
}

func controlTextDidChange(_ notification: Notification) {
guard let searchField = notification.object as? NSSearchField else {
print("Unexpected control in update notification")
return
}
self.parent.search = searchField.stringValue
}

}

@Binding var search: String

func makeNSView(context: Context) -> NSSearchField {
NSSearchField(frame: .zero)
}

func updateNSView(_ searchField: NSSearchField, context: Context) {
searchField.stringValue = search
searchField.delegate = context.coordinator
}

func makeCoordinator() -> Coordinator {
return Coordinator(self)
}

}
7 changes: 7 additions & 0 deletions Spottie/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum Screen: Hashable {

struct ContentView<M: PlayerStateProtocol>: View {
@State var screen: Screen? = .home
@State var searchText = ""

var body: some View {
VStack {
Expand All @@ -24,6 +25,12 @@ struct ContentView<M: PlayerStateProtocol>: View {
.frame(height: 66)
.padding()
}
.toolbar {
ToolbarItem {
SearchField(search: $searchText)
.frame(minWidth: 100, idealWidth: 200, maxWidth: .infinity)
}
}
}
}

Expand Down

0 comments on commit 14cd2c7

Please sign in to comment.