Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MeowWidget错误修复与优化 第二次提交 #427

Merged
merged 4 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions DarockBili.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,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 */; };
B2B8139B2CC3D37500C69D17 /* TrendingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B8139A2CC3D37500C69D17 /* TrendingView.swift */; };
B2B8139C2CC3D37500C69D17 /* TrendingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B8139A2CC3D37500C69D17 /* TrendingView.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 @@ -1251,7 +1249,6 @@
8CA7CCA32B77B2F3008E587F /* NoticeView.swift in Sources */,
B2B813972CC3D22800C69D17 /* BiliBiliAPIService.swift in Sources */,
8CA7CC952B77B0FB008E587F /* SkinChooserView.swift in Sources */,
B2B8139B2CC3D37500C69D17 /* TrendingView.swift in Sources */,
8CA7CCA62B77B315008E587F /* CommentsView.swift in Sources */,
8CA7CC6F2B77AEB4008E587F /* SearchView.swift in Sources */,
8C2DFCB02C4AA15C003AEC67 /* NewFeaturesView.swift in Sources */,
Expand Down Expand Up @@ -1320,7 +1317,6 @@
8CBFF0C02B85E55F00350E0F /* SkinDownloadView.swift in Sources */,
B2B813982CC3D22800C69D17 /* BiliBiliAPIService.swift in Sources */,
8CBFF0C82B85E55F00350E0F /* BangumiPlayerView.swift in Sources */,
B2B8139C2CC3D37500C69D17 /* TrendingView.swift in Sources */,
8CBFF0C72B85E55F00350E0F /* VideoDownloadView.swift in Sources */,
03B2F8102C45AED7007EB850 /* PhoneFormatterExt.swift in Sources */,
8CBFF0E32B85E55F00350E0F /* AboutView.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 @@ -77,8 +77,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES"
notificationPayloadFile = "Examples/SDWebImage Watch Demo Extension/PushNotificationPayload.apns">
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
Expand Down
14 changes: 0 additions & 14 deletions MeowWidget/Assets.xcassets/MeowBili.appiconset/Contents.json

This file was deleted.

Binary file not shown.

This file was deleted.

28 changes: 11 additions & 17 deletions MeowWidget/MeowWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct MeowWidgetEntry: TimelineEntry {

struct Provider: TimelineProvider {
func placeholder(in context: Context) -> MeowWidgetEntry {
MeowWidgetEntry(date: Date(), videoTitle: "视频名称", videoDescription: "描述", videoAuthor: "作者", videoViews: "播放量")
MeowWidgetEntry(date: Date(), videoTitle: "miku miku oo ee oo", videoDescription: "https://twitter.com/i/status/1697029186777706544 channel(twi:_CASTSTATION)", videoAuthor: "未来de残像", videoViews: "365.4万")
}

func getSnapshot(in context: Context, completion: @escaping (MeowWidgetEntry) -> ()) {
Expand Down Expand Up @@ -80,16 +80,13 @@ struct Provider: TimelineProvider {
}
}

struct MeowWidgetEntryView : View {
struct MeowWidgetEntryView: View {
var entry: Provider.Entry
@Environment(\.widgetFamily) var family

var body: some View {
VStack(alignment: .leading) {
HStack {
Image("MeowBili")
.resizable()
.frame(width: 24, height: 24)
Text("喵哩喵哩")
.font(.headline)
.foregroundColor(Color("WidgetTitleColor"))
Expand All @@ -99,30 +96,28 @@ struct MeowWidgetEntryView : View {

switch family {
case .systemSmall:
Text(entry.videoTitle)
Text("在喵哩喵哩查看视频")
.font(.headline)

case .systemMedium:
Text(entry.videoTitle)
.font(.headline)
Text(entry.videoDescription)
.font(.subheadline)
Spacer()
Text("在喵哩喵哩查看更多内容")
.font(.footnote)
.foregroundColor(.gray)

case .systemLarge:
Text(entry.videoTitle)
.font(.headline)
Text(entry.videoDescription)
.font(.subheadline)
Text("作者: \(entry.videoAuthor)")
.font(.footnote)
Text("播放量: \(entry.videoViews)")
.font(.footnote)
VStack(alignment: .leading) {
Text("作者: \(entry.videoAuthor)")
.font(.footnote)
Text("播放量: \(entry.videoViews)")
.font(.footnote)
}
Spacer()
Text("在喵哩喵哩查看更多内容")
Text("在喵哩喵哩查看视频")
.font(.footnote)
.foregroundColor(.gray)

Expand All @@ -143,7 +138,6 @@ struct MeowWidgetEntryView : View {
}
}
.padding()
.background(Color("WidgetBackgroundColor"))
.widgetURL(URL(string: "meowbili://")!)
}
}
Expand All @@ -156,7 +150,7 @@ struct MeowWidget: Widget {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
MeowWidgetEntryView(entry: entry)
}
.configurationDisplayName("喵哩喵哩Widget")
.configurationDisplayName("喵哩喵哩小组件")
.description("热门或推荐的视频内容")
.supportedFamilies(families)
}
Expand Down
17 changes: 8 additions & 9 deletions SharedCode/BiliBiliAPIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,28 @@ class BiliBiliAPIService {
switch type {
case .trending:
urlString = "https://api.bilibili.com/x/web-interface/popular?ps=\(limit)"
//这里ps=几就会让返回数据有几条
case .recommendations:
urlString = "https://api.bilibili.com/x/web-interface/index/top/rcmd?ps=1"
//这里ps=任何数字都不影响返回值 所以不更改\(limit)
}

guard let url = URL(string: urlString) else {
throw URLError(.badURL)
}

let (data, _) = try await URLSession.shared.data(from: url)

if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let dataDict = json["data"] as? [String: Any],
let list = dataDict["list"] as? [[String: Any]] {
let itemList = dataDict["item"] as? [[String: Any]] {

let videos = list.prefix(limit).map { video -> (String, String, String, String) in
let videos = itemList.prefix(limit).map { video -> (String, String, String, String) in
let videoTitle = video["title"] as? String ?? "无标题"
let videoDesc = video["desc"] as? String ?? "无描述"
let videoAuthor = video["author"] as? String ?? "未知作者"
let videoViews = video["view"] as? String ?? "未知播放量"
return (title: videoTitle, description: videoDesc, author: videoAuthor, views: videoViews)
let videoAuthor = (video["owner"] as? [String: Any])?["name"] as? String ?? "未知作者"
let videoViews = (video["stat"] as? [String: Any])?["view"] as? Int ?? 0
return (title: videoTitle, description: videoDesc, author: videoAuthor, views: "\(videoViews)")
}

return .success(videos)
} else {
return .failure(NSError(domain: "BiliBiliAPIService", code: -1, userInfo: [NSLocalizedDescriptionKey: "数据格式错误"]))
Expand All @@ -67,4 +65,5 @@ class BiliBiliAPIService {
return .failure(error)
}
}

}