153 lines
5.8 KiB
Swift
153 lines
5.8 KiB
Swift
//
|
||
// PostCompanyView.swift
|
||
// IOS_study
|
||
//
|
||
// Created by CC-star on 2025/7/15.
|
||
//
|
||
|
||
import SwiftUI
|
||
import PhotosUI
|
||
|
||
struct PostCompanyView: View {
|
||
@State var isEditing = false//是否在编辑
|
||
@Environment(HUD.self) var hud//提示信息功能
|
||
@Environment(ProfileViewModel.self) var ProfileVM
|
||
@Binding var naviPath: NavigationPath
|
||
var draftUser: DBUser { ProfileVM.draftUser }
|
||
var invalidMsg: String? {
|
||
if draftUser.cName.isBlank { return "公司名字没有写" }
|
||
if draftUser.cProvince.isBlank { return "公司所在省份没有写" }
|
||
if draftUser.cCity.isBlank { return "公司所在城市没有写" }
|
||
if draftUser.cName.count > 50 { return "公司名字不能超过50字" }
|
||
if draftUser.cCity.count > 50 { return "公司所在城市名不能超过50字" }
|
||
if draftUser.cAboutUS.count > 20 { return "公司简介不能超过20字" }
|
||
return nil
|
||
}
|
||
|
||
|
||
var body: some View {
|
||
@Bindable var vm = ProfileVM
|
||
ScrollView {
|
||
VStack(alignment: .leading, spacing: 14) {
|
||
HStack {
|
||
Text("公司名称:").littleTStyle()
|
||
TextField("请输入完整公司名称", text: $vm.draftUser.cName, axis: .vertical)
|
||
.tfStyle()//单行
|
||
}
|
||
HStack {
|
||
Text("公司地址:").littleTStyle()
|
||
Picker("省份", selection: $vm.draftUser.cProvince) {
|
||
ForEach(kProvinceArr, id: \.self) { Text($0) }
|
||
}.pStyle()
|
||
TextField("城市",text: $vm.draftUser.cCity).tfStyle()
|
||
}
|
||
VStack(alignment: .leading, spacing: 9) {
|
||
Text("公司简介(选填)").littleTStyle()
|
||
TextField("", text: $vm.draftUser.cAboutUS).tfStyleMultiL()
|
||
}
|
||
VStack(alignment: .leading) {
|
||
HStack {
|
||
Text("公司Logo(选填)").littleTStyle()
|
||
Text(vm.logoImageErrorMsg).size15pb()
|
||
}
|
||
PhotosPicker(selection: $vm.logoItem, matching: .images, photoLibrary: .shared()) {
|
||
if let uiImage = vm.logoUIImage {
|
||
Image(uiImage: uiImage)
|
||
.resizable()//可改变大小
|
||
// .scaledToFit()//保持宽高比不变,显示完整图片
|
||
.scaledToFill()//保持宽高比不变,充满空间,会有裁剪
|
||
.thumbFrame().radius()
|
||
} else {
|
||
addImageBtn
|
||
}
|
||
}
|
||
}
|
||
VStack(alignment: .leading) {
|
||
HStack {
|
||
Text("公司图片(选填)").littleTStyle()
|
||
Text(vm.companyUIImagesErrorMsg).size15pb()
|
||
}
|
||
ScrollView(.horizontal) {
|
||
HStack {
|
||
if vm.isLoadingCompanyImages {
|
||
ProgressView().thumbFrame()
|
||
} else {
|
||
ForEach(vm.companyUIImages.indices, id: \.self) { i in
|
||
NavigationLink {
|
||
CompanyGalleryView(currentPage: i)
|
||
} label: {
|
||
Image(uiImage: vm.companyUIImages[i]).resizable().scaledToFill().thumbFrame().radius()
|
||
}.disabled(!vm.isCompanyImagesLoaded)
|
||
}
|
||
}
|
||
if vm.companyUIImages.count < 6 {
|
||
PhotosPicker(selection: $vm.companyItems, maxSelectionCount: 6 - vm.companyUIImages.count, matching: .images, photoLibrary: .shared()) { addImageBtn }
|
||
}
|
||
}
|
||
}.scrollIndicators(.hidden)
|
||
}
|
||
|
||
Button {
|
||
// hud.show("提交成功")
|
||
Task {//开启异步任务
|
||
await postCompany()
|
||
}
|
||
} label: {
|
||
if vm.isPostingCompany {
|
||
ProgressView().appleStyleP()//提交加载
|
||
}else {
|
||
Text("提交").appleStyle()
|
||
}
|
||
}.disabled(vm.isPostingCompany)
|
||
|
||
}.padding()
|
||
postBottomEView()
|
||
}.scrollIndicators(.hidden).tc().size17().bg()
|
||
.navigationBarBackButtonHidden().navigationBarTitleDisplayMode(.inline)
|
||
.toolbar {
|
||
ToolbarItem(placement: .principal) { Text("公司信息").tc().bold() }
|
||
ToolbarItem(placement: .topBarLeading) {
|
||
Button {
|
||
if !naviPath.isEmpty {//检查是否为最后一页
|
||
naviPath.removeLast()//返回上一页
|
||
}
|
||
} label: {
|
||
Image(systemName: "chevron.left").secondary().padding(.horizontal,6)
|
||
}
|
||
}
|
||
}
|
||
.toolbarVisibility(.hidden, for: .tabBar)
|
||
.onAppear {
|
||
if ProfileVM.isCompanyImagesLoaded { return }
|
||
Task {
|
||
await vm.loadCompanyLogoAndIamges()
|
||
}
|
||
}
|
||
}
|
||
|
||
var addImageBtn: some View {
|
||
Image(systemName: "plus").font(.title).secondary().thumbFrame()
|
||
.border(borderColor: Color(.systemGray4))
|
||
}
|
||
func postCompany() async {
|
||
if let invalidMsg = invalidMsg {//也可以写成if let invalidMsg
|
||
hud.show(invalidMsg)
|
||
return
|
||
}
|
||
ProfileVM.isPostingCompany = true
|
||
defer { ProfileVM.isPostingCompany = false }
|
||
UIApplication.shared.hideKeyboard()//收起键盘
|
||
|
||
do {
|
||
try await ProfileVM.postCompany()
|
||
hud.show("发布成功")
|
||
if !naviPath.isEmpty { naviPath.removeLast() }
|
||
} catch {
|
||
print("上传或修改公司信息失败:\(error)")
|
||
hud.show("上传或修改公司信息失败:\(error)")
|
||
}
|
||
|
||
}
|
||
}
|
||
|