Skip to content

Instantly share code, notes, and snippets.

@jverkoey
Created July 31, 2024 15:05
Show Gist options
  • Save jverkoey/2bdffa278c092474e8bcd70374641486 to your computer and use it in GitHub Desktop.
Save jverkoey/2bdffa278c092474e8bcd70374641486 to your computer and use it in GitHub Desktop.
Spotify drilldown filter
import SwiftUI
struct SpotifyToggle: View {
let value: String
@Binding var selection: String?
let didTap: () -> Void
var body: some View {
Toggle(value, isOn: .init(get: { selection == value }, set: { _ in
withAnimation {
if selection == nil {
selection = value
}
didTap()
}
}))
.toggleStyle(.button)
.tint(.white)
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.transition(.blurReplace)
}
}
struct SpotifyDrilldown: View {
@State private var selection: String?
@State private var subSelection: String?
var body: some View {
HStack {
ForEach([ "Playlists", "Artists", "Albums" ], id: \.self) { filter in
if selection == nil || filter == selection {
if selection != nil {
Button {
withAnimation {
selection = nil
subSelection = nil
}
} label: { Image(systemName: "xmark") }
.tint(.white)
.buttonStyle(.bordered)
.buttonBorderShape(.circle)
.transition(.blurReplace)
}
SpotifyToggle(value: filter, selection: $selection) {
subSelection = nil
}
let filterSubFilters = [ "Playlists": [ "By Spotify", "By you" ] ]
if selection != nil, let subFilters = filterSubFilters[filter] {
ForEach(subFilters.filter { subSelection == nil || subSelection == $0 }, id: \.self) { subFilter in
SpotifyToggle(value: subFilter, selection: $subSelection) { }
}
}
}
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
}
#Preview {
SpotifyDrilldown()
.padding()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment