Skip to content

Commit ce5c18b

Browse files
committed
添加设置页面,多一种在设置中设置 token access 的方式
1 parent cf303fc commit ce5c18b

File tree

10 files changed

+78
-10
lines changed

10 files changed

+78
-10
lines changed

SwiftPamphletApp.xcodeproj/project.pbxproj

+12
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
08CD61FF27758B8A008C0935 /* Token.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08CD61FD27758B8A008C0935 /* Token.swift */; };
253253
08D107BD278826BB007B7009 /* HTMLEntities in Frameworks */ = {isa = PBXBuildFile; productRef = 08D107BC278826BB007B7009 /* HTMLEntities */; };
254254
08ED80162B9C54DE0069B7EC /* SMNetwork in Frameworks */ = {isa = PBXBuildFile; productRef = 08ED80152B9C54DE0069B7EC /* SMNetwork */; };
255+
08ED801C2B9D1EEC0069B7EC /* SettingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08ED801B2B9D1EEC0069B7EC /* SettingView.swift */; };
255256
08F4BE6028609D8700733F12 /* PlayCharts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F4BE5F28609D8600733F12 /* PlayCharts.swift */; };
256257
08F4BE6228616DC100733F12 /* PlayWeatherKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F4BE6128616DC100733F12 /* PlayWeatherKit.swift */; };
257258
08F51BC527A374A500693AB6 /* footer_js.html in Resources */ = {isa = PBXBuildFile; fileRef = 08F51BC427A374A500693AB6 /* footer_js.html */; };
@@ -500,6 +501,7 @@
500501
08C411F327951181006FC340 /* PlaySyntax.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaySyntax.swift; sourceTree = "<group>"; };
501502
08CD61FC27758B8A008C0935 /* Lexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lexer.swift; sourceTree = "<group>"; };
502503
08CD61FD27758B8A008C0935 /* Token.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Token.swift; sourceTree = "<group>"; };
504+
08ED801B2B9D1EEC0069B7EC /* SettingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingView.swift; sourceTree = "<group>"; };
503505
08F4BE5F28609D8600733F12 /* PlayCharts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayCharts.swift; sourceTree = "<group>"; };
504506
08F4BE6128616DC100733F12 /* PlayWeatherKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayWeatherKit.swift; sourceTree = "<group>"; };
505507
08F51BC427A374A500693AB6 /* footer_js.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = footer_js.html; sourceTree = "<group>"; };
@@ -1051,6 +1053,7 @@
10511053
086A5F052744E88E00FECE02 /* SwiftPamphletApp */ = {
10521054
isa = PBXGroup;
10531055
children = (
1056+
08ED801A2B9D1EDA0069B7EC /* Setting */,
10541057
080124FD27EC62DC00E44222 /* SwiftPamphletAppDebug.entitlements */,
10551058
08CD61FB27758B22008C0935 /* Core */,
10561059
080387652766FF3200519B15 /* Network */,
@@ -1294,6 +1297,14 @@
12941297
path = Core;
12951298
sourceTree = "<group>";
12961299
};
1300+
08ED801A2B9D1EDA0069B7EC /* Setting */ = {
1301+
isa = PBXGroup;
1302+
children = (
1303+
08ED801B2B9D1EEC0069B7EC /* SettingView.swift */,
1304+
);
1305+
path = Setting;
1306+
sourceTree = "<group>";
1307+
};
12971308
/* End PBXGroup section */
12981309

12991310
/* Begin PBXNativeTarget section */
@@ -1572,6 +1583,7 @@
15721583
08F4BE6028609D8700733F12 /* PlayCharts.swift in Sources */,
15731584
086A5F2E2744ED8600FECE02 /* ActiveDeveloperListView.swift in Sources */,
15741585
08BE637227CC6F13002BC6A8 /* PlayScrollView.swift in Sources */,
1586+
08ED801C2B9D1EEC0069B7EC /* SettingView.swift in Sources */,
15751587
08CD61FE27758B8A008C0935 /* Lexer.swift in Sources */,
15761588
086A5F422744EDCE00FECE02 /* IssueVM.swift in Sources */,
15771589
08AEAEF3277F09FA00B969E2 /* NavView.swift in Sources */,

SwiftPamphletApp/AppVM.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ final class AppVM {
361361
// MARK: - 计算通知数量
362362
@MainActor
363363
func calculateExpCountNotis() {
364-
if SPC.gitHubAccessToken.isEmpty == true {
364+
if SPC.gitHubAccessToken.isEmpty == true && SPC.githubAccessToken().isEmpty == true {
365365
return
366366
}
367367
var count = 0

SwiftPamphletApp/GitHubAPI/ListView/ExploreRepoListView.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct ExploreRepoListView: View {
1414
var isArchive = false
1515
var body: some View {
1616
List {
17-
if SPC.gitHubAccessToken.isEmpty == false && showAsGroup == false {
17+
if (SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false) && showAsGroup == false {
1818
Section {
1919
ForEach(isArchive ? appVM.archiveRepos : appVM.exps) { er in
2020
ForEach(er.repos) { r in
@@ -29,7 +29,7 @@ struct ExploreRepoListView: View {
2929

3030
// end Section
3131
ForEach(isArchive ? appVM.archiveRepos : appVM.exps) { er in
32-
if SPC.gitHubAccessToken.isEmpty == false && showAsGroup == false {
32+
if (SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false) && showAsGroup == false {
3333
// Section {
3434
// ForEach(er.repos) { r in
3535
// if (appVM.expNotis[r.id]?.unRead ?? 0) > 0 {
@@ -47,7 +47,7 @@ struct ExploreRepoListView: View {
4747
} else {
4848
DisclosureGroupLikeButton {
4949
ForEach(er.repos) { r in
50-
if SPC.gitHubAccessToken.isEmpty == false {
50+
if SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false {
5151
NavigationLink(destination: RepoView(vm: RepoVM(repoName: r.id))) {
5252
ExpListLinkView(r: r)
5353
}

SwiftPamphletApp/GitHubAPI/Network/APIRequest.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ final class APISev: APISevType {
4242
// token 处理
4343
// TODO: 支持 OAuth
4444
// TODO: 访问受限后会crash,异常待处理
45-
req.addValue("token \(SPC.gitHubAccessToken)", forHTTPHeaderField: "Authorization")
45+
var githubat = ""
46+
if SPC.gitHubAccessToken.isEmpty == true {
47+
githubat = SPC.githubAccessToken()
48+
} else {
49+
githubat = SPC.gitHubAccessToken
50+
}
51+
52+
req.addValue("token \(githubat)", forHTTPHeaderField: "Authorization")
4653

4754
// print(req.allHTTPHeaderFields!)
4855
let de = JSONDecoder()

SwiftPamphletApp/HomeUI/DataLink.swift

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct DataLink: Identifiable {
3838
IssuesListFromCustomView(vm: IssueVM(guideName:"lib-Combine"))
3939
case "Concurrency":
4040
IssuesListFromCustomView(vm: IssueVM(guideName:"lib-Concurrency"))
41+
case "设置":
42+
SettingView()
4143
default:
4244
// 默认是语法速查
4345
IssuesListFromCustomView(vm: IssueVM(guideName: "guide-syntax"))
@@ -65,6 +67,9 @@ extension DataLink {
6567
DataLink(title: "SwiftUI", imageName: "p3"),
6668
DataLink(title: "Combine", imageName: "p19"),
6769
DataLink(title: "Concurrency", imageName: "p1")
70+
]),
71+
DataLink(title: "更多", imageName: "", children: [
72+
DataLink(title: "设置", imageName: "p4")
6873
])
6974
]
7075
}

SwiftPamphletApp/Network/CCYRESTfulAPI.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,18 @@ extension RESTful {
115115
}
116116
req.setValue("application/json", forHTTPHeaderField: "Accept")
117117
// 不同平台接口的 token
118+
// 处理 github token
119+
var githubat = ""
120+
if SPC.gitHubAccessToken.isEmpty == true {
121+
githubat = SPC.githubAccessToken()
122+
} else {
123+
githubat = SPC.gitHubAccessToken
124+
}
125+
126+
// 不同情况的设置
118127
switch self.conf.host {
119128
case .github:
120-
req.setValue("token \(SPC.gitHubAccessToken)", forHTTPHeaderField: "Authorization")
129+
req.setValue("token \(githubat)", forHTTPHeaderField: "Authorization")
121130
}
122131

123132
return req

SwiftPamphletApp/SPSidebar.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct SPSidebar: View {
2121
}
2222
Section("Github") {
2323

24-
if SPC.gitHubAccessToken.isEmpty == false {
24+
if SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false {
2525
NavigationLink {
2626
ExploreRepoListView(showAsGroup: false)
2727
} label: {
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// SettingView.swift
3+
// SwiftPamphletApp
4+
//
5+
// Created by Ming Dai on 2024/3/10.
6+
//
7+
8+
import SwiftUI
9+
10+
struct SettingView: View {
11+
@State private var tokenString = ""
12+
var body: some View {
13+
Form {
14+
Section {
15+
Text("在这里写上 Github 的 access token")
16+
TextField("", text: $tokenString)
17+
.padding(10)
18+
.onSubmit {
19+
let ud = UserDefaults.standard
20+
ud.set(tokenString, forKey: SPC.githubUDTokenKey)
21+
}
22+
}
23+
.onAppear(perform: {
24+
let ud = UserDefaults.standard
25+
tokenString = ud.string(forKey: SPC.githubUDTokenKey) ?? ""
26+
})
27+
}
28+
}
29+
}

SwiftPamphletApp/SwiftPamphletAppApp.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ struct SwiftPamphletApp: View {
6767
NavigationView {
6868
SPSidebar()
6969
.onReceive(timerForDevs, perform: { _ in
70-
if SPC.gitHubAccessToken.isEmpty == false {
70+
if SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false {
7171
if let userName = appVM.timeForDevsEvent() {
7272
let vm = UserVM(userName: userName)
7373
vm.doing(.notiEvent)
7474
}
7575
}
7676
})
7777
.onReceive(timerForExp) { _ in
78-
if SPC.gitHubAccessToken.isEmpty == false {
78+
if SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false {
7979
appVM.timeForExpEvent()
8080
}
8181
}

SwiftPamphletApp/SwiftPamphletAppConfig.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
import Foundation
99

1010
struct SPC {
11-
static let gitHubAccessToken = "" // 在这里写上 Github 的access token。在 https://github.com/settings/tokens 申请你的access token。
11+
static let gitHubAccessToken = "" // 在这里可以手动写上 Github 的 access token。在 https://github.com/settings/tokens 申请你的access token。
12+
static let githubUDTokenKey = "udtoken" // UserDefault 存储 token 的 key
13+
static func githubAccessToken() -> String {
14+
let ud = UserDefaults.standard
15+
return ud.string(forKey: SPC.githubUDTokenKey) ?? ""
16+
}
17+
1218
static let detailMinWidth: CGFloat = 550
1319
static let githubHost = "https://github.com/"
1420
static let pamphletIssueRepoName = "ming1016/SwiftPamphletApp"

0 commit comments

Comments
 (0)