mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 16:05:18 +00:00

Add features to display current playing track information and support media controls on iOS. * **HomePlayerWidget.swift** - Add track title, artist name, and album art display in the HomePlayerWidget. - Update SimpleEntry struct to include trackTitle, artistName, and albumArt properties. - Modify placeholder, getSnapshot, and getTimeline methods to include new properties. - Update HomePlayerWidgetEntryView to display new track information. * **Info.plist** - Add support for media controls on the lock screen and control center. - Add integration with Siri for voice commands to control playback. * **main.dart** - Register the HomePlayerWidget for iOS by adding glanceProvider listener. * **glance.dart** - Add code to update the HomePlayerWidget with the current track information, including track title, artist name, and album art.
89 lines
2.8 KiB
Swift
89 lines
2.8 KiB
Swift
import WidgetKit
|
|
import SwiftUI
|
|
|
|
private let widgetGroupId = "group.spotube_home_player_widget"
|
|
|
|
struct Provider: TimelineProvider {
|
|
func placeholder(in context: Context) -> SimpleEntry {
|
|
SimpleEntry(date: Date(), emoji: "😀", trackTitle: "Track Title", artistName: "Artist Name", albumArt: UIImage())
|
|
}
|
|
|
|
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
|
|
let entry = SimpleEntry(date: Date(), emoji: "😀", trackTitle: "Track Title", artistName: "Artist Name", albumArt: UIImage())
|
|
completion(entry)
|
|
}
|
|
|
|
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
|
|
var entries: [SimpleEntry] = []
|
|
|
|
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
|
|
let currentDate = Date()
|
|
for hourOffset in 0 ..< 5 {
|
|
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
|
|
let entry = SimpleEntry(date: entryDate, emoji: "😀", trackTitle: "Track Title", artistName: "Artist Name", albumArt: UIImage())
|
|
entries.append(entry)
|
|
}
|
|
|
|
let timeline = Timeline(entries: entries, policy: .atEnd)
|
|
completion(timeline)
|
|
}
|
|
}
|
|
|
|
struct SimpleEntry: TimelineEntry {
|
|
let date: Date
|
|
let emoji: String
|
|
let trackTitle: String
|
|
let artistName: String
|
|
let albumArt: UIImage
|
|
}
|
|
|
|
struct HomePlayerWidgetEntryView : View {
|
|
var entry: Provider.Entry
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Text("Time:")
|
|
Text(entry.date, style: .time)
|
|
|
|
Text("Emoji:")
|
|
Text(entry.emoji)
|
|
|
|
Text("Track Title:")
|
|
Text(entry.trackTitle)
|
|
|
|
Text("Artist Name:")
|
|
Text(entry.artistName)
|
|
|
|
Image(uiImage: entry.albumArt)
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct HomePlayerWidget: Widget {
|
|
let kind: String = "HomePlayerWidget"
|
|
|
|
var body: some WidgetConfiguration {
|
|
StaticConfiguration(kind: kind, provider: Provider()) { entry in
|
|
if #available(iOS 17.0, *) {
|
|
HomePlayerWidgetEntryView(entry: entry)
|
|
.containerBackground(.fill.tertiary, for: .widget)
|
|
} else {
|
|
HomePlayerWidgetEntryView(entry: entry)
|
|
.padding()
|
|
.background()
|
|
}
|
|
}
|
|
.configurationDisplayName("My Widget")
|
|
.description("This is an example widget.")
|
|
}
|
|
}
|
|
|
|
#Preview(as: .systemSmall) {
|
|
HomePlayerWidget()
|
|
} timeline: {
|
|
SimpleEntry(date: .now, emoji: "😀", trackTitle: "Track Title", artistName: "Artist Name", albumArt: UIImage())
|
|
SimpleEntry(date: .now, emoji: "🤩", trackTitle: "Track Title", artistName: "Artist Name", albumArt: UIImage())
|
|
}
|