diff --git a/SwiftPamphletApp.xcodeproj/project.pbxproj b/SwiftPamphletApp.xcodeproj/project.pbxproj index 0ee4b6643..c0d56d877 100644 --- a/SwiftPamphletApp.xcodeproj/project.pbxproj +++ b/SwiftPamphletApp.xcodeproj/project.pbxproj @@ -170,12 +170,10 @@ 086A5F0E2744E89100FECE02 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 086A5F0D2744E89100FECE02 /* Preview Assets.xcassets */; }; 086A5F2E2744ED8600FECE02 /* ActiveDeveloperListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F2A2744ED8600FECE02 /* ActiveDeveloperListView.swift */; }; 086A5F302744ED8600FECE02 /* IssuesListFromCustomView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F2C2744ED8600FECE02 /* IssuesListFromCustomView.swift */; }; - 086A5F352744ED9600FECE02 /* IssueView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F322744ED9600FECE02 /* IssueView.swift */; }; 086A5F362744ED9600FECE02 /* RepoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F332744ED9600FECE02 /* RepoView.swift */; }; 086A5F372744ED9600FECE02 /* UserView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F342744ED9600FECE02 /* UserView.swift */; }; 086A5F3A2744EDA500FECE02 /* APIRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F382744EDA500FECE02 /* APIRequest.swift */; }; 086A5F3B2744EDA500FECE02 /* GitHubAPIModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F392744EDA500FECE02 /* GitHubAPIModel.swift */; }; - 086A5F402744EDCE00FECE02 /* RepoVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F3D2744EDCE00FECE02 /* RepoVM.swift */; }; 086A5F412744EDCE00FECE02 /* UserVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F3E2744EDCE00FECE02 /* UserVM.swift */; }; 086A5F422744EDCE00FECE02 /* IssueVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F3F2744EDCE00FECE02 /* IssueVM.swift */; }; 086A5F442744EE2800FECE02 /* SwiftPamphletAppConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F432744EE2800FECE02 /* SwiftPamphletAppConfig.swift */; }; @@ -444,12 +442,10 @@ 086A5F0F2744E89100FECE02 /* SwiftPamphletApp.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SwiftPamphletApp.entitlements; sourceTree = ""; }; 086A5F2A2744ED8600FECE02 /* ActiveDeveloperListView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActiveDeveloperListView.swift; sourceTree = ""; }; 086A5F2C2744ED8600FECE02 /* IssuesListFromCustomView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IssuesListFromCustomView.swift; sourceTree = ""; }; - 086A5F322744ED9600FECE02 /* IssueView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IssueView.swift; sourceTree = ""; }; 086A5F332744ED9600FECE02 /* RepoView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepoView.swift; sourceTree = ""; }; 086A5F342744ED9600FECE02 /* UserView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserView.swift; sourceTree = ""; }; 086A5F382744EDA500FECE02 /* APIRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIRequest.swift; sourceTree = ""; }; 086A5F392744EDA500FECE02 /* GitHubAPIModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitHubAPIModel.swift; sourceTree = ""; }; - 086A5F3D2744EDCE00FECE02 /* RepoVM.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepoVM.swift; sourceTree = ""; }; 086A5F3E2744EDCE00FECE02 /* UserVM.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserVM.swift; sourceTree = ""; }; 086A5F3F2744EDCE00FECE02 /* IssueVM.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IssueVM.swift; sourceTree = ""; }; 086A5F432744EE2800FECE02 /* SwiftPamphletAppConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftPamphletAppConfig.swift; sourceTree = ""; }; @@ -1161,7 +1157,6 @@ 086A5F1E2744ECA400FECE02 /* DetailView */ = { isa = PBXGroup; children = ( - 086A5F322744ED9600FECE02 /* IssueView.swift */, 086A5F332744ED9600FECE02 /* RepoView.swift */, 086A5F342744ED9600FECE02 /* UserView.swift */, 08448F4A279E872B00B61353 /* GuideView.swift */, @@ -1189,7 +1184,6 @@ isa = PBXGroup; children = ( 086A5F3F2744EDCE00FECE02 /* IssueVM.swift */, - 086A5F3D2744EDCE00FECE02 /* RepoVM.swift */, 086A5F3E2744EDCE00FECE02 /* UserVM.swift */, ); path = VM; @@ -1720,7 +1714,6 @@ 08BE635027C4C0F2002BC6A8 /* PlayFormView.swift in Sources */, 08397E252B9EEE1300DFDD02 /* InfoListView.swift in Sources */, 08397E2F2B9F353B00DFDD02 /* CategoryListView.swift in Sources */, - 086A5F402744EDCE00FECE02 /* RepoVM.swift in Sources */, 08BE636627C88750002BC6A8 /* PlayProgressView.swift in Sources */, 3AE0D59A2BAB0A0600D6D925 /* DeveloperListView.swift in Sources */, 084417772B99BA3F0049297D /* SidebarView.swift in Sources */, @@ -1730,7 +1723,6 @@ 08ED801C2B9D1EEC0069B7EC /* SettingView.swift in Sources */, 08CD61FE27758B8A008C0935 /* Lexer.swift in Sources */, 086A5F422744EDCE00FECE02 /* IssueVM.swift in Sources */, - 086A5F352744ED9600FECE02 /* IssueView.swift in Sources */, 08522BF027CF9FFA005FF059 /* PlayAnimation.swift in Sources */, 08AEAEDD277EA64900B969E2 /* DBDevNoti.swift in Sources */, 08BE635E27C72F50002BC6A8 /* PlayNavigationView.swift in Sources */, diff --git a/SwiftPamphletApp/GitHubAPI/DetailView/IssueView.swift b/SwiftPamphletApp/GitHubAPI/DetailView/IssueView.swift deleted file mode 100644 index 17f8b0d08..000000000 --- a/SwiftPamphletApp/GitHubAPI/DetailView/IssueView.swift +++ /dev/null @@ -1,95 +0,0 @@ -// -// IssueView.swift -// PresentSwiftUI -// -// Created by Ming Dai on 2021/11/12. -// - -import SwiftUI - -struct IssueView: View { - enum EnterType { - case normal, hiddenUserInfo - } - - @State var vm: IssueVM - @State var type: EnterType = .normal - - var body: some View { - ScrollView { - HStack { - VStack(alignment: .leading, spacing: 10) { - HStack { - Text(vm.issue.title).font(.system(.largeTitle)) - ButtonGoGitHubWeb(url: vm.issue.htmlUrl, text: "在 GitHub 上访问") - Button { - vm.doing(.update) - } label: { - Image(systemName: "arrow.triangle.2.circlepath") - } - - } - if type == .hiddenUserInfo { - Text(" \(howLongFromNow(timeStr:vm.issue.updatedAt))更新过").font(.footnote) - } else { - HStack { - NukeImage(width: 40, height:40, url: vm.issue.user.avatarUrl) - VStack(alignment:.leading) { - NavigationLink(destination: UserView(vm: UserVM(userName: vm.issue.user.login)), label: { - Text(vm.issue.user.login) - }) - HStack { - Text("更新于 ") - GitHubApiTimeView(timeStr: vm.issue.updatedAt) - } - } - } // end HStack - } - MarkdownView(s: vm.issue.body ?? "") // TODO: 等 SwiftUI 的 Text 支持完整的 markdown,再进行替换 - } // end VStack - Spacer() - } // end HStack - .padding(EdgeInsets(top: 20, leading: 10, bottom: 10, trailing: 10)) - - if vm.comments.count > 0 { - HStack { - VStack(alignment: .leading, spacing: 0) { - Text("留言").font(.system(.largeTitle)) - } - .padding(10) - Spacer() - } - ForEach(vm.comments) { comment in - VStack(alignment: .leading) { - GitHubApiTimeView(timeStr: comment.updatedAt) - HStack { - NukeImage(width: 40, height: 40, url: comment.user.avatarUrl) - ButtonGoGitHubWeb(url: comment.user.login, text: comment.user.login, ignoreHost: true) - - Text(comment.authorAssociation) - .font(.footnote) - .foregroundColor(.secondary) - - } - HStack { - VStack(alignment: .leading, spacing: 0) { - MarkdownView(s: comment.body) - } - Spacer() - } - } // end VStack - .padding(10) - Divider() - .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10)) - } // end ForEach - } - - } // end ScrollView - .alert(vm.errMsg, isPresented: $vm.errHint, actions: {}) - .frame(minWidth: SPC.detailMinWidth) - .onAppear { - vm.doing(.inInit) - } - - } -} diff --git a/SwiftPamphletApp/GitHubAPI/DetailView/RepoView.swift b/SwiftPamphletApp/GitHubAPI/DetailView/RepoView.swift index 6e74c070b..0efb83d93 100644 --- a/SwiftPamphletApp/GitHubAPI/DetailView/RepoView.swift +++ b/SwiftPamphletApp/GitHubAPI/DetailView/RepoView.swift @@ -7,127 +7,6 @@ import SwiftUI -struct RepoView: View { - enum EnterType { - case normal, readme - } - @Environment(AppVM.self) var appVM - @State var vm: RepoVM - @State private var tabSelct = 1 - @State var type: EnterType = .normal - var isShowRepoCommitsLink = true - var isShowIssuesLink = true - var isCleanUnread = false - var isCleanExpUnread = false - @State private var expUnreadCount = 0 - - @State var isEnterFullScreen: Bool = false // 全屏控制 - - var body: some View { - HStack { - VStack(alignment: .leading, spacing: 10) { - HStack { - Text(vm.repo.name).font(.system(.largeTitle)) - Text("(\(vm.repo.fullName))") - } - HStack { - Image(systemName: "star.fill").foregroundColor(.red) - Text("\(vm.repo.stargazersCount)") - Image(systemName: "tuningfork").foregroundColor(.cyan) - Text("\(vm.repo.forks)") - Text("议题 \(vm.repo.openIssues)") - Text("语言 \(vm.repo.language ?? "")") - ButtonGoGitHubWeb(url: vm.repo.htmlUrl ?? "https://github.com", text: "在 GitHub 上访问") - Button { - withAnimation { - isEnterFullScreen.toggle() - appVM.fullScreen(isEnter: isEnterFullScreen) - } - } label: { - Image(systemName: isEnterFullScreen == true ? "arrow.down.right.and.arrow.up.left" : "arrow.up.left.and.arrow.down.right") - } - - } - if vm.repo.description != nil { - Text("简介:\(vm.repo.description ?? "")") - } - - HStack { - Text("作者:") - NukeImage(width: 40, height: 40, url: vm.repo.owner.avatarUrl) - ButtonGoGitHubWeb(url: vm.repo.owner.login, text: vm.repo.owner.login, ignoreHost: true) - } - } // end VStack - Spacer() - } - .alert(vm.errMsg, isPresented: $vm.errHint, actions: {}) - .frame(minWidth: SPC.detailMinWidth) - - .padding(EdgeInsets(top: 20, leading: 10, bottom: 0, trailing: 10)) - .onAppear { - if type == .readme { - vm.doing(.inInitJustRepo) - tabSelct = 4 - } else { - vm.doing(.inInit) - } - } - .onDisappear { - appVM.expNotis[vm.repoName]?.unRead = 0 - appVM.calculateExpCountNotis() - } - // end HStack - - TabView(selection: $tabSelct) { - RepoCommitsView(commits: vm.commits, repo: vm.repo, isShowLink: isShowRepoCommitsLink, unReadCount: expUnreadCount) - .tabItem { - Text("新提交") - } - .onAppear(perform: { - vm.doing(.inCommit) - if isCleanExpUnread == true { - expUnreadCount = appVM.expNotis[vm.repoName]?.unRead ?? 0 - vm.doing(.clearExpUnReadCommit) - appVM.expNotis[vm.repoName]?.unRead = SPC.unreadMagicNumber - appVM.calculateExpCountNotis() - } - }) - .tag(1) - - - IssuesView(issues: vm.issues, repo: vm.repo, isShowLink: isShowIssuesLink) - .tabItem { - Text("议题列表") - } - .onAppear { - vm.doing(.inIssues) - } - .tag(2) - - IssueEventsView(issueEvents: vm.issueEvents, repo: vm.repo, isShowLink: isShowIssuesLink) - .tabItem { - Text("议题事件") - } - .onAppear { - vm.doing(.inIssueEvents) - } - .tag(3) - - ReadmeView(content: vm.readme.content.replacingOccurrences(of: "\n", with: "")) - .tabItem { - Text("README") - } - .onAppear { - vm.doing(.inReadme) - } - .tag(4) - - } // end TabView - Spacer() - } - -} - struct ReadmeView: View { @Environment(\.colorScheme) private var colorScheme var content: String @@ -143,17 +22,10 @@ struct ReadmeView: View { struct IssuesView: View { var issues: [IssueModel] var repo: RepoModel - var isShowLink = true var body: some View { List { ForEach(issues) { issue in - if isShowLink == true { - NavigationLink(destination: IssueView(vm: IssueVM(repoName: repo.fullName, issueNumber: issue.number))) { - IssueLabelView(issue: issue) - } - } else { - IssueLabelView(issue: issue) - } + IssueLabelView(issue: issue) } // end ForEach } // end List } // end body @@ -162,17 +34,10 @@ struct IssuesView: View { struct IssueEventsView: View { var issueEvents: [IssueEventModel] var repo: RepoModel - var isShowLink = true var body: some View { List { ForEach(issueEvents) { issueEvent in - if isShowLink == true { - NavigationLink(destination: IssueView(vm: IssueVM(repoName: repo.fullName, issueNumber: issueEvent.issue.number))) { - IssueEventLabelView(issueEvent: issueEvent) - } // end NavigationLink - } else { - IssueEventLabelView(issueEvent: issueEvent) - } + IssueEventLabelView(issueEvent: issueEvent) } // end ForEach } // end List } // end body diff --git a/SwiftPamphletApp/GitHubAPI/DetailView/UserView.swift b/SwiftPamphletApp/GitHubAPI/DetailView/UserView.swift index 46f289ea8..e3dd9f910 100644 --- a/SwiftPamphletApp/GitHubAPI/DetailView/UserView.swift +++ b/SwiftPamphletApp/GitHubAPI/DetailView/UserView.swift @@ -106,19 +106,7 @@ struct UserEventView: View { List { ForEach(Array(events.enumerated()), id: \.0) { i, event in - if isShowUserEventLink == true { - NavigationLink { - UserEventLinkDestination(event: event) - } label: { - AUserEventLabel( - event: event, - isShowActor: isShowActor, - isUnRead: unReadCount > 0 && i < unReadCount - ) - } // end NavigationLink - } else { - AUserEventLabel(event: event, isShowActor: isShowActor, isUnRead: unReadCount > 0 && i < unReadCount) - } + AUserEventLabel(event: event, isShowActor: isShowActor, isUnRead: unReadCount > 0 && i < unReadCount) Divider() } // end ForEach }// end List @@ -138,19 +126,6 @@ struct ListCommits: View { } } -struct UserEventLinkDestination: View { - var event: EventModel - var body: some View { - VStack { - if event.payload.issue?.number != nil { - IssueView(vm: IssueVM(repoName: event.repo.name, issueNumber: event.payload.issue?.number ?? 0)) - } else { - RepoView(vm: RepoVM(repoName: event.repo.name), type: .readme, isShowRepoCommitsLink: false, isShowIssuesLink: false) - } - } - } -} - struct AUserEventLabel: View { var event: EventModel var isShowActor: Bool = false diff --git a/SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift b/SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift index 7dd67e6b4..d807a3c95 100644 --- a/SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift +++ b/SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift @@ -111,13 +111,13 @@ struct EditDeveloper: View { } .tag(1) - IssuesView(issues: repoVM.issues, repo: repoVM.repo, isShowLink: false) + IssuesView(issues: repoVM.issues, repo: repoVM.repo) .tabItem { Text("议题列表") } .tag(2) - IssueEventsView(issueEvents: repoVM.issuesEvents, repo: repoVM.repo, isShowLink: false) + IssueEventsView(issueEvents: repoVM.issuesEvents, repo: repoVM.repo) .tabItem { Text("议题事件") } diff --git a/SwiftPamphletApp/GitHubAPI/ListView/ActiveDeveloperListView.swift b/SwiftPamphletApp/GitHubAPI/ListView/ActiveDeveloperListView.swift index bfd18a275..1d392a5b1 100644 --- a/SwiftPamphletApp/GitHubAPI/ListView/ActiveDeveloperListView.swift +++ b/SwiftPamphletApp/GitHubAPI/ListView/ActiveDeveloperListView.swift @@ -7,44 +7,44 @@ import SwiftUI -struct ActiveDeveloperListView: View { - @Environment(AppVM.self) var appVM - @State var vm: IssueVM - var body: some View { - List { - Section { - ForEach(vm.cIADs) { ad in - ForEach(ad.users) { u in - ActiveDeveloperUnreadLinkView(u: u) - } // end ForEach - } // end ForEach - } header: { - Text("刚更新的").font(.title) - } - ForEach(vm.cIADs) { ad in - Section { - ForEach(ad.users) { u in - if (appVM.devsNotis[u.id] ?? 0) > 0 { - - } else { - NavigationLink(destination: UserView(vm: .init(userName: u.id))) { - ActiveDeveloperListLinkView(u: u) - } - } - } // end ForEach - } header: { - Text(ad.name).font(.title) - } // end Sectioin - } // end Foreach - } // end List - .alert(vm.errMsg, isPresented: $vm.errHint, actions: {}) - .navigationTitle("🤔 开发者") - .onAppear { - vm.doing(.ciads) - } - - } -} +//struct ActiveDeveloperListView: View { +// @Environment(AppVM.self) var appVM +// @State var vm: IssueVM +// var body: some View { +// List { +// Section { +// ForEach(vm.cIADs) { ad in +// ForEach(ad.users) { u in +// ActiveDeveloperUnreadLinkView(u: u) +// } // end ForEach +// } // end ForEach +// } header: { +// Text("刚更新的").font(.title) +// } +// ForEach(vm.cIADs) { ad in +// Section { +// ForEach(ad.users) { u in +// if (appVM.devsNotis[u.id] ?? 0) > 0 { +// +// } else { +// NavigationLink(destination: UserView(vm: .init(userName: u.id))) { +// ActiveDeveloperListLinkView(u: u) +// } +// } +// } // end ForEach +// } header: { +// Text(ad.name).font(.title) +// } // end Sectioin +// } // end Foreach +// } // end List +// .alert(vm.errMsg, isPresented: $vm.errHint, actions: {}) +// .navigationTitle("🤔 开发者") +// .onAppear { +// vm.doing(.ciads) +// } +// +// } +//} // MARK: - 碎视图 diff --git a/SwiftPamphletApp/GitHubAPI/ListView/ExploreRepoListView.swift b/SwiftPamphletApp/GitHubAPI/ListView/ExploreRepoListView.swift index f5e9e52c5..c12a5869a 100644 --- a/SwiftPamphletApp/GitHubAPI/ListView/ExploreRepoListView.swift +++ b/SwiftPamphletApp/GitHubAPI/ListView/ExploreRepoListView.swift @@ -7,103 +7,7 @@ import SwiftUI -struct ExploreRepoListView: View { - @Environment(AppVM.self) var appVM - var showAsGroup: Bool = false - var isArchive = false - var body: some View { - List { - if (SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false) && showAsGroup == false { - Section { - ForEach(isArchive ? appVM.archiveRepos : appVM.exps) { er in - ForEach(er.repos) { r in - ExpListUnreadLinkView(r: r) - } - } - } header: { - Text("刚更新的").font(.title3) - } - - } - - // end Section - ForEach(isArchive ? appVM.archiveRepos : appVM.exps) { er in - if (SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false) && showAsGroup == false { -// Section { -// ForEach(er.repos) { r in -// if (appVM.expNotis[r.id]?.unRead ?? 0) > 0 { -// -// } else { -// NavigationLink(destination: RepoView(vm: RepoVM(repoName: r.id))) { -// ExpListLinkView(r: r) -// } -// } // end if -// -// } -// } header: { -// Text(er.name).font(.title3) -// } - } else { - DisclosureGroupLikeButton { - ForEach(er.repos) { r in - if SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false { - NavigationLink(destination: RepoView(vm: RepoVM(repoName: r.id))) { - ExpListLinkView(r: r) - } - } else { - NavigationLink(destination: RepoWebView(urlStr: SPC.githubHost + r.id)) { - ExpListLinkView(r: r) - } - } - } // end ForEach - } label: { - HStack { - Text(er.name).font(.title3) - Spacer() - } - .background( - // 扩大可选面积 - RoundedRectangle(cornerRadius: 1) - .fill(Color.secondary.opacity(0.0001)) - ) - } - .padding(EdgeInsets(top: 2, leading: 0, bottom: 2, trailing: 0)) - } // end if token - - } // end ForEach - } // end List - .navigationTitle(showAsGroup == false ? "🥷🏻 库动态" : "👾 探索库" ) - .onAppear { - if isArchive { - appVM.loadArchiveRepos() - } else { - appVM.loadExpFromServer() - } - - - } - .onDisappear { - appVM.updateWebLink(s: "") - } - } -} - // MARK: - 碎视图 -struct ExpListUnreadLinkView: View { - @Environment(AppVM.self) var appVM - var r: ARepoModel - var body: some View { - if appVM.expNotis[r.id]?.unRead ?? 0 > 0 { - NavigationLink { - RepoView(vm: RepoVM(repoName: r.id), isCleanExpUnread: true) - } label: { - ExpListLinkView(r: r) - .badge(appVM.expNotis[r.id]?.unRead == SPC.unreadMagicNumber ? 0 : appVM.expNotis[r.id]?.unRead ?? 0) - } // end NavigationLink - } // end if - } // end body -} - struct ExpListLinkView: View { @Environment(AppVM.self) var appVM var r: ARepoModel diff --git a/SwiftPamphletApp/GitHubAPI/VM/RepoVM.swift b/SwiftPamphletApp/GitHubAPI/VM/RepoVM.swift deleted file mode 100644 index 5b8f94d7f..000000000 --- a/SwiftPamphletApp/GitHubAPI/VM/RepoVM.swift +++ /dev/null @@ -1,242 +0,0 @@ -// -// RepoVM.swift -// PresentSwiftUI -// -// Created by Ming Dai on 2021/11/11. -// - -import Foundation -import Combine - -@Observable -final class RepoVM: APIVMable { - private var cancellables: [AnyCancellable] = [] - - @ObservationIgnored - public let repoName: String - - var repo: RepoModel - var commits: [CommitModel] - var issueEvents: [IssueEventModel] - var issues: [IssueModel] - var readme: RepoContent - - var errHint = false - var errMsg = "" - private let errSj = PassthroughSubject() - - private let apiSev: APISev - - private let apRepoSj = PassthroughSubject() - private let apCommitsSj = PassthroughSubject() - - private let apIssueEventsSj = PassthroughSubject() - private let apIssuesSj = PassthroughSubject() - private let apReadmeSj = PassthroughSubject() - - private let resRepoSj = PassthroughSubject() - private let resCommitsSj = PassthroughSubject<[CommitModel], Never>() - private let resNotiCommitsSj = PassthroughSubject<[CommitModel], Never>() - private let resIssueEventsSj = PassthroughSubject<[IssueEventModel], Never>() - private let resIssuesSj = PassthroughSubject<[IssueModel], Never>() - private let resReadmeSj = PassthroughSubject() - - enum RepoActionType { - case inInit, inCommit, inInitJustRepo, inIssueEvents, inIssues, inReadme, clearExpUnReadCommit - } - func doing(_ somethinglike: RepoActionType) { - switch somethinglike { - case .inInit: - apRepoSj.send(()) - apCommitsSj.send(()) - apIssueEventsSj.send(()) - apIssuesSj.send(()) - case .inCommit: - apCommitsSj.send(()) - case .inInitJustRepo: - apRepoSj.send(()) - case .inIssueEvents: - apIssueEventsSj.send(()) - case .inIssues: - apIssuesSj.send(()) - case .inReadme: - apReadmeSj.send(()) - case .clearExpUnReadCommit: - clearExpUnReadCommit() - } - } - - func clearExpUnReadCommit() { - do { - _ = try RepoStoreDataHelper.updateUnread(name: self.repoName, unread: 0) - } catch {} - } - - init(repoName: String) { - self.repoName = repoName - self.apiSev = APISev() - self.repo = RepoModel() - self.commits = [CommitModel]() - self.issueEvents = [IssueEventModel]() - self.issues = [IssueModel]() - self.readme = RepoContent() - - // MARK: - 仓库信息获取 - let reqRepo = RepoRequest(repoName: repoName) - let resRepoSm = apRepoSj - .flatMap { [apiSev] in - apiSev.response(from: reqRepo) - .catch { [weak self] error -> Empty in - self?.errSj.send(error) - return .init() - } - } - .share() - .subscribe(resRepoSj) - let repRepoSm = resRepoSj - .assign(to: \.repo, on: self) - - // MARK: - 获取Commit - let reqCommits = CommitsRequest(repoName: repoName) - let resCommitsSm = apCommitsSj - .flatMap { [apiSev] in - apiSev.response(from: reqCommits) - .catch { [weak self] error -> Empty<[CommitModel], Never> in - self?.errSj.send(error) - return .init() - } - } - .share() - .subscribe(resCommitsSj) - let repCommitsSm = resCommitsSj - .assign(to: \.commits, on: self) - - // MARK: - 获取议题事件 - let reqIssueEvents = IssueEventsRequest(repoName: repoName) - let resIssueEventsSm = apIssueEventsSj - .flatMap { [apiSev] in - apiSev.response(from: reqIssueEvents) - .catch { [weak self] error -> Empty<[IssueEventModel], Never> in - self?.errSj.send(error) - return .init() - } - } - .share() - .subscribe(resIssueEventsSj) - let repIssueEventsSm = resIssueEventsSj - .assign(to: \.issueEvents, on: self) - - // MARK: - 获取议题列表 - let reqIssues = IssuesRequest(repoName: repoName) - let resIssuesSm = apIssuesSj - .flatMap { [apiSev] in - apiSev.response(from: reqIssues) - .catch { [weak self] error -> Empty<[IssueModel], Never> in - self?.errSj.send(error) - return .init() - } - } - .share() - .subscribe(resIssuesSj) - let repIssuesSm = resIssuesSj - .assign(to: \.issues, on: self) - - // MARK: - 获取 Readme - let reqReadme = ReadmeRequest(repoName: repoName) - let resReadmeSm = apReadmeSj - .flatMap { [apiSev] in - apiSev.response(from: reqReadme) - .catch { [weak self] error -> Empty in - self?.errSj.send(error) - return .init() - } - } - .share() - .subscribe(resReadmeSj) - let repReadmeSm = resReadmeSj - .assign(to: \.readme, on: self) - - // MARK: - 错误 - let errMsgSm = errSj - .map { err -> String in - err.message - } - .assign(to: \.errMsg, on: self) - let errHintSm = errSj - .map { _ in - true - } - .assign(to: \.errHint, on: self) - - cancellables += [ - resRepoSm, repRepoSm, - resCommitsSm, repCommitsSm, - resIssueEventsSm, repIssueEventsSm, - resIssuesSm, repIssuesSm, - resReadmeSm, repReadmeSm, - errMsgSm, errHintSm - ] - } - -} - -struct ReadmeRequest: APIReqType { - typealias Res = RepoContent - var repoName: String - var path: String { - return "repos/\(repoName)/readme" - } - var qItems: [URLQueryItem]? { - return [] - } -} - -struct IssuesRequest: APIReqType { - typealias Res = [IssueModel] - var repoName: String - var path: String { - return "repos/\(repoName)/issues" - } - var qItems: [URLQueryItem]? { - return [ -// .init(name: "per_page", value: "100") - ] - } -} - -struct IssueEventsRequest: APIReqType { - typealias Res = [IssueEventModel] - var repoName: String - var path: String { - return "repos/\(repoName)/issues/events" - } - var qItems: [URLQueryItem]? { - return [ - .init(name: "per_page", value: "100") - ] - } -} - -struct CommitsRequest: APIReqType { - typealias Res = [CommitModel] - var repoName: String - var path: String { - return "repos/\(repoName)/commits" - } - var qItems: [URLQueryItem]? { - return [ - .init(name: "per_page", value: "100") - ] - } -} - -struct RepoRequest: APIReqType { - typealias Res = RepoModel - var repoName: String - var path: String { - return "/repos/\(repoName)" - } - var qItems: [URLQueryItem]? { - return nil - } -}