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错误修复与优化 #426

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
189 changes: 189 additions & 0 deletions DarockBili.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

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.

13 changes: 3 additions & 10 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 @@ -87,9 +87,6 @@ struct MeowWidgetEntryView : View {
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,7 +96,7 @@ struct MeowWidgetEntryView : View {

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

case .systemMedium:
Expand All @@ -108,9 +105,6 @@ struct MeowWidgetEntryView : View {
Text(entry.videoDescription)
.font(.subheadline)
Spacer()
Text("在喵哩喵哩查看更多内容")
.font(.footnote)
.foregroundColor(.gray)

case .systemLarge:
Text(entry.videoTitle)
Expand Down Expand Up @@ -143,7 +137,6 @@ struct MeowWidgetEntryView : View {
}
}
.padding()
.background(Color("WidgetBackgroundColor"))
.widgetURL(URL(string: "meowbili://")!)
}
}
Expand All @@ -156,7 +149,7 @@ struct MeowWidget: Widget {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
MeowWidgetEntryView(entry: entry)
}
.configurationDisplayName("喵哩喵哩Widget")
.configurationDisplayName("喵哩喵哩小组件")
.description("热门或推荐的视频内容")
.supportedFamilies(families)
}
Expand Down
20 changes: 9 additions & 11 deletions SharedCode/BiliBiliAPIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,33 @@ 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 videos = list.prefix(limit).map { video -> (String, String, String, String) in
let itemList = dataDict["item"] as? [[String: Any]] {
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: "数据格式错误"]))
}

} catch {
return .failure(error)
}
Expand Down