Skip to content

Commit

Permalink
Merge pull request #432 from FengzihangCode/main
Browse files Browse the repository at this point in the history
重写Widget
  • Loading branch information
FengzihangCode authored Dec 28, 2024
1 parent f70e0b8 commit 248b6d0
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 100 deletions.
4 changes: 0 additions & 4 deletions DarockBili.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@
B2B813972CC3D22800C69D17 /* BiliBiliAPIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B813962CC3D22800C69D17 /* BiliBiliAPIService.swift */; };
B2B813982CC3D22800C69D17 /* BiliBiliAPIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B813962CC3D22800C69D17 /* BiliBiliAPIService.swift */; };
B2B813992CC3D22800C69D17 /* BiliBiliAPIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B813962CC3D22800C69D17 /* BiliBiliAPIService.swift */; };
B2B8139D2CC3D37500C69D17 /* TrendingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B8139A2CC3D37500C69D17 /* TrendingView.swift */; };
B4DAF0DD2B80725800755F0C /* LinkDetectText.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4DAF0DB2B80725800755F0C /* LinkDetectText.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -433,7 +432,6 @@
B2B8138B2CC3D0F300C69D17 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
B2B8138D2CC3D0F300C69D17 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B2B813962CC3D22800C69D17 /* BiliBiliAPIService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BiliBiliAPIService.swift; sourceTree = "<group>"; };
B2B8139A2CC3D37500C69D17 /* TrendingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrendingView.swift; sourceTree = "<group>"; };
B4468A152B4FC24A002CCEB2 /* Dynamic_Feed_All.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Dynamic_Feed_All.json; sourceTree = "<group>"; };
B4468A162B4FC24A002CCEB2 /* Search_With_UP_V2.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Search_With_UP_V2.json; sourceTree = "<group>"; };
B4DAF0DB2B80725800755F0C /* LinkDetectText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkDetectText.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -817,7 +815,6 @@
children = (
B2B813832CC3D0ED00C69D17 /* MeowWidgetBundle.swift */,
B2B813872CC3D0ED00C69D17 /* MeowWidget.swift */,
B2B8139A2CC3D37500C69D17 /* TrendingView.swift */,
B2B8138B2CC3D0F300C69D17 /* Assets.xcassets */,
B2B8138D2CC3D0F300C69D17 /* Info.plist */,
);
Expand Down Expand Up @@ -1403,7 +1400,6 @@
buildActionMask = 2147483647;
files = (
B2B813842CC3D0ED00C69D17 /* MeowWidgetBundle.swift in Sources */,
B2B8139D2CC3D37500C69D17 /* TrendingView.swift in Sources */,
B2B813882CC3D0ED00C69D17 /* MeowWidget.swift in Sources */,
B2B813992CC3D22800C69D17 /* BiliBiliAPIService.swift in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1600"
version = "1.7">
version = "1.8">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES"
askForAppToLaunch = "Yes"
launchAutomaticallySubstyle = "2">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
157 changes: 157 additions & 0 deletions MeowWidget/MeowWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,162 @@
//
//===----------------------------------------------------------------------===//

import WidgetKit
import SwiftUI

struct MeowWidgetEntry: TimelineEntry {
let date: Date
let video: Video
}

struct Provider: TimelineProvider {
func placeholder(in context: Context) -> MeowWidgetEntry {
MeowWidgetEntry(date: Date(), video: Video(id: 0, title: "miku miku oo ee oo", description: "https://twitter.com/i/status/1697029186777706544 channel(twi:_CASTSTATION)", authorName: "未来de残像", viewCount: 0, likeCount: 0, coinCount: 0, shareCount: 0, danmakuCount: 0))
}

func getSnapshot(in context: Context, completion: @escaping (MeowWidgetEntry) -> Void) {
let placeholder = MeowWidgetEntry(date: Date(), video: Video(id: 0, title: "miku miku oo ee o", description: "https://twitter.com/i/status/1697029186777706544 channel(twi:_CASTSTATION)", authorName: "未来de残像", viewCount: 0, likeCount: 0, coinCount: 0, shareCount: 0, danmakuCount: 0))
completion(placeholder)
}

func getTimeline(in context: Context, completion: @escaping (Timeline<MeowWidgetEntry>) -> Void) {
BiliBiliAPIService.shared.fetchPopularVideos { videos in
let entries: [MeowWidgetEntry] = videos.enumerated().map { index, video in
let interval = 10 * 60 // 每10分钟更新
let date = Calendar.current.date(byAdding: .second, value: interval * index, to: Date()) ?? Date()
return MeowWidgetEntry(date: date, video: video)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
}

struct MeowWidgetView: View {
@Environment(\.widgetFamily) var family
var entry: MeowWidgetEntry

var body: some View {
let widgetURL = URL(string: "wget://openURL/\(entry.video.id)")
switch family {
case .accessoryInline:
Text(entry.video.title)
.widgetURL(widgetURL)
case .accessoryCircular:
VStack {
Image(systemName: "play.circle.fill")
.foregroundColor(Color("WidgetTitleColor"))
Text(entry.video.title)
.font(.caption)
}
.widgetURL(widgetURL)
case .accessoryRectangular:
VStack(alignment: .leading) {
Text(entry.video.title)
.font(.headline)
Text(entry.video.authorName)
.font(.subheadline)
}
.widgetURL(widgetURL)
case .systemSmall:
VStack {
Text(entry.video.title)
.font(.headline)
HStack {
Image(systemName: "person.fill")
.foregroundColor(Color("WidgetTitleColor"))
Text(entry.video.authorName)
.font(.caption)
}
}
.widgetURL(widgetURL)
case .systemMedium:
HStack {
VStack(alignment: .leading) {
Text(entry.video.title)
.font(.headline)
Text(entry.video.description)
.font(.caption)
.lineLimit(2)
}
Spacer()
VStack(alignment: .leading) {
HStack {
Image(systemName: "play.rectangle")
.foregroundColor(Color("WidgetTitleColor"))
Text("\(entry.video.viewCount)")
.font(.caption)
}
HStack {
Image(systemName: "heart.fill")
.foregroundColor(Color("WidgetTitleColor"))
Text("\(entry.video.likeCount)")
.font(.caption)
}
}
}
.widgetURL(widgetURL)
case .systemLarge:
VStack(alignment: .leading) {
Text(entry.video.title)
.font(.title)
Text(entry.video.description)
.font(.body)
.lineLimit(3)
Spacer()
HStack {
HStack {
Image(systemName: "play.rectangle.fill")
.foregroundColor(Color("WidgetTitleColor"))
Text("\(entry.video.viewCount)")
.font(.footnote)
}
Spacer()
HStack {
Image(systemName: "heart.fill")
.foregroundColor(Color("WidgetTitleColor"))
Text("\(entry.video.likeCount)")
.font(.footnote)
}
}
Spacer()
Text("在喵哩喵哩查看视频")
.font(.footnote)
.foregroundColor(.gray)
}
.widgetURL(widgetURL)
default:
Text("Unsupported Widget Family")
}
}
}

private var families: [WidgetFamily] {
#if os(watchOS)
return [.accessoryInline, .accessoryCircular, .accessoryRectangular]
#else
return [.systemSmall, .systemMedium, .systemLarge, .accessoryCircular, .accessoryRectangular]
#endif
}

struct MeowWidget: Widget {
let kind: String = "MeowWidget"

var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
MeowWidgetView(entry: entry)
}
.configurationDisplayName("MeowWidget")
.description("热门或推荐的视频内容")
.supportedFamilies([.accessoryInline, .accessoryCircular, .accessoryRectangular, .systemSmall, .systemMedium, .systemLarge])
}
}


/*

! The older version for reference in few future updates, will be removed soon after more UI and function intergration

import WidgetKit
import SwiftUI
import Intents
Expand Down Expand Up @@ -163,3 +319,4 @@ struct MeowWidget: Widget {
#endif
}
}
*/
1 change: 1 addition & 0 deletions MeowWidget/MeowWidgetBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import SwiftUI

@main
struct MeowWidgetBundle: WidgetBundle {
@WidgetBundleBuilder
var body: some Widget {
MeowWidget()
}
Expand Down
60 changes: 0 additions & 60 deletions MeowWidget/TrendingView.swift

This file was deleted.

Loading

0 comments on commit 248b6d0

Please sign in to comment.