Skip to content

Commit

Permalink
项目
Browse files Browse the repository at this point in the history
  • Loading branch information
PGzxc committed Jun 18, 2023
1 parent 8dfc5f2 commit 8b27703
Show file tree
Hide file tree
Showing 10 changed files with 999 additions and 17 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,27 @@ ViewModel(MVVM设计模式):
* Section:列表分组
* LazyVGrid:网格布局

### v5.0-项目

使用到的组件

* Picker:菜单

功能开发

* 1-导航菜单

> 调用项目分类接口,展示导航菜单(每个菜单有一个对应id,作为cid,获取项目列表数据
> 定义一个字典,一个存储项目名称,另一个存储对应id(cid),在项目分类接口完成后设置
* 2-菜单下的文章

> 点击导航菜单时,从字典中取出对应的cid,获取项目列表数据
> 定义一个字典,一个存储cid,一个存储文章列表\<Article>,在项目列表数据接口完成后设置
未完成

* 所有的数据都放在了第一个菜单下(待解决)



32 changes: 32 additions & 0 deletions WanAndroid_SwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
F30653422A2DEA5300AEB416 /* WanAndroid_SwiftUIApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = F30653412A2DEA5300AEB416 /* WanAndroid_SwiftUIApp.swift */; };
F30653462A2DEA5500AEB416 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F30653452A2DEA5500AEB416 /* Assets.xcassets */; };
F30653492A2DEA5500AEB416 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F30653482A2DEA5500AEB416 /* Preview Assets.xcassets */; };
F39290322A3D961D00DCCFC9 /* ProjectModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F39290312A3D961D00DCCFC9 /* ProjectModel.swift */; };
F39290362A3D975800DCCFC9 /* ProjectViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F39290352A3D975800DCCFC9 /* ProjectViewModel.swift */; };
F39290382A3D9C4100DCCFC9 /* ProjectBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = F39290372A3D9C4100DCCFC9 /* ProjectBuilder.swift */; };
F392903A2A3DF6D200DCCFC9 /* ProjectSonModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F39290392A3DF6D200DCCFC9 /* ProjectSonModel.swift */; };
F3B87C5B2A3B16CD0072738D /* LoadingWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3B87C592A3B16CC0072738D /* LoadingWebView.swift */; };
F3B87C5C2A3B16CD0072738D /* PlatformIndependentWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3B87C5A2A3B16CC0072738D /* PlatformIndependentWebView.swift */; };
F3B87C622A3B170D0072738D /* ArticleCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3B87C5E2A3B170D0072738D /* ArticleCellView.swift */; };
Expand Down Expand Up @@ -47,6 +51,10 @@
F30653432A2DEA5300AEB416 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
F30653452A2DEA5500AEB416 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
F30653482A2DEA5500AEB416 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
F39290312A3D961D00DCCFC9 /* ProjectModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProjectModel.swift; sourceTree = "<group>"; };
F39290352A3D975800DCCFC9 /* ProjectViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProjectViewModel.swift; sourceTree = "<group>"; };
F39290372A3D9C4100DCCFC9 /* ProjectBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProjectBuilder.swift; sourceTree = "<group>"; };
F39290392A3DF6D200DCCFC9 /* ProjectSonModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProjectSonModel.swift; sourceTree = "<group>"; };
F3B87C592A3B16CC0072738D /* LoadingWebView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoadingWebView.swift; sourceTree = "<group>"; };
F3B87C5A2A3B16CC0072738D /* PlatformIndependentWebView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlatformIndependentWebView.swift; sourceTree = "<group>"; };
F3B87C5E2A3B170D0072738D /* ArticleCellView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArticleCellView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -144,6 +152,22 @@
path = "Preview Content";
sourceTree = "<group>";
};
F39290332A3D973300DCCFC9 /* view */ = {
isa = PBXGroup;
children = (
F39290372A3D9C4100DCCFC9 /* ProjectBuilder.swift */,
);
path = view;
sourceTree = "<group>";
};
F39290342A3D973B00DCCFC9 /* viewmodel */ = {
isa = PBXGroup;
children = (
F39290352A3D975800DCCFC9 /* ProjectViewModel.swift */,
);
path = viewmodel;
sourceTree = "<group>";
};
F3B87C582A3B16CC0072738D /* WebView */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -181,6 +205,8 @@
F3BDCCE02A37078F00FC3CCD /* BannerModel.swift */,
F3BDCCE22A3707B900FC3CCD /* CollectListModel.swift */,
F3D5EE162A3C5748000ACDFF /* TreeModel.swift */,
F39290312A3D961D00DCCFC9 /* ProjectModel.swift */,
F39290392A3DF6D200DCCFC9 /* ProjectSonModel.swift */,
);
path = model;
sourceTree = "<group>";
Expand Down Expand Up @@ -264,6 +290,8 @@
F3D56E9E2A36CC4D008876F0 /* Project */ = {
isa = PBXGroup;
children = (
F39290342A3D973B00DCCFC9 /* viewmodel */,
F39290332A3D973300DCCFC9 /* view */,
F3D56E9F2A36CC4D008876F0 /* ProjectView.swift */,
);
path = Project;
Expand Down Expand Up @@ -407,7 +435,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F39290362A3D975800DCCFC9 /* ProjectViewModel.swift in Sources */,
F3D5EE152A3C5687000ACDFF /* NavigationViewModel.swift in Sources */,
F39290382A3D9C4100DCCFC9 /* ProjectBuilder.swift in Sources */,
F3B87C652A3B170D0072738D /* ImageCarouseViewBuilder.swift in Sources */,
F3BDCCED2A370A0800FC3CCD /* HomeViewModel.swift in Sources */,
F3D5EE172A3C5748000ACDFF /* TreeModel.swift in Sources */,
Expand All @@ -425,6 +455,8 @@
F3BDCCE82A37094D00FC3CCD /* Router.swift in Sources */,
F3BDCCDB2A37060500FC3CCD /* ModelProtocol.swift in Sources */,
F3D56EA42A36CC4D008876F0 /* TabBarView.swift in Sources */,
F392903A2A3DF6D200DCCFC9 /* ProjectSonModel.swift in Sources */,
F39290322A3D961D00DCCFC9 /* ProjectModel.swift in Sources */,
F3B87C5C2A3B16CD0072738D /* PlatformIndependentWebView.swift in Sources */,
F3B87C5B2A3B16CD0072738D /* LoadingWebView.swift in Sources */,
F3BDCCDD2A37063100FC3CCD /* ArticleModel.swift in Sources */,
Expand Down
8 changes: 7 additions & 1 deletion WanAndroid_SwiftUI/Content/Project/ProjectView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import SwiftUI

struct ProjectView: View {
@StateObject private var vm: ProjectViewModel = ProjectViewModel()
var body: some View {
Text("ProjectView")
ProjectBuilder(vm: .constant(vm)).pickView
ProjectBuilder(vm: .constant(vm)).articleView
// ProjectBuilder(vm: .constant(vm)).onAppear(){
// vm.getProjectModel()
// }

}
}

Expand Down
72 changes: 72 additions & 0 deletions WanAndroid_SwiftUI/Content/Project/view/ProjectBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// ProjectBuilderView.swift
// WanAndroid_SwiftUI
//
// Created by zxc on 2023/6/17.
//

import SwiftUI

struct ProjectBuilder: View {

@Binding var vm: ProjectViewModel

var body: some View {

VStack(alignment: .center){

ScrollView{

//1-导航栏
//pickView
//文章
//articleView()

}
}
}

var pickView: some View{
ScrollView(.horizontal){
HStack(spacing: 0){
Picker("Options",selection: $vm.options.first ?? $vm.options_first){

ForEach(vm.options,id: \.self) { option in
Text(option).onAppear(){
vm.getProjectTreeArticle(page: 1, cid: vm.optionsDic[option] ?? 0)
}
}

}
.pickerStyle(SegmentedPickerStyle())
.shadow(color: .white, radius: 100)
.shadow(color: Color.brown.opacity(0.8), radius: 5, x: 0, y: 2)
.padding()
}
}

}

var articleView: some View{
ScrollView{
ForEach(vm.options,id: \.self) { option in
let articleList = vm.projectDictionArticleDataList[vm.optionsDic[option]!]
if(articleList != nil){
ForEach(articleList!){ article in
ArticleCellView(article: .constant(article))
}
}
}
}

}
}


struct ProjectBuilderView_Previews: PreviewProvider {

static var previews: some View {
let vm: ProjectViewModel = ProjectViewModel()
ProjectBuilder(vm: .constant(vm))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// ProjectViewModel.swift
// WanAndroid_SwiftUI
//
// Created by zxc on 2023/6/17.
//

import Foundation

class ProjectViewModel:ObservableObject{

@Published var projectModel:ProjectModel? = nil
@Published var projectSonModel:ProjectSonModel? = nil
@Published var projectDictionArticleDataList:Dictionary<Int,[Article]> = [:]

var optionsDic:Dictionary<String,Int> = [:]
var options:[String] = []
var options_first: String = ""

init(){

getProjectModel()

}

//体系数据
func getProjectModel(){
APIService.instance.getProject(completion: {(projectModel,error) in
if let error = error{
debugPrint(error)
return
}
projectModel?.data.forEach({ project in
self.options.append(project.name)
self.optionsDic.updateValue(project.id, forKey: project.name)
})
self.options_first = self.options.first ?? ""
self.projectModel = projectModel
})
}

//知识体系下的文章
func getProjectTreeArticle(page:Int,cid:Int){
APIService.instance.getProjectArticle(with: page, andWith: cid,completion: {(projectSonModel,error) in
if let error = error{
debugPrint(error)
return
}

self.projectSonModel = projectSonModel
self.projectDictionArticleDataList.updateValue(projectSonModel!.data.datas, forKey: cid)
//self.projectSonModelDataList.append(projectSonModel!.data)
})

}

}
2 changes: 1 addition & 1 deletion WanAndroid_SwiftUI/api/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct API {
/// - 页码:拼接在链接中,从1开始
///******************************************************************/

static var projectArticleList = "/project/list/%s/json?cid=%s";
static var projectArticleList = "/project/list/%d/json";

///5- 登陆与注册

Expand Down
34 changes: 31 additions & 3 deletions WanAndroid_SwiftUI/api/APIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Alamofire


struct APIService {

//MARK: - Singleton
static let instance = APIService()

Expand All @@ -32,7 +32,7 @@ struct APIService {
}

func getArticle(with page: Int,completion:@escaping (ArticleModel?, Error?)->()){

Alamofire.request(Router.homeArticleList(page)).responseArticleModel{(response: DataResponse<ArticleModel>) in

if let error = response.error{
Expand Down Expand Up @@ -68,7 +68,7 @@ struct APIService {
func getCollectList(with index: Int,completion:@escaping (CollectListModel?, Error?)->()){

Alamofire.request(Router.collectList(index)).responseCollectListModel{(response:DataResponse<CollectListModel>) in

if let error = response.error{
completion(nil,error)
return
Expand All @@ -93,6 +93,34 @@ struct APIService {
}
}
}
//项目
func getProject(completion:@escaping (ProjectModel?, Error?)->()){
Alamofire.request(Router.project).responseProjectModel{(response: DataResponse<ProjectModel>) in
if let error = response.error{
completion(nil,error)
return
}
if let projectmodel = response.result.value {
completion(projectmodel,nil)
return
}
}
}
//项目-子-文章
func getProjectArticle(with page: Int, andWith cid:Int,completion:@escaping (ProjectSonModel?, Error?)->()){
Alamofire.request(Router.projectArticle(page, cid)).responseProjectSonModel{(response: DataResponse<ProjectSonModel>) in
if let error = response.error{
completion(nil,error)
return
}
if let projectSonModel = response.result.value {
completion(projectSonModel,nil)
return
}
}
}


}


Expand Down
32 changes: 20 additions & 12 deletions WanAndroid_SwiftUI/api/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ internal extension APIService{
case login(String,String) //用户登录
case collectList(Int?)
case tree //体系


case project //项目
case projectArticle(Int?,Int) //项目文章


var baseURL: URL {
return URL(string: API.baseURL)!
}

var method: HTTPMethod {
switch self {
case .homeBanner: return .get
case .homeArticleList: return .get
case .login: return .post
case .collectList: return .get
case .tree: return .get
case .project: return .get
case .projectArticle: return .get

}
}
Expand All @@ -41,20 +45,24 @@ internal extension APIService{
case .login(_, _): return API.login
case .collectList(let index):return String(format: API.collectList, index ?? 0)
case .tree: return API.treeList
case .project: return API.projectTreeList
case .projectArticle(let page,_): return String(format: API.projectArticleList, page ?? 0)
}
}

var params: [String: Any]? {
switch self {
case .homeArticleList:return nil
case .homeBanner:return nil
case .login(let username, let password):
return ["username":username,"password":password]
case .collectList: return nil
case .tree: return nil
}
switch self {
case .homeArticleList:return nil
case .homeBanner:return nil
case .login(let username, let password):
return ["username":username,"password":password]
case .collectList: return nil
case .tree: return nil
case .project: return nil
case .projectArticle(_,let cid): return ["cid":cid]
}

}

func asURLRequest() throws -> URLRequest {

let url = baseURL.appendingPathComponent(path)
Expand Down
Loading

0 comments on commit 8b27703

Please sign in to comment.