Skip to content

Commit 184eda2

Browse files
committed
完成离线版
* md内容本地读取 * json列表内容本地读 * 离线处理后,内容读取速度提升 * 没有设置token时,不会读取和展示Github内容 * 可以生成不带token的dmg包和上传mac商店
1 parent 6045f93 commit 184eda2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+5549
-1740
lines changed

SwiftPamphletApp.xcodeproj/project.pbxproj

+657-16
Large diffs are not rendered by default.

SwiftPamphletApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
"version": "0.5.3"
2020
}
2121
},
22+
{
23+
"package": "Ink",
24+
"repositoryURL": "https://github.com/johnsundell/ink.git",
25+
"state": {
26+
"branch": null,
27+
"revision": "77c3d8953374a9cf5418ef0bd7108524999de85a",
28+
"version": "0.5.1"
29+
}
30+
},
2231
{
2332
"package": "MarkdownUI",
2433
"repositoryURL": "https://github.com/gonzalezreal/MarkdownUI",

SwiftPamphletApp/AppVM.swift

+32-212
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ final class AppVM: ObservableObject {
1414
@Published var alertMsg = "" // 警告信息
1515
@Published var webLinkStr = "" // 导航上的外部链接
1616

17-
// 仓库动态
18-
@Published var reposNotis = [String: Int]()
19-
@Published var reposCountNotis = 0
2017
// 开发者动态
2118
@Published var devsNotis = [String: Int]()
2219
@Published var devsCountNotis = 0
@@ -29,6 +26,19 @@ final class AppVM: ObservableObject {
2926
@Published var expCountNotis = 0
3027
@Published var exps = [SPReposModel]()
3128

29+
// MARK: - Combine
30+
private var cc: [AnyCancellable] = []
31+
private let apiSev: APISev
32+
33+
// MARK: - Timer for get intervals data
34+
35+
// 开发者动态
36+
private var stepCountDevs = 0
37+
private var devsNotisKeys = [String]()
38+
// 探索库
39+
private var stepCountExp = 0
40+
private var expNotisKeys = [String]()
41+
3242
// MARK: - RSS 读取
3343
func rssFetch() {
3444
Task {
@@ -72,15 +82,9 @@ final class AppVM: ObservableObject {
7282

7383
Task {
7484
var expDic = [String: DBRepoStore]()
75-
let gAPI = RESTful(host: .github)
7685
do {
77-
let issueModel = try await gAPI.value(for: Github.repos(SPC.pamphletIssueRepoName).issues(108).get)
78-
let str = issueModel.body?.base64Decoded() ?? ""
79-
let data = str.data(using: String.Encoding.utf8)!
8086
var grs = [SPReposModel]()
81-
82-
let decoder = JSONDecoder()
83-
grs = try decoder.decode([SPReposModel].self, from: data)
87+
grs = loadBundleJSONFile("repos.json")
8488

8589
for gr in grs {
8690
for r in gr.repos {
@@ -129,15 +133,6 @@ final class AppVM: ObservableObject {
129133
}
130134

131135
// MARK: - Timer for get intervals data
132-
// 仓库动态
133-
private var stepCountRepos = 0
134-
private var reposNotisKeys = [String]()
135-
// 开发者动态
136-
private var stepCountDevs = 0
137-
private var devsNotisKeys = [String]()
138-
// 探索库
139-
private var stepCountExp = 0
140-
private var expNotisKeys = [String]()
141136

142137
// 探索库
143138
func timeForExpEvent() {
@@ -231,94 +226,22 @@ final class AppVM: ObservableObject {
231226
}
232227
}
233228

234-
// 仓库动态
235-
@MainActor
236-
func timeForReposEvent() -> String? {
237-
if reposNotis.count > 0 {
238-
if stepCountRepos >= reposNotis.count {
239-
stepCountRepos = 0
240-
}
241-
if reposNotisKeys.count == 0 {
242-
for (k, _) in reposNotis {
243-
reposNotisKeys.append(k)
244-
}
245-
}
246-
if stepCountRepos >= reposNotisKeys.count {
247-
stepCountRepos = 0
248-
return nil
249-
} else {
250-
let repoName = reposNotisKeys[stepCountRepos]
251-
updateAlertMsg(msg: "已同步 \(repoName)")
252-
loadDBReposLoal()
253-
calculateReposCountNotis()
254-
stepCountRepos += 1
255-
return repoName
256-
} // end if else
257-
258-
} else {
259-
return nil
260-
}
261-
}
262-
263229
// MARK: - On Appear Event
264230
func onAppearEvent() {
265231
nsck()
266-
// 仓库数据读取
267-
loadDBReposLoal()
268-
apReposSj.send(())
269232
// 开发者数据读取
270233
loadDBDevsLoal()
271-
apDevsSj.send(())
272234
// 探索更多库
273235
loadDBExpLoal()
274236
loadExpFromServer()
275237
}
276238

277-
278-
279-
// MARK: - Combine
280-
281-
private var cc: [AnyCancellable] = []
282-
283-
private let apiSev: APISev
284239

285-
private let apReposSj = PassthroughSubject<Void, Never>()
286-
private let resReposSj = PassthroughSubject<IssueModel, Never>()
287-
private let apDevsSj = PassthroughSubject<Void, Never>()
288-
private let resDevsSj = PassthroughSubject<IssueModel, Never>()
289-
290-
init() {
291-
self.apiSev = APISev()
292-
// MARK: - 初始化数据库
293-
let db = DB.shared
294-
do {
295-
try db.cTbs()
296-
} catch {
297-
298-
}
240+
func refreshDev() {
299241

300-
// MARK: - 获取所有开发者通知信息
301-
let reqDevsCustomIssues = IssueRequest(repoName: SPC.pamphletIssueRepoName, issueNumber: 30)
302-
let resDevsSm = apDevsSj
303-
.flatMap { [apiSev] in
304-
apiSev.response(from: reqDevsCustomIssues)
305-
.catch { error -> Empty<IssueModel, Never> in
306-
return .init()
307-
}
308-
}
309-
.share()
310-
.subscribe(resDevsSj)
311-
var devsDic = [String: Int]()
312-
func switchToDevsDic(issueModel: IssueModel) -> [String: Int] {
313-
let str = issueModel.body?.base64Decoded() ?? ""
314-
let data = str.data(using: String.Encoding.utf8)!
315-
var ads = [SPActiveDevelopersModel]()
316-
do {
317-
let decoder = JSONDecoder()
318-
ads = try decoder.decode([SPActiveDevelopersModel].self, from: data)
319-
} catch {
320-
return devsDic
321-
}
242+
func switchToDevsDic() -> [String: Int] {
243+
var devsDic = [String: Int]()
244+
let ads:[SPActiveDevelopersModel] = loadBundleJSONFile("developers.json")
322245
for ad in ads {
323246
for d in ad.users {
324247
do {
@@ -361,88 +284,22 @@ final class AppVM: ObservableObject {
361284

362285
return devsDic
363286
}
364-
let repDevsSm = resDevsSj
365-
.map { issueModel in
366-
return switchToDevsDic(issueModel: issueModel)
367-
} // end map
368-
.assign(to: \.devsNotis, on: self)
369-
370-
371-
// MARK: - 获取所有仓库通知信息
372-
let reqReposCustomIssues = IssueRequest(repoName: SPC.pamphletIssueRepoName, issueNumber: 31)
373-
let resReposSm = apReposSj
374-
.flatMap { [apiSev] in
375-
apiSev.response(from: reqReposCustomIssues)
376-
.catch { error -> Empty<IssueModel, Never> in
377-
return .init()
378-
}
379-
}
380-
.share()
381-
.subscribe(resReposSj)
382-
var reposDic = [String: Int]()
383-
func switchToReposDic(issueModel: IssueModel) -> [String: Int] {
384-
let str = issueModel.body?.base64Decoded() ?? ""
385-
let data = str.data(using: String.Encoding.utf8)!
386-
var grs = [SPReposModel]()
387-
do {
388-
let decoder = JSONDecoder()
389-
grs = try decoder.decode([SPReposModel].self, from: data)
390-
} catch {
391-
return reposDic
392-
}
393-
for gr in grs {
394-
for r in gr.repos {
395-
do {
396-
if let fd = try ReposNotiDataHelper.find(sFullName: r.id) {
397-
reposDic[fd.fullName] = fd.unRead
398-
} else {
399-
do {
400-
let _ = try ReposNotiDataHelper.insert(i: DBRepoNoti(fullName: r.id, lastReadCommitSha: "", unRead: 0))
401-
reposDic[r.id] = 0
402-
} catch {
403-
return reposDic
404-
}
405-
}
406-
} catch {
407-
return reposDic
408-
}
409-
410-
} // end for
411-
} // end for
412-
413-
// 远程已经删除的仓库,同步本地删除
414-
if !(reposDic.count > 0) {
415-
return reposDic
416-
}
417-
let reposDicKeys = reposDic.keys
418-
do {
419-
if let rpsn = try ReposNotiDataHelper.findAll() {
420-
for rpn in rpsn {
421-
if !reposDicKeys.contains(rpn.fullName) {
422-
do {
423-
try ReposNotiDataHelper.delete(i: rpn)
424-
} catch {
425-
return reposDic
426-
} // end do
427-
} // end if
428-
} // end for
429-
} // end if let
430-
} catch {
431-
return reposDic
432-
}
287+
devsNotis = switchToDevsDic()
288+
}
289+
290+
// MARK: Combine
291+
292+
init() {
293+
self.apiSev = APISev()
294+
// MARK: - 初始化数据库
295+
let db = DB.shared
296+
do {
297+
try db.cTbs()
298+
} catch {
433299

434-
return reposDic
435300
}
436-
let repReposSm = resReposSj
437-
.map { issueModel in
438-
return switchToReposDic(issueModel: issueModel)
439-
} // end map
440-
.assign(to: \.reposNotis, on: self)
441301

442-
cc += [
443-
resReposSm, repReposSm,
444-
resDevsSm, repDevsSm
445-
]
302+
446303
}
447304

448305
// MARK: 探索更多库,本地数据库读取
@@ -468,26 +325,6 @@ final class AppVM: ObservableObject {
468325
}
469326
}
470327

471-
472-
// MARK: 仓库动态,本地数据库读取
473-
func loadDBReposLoal() {
474-
do {
475-
if let arr = try ReposNotiDataHelper.findAll() {
476-
if arr.count > 0 {
477-
var ReposDic = [String: Int]()
478-
for i in arr {
479-
if reposNotis[i.fullName] ?? 0 >= SPC.unreadMagicNumber {
480-
ReposDic[i.fullName] = SPC.unreadMagicNumber
481-
} else {
482-
ReposDic[i.fullName] = i.unRead
483-
}
484-
} // end for
485-
reposNotis = ReposDic
486-
} // end if
487-
} // end if
488-
} catch {}
489-
}
490-
491328
// MARK: 开发者动态,本地数据库读取
492329
func loadDBDevsLoal() {
493330
do {
@@ -524,23 +361,6 @@ final class AppVM: ObservableObject {
524361
showAppBadgeLabel()
525362
}
526363

527-
@MainActor
528-
func calculateReposCountNotis() {
529-
var count = 0
530-
for i in reposNotis {
531-
count += i.value
532-
if count > SPC.unreadMagicNumber * 10 {
533-
break
534-
}
535-
}
536-
if count >= SPC.unreadMagicNumber {
537-
count = count - SPC.unreadMagicNumber
538-
}
539-
reposCountNotis = count
540-
showAppBadgeLabel()
541-
542-
}
543-
544364
@MainActor
545365
func calculateDevsCountNotis() {
546366
var count = 0
@@ -558,7 +378,7 @@ final class AppVM: ObservableObject {
558378
}
559379

560380
func showAppBadgeLabel() {
561-
var count = reposCountNotis + devsCountNotis + expCountNotis + rssCountNotis
381+
var count = devsCountNotis + expCountNotis + rssCountNotis
562382
if count > 0 {
563383
if count > SPC.unreadMagicNumber * 10 {
564384
count = SPC.unreadMagicNumber * 10

SwiftPamphletApp/Core/FundationFunction.swift

+23-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ import Combine
1111
import Network
1212
import SwiftDate
1313

14+
15+
// MARK: - Web
16+
func wrapperHtmlContent(content: String, codeStyle: String = "lioshi.min") -> String {
17+
return """
18+
<html lang="zh-Hans" data-darkmode="auto">
19+
\(SPC.rssStyle())
20+
<body>
21+
<main class="container">
22+
<article class="article heti heti--classic">
23+
\(content)
24+
</article>
25+
</main>
26+
</body>
27+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/styles/\(codeStyle).css">
28+
</html>
29+
"""
30+
}
31+
1432
// MARK: - 时间
1533
func howLongFromNow(timeStr: String) -> String {
1634
let cn = Region(zone: Zones.asiaShanghai, locale: Locales.chineseChina)
@@ -118,7 +136,11 @@ func loadBundleData(_ filename: String) -> Data {
118136
} catch {
119137
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
120138
}
121-
139+
}
140+
// 从 Bundle 中取出 String
141+
func loadBundleString(_ filename: String) -> String {
142+
let d = loadBundleData(filename)
143+
return String(decoding: d, as: UTF8.self)
122144
}
123145

124146
// 读取指定路径下文件内容

SwiftPamphletApp/GItHubAPI/DB/DBHandler.swift

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ struct DB {
3434

3535
func cTbs() throws {
3636
do {
37-
try ReposNotiDataHelper.createTable()
3837
try DevsNotiDataHelper.createTable()
3938
try RepoStoreDataHelper.createTable()
4039
try RSSFeedDataHelper.createTable()

0 commit comments

Comments
 (0)