IOS_Boss/IOS_study/Post/PostCompanyView.swift
2025-07-27 12:33:06 +08:00

153 lines
5.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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)")
}
}
}