Skip to content

Instantly share code, notes, and snippets.

@rudrankriyam
Created September 13, 2021 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudrankriyam/22f52d1342f18f837f15c2435a78e465 to your computer and use it in GitHub Desktop.
Save rudrankriyam/22f52d1342f18f837f15c2435a78e465 to your computer and use it in GitHub Desktop.
HomeButtonsView
extension Image {
func imageButton(with size: CGFloat, color: Color) -> some View {
self
.resizable()
.scaledToFit()
.frame(width: size, height: size)
.foregroundColor(color)
}
}
struct ButtonImageType {
static let addToLibrary = "square.and.arrow.down.fill"
static let startRecognition = "shazamIcon"
static let stopRecognition = "stop.circle.fill"
}
struct HomeButtonsView: View {
@ObservedObject var viewModel: HomeViewModel
private let size = 50.0
private var isRecognizingSong: Bool {
viewModel.isRecognizingSong
}
var body: some View {
HStack {
// 1
Button(action: { viewModel.addToShazamLibrary() }) {
Image(systemName: ButtonImageType.addToLibrary)
.imageButton(with: size, color: .green)
}
Spacer()
// 2
Button(action: { viewModel.startRecognition() }) {
Image(ButtonImageType.startRecognition)
.imageButton(with: size * 2, color: .clear)
}
.disabled(isRecognizingSong)
.scaleEffect(isRecognizingSong ? 0.8 : 1)
.animation(songRecognitionAnimation(), value: isRecognizingSong)
Spacer()
// 3
Button(action: { viewModel.stopRecognition() }) {
Image(systemName: ButtonImageType.stopRecognition)
.imageButton(with: size, color: .red)
}
}
.padding(.horizontal, 24)
}
// 4
private func songRecognitionAnimation() -> Animation {
isRecognizingSong ? .easeInOut(duration: 1.5).repeatForever() : .default
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment