Skip to content

Commit cf0e211

Browse files
committed
【修改信息】 git diff 支持二进制,大文件,图片以及重命名场景
1 parent 78275c2 commit cf0e211

File tree

11 files changed

+149
-53
lines changed

11 files changed

+149
-53
lines changed

ZLGitHubClient/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ CHECKOUT OPTIONS:
351351
:git: https://github.com/ExistOrLive/ZLGithubOAuth.git
352352
:tag: 1.3.1
353353
ZLGitRemoteService:
354-
:commit: fd499dd4e8cfa9bc5b7f07d81ff3df67ca9a2d3e
354+
:commit: bf6ff8e5da6a8108360c0002811085751471b6a7
355355
:git: https://github.com/ExistOrLive/ZLGitRemoteService.git
356356
ZLUIUtilities:
357357
:git: https://github.com/ExistOrLive/ZLUIUtilities.git

ZLGitHubClient/ZLGitHubClient/Class/UI/Commit/ZLCommitInfoController.swift

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,47 @@ class ZLCommitInfoController: ZMTableViewController {
4444
refreshLoadNewData()
4545
}
4646

47+
override func viewDidAppear(_ animated: Bool) {
48+
super.viewDidAppear(animated)
49+
50+
if ZLDeviceInfo.isIPhone() {
51+
guard let appdelegate: AppDelegate = UIApplication.shared.delegate as? AppDelegate else {
52+
return
53+
}
54+
appdelegate.allowRotation = true
55+
}
56+
}
57+
58+
override func viewWillDisappear(_ animated: Bool) {
59+
super.viewWillDisappear(animated)
60+
61+
if ZLDeviceInfo.isIPhone() {
62+
guard let appdelegate: AppDelegate = UIApplication.shared.delegate as? AppDelegate else {
63+
return
64+
}
65+
appdelegate.allowRotation = false
66+
}
67+
}
68+
69+
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
70+
71+
super.viewWillTransition(to: size, with: coordinator)
72+
73+
if ZLDeviceInfo.isIPhone() {
74+
guard let navigationVC: ZMNavigationController = self.navigationController as? ZMNavigationController else {
75+
return
76+
}
77+
if size.height > size.width {
78+
// 横屏变竖屏
79+
self.isZmNavigationBarHidden = false
80+
navigationVC.forbidGestureBack = false
81+
} else {
82+
self.isZmNavigationBarHidden = true
83+
navigationVC.forbidGestureBack = true
84+
}
85+
}
86+
}
87+
4788

4889
override func setupUI() {
4990
super.setupUI()
@@ -174,8 +215,8 @@ extension ZLCommitInfoController {
174215

175216
sectionDatas.append(ZMBaseTableViewSectionData(
176217
cellDatas: [ZLCommitInfoHeaderCellData(model: data)],
177-
headerData: ZLCommonSectionHeaderFooterViewDataV2(backColor: .clear, viewHeight: 8),
178-
footerData: ZLCommonSectionHeaderFooterViewDataV2(backColor: .clear, viewHeight: 8)))
218+
headerData: ZLCommonSectionHeaderFooterViewDataV2(backColor: .back(withName: "ZLVCBackColor"), viewHeight: 8),
219+
footerData: ZLCommonSectionHeaderFooterViewDataV2(backColor: .back(withName: "ZLVCBackColor"), viewHeight: 8)))
179220

180221

181222
var files = data.files

ZLGitHubClient/ZLGitHubClient/Class/UI/Commit/ZLCompareCommitFilesController.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,47 @@ class ZLCompareCommitFilesController: ZMTableViewController {
3737
refreshLoadNewData()
3838
}
3939

40+
override func viewDidAppear(_ animated: Bool) {
41+
super.viewDidAppear(animated)
42+
43+
if ZLDeviceInfo.isIPhone() {
44+
guard let appdelegate: AppDelegate = UIApplication.shared.delegate as? AppDelegate else {
45+
return
46+
}
47+
appdelegate.allowRotation = true
48+
}
49+
}
50+
51+
override func viewWillDisappear(_ animated: Bool) {
52+
super.viewWillDisappear(animated)
53+
54+
if ZLDeviceInfo.isIPhone() {
55+
guard let appdelegate: AppDelegate = UIApplication.shared.delegate as? AppDelegate else {
56+
return
57+
}
58+
appdelegate.allowRotation = false
59+
}
60+
}
61+
62+
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
63+
64+
super.viewWillTransition(to: size, with: coordinator)
65+
66+
if ZLDeviceInfo.isIPhone() {
67+
guard let navigationVC: ZMNavigationController = self.navigationController as? ZMNavigationController else {
68+
return
69+
}
70+
if size.height > size.width {
71+
// 横屏变竖屏
72+
self.isZmNavigationBarHidden = false
73+
navigationVC.forbidGestureBack = false
74+
} else {
75+
self.isZmNavigationBarHidden = true
76+
navigationVC.forbidGestureBack = true
77+
}
78+
}
79+
}
80+
4081
override func setupUI() {
4182
super.setupUI()
4283

ZLGitHubClient/ZLGitHubClient/Class/UI/ZLReuseView/ZLCommitTableViewCell/view/ZLCommitInfoFilePathHeaderView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class ZLCommitInfoFilePathHeaderView: UITableViewHeaderFooterView {
2727
override init(reuseIdentifier: String?) {
2828
super.init(reuseIdentifier: reuseIdentifier)
2929

30+
backgroundView = UIView()
31+
backgroundView?.backgroundColor = .clear
3032
contentView.backgroundColor = .back(withName: "ZLCellBack")
3133
contentView.addSubview(self.titleLabel)
3234

ZLGitHubClient/ZLGitHubClient/Class/UI/ZLReuseView/ZLCommitTableViewCell/view/ZLCommitInfoHeaderCell.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ZLCommitInfoHeaderCell: UITableViewCell {
3434

3535
func setupUI() {
3636
selectionStyle = .none
37+
backgroundColor = .clear
3738
contentView.backgroundColor = UIColor(named: "ZLIssueCommentCellColor")
3839
contentView.addSubview(titleLabel)
3940
contentView.addSubview(authorAvatarButton)

ZLGitHubClient/ZLGitHubClient/Class/UI/ZLReuseView/ZLCommitTableViewCell/viewModel/ZLCommitInfoHeaderCellData.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,22 @@ extension ZLCommitInfoHeaderCellData: ZLCommitInfoHeaderCellSourceAndDelegate {
5858

5959
if let additions = model.stats?.additions ,
6060
additions > 0 {
61-
str.append("\(additions)"
61+
str.append("+\(additions)"
6262
.asMutableAttributedString()
6363
.font(.zlMediumFont(withSize: 13))
6464
.foregroundColor(.label(withName: "addColor")))
65-
str.append(" 个添加 "
66-
.asMutableAttributedString()
67-
.font(.zlRegularFont(withSize: 13))
68-
.foregroundColor(.label(withName: "ZLLabelColor1")))
6965
}
7066

7167
if let deletions = model.stats?.deletions ,
7268
deletions > 0 {
73-
str.append("\(deletions)"
69+
str.append(" -\(deletions)"
7470
.asMutableAttributedString()
7571
.font(.zlMediumFont(withSize: 13))
7672
.foregroundColor(.label(withName: "deleteColor")))
77-
str.append(" 个删除"
78-
.asMutableAttributedString()
79-
.font(.zlRegularFont(withSize: 13))
80-
.foregroundColor(.label(withName: "ZLLabelColor1")))
8173
}
8274

75+
str.paraghStyle(NSMutableParagraphStyle().lineSpacing(10))
76+
8377
return str
8478
}
8579

ZLGitHubClient/ZLGitHubClient/Class/UI/ZLReuseView/ZLCommitTableViewCell/viewModel/ZLCommitInfoPatchCellData.swift

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ import ZLGitRemoteService
1313
import ZMMVVM
1414
import WebKit
1515

16+
enum PatchType {
17+
case normal
18+
case renamed
19+
case image
20+
case binary
21+
case largeChanged
22+
}
23+
1624
class ZLCommitInfoPatchCellData: ZMBaseTableViewCellViewModel {
1725

1826
private var cellHeight: CGFloat = 0
1927

20-
let model: ZLGithubFileModel
28+
let model: ZLGithubFileDiffModel
2129
var patchStr: String = ""
2230
var imagePath: String = ""
23-
var isBinary: Bool = false
24-
var isImage: Bool = false
31+
var type: PatchType = .normal
32+
2533

2634
var cacheHtml: String?
2735

@@ -51,9 +59,6 @@ class ZLCommitInfoPatchCellData: ZMBaseTableViewCellViewModel {
5159

5260
})
5361
}
54-
let script = WKUserScript(source: self.renderDiffContentScript(), injectionTime: .atDocumentEnd, forMainFrameOnly: false)
55-
webView.configuration.userContentController.addUserScript(script)
56-
5762
return webView
5863
}()
5964

@@ -71,7 +76,7 @@ class ZLCommitInfoPatchCellData: ZMBaseTableViewCellViewModel {
7176
_webView.loadHTML(cacheHtml ?? "", baseURL: Bundle.main.bundleURL)
7277
}
7378

74-
init(model: ZLGithubFileModel, cellHeight: CGFloat?) {
79+
init(model: ZLGithubFileDiffModel, cellHeight: CGFloat?) {
7580
self.model = model
7681
super.init()
7782
self.initData(model: model)
@@ -82,45 +87,33 @@ class ZLCommitInfoPatchCellData: ZMBaseTableViewCellViewModel {
8287
_webView.loadHTML(cacheHtml ?? "", baseURL: Bundle.main.bundleURL)
8388
}
8489

85-
func initData(model: ZLGithubFileModel) {
90+
func initData(model: ZLGithubFileDiffModel) {
8691
if !model.patch.isEmpty {
8792
let patch = model.patch
8893
self.patchStr = patch.htmlEscaped()
89-
self.isBinary = false
90-
self.isImage = false
94+
self.type = .normal
95+
} else if model.changes == 0 && model.status == "renamed" {
96+
self.type = .renamed
97+
} else if model.changes > 0 {
98+
self.type = .largeChanged
9199
} else {
92-
self.isBinary = true
93100
let pathExtension = (model.filename as NSString).pathExtension.lowercased()
94101
if ["png","jpg","jpeg","gif","svg","webp","bmp","icon"].contains(pathExtension) {
95-
self.isImage = true
102+
self.type = .image
96103
let imagePath = model.raw_url
97104
self.imagePath = imagePath.htmlEscaped()
105+
} else {
106+
self.type = .binary
98107
}
99108
}
100-
101109
}
102110

103111
}
104112

105113
// MARK: - HTML
106114
extension ZLCommitInfoPatchCellData {
107115

108-
func renderDiffContentScript() -> String {
109-
if !isBinary {
110-
return """
111-
render(`\(patchStr)`,\(isLight ? "false" : "true"));
112-
"""
113-
} else if isImage {
114-
return """
115-
renderImage(`\(imagePath)`,\(isLight ? "false" : "true"));
116-
"""
117-
} else {
118-
return """
119-
renderBinary()
120-
"""
121-
}
122-
}
123-
116+
124117
var isLight: Bool {
125118
if #available(iOS 12.0, *) {
126119
return getRealUserInterfaceStyle() == .light
@@ -145,20 +138,31 @@ extension ZLCommitInfoPatchCellData {
145138

146139
func generateHTML() {
147140
var gitPatchDivContent = ""
148-
if !isBinary {
141+
switch type {
142+
case .normal:
149143
let tag = parsePatchAndGenerateHTML(patchText: self.patchStr)
150144
gitPatchDivContent = tag?.toHTMLString() ?? ""
151-
} else if isImage {
145+
case .image:
152146
gitPatchDivContent = "<img src=\"\(imagePath)\" class=\"img_binary\"/>"
153-
} else {
147+
case .binary,.largeChanged,.renamed:
148+
var text = ""
149+
switch type {
150+
case .binary:
151+
text = ZLLocalizedString(string:"Binary File Patch", comment: "")
152+
case .largeChanged:
153+
text = ZLLocalizedString(string:"Large File Changed", comment: "")
154+
case .renamed:
155+
text = ZLLocalizedString(string:"File Renamed", comment: "")
156+
default:
157+
break
158+
}
154159
if isLight {
155-
gitPatchDivContent = "<div class=\"div_binary\">Binary File</div>"
160+
gitPatchDivContent = "<div class=\"div_binary\">\(text)</div>"
156161
} else {
157-
gitPatchDivContent = "<div class=\"div_binary dark\">Binary File</div>"
162+
gitPatchDivContent = "<div class=\"div_binary dark\">\(text)</div>"
158163
}
159-
160164
}
161-
165+
162166
let htmlURL: URL? = Bundle.main.url(forResource: "gitpatchV2", withExtension: "html")
163167

164168
if let url = htmlURL {
@@ -180,7 +184,7 @@ extension ZLCommitInfoPatchCellData {
180184
self.cacheHtml = newHtmlStr as String
181185

182186
} catch {
183-
ZLToastView.showMessage("load Code index html failed")
187+
ZLToastView.showMessage("load Diff Patch failed")
184188
}
185189
}
186190
}

ZLGitHubClient/ZLGitHubClient/Class/UI/ZLReuseView/ZLCommonTableViewCell/ZLCommonSectionHeaderFooterView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ZLCommonSectionHeaderFooterView: UITableViewHeaderFooterView {
1616

1717
override init(reuseIdentifier: String?) {
1818
super.init(reuseIdentifier: reuseIdentifier)
19+
backgroundView = UIView()
1920
}
2021

2122
required init?(coder: NSCoder) {
@@ -26,5 +27,6 @@ extension ZLCommonSectionHeaderFooterView: ZMBaseViewUpdatableWithViewData {
2627
func zm_fillWithViewData(viewData: ZLCommonSectionHeaderFooterViewDataV2) {
2728
contentView.backgroundColor = viewData.backColor
2829
backgroundColor = viewData.backColor
30+
backgroundView?.backgroundColor = viewData.backColor
2931
}
3032
}

ZLGitHubClient/ZLGitHubClient/System/ZLSupportFiles/GitPatch/gitpatchV2.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@
4040

4141
/* 二进制文安 默认根字体大小 */
4242
.div_binary {
43-
width: 50%;
44-
height: 100px;
45-
font-size: 1.5rem;
43+
margin-top: 20px;
44+
margin-bottom: 20px;
45+
font-size: 1.2rem;
4646
color: #093069;
4747
display: flex;
48+
text-align: center;
4849
justify-content: center;
4950
/* 横向居中 */
5051
align-items: center;

ZLGitHubClient/ZLGitHubClient/System/ZLSupportFiles/en.lproj/ZLGitHubClientLocalizable.strings

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,8 @@
238238
"Draft Release" = "Draft";
239239
"Pre Release" = "Pre";
240240
"released" = "released";
241+
242+
/// diff patch
243+
"Binary File Patch" = "Binary file not shown";
244+
"File Renamed" = "File renamed without changes";
245+
"Large File Changed" = "File vary greatly not shown";

0 commit comments

Comments
 (0)