130 lines
4.7 KiB
Swift
130 lines
4.7 KiB
Swift
//
|
||
// 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)
|
||
}
|
||
}
|
||
}
|
||
|