Created
July 31, 2024 15:05
-
-
Save jverkoey/2bdffa278c092474e8bcd70374641486 to your computer and use it in GitHub Desktop.
Spotify drilldown filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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