Skip to content

Commit

Permalink
refactor(view): cleanup ScrobbledTrackRowView
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Dec 1, 2024
1 parent 8f1935b commit 25d7877
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 53 deletions.
3 changes: 2 additions & 1 deletion firstfm/ViewModel/ScobbledTrackViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import Foundation
import NotificationBannerSwift
import SwiftUI
import Valet
import Combine

struct Nothing: Codable {
}

class ScrobbledTrackViewModel {
class ScrobbledTrackViewModel: ObservableObject {
let valet = getValet()

func loveTrack(track: ScrobbledTrack) {
Expand Down
113 changes: 61 additions & 52 deletions firstfm/Views/Scrobbles/ScrobbledTrackRowView.swift
Original file line number Diff line number Diff line change
@@ -1,66 +1,75 @@
import SwiftUI
import Kingfisher
import SwiftUI

struct ScrobbledTrackRow: View {
@State var track: ScrobbledTrack
var vm = ScrobbledTrackViewModel()
@StateObject private var vm = ScrobbledTrackViewModel()

private let imageSize: CGFloat = 60

var body: some View {
HStack {
KFImage.url(URL(string: track.image[3].url)!)
.resizable()
.onSuccess { res in
print("Success: \(self.track.name) - \(res.cacheType)")
}
.onFailure { err in
print("Error \(self.track.name): \(err)")
}
.fade(duration: 0.5)
.cancelOnDisappear(true)
.cornerRadius(5)
.frame(width: 60, height: 60)
.padding(.trailing, 5)
trackImage
trackInfo
loveButton
}
}

VStack(alignment: .leading) {
Spacer()
HStack {
Text(track.name)
.font(.system(size: 16, weight: .semibold))
.lineLimit(1)
}
Spacer()
HStack {
Text(track.artist.name)
.font(.subheadline)
.foregroundColor(.gray)
.lineLimit(1)
Spacer()
Text(track.date?.getRelative() ?? "Now Playing")
.font(.subheadline)
.foregroundColor(.gray)
.frame(alignment: .leading)
.lineLimit(1)
}
private var trackImage: some View {
KFImage.url(URL(string: track.image[3].url))
.placeholder { ProgressView() }
.resizable()
.onSuccess { res in
print("Success: \(self.track.name) - \(res.cacheType)")
}
.onFailure { err in
print("Error \(self.track.name): \(err)")
}
.fade(duration: 0.5)
.cancelOnDisappear(true)
.cornerRadius(5)
.frame(width: imageSize, height: imageSize)
.padding(.trailing, 5)
}

private var trackInfo: some View {
VStack(alignment: .leading) {
Spacer()
Text(track.name)
.font(.system(size: 16, weight: .semibold))
.lineLimit(1)
Spacer()
HStack {
Text(track.artist.name)
.font(.subheadline)
.foregroundColor(.gray)
.lineLimit(1)
Spacer()
Text(track.date?.getRelative() ?? "Now Playing")
.font(.subheadline)
.foregroundColor(.gray)
.frame(alignment: .leading)
.lineLimit(1)
}
Button(action: {
let hapticFeedback = UIImpactFeedbackGenerator(style: .light)
if self.track.loved == "0" {
self.track.loved = "1"
vm.loveTrack(track: self.track)
} else {
self.track.loved = "0"
vm.unloveTrack(track: self.track)
}
hapticFeedback.impactOccurred()
}) {
if self.track.loved == "1" {
Image(systemName: "heart.fill").imageScale(.large).foregroundColor(Color(red: 157, green: 0, blue: 0))
} else {
Image(systemName: "heart").imageScale(.large)
}
Spacer()
}
}

private var loveButton: some View {
Button(action: {
if track.loved == "0" {
track.loved = "1"
vm.loveTrack(track: track)
} else {
track.loved = "0"
vm.unloveTrack(track: track)
}
.buttonStyle(PlainButtonStyle())
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}) {
Image(systemName: track.loved == "1" ? "heart.fill" : "heart")
.imageScale(.large)
.foregroundColor(track.loved == "1" ? Color(red: 157/255, green: 0, blue: 0) : .primary)
}
.buttonStyle(PlainButtonStyle())
}
}

0 comments on commit 25d7877

Please sign in to comment.