code: i have “view” and download buttons and initially set isViewButtonCLicked: Bool = false
but before clicking view button if i click download why view button statements prints and in both view and download buttons only sheet is opening why???
if i click view i need to open sheet and if i click on download i need to call downloadFile() function. how to fix this?? where am i wrong??? plz guide.
struct AssignmentDetailsVIew: View {
@Environment(.dismiss) var dismiss
@StateObject private var viewModel = AssignmentViewModel()
@State private var isViewButtonCLicked: Bool = false
@State private var isDownload: Bool = false
@State private var isLoading = true
var body: some View {
ZStack {
VStack(spacing: 0) {
List {
detailsView()
.listRowInsets(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 0))
}
.listStyle(.plain)
}
}
.onAppear {
viewModel.assignmentDetails(ID: self.ID){ status in
}
viewModel.assignmentSubmissionRpt(ID: self.ID, NotSubmitted: "false"){ status in
}
}
.sheet(isPresented: $isViewButtonCLicked) {
FileView(isLoading: $isLoading, filePath: viewModel.assignmentDetails?.file)
}
}
@ViewBuilder func detailsView() -> some View {
VStack(alignment: .leading, spacing: 10) {
HStack{
HStack {
Spacer()
Button {
print("View button clicked before setting isFileView")
isViewButtonCLicked = true
print("View button clicked, isFileView set to true")
} label: {
VStack(spacing: 3) {
Image("EyeProfile")
.font(.system(size: 30))
.foregroundStyle(Color.brandColor)
Text("View")
.fixedSize(horizontal: false, vertical: true)
.font(.calibriBold(with: 18))
.foregroundStyle(Color.hex49493C)
}
}
Spacer()
}
HStack {
Spacer()
Button {
isDownload = true
print("Download button clicked, initiating download")
if isDownload{
downloadFile()
}
} label: {
VStack(spacing: 3) {
Image("EyeProfile")
.font(.system(size: 30))
.foregroundStyle(Color.brandColor)
Text("Download")
.fixedSize(horizontal: false, vertical: true)
.font(.calibriBold(with: 18))
.foregroundStyle(Color.hex49493C)
}
}
Spacer()
}
}
.padding(.top, 25)
}
}
private func downloadFile() {
guard let filePathdownload = viewModel.assignmentDetails?.file else {
return
}
let strPrefix = "https://api-testncklznc/Assignment/"
let finalUrldownload = "(strPrefix)(filePathdownload)"
guard let url = URL(string: finalUrldownload) else {
print("Invalid URL")
return
}
//some download code
}
}
import WebKit
struct WebViewNew: UIViewRepresentable {
let url: URL
@Binding var isLoading: Bool
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
//somewebviewcode
}
struct FileView: View {
@Binding var isLoading: Bool
var filePath: String?
var body: some View {
let strPrefix = "https://apitest/Assignment/"
let finalUrl = "(strPrefix)(filePath ?? "")"
Text("finalUrl.....(finalUrl)")
if let url = URL(string: finalUrl) {
WebViewNew(url: url, isLoading: $isLoading)
.edgesIgnoringSafeArea(.all)
if isLoading {
ProgressView()
}
}
}
}
o/p: for debugging got in console after clicking download button…. why isFileView becoming true before clicking view button??? how to solve this
View button clicked before setting isFileView
View button clicked, isFileView set to true
Download button clicked, initiating download