Skip to content

Commit aaeac02

Browse files
committed
Use SwiftFormat to format code and optimize comment list loading performance
1 parent 8f235f7 commit aaeac02

22 files changed

+209
-257
lines changed

V2exOS.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
0D2334EF29969A3B00DF4B73 /* TimeFormat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D8F3358288D35D800C27AF1 /* TimeFormat.swift */; };
3333
0D2334F029969A6400DF4B73 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DD2A4DC2987C57B0018C2CF /* Constants.swift */; };
3434
0D2334F129969A6B00DF4B73 /* PreviewData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D8A2E702896AE19001B2769 /* PreviewData.swift */; };
35+
0D321EA629A18787009DFB25 /* ColorExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D321EA529A18787009DFB25 /* ColorExt.swift */; };
36+
0D321EA729A18787009DFB25 /* ColorExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D321EA529A18787009DFB25 /* ColorExt.swift */; };
37+
0D321EA829A18787009DFB25 /* ColorExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D321EA529A18787009DFB25 /* ColorExt.swift */; };
3538
0D34DF8F2942FED1000A0096 /* AppearanceConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D34DF8E2942FED1000A0096 /* AppearanceConfig.swift */; };
3639
0D40378C28E6E4EA00B1A341 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D40378B28E6E4EA00B1A341 /* SettingsView.swift */; };
3740
0D40378E28E6E4F500B1A341 /* ProxyHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D40378D28E6E4F500B1A341 /* ProxyHelper.swift */; };
@@ -88,6 +91,7 @@
8891
0D2334D72996944600DF4B73 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
8992
0D2334DD2996972E00DF4B73 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
9093
0D2334E12996990700DF4B73 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
94+
0D321EA529A18787009DFB25 /* ColorExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorExt.swift; sourceTree = "<group>"; };
9195
0D34DF8E2942FED1000A0096 /* AppearanceConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppearanceConfig.swift; sourceTree = "<group>"; };
9296
0D40378B28E6E4EA00B1A341 /* SettingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
9397
0D40378D28E6E4F500B1A341 /* ProxyHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProxyHelper.swift; sourceTree = "<group>"; };
@@ -262,6 +266,7 @@
262266
0D34DF8E2942FED1000A0096 /* AppearanceConfig.swift */,
263267
0DD2A4DC2987C57B0018C2CF /* Constants.swift */,
264268
0D5555F82997B6B100A8B9FD /* ViewExt.swift */,
269+
0D321EA529A18787009DFB25 /* ColorExt.swift */,
265270
);
266271
path = Utils;
267272
sourceTree = "<group>";
@@ -478,6 +483,7 @@
478483
buildActionMask = 2147483647;
479484
files = (
480485
0D8A2E712896AE19001B2769 /* PreviewData.swift in Sources */,
486+
0D321EA629A18787009DFB25 /* ColorExt.swift in Sources */,
481487
0DD2A4DD2987C57B0018C2CF /* Constants.swift in Sources */,
482488
0DA3F3A128A40ACF00E7224D /* ProfileView.swift in Sources */,
483489
0DA3F3A728A412FE00E7224D /* CurrentUserStore.swift in Sources */,
@@ -503,6 +509,7 @@
503509
buildActionMask = 2147483647;
504510
files = (
505511
0D2334E32996996B00DF4B73 /* TopicListView.swift in Sources */,
512+
0D321EA829A18787009DFB25 /* ColorExt.swift in Sources */,
506513
0D2334F029969A6400DF4B73 /* Constants.swift in Sources */,
507514
0D2334E22996990700DF4B73 /* HomeView.swift in Sources */,
508515
0D2334ED299699BC00DF4B73 /* CommentListView.swift in Sources */,
@@ -536,6 +543,7 @@
536543
0DA7FA852987A76500374B3A /* AppearanceConfig.swift in Sources */,
537544
0DA7FA7829879AB200374B3A /* TopicListView.swift in Sources */,
538545
0DD2A4DE2987C57B0018C2CF /* Constants.swift in Sources */,
546+
0D321EA729A18787009DFB25 /* ColorExt.swift in Sources */,
539547
);
540548
runOnlyForDeploymentPostprocessing = 0;
541549
};

V2exOS/User/CurrentUserStore.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
//
77

88
import Foundation
9+
import KeychainAccess
910
import SwiftUI
1011
import V2exAPI
11-
import KeychainAccess
1212

1313
public class CurrentUserStore: ObservableObject {
14-
1514
public static let shared = CurrentUserStore()
1615

1716
@Published public private(set) var user: V2Member?
@@ -24,10 +23,8 @@ public class CurrentUserStore: ObservableObject {
2423
let keychain = Keychain(service: "com.isaced.v2exos")
2524
let KeychainTokenKey = "AccessToken"
2625

27-
28-
2926
public init() {
30-
if let token = readToken(){
27+
if let token = readToken() {
3128
accessToken = token
3229

3330
fetchUser()
@@ -48,12 +45,12 @@ public class CurrentUserStore: ObservableObject {
4845
func clearToken() {
4946
do {
5047
try keychain.remove(KeychainTokenKey)
51-
}catch {
48+
} catch {
5249
print(error)
5350
}
5451
}
5552

56-
public func logout(){
53+
public func logout() {
5754
accessToken = nil
5855
user = nil
5956
clearToken()
@@ -69,7 +66,7 @@ public class CurrentUserStore: ObservableObject {
6966
return true
7067
}
7168
}
72-
}catch{
69+
} catch {
7370
print(error)
7471
}
7572
v2ex.accessToken = nil
@@ -79,12 +76,12 @@ public class CurrentUserStore: ObservableObject {
7976
func fetchUser() {
8077
Task {
8178
do {
82-
if let res = try await v2ex.member(){
79+
if let res = try await v2ex.member() {
8380
if res.success {
84-
user = res.result;
81+
user = res.result
8582
}
8683
}
87-
}catch{
84+
} catch {
8885
return
8986
}
9087
}

V2exOS/Utils/AppearanceConfig.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import Foundation
99

1010
class SettingsConfig: ObservableObject {
11-
1211
public static let shared = SettingsConfig()
1312

1413
static let FontSizeKey = "settings.font.size"

V2exOS/Utils/ColorExt.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// ColorExt.swift
3+
// V2exOS
4+
//
5+
// Created by isaced on 2023/2/19.
6+
//
7+
8+
import SwiftUI
9+
10+
extension Color {
11+
static var label: Color {
12+
#if os(macOS)
13+
return Color(.labelColor)
14+
#endif
15+
#if os(iOS)
16+
return Color(.label)
17+
#endif
18+
}
19+
}

V2exOS/Utils/PreviewData.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct PreviewData {
1717
avatarLarge: "https://cdn.v2ex.com/avatar/ff10/6e6c/79764_large.png?m=1657684598",
1818
created: 1414903742,
1919
lastModified: 1657684598)
20-
20+
2121
static let topic = V2Topic(id: 1,
2222
node: nil,
2323
member: PreviewData.member,
@@ -31,7 +31,7 @@ struct PreviewData {
3131
contentRendered: "<p>想拆成 Typescript 模块化,有没有什么指路手册?</p>\n",
3232
lastModified: 1658649797,
3333
replies: 1111)
34-
34+
3535
static let comment = V2Comment(id: 1,
3636
content: "Testtt",
3737
contentRendered: "Testtt",

V2exOS/Utils/ProxyHelper.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import Foundation
99
import Kingfisher
1010

11-
enum ProxyType : Int , Codable {
11+
enum ProxyType: Int, Codable {
1212
case http
1313
case socks
1414
}
1515

16-
struct ProxyInfo : Codable {
16+
struct ProxyInfo: Codable {
1717
var enabled: Bool
1818
var type: ProxyType = .http
1919
var host: String?
@@ -22,8 +22,7 @@ struct ProxyInfo : Codable {
2222
var password: String?
2323
}
2424

25-
struct ProxyHelper {
26-
25+
enum ProxyHelper {
2726
static let ProxyCacheKey = "ProxyInfo"
2827

2928
/**
@@ -43,14 +42,14 @@ struct ProxyHelper {
4342
kCFNetworkProxiesHTTPProxy: proxyInfo.host ?? "",
4443
kCFNetworkProxiesHTTPPort: proxyInfo.port ?? 0,
4544
kCFNetworkProxiesHTTPSEnable: true,
46-
kCFNetworkProxiesHTTPSProxy: proxyInfo.host ?? "",
47-
kCFNetworkProxiesHTTPSPort: proxyInfo.port ?? 0,
45+
kCFNetworkProxiesHTTPSProxy: proxyInfo.host ?? "",
46+
kCFNetworkProxiesHTTPSPort: proxyInfo.port ?? 0,
4847
]
4948
} else if proxyInfo.type == .socks {
5049
sessionConfiguration.connectionProxyDictionary = [
5150
kCFNetworkProxiesSOCKSEnable: true,
52-
kCFNetworkProxiesSOCKSProxy: proxyInfo.host ?? "",
53-
kCFNetworkProxiesSOCKSPort: proxyInfo.port ?? 0,
51+
kCFNetworkProxiesSOCKSProxy: proxyInfo.host ?? "",
52+
kCFNetworkProxiesSOCKSPort: proxyInfo.port ?? 0,
5453
]
5554
}
5655

V2exOS/Utils/StringExt.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Foundation
1010
extension String {
1111
func htmlToString() -> String? {
1212
do {
13-
1413
let text = try NSAttributedString(data: self.data(using: .unicode)!,
1514
options: [.documentType: NSAttributedString.DocumentType.html],
1615
documentAttributes: nil).string

V2exOS/Utils/TimeFormat.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
import Foundation
99

1010
extension Date {
11-
1211
func fromNow() -> String {
1312
// ask for the full relative date
1413
let formatter = RelativeDateTimeFormatter()
1514
formatter.unitsStyle = .full
16-
formatter.locale = Locale.init(identifier: Locale.preferredLanguages.first!)
15+
formatter.locale = Locale(identifier: Locale.preferredLanguages.first!)
1716

1817
// get exampleDate relative to the current date
1918
let relativeDate = formatter.localizedString(for: self, relativeTo: Date.now)
2019

2120
return relativeDate
2221
}
23-
2422
}

V2exOS/Views/Components/UserName.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ import Foundation
99
import SwiftUI
1010

1111
struct UserName: View {
12-
1312
var username: String
14-
13+
1514
init(_ username: String) {
1615
self.username = username
1716
}
18-
17+
1918
var body: some View {
2019
#if os(macOS) || os(iOS)
2120
Button {
2221
if let url = URL(string: "https://www.v2ex.com/member/\(username)") {
23-
2422
#if os(iOS)
2523
UIApplication.shared.open(url)
2624
#endif
@@ -31,15 +29,15 @@ struct UserName: View {
3129
} label: {
3230
Text(username)
3331
.foregroundColor(.secondary)
34-
// .fontWeight(.bold)
32+
// .fontWeight(.bold)
3533
#if os(macOS)
36-
.onHover { inside in
37-
if inside {
38-
NSCursor.pointingHand.push()
39-
} else {
40-
NSCursor.pop()
41-
}
42-
}
34+
.onHover { inside in
35+
if inside {
36+
NSCursor.pointingHand.push()
37+
} else {
38+
NSCursor.pop()
39+
}
40+
}
4341
#endif
4442
}
4543
.buttonStyle(PlainButtonStyle())

V2exOS/Views/InboxListView.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import SwiftUI
99
import V2exAPI
1010

1111
struct InboxListView: View {
12-
1312
@EnvironmentObject private var currentUser: CurrentUserStore
1413

15-
@State var notificationList : [V2Notification] = []
14+
@State var notificationList: [V2Notification] = []
1615

1716
var body: some View {
1817
VStack {
1918
if let _ = currentUser.accessToken {
20-
2119
List {
2220
ForEach(notificationList) { noti in
2321
VStack(alignment: .leading) {
@@ -28,7 +26,7 @@ struct InboxListView: View {
2826
Text(noti.member?.username ?? "")
2927
.font(.body)
3028

31-
if let created = noti.created{
29+
if let created = noti.created {
3230
Text(Date(timeIntervalSince1970: TimeInterval(created)).fromNow())
3331
}
3432
}
@@ -44,29 +42,27 @@ struct InboxListView: View {
4442
}
4543
}.listRowInsets(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
4644

47-
}else{
45+
} else {
4846
Text("请先登录")
4947
}
50-
}.onAppear{
48+
}.onAppear {
5149
Task {
5250
do {
5351
if let res = try await v2ex.notifications(page: 1) {
54-
if let list = res.result{
52+
if let list = res.result {
5553
notificationList.append(contentsOf: list)
5654
}
5755
}
58-
}catch {
56+
} catch {
5957
print(error)
6058
}
6159
}
6260
}
6361
}
64-
6562
}
6663

6764
struct InboxListView_Previews: PreviewProvider {
6865
static var previews: some View {
6966
InboxListView()
7067
}
7168
}
72-

0 commit comments

Comments
 (0)