-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCreatePostView.swift
128 lines (114 loc) · 4.75 KB
/
CreatePostView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import SwiftUI
import PhotosUI
struct CreatePostView: View {
@State private var userInput: String = ""
@State private var showAlert: Bool = false
@State private var alertTitle: String = ""
@State private var alertMessage: String = ""
@State private var selectedImage: UIImage? = nil
@State private var showPhotoPicker = false
@State private var isUploadingImage = false
@Environment(\.presentationMode) var presentationMode // Handle modal dismissal
var body: some View {
VStack(alignment: .center) {
HStack {
// Cancel Button to dismiss CreatePostView
Button(action: {
self.presentationMode.wrappedValue.dismiss() // Dismiss the view
}) {
Text("Cancel")
.foregroundColor(.blue)
}
.padding(.leading, 20)
Spacer()
}
.padding(.top, 20)
Spacer()
Text("Make a Post")
.font(.largeTitle)
.bold()
.padding(.bottom, 20)
// Post Text Field - Centered
TextField(
"Post text, lorem ipsum day...",
text: $userInput,
axis: .vertical
)
.textFieldStyle(.roundedBorder)
.lineLimit(10, reservesSpace: true)
.multilineTextAlignment(.leading)
.frame(minWidth: 100, maxWidth: 400, minHeight: 100, maxHeight: 250)
.padding(.horizontal, 20)
// Show selected image preview
if let image = selectedImage {
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(width: 200, height: 200)
.cornerRadius(10)
}
// Action Buttons - Centered
HStack(alignment: .center, spacing: 20) {
Button("Add Image") {
showPhotoPicker = true // Show the photo picker
}
.frame(width: 120, height: 44)
.background(Color.blue)
.cornerRadius(40)
.foregroundColor(.white)
Button("Create Post") {
Task {
do {
var imageUrl: String? = nil
if let selectedImage = selectedImage {
isUploadingImage = true
// Upload the image to Cloudinary and get the image URL
imageUrl = try await PostService.uploadImageToCloudinary(image: selectedImage)
}
// Create the post with or without an image URL
_ = try await PostService.createPost(message: userInput, image: selectedImage)
// Show success alert
alertTitle = "Post Created"
alertMessage = "Your post has been created successfully."
showAlert = true
} catch {
// Show error alert
alertTitle = "Error"
alertMessage = "Failed to create the post. Please try again."
showAlert = true
}
}
}
.frame(width: 120, height: 44)
.background(Color.blue)
.cornerRadius(40)
.foregroundColor(.white)
.disabled(isUploadingImage) // Disable if image is uploading
}
.padding(.top, 30)
Spacer()
// Alert for showing success or error message
.alert(isPresented: $showAlert) {
Alert(
title: Text(alertTitle),
message: Text(alertMessage),
dismissButton: .default(Text("OK"), action: {
if alertTitle == "Post Created" {
// Dismiss the CreatePostView modal and return to MainView
self.presentationMode.wrappedValue.dismiss()
}
})
)
}
}
.background(Color(red: 0, green: 0.96, blue: 1).ignoresSafeArea()) // Cover entire screen with background color
.navigationBarHidden(true) // Hide default navigation bar
.sheet(isPresented: $showPhotoPicker) {
// Use SwiftUI's photo picker
PhotoPicker(selectedImage: $selectedImage)
}
}
}
#Preview {
CreatePostView()
}