Fableon/FAWidget/FAWidgetLiveActivity.swift
湖北秦九 008d5b55b8 1
2025-11-21 09:12:46 +08:00

130 lines
4.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// FAWidgetLiveActivity.swift
// FAWidget
//
// Created by on 2025/11/10.
//
import ActivityKit
import WidgetKit
import SwiftUI
struct FAWidgetLiveActivity: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: ActivityManagerAttributes.self) { context in
// 🔹 Live Activity UI & Banner
HStack(spacing: 16) {
// 🔹
if let imageData = try? Data(contentsOf: URL(string: context.state.videoCoverPath)!),
let uiImage = UIImage(data: imageData) {
Image(uiImage: uiImage)
.resizable()
.frame(width: 108, height: 146)
.cornerRadius(8)
.clipped()
} else {
Image("logo100")
.frame(width: 100, height: 100)
.cornerRadius(8)
.clipped()
}
// 🔹
VStack(alignment: .leading, spacing: 4) {
VStack(alignment: .leading, spacing: 4) {
HStack {
Text("Playing")
.font(.caption)
.foregroundColor(.gray)
Spacer()
Image("logo40")
}
Text(context.state.videoTitle)
.font(.headline)
.foregroundColor(.black)
Text("Ep.\(context.state.videoEpisode)")
.font(.subheadline)
.foregroundColor(.gray)
}
Spacer()
// 🔹
Button(action: { print("Watch Now") }) {
HStack {
Spacer()
Image(systemName: "play.fill")
Text("Watch Now")
Spacer()
}
.padding(.horizontal, 14)
.padding(.vertical, 8)
.background(
LinearGradient(colors: [Color.red, Color.orange], startPoint: .leading, endPoint: .trailing)
)
.foregroundColor(.white)
.cornerRadius(20)
}
}
}
.padding()
.background(RoundedRectangle(cornerRadius: 8).fill(Color.white))
.activityBackgroundTint(Color.white)
.activitySystemActionForegroundColor(.primary)
.widgetURL(URL(string: "fableonapp://liveActivity?short_play_id=\(context.state.videoId)"))
} dynamicIsland: { context in
DynamicIsland {
// 🔹 Expanded UI
DynamicIslandExpandedRegion(.leading) {
VStack (alignment: .center) {
Spacer()
Image("logo40")
.resizable()
.frame(width: 40, height: 40)
.cornerRadius(5)
Spacer()
}
}
DynamicIslandExpandedRegion(.center) {
VStack (alignment: .center) {
Spacer()
Text(context.state.videoTitle)
.font(.headline)
.foregroundColor(.primary)
Spacer()
}
}
DynamicIslandExpandedRegion(.trailing) {
VStack (alignment: .center) {
Spacer()
Text("Ep.\(context.state.videoEpisode)")
.font(.subheadline)
.foregroundColor(.secondary)
Spacer()
}
}
} compactLeading: {
Image("logo40")
} compactTrailing: {
Text("Ep.\(context.state.videoEpisode)")
.font(.caption2)
} minimal: {
Image("logo40")
}
.widgetURL(URL(string: "fableonapp://liveActivity?short_play_id=\(context.state.videoId)"))
.keylineTint(Color.red)
}
}
}