Skip to content

Commit

Permalink
feat: 收藏夹支持订阅文件夹
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamSaddle authored and yichengchen committed Feb 7, 2024
1 parent d36fa1f commit 97baa6d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
5 changes: 4 additions & 1 deletion BilibiliLive/Module/Personal/ToViewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ struct ToViewData: PlayableData, Codable {
}

var avatar: URL? {
return URL(string: owner.face)
if owner.face != nil {
return URL(string: owner.face!)
}
return nil
}

var date: String? {
Expand Down
23 changes: 18 additions & 5 deletions BilibiliLive/Module/ViewController/FavoriteViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ class FavoriteViewController: CategoryViewController {
super.viewDidLoad()

Task {
guard let favList = try? await WebRequest.requestFavVideosList() else {
return
// 用户创建的收藏夹
let favList = try? await WebRequest.requestFavVideosList()
if favList != nil {
categories = favList!.map {
return CategoryDisplayModel(title: $0.title, contentVC: FavoriteVideoContentViewController(info: $0))
}
}
categories = favList.map {
return CategoryDisplayModel(title: $0.title, contentVC: FavoriteVideoContentViewController(info: $0))

// 用户收藏的订阅
let favFolderCollectedList = try? await WebRequest.requestFavFolderCollectedList()
if favFolderCollectedList != nil {
favFolderCollectedList!.forEach {
categories.append(CategoryDisplayModel(title: $0.title, contentVC: FavoriteVideoContentViewController(info: $0)))
}
}

initTypeCollectionView()
Expand All @@ -45,7 +54,11 @@ class FavoriteVideoContentViewController: StandardVideoCollectionViewController<
}

override func request(page: Int) async throws -> [FavData] {
return try await WebRequest.requestFavVideos(mid: String(info.id), page: page)
if info.createBySelf {
return try await WebRequest.requestFavVideos(mid: String(info.id), page: page)
} else {
return try await WebRequest.requestFavSeason(seasonId: String(info.id), page: page)
}
}

override func goDetail(with record: FavData) {
Expand Down
52 changes: 47 additions & 5 deletions BilibiliLive/Request/WebRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ enum WebRequest {
static let info = "https://api.bilibili.com/x/web-interface/view"
static let fav = "https://api.bilibili.com/x/v3/fav/resource/list"
static let favList = "https://api.bilibili.com/x/v3/fav/folder/created/list-all"
static let favFolderCollectedList = "https://api.bilibili.com/x/v3/fav/folder/collected/list"
static let favSeason = "https://api.bilibili.com/x/space/fav/season/list"
static let reportHistory = "https://api.bilibili.com/x/v2/history/report"
static let upSpace = "https://api.bilibili.com/x/space/wbi/arc/search"
static let like = "https://api.bilibili.com/x/web-interface/archive/like"
Expand Down Expand Up @@ -295,7 +297,10 @@ extension WebRequest {
let list: [FavListData]
}
let res: Resp = try await request(method: .get, url: EndPoint.favList, parameters: ["up_mid": mid])
return res.list
return res.list.map {
$0.createBySelf = true
return $0
}
}

static func requestFavVideos(mid: String, page: Int) async throws -> [FavData] {
Expand All @@ -306,6 +311,23 @@ extension WebRequest {
return res.medias ?? []
}

static func requestFavFolderCollectedList() async throws -> [FavListData] {
guard let mid = ApiRequest.getToken()?.mid else { return [] }
struct Resp: Codable {
let list: [FavListData]
}
let res: Resp = try await request(method: .get, url: EndPoint.favFolderCollectedList, parameters: ["up_mid": mid, "pn": 1, "ps": 100, "platform": "web"])
return res.list
}

static func requestFavSeason(seasonId: String, page: Int) async throws -> [FavData] {
struct Resp: Codable {
let medias: [FavData]?
}
let res: Resp = try await request(method: .get, url: EndPoint.favSeason, parameters: ["season_id": seasonId, "ps": "20", "pn": page, "platform": "web"])
return res.medias ?? []
}

static func reportWatchHistory(aid: Int, cid: Int, currentTime: Int) {
requestJSON(method: .post,
url: EndPoint.reportHistory,
Expand Down Expand Up @@ -488,7 +510,13 @@ struct HistoryData: DisplayData, Codable {

let title: String
var ownerName: String { owner.name }
var avatar: URL? { URL(string: owner.face) }
var avatar: URL? {
if owner.face != nil {
return URL(string: owner.face!)
}
return nil
}

let pic: URL?

let owner: VideoOwner
Expand Down Expand Up @@ -528,6 +556,8 @@ class FavListData: Codable, Hashable {
var currentPage = 1
var end = false
var loading = false
// 收藏夹是否为用户自己创建
var createBySelf = false
enum CodingKeys: String, CodingKey {
case title, id
}
Expand Down Expand Up @@ -625,13 +655,25 @@ extension VideoDetail: DisplayData {
var title: String { View.title }
var ownerName: String { View.owner.name }
var pic: URL? { View.pic }
var avatar: URL? { URL(string: View.owner.face) }
var avatar: URL? {
if View.owner.face != nil {
return URL(string: View.owner.face!)
}
return nil
}

var date: String? { DateFormatter.stringFor(timestamp: View.pubdate) }
}

extension VideoDetail.Info: DisplayData, PlayableData {
var ownerName: String { owner.name }
var avatar: URL? { URL(string: owner.face) }
var avatar: URL? {
if owner.face != nil {
return URL(string: owner.face!)
}
return nil
}

var date: String? { DateFormatter.stringFor(timestamp: pubdate) }
}

Expand Down Expand Up @@ -756,7 +798,7 @@ struct UserEpisodeInfo: Codable, Hashable {
struct VideoOwner: Codable, Hashable {
let mid: Int
let name: String
let face: String
var face: String?
}

struct VideoPage: Codable, Hashable {
Expand Down

0 comments on commit 97baa6d

Please sign in to comment.