Skip to content

Commit 8797634

Browse files
committed
【修改信息】 repo 内容路由
1 parent 8641547 commit 8797634

5 files changed

Lines changed: 112 additions & 27 deletions

File tree

ZLGitHubClient/ZLGitHubClient/Class/Router/ZLUIRouter+Github.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ enum ZLGithubPathType {
1717
case release(login: String, repoName: String, tagName: String)
1818
case commit(login: String, repoName: String, ref: String)
1919
case compare(login: String, repoName: String, baseRef: String, headRef: String)
20+
case tree(login: String, repoName: String, ref: String, path: String)
21+
case blob(login: String, repoName: String, ref: String, path: String)
2022

2123
func routerParams() -> (SYDCentralRouterModelType, ZLRouterKey, [String:Any]) {
2224
switch self {
@@ -73,6 +75,20 @@ enum ZLGithubPathType {
7375
"repoName": repoName,
7476
"baseRef": baseRef,
7577
"headRef": headRef])
78+
79+
case .tree(let login, let repoName,let ref, let path):
80+
return (.uiViewController,
81+
.RepoContentController,
82+
["repoFullName": "\(login)/\(repoName)",
83+
"branch": ref,
84+
"path": path])
85+
86+
case .blob(let login, let repoName, let ref, let path):
87+
return (.uiViewController,
88+
.RepoCodePreview3Controller,
89+
["repoFullName": "\(login)/\(repoName)",
90+
"branch": ref,
91+
"path": path])
7692
}
7793
}
7894

@@ -86,6 +102,8 @@ extension ZLGithubKeyWord {
86102
static let releases: ZLGithubKeyWord = "releases"
87103
static let commit: ZLGithubKeyWord = "commit"
88104
static let compare: ZLGithubKeyWord = "compare"
105+
static let tree: ZLGithubKeyWord = "tree"
106+
static let blob: ZLGithubKeyWord = "blob"
89107
}
90108

91109

@@ -188,6 +206,27 @@ extension ZLUIRouter {
188206
headRef: String(refs[1]))
189207
}
190208
}
209+
case .tree:
210+
// https://github.com/ExistOrLive/GithubClient/tree/master/Document
211+
if pathCount >= 5 {
212+
let ref = pathComponents[4]
213+
let paths = Array(pathComponents[5...])
214+
pathType = .tree(login: pathComponents[1],
215+
repoName: pathComponents[2],
216+
ref: pathComponents[4],
217+
path: paths.joined(separator: "/"))
218+
}
219+
220+
case .blob:
221+
// https://github.com/ExistOrLive/GithubClient/blob/master/Document/GithubAction/DailyCI%E8%AF%B4%E6%98%8E.md
222+
if pathCount >= 5 {
223+
let ref = pathComponents[4]
224+
let paths = Array(pathComponents[5...])
225+
pathType = .blob(login: pathComponents[1],
226+
repoName: pathComponents[2],
227+
ref: pathComponents[4],
228+
path: paths.joined(separator: "/"))
229+
}
191230

192231
default:
193232
break

ZLGitHubClient/ZLGitHubClient/Class/Router/ZLUIRouter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ extension ZLRouterKey {
8989
static let ReleaseInfoController: ZLRouterKey = "ZLReleaseInfoController"
9090
static let CommitInfoController: ZLRouterKey = "ZLCommitInfoController"
9191
static let CompareInfoController: ZLRouterKey = "ZLRepoCompareCommitController"
92+
static let RepoContentController: ZLRouterKey = "ZLRepoContentController"
93+
static let RepoCodePreview3Controller: ZLRouterKey = "ZLRepoCodePreview3Controller"
94+
9295

9396
static let WebContentController: ZLRouterKey = "ZLWebContentController"
9497

ZLGitHubClient/ZLGitHubClient/Class/UI/repo/ZLRepoContentController/viewcontroller/ZLRepoCodePreview3Controller.swift

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ import ZMMVVM
2121
class ZLRepoCodePreview3Controller: ZMViewController {
2222

2323
// model
24-
let contentModel: ZLGithubContentModel
25-
let repoFullName: String
26-
let branch: String
24+
@objc var repoFullName: String = ""
25+
@objc var branch: String = ""
26+
@objc var path: String = ""
2727

2828
var htmlStr: String?
2929

3030

31-
init(repoFullName: String, contentModel: ZLGithubContentModel, branch: String) {
32-
self.repoFullName = repoFullName
33-
self.contentModel = contentModel
34-
self.branch = branch
31+
@objc init() {
3532
super.init(nibName: nil, bundle: nil)
3633
}
3734

@@ -48,7 +45,8 @@ class ZLRepoCodePreview3Controller: ZMViewController {
4845

4946
NotificationCenter.default.addObserver(self, selector: #selector(onNotificationArrived(notification:)), name: ZLUserInterfaceStyleChange_Notification, object: nil)
5047

51-
let fileExtension = (URL.init(string: self.contentModel.name.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlPathAllowed) ?? "") as NSURL?)?.pathExtension ?? ""
48+
49+
let fileExtension = pathExtension()
5250
if fileExtension.lowercased() == "md" || fileExtension.lowercased() == "markdown" {
5351
self.sendQueryContentRequest()
5452
} else {
@@ -99,7 +97,7 @@ class ZLRepoCodePreview3Controller: ZMViewController {
9997

10098
override func setupUI() {
10199
super.setupUI()
102-
self.title = self.contentModel.path
100+
self.title = path
103101

104102
self.zmNavigationBar.backButton.isHidden = false
105103
self.zmNavigationBar.addRightView(moreButton)
@@ -110,9 +108,23 @@ class ZLRepoCodePreview3Controller: ZMViewController {
110108
})
111109
}
112110

111+
func html_url() -> String {
112+
let html_url = "https://github.com/\(repoFullName.urlPathEncoding)/blob/\(branch.urlPathEncoding)/\(path.urlPathEncoding)"
113+
return html_url
114+
}
115+
116+
func download_url() -> String {
117+
let download_url = "https://raw.githubusercontent.com/\(repoFullName.urlPathEncoding)/\(branch.urlPathEncoding)/\(path.urlPathEncoding)"
118+
return download_url
119+
}
120+
121+
func pathExtension() -> String {
122+
String(path.split(separator: ".").last ?? "")
123+
}
124+
113125
func switchToWebVC() {
114-
115-
if let url = URL.init(string: self.contentModel.html_url),
126+
127+
if let url = URL.init(string: html_url()),
116128
let vc = ZLUIRouter.getVC(key: .WebContentController, params: ["requestURL": url]) {
117129
if var viewControllers = self.navigationController?.viewControllers,
118130
!viewControllers.isEmpty {
@@ -163,7 +175,7 @@ extension ZLRepoCodePreview3Controller {
163175

164176
@objc func onMoreButtonClick(button: UIButton) {
165177

166-
guard let url = URL(string: self.contentModel.html_url) else {
178+
guard let url = URL(string: html_url()) else {
167179
return
168180
}
169181
button.showShareMenu(title: url.absoluteString, url: url, sourceViewController: self )
@@ -178,7 +190,7 @@ extension ZLRepoCodePreview3Controller {
178190
ZLProgressHUD.show()
179191

180192
ZLServiceManager.sharedInstance.repoServiceModel?.getRepositoryFileHTMLInfo(withFullName: self.repoFullName,
181-
path: self.contentModel.path,
193+
path: self.path,
182194
branch: self.branch,
183195
serialNumber: NSString.generateSerialNumber()) { [weak self] (resultModel: ZLOperationResultModel) in
184196

@@ -205,7 +217,7 @@ extension ZLRepoCodePreview3Controller {
205217

206218
ZLProgressHUD.show()
207219
ZLServiceManager.sharedInstance.repoServiceModel?.getRepositoryFileRawInfo(withFullName: self.repoFullName,
208-
path: self.contentModel.path,
220+
path: self.path,
209221
branch: self.branch,
210222
serialNumber: NSString.generateSerialNumber()) {[weak self] (resultModel: ZLOperationResultModel) in
211223

@@ -223,7 +235,7 @@ extension ZLRepoCodePreview3Controller {
223235
return
224236
}
225237

226-
let code = "```\(self.getFileType(fileExtension: URL.init(string: self.contentModel.path)?.pathExtension ?? ""))\n\(data)\n```"
238+
let code = "```\(self.getFileType(fileExtension: self.pathExtension()))\n\(data)\n```"
227239

228240
ZLServiceManager.sharedInstance.additionServiceModel?.renderCodeToMarkdown(withCode: code, serialNumber: NSString.generateSerialNumber(), completeHandle: {[weak self](resultModel: ZLOperationResultModel) in
229241

@@ -339,7 +351,7 @@ extension ZLRepoCodePreview3Controller: WKUIDelegate, WKNavigationDelegate {
339351

340352
var url = URL(string: urlStr)
341353
if url?.host == nil { // 如果是相对路径,组装baseurl
342-
url = (URL.init(string: self.contentModel.html_url) as NSURL?)?.deletingLastPathComponent
354+
url = (URL.init(string: html_url()) as NSURL?)?.deletingLastPathComponent
343355
url = URL(string: "\(url?.absoluteString ?? "" )\(urlStr)")
344356
}
345357

@@ -355,13 +367,13 @@ extension ZLRepoCodePreview3Controller: WKUIDelegate, WKNavigationDelegate {
355367
}
356368

357369
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
358-
if let download_url = self.contentModel.download_url {
359-
let baseURLStr = (URL.init(string: download_url) as NSURL?)?.deletingLastPathComponent?.absoluteString
370+
371+
let baseURLStr = (URL.init(string: download_url()) as NSURL?)?.deletingLastPathComponent?.absoluteString
360372
let addBaseScript = "let a = '\(baseURLStr ?? "")';let array = document.getElementsByTagName('img');for(i=0;i<array.length;i++){let item=array[i];if(item.getAttribute('src').indexOf('http') == -1){item.src = a + item.getAttribute('src');}}"
361373

362374
webView.evaluateJavaScript(addBaseScript) { (_: Any?, _: Error?) in
363375

364-
}
376+
365377
}
366378

367379
}

ZLGitHubClient/ZLGitHubClient/Class/UI/repo/ZLRepoContentController/viewcontroller/ZLRepoContentController.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@ class ZLRepoContentNode: ZLBaseObject {
2323
class ZLRepoContentController: ZMViewController, ZLRefreshProtocol {
2424

2525
// model
26-
var repoFullName: String?
27-
var path: String?
28-
var branch: String?
26+
@objc var repoFullName: String?
27+
@objc var path: String?
28+
@objc var branch: String?
2929

3030
/// 根节点
3131
var rootContentNode: ZLRepoContentNode?
3232
/// 当前节点
3333
var currentContentNode: ZLRepoContentNode?
3434

35+
@objc init() {
36+
super.init(nibName: nil, bundle: nil)
37+
}
38+
39+
@MainActor required init?(coder: NSCoder) {
40+
fatalError("init(coder:) has not been implemented")
41+
}
42+
3543

3644
lazy var closeButton: UIButton = {
3745
let button = UIButton.init(type: .custom)
@@ -263,9 +271,10 @@ extension ZLRepoContentController: UITableViewDelegate, UITableViewDataSource {
263271
self.currentContentNode = self.currentContentNode?.subNodes?[indexPath.row]
264272
self.reloadData()
265273
} else if "file" == contentModel.type {
266-
let controller = ZLRepoCodePreview3Controller(repoFullName: repoFullName ?? "",
267-
contentModel: contentModel,
268-
branch: branch ?? "")
274+
let controller = ZLRepoCodePreview3Controller()
275+
controller.repoFullName = repoFullName ?? ""
276+
controller.path = contentModel.path
277+
controller.branch = branch ?? ""
269278
self.navigationController?.pushViewController(controller, animated: true)
270279
}
271280
}

ZLGitHubClient/ZLGitHubClient/System/ZLSupportFiles/SYDCenteralFactoryConfig.plist

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,31 @@
308308
<key>claStr</key>
309309
<string>ZLRepoCompareCommitController</string>
310310
<key>type</key>
311-
<string>0</string>
311+
<integer>0</integer>
312+
<key>isSingle</key>
313+
<false/>
314+
<key>moduleName</key>
315+
<string>ZLGitHubClient</string>
316+
</dict>
317+
<key>ZLRepoContentController</key>
318+
<dict>
319+
<key>claStr</key>
320+
<string>ZLRepoContentController</string>
321+
<key>type</key>
322+
<integer>0</integer>
312323
<key>isSingle</key>
313-
<string>NO</string>
324+
<false/>
325+
<key>moduleName</key>
326+
<string>ZLGitHubClient</string>
327+
</dict>
328+
<key>ZLRepoCodePreview3Controller</key>
329+
<dict>
330+
<key>claStr</key>
331+
<string>ZLRepoCodePreview3Controller</string>
332+
<key>type</key>
333+
<integer>0</integer>
334+
<key>isSingle</key>
335+
<false/>
314336
<key>moduleName</key>
315337
<string>ZLGitHubClient</string>
316338
</dict>

0 commit comments

Comments
 (0)