Here is my whole code:
Post
Replies
Boosts
Views
Activity
import SwiftUI
@available(macOS 10.15, *)
struct AppView: View {
@State private var phoneNumber: String = ""
@State private var isLoggedIn: Bool = false
@State private var username: String = ""
@State private var selectedView: Int = 0
@State private var messageToSend: String = ""
@StateObject private var friendManager = FriendManager()
@StateObject private var chatManager = ChatManager()
var body: some View {
VStack {
if !isLoggedIn {
// If the user is not logged in, display the login screen
VStack(alignment: .leading) {
Text("Please enter your phone number:")
.font(.headline)
TextField("Phone number", text: $phoneNumber)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.bottom, 20)
Button(action: {
// Attempt to log in using phone number
isLoggedIn = true
}) {
Text("Log in")
.foregroundColor(.white)
.padding()
.background(Color.blue)
.cornerRadius(5)
}
}
.padding()
} else if username == "" {
// If the user is logged in but has not selected a username, display the username selection screen
VStack(alignment: .leading) {
Text("Please select a username:")
.font(.headline)
TextField("Username", text: $username)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.bottom, 20)
Button(action: {
// Save username to user defaults or server
}) {
Text("Save")
.foregroundColor(.white)
.padding()
.background(Color.blue)
.cornerRadius(5)
}
}
.padding()
} else {
// If the user is logged in and has selected a username, display the chat, call, and map views
ZStack {
TabView(selection: $selectedView) {
ChatView(friendManager: friendManager, chatManager: chatManager, username: username)
.tabItem {
Image(systemName: "bubble.left.and.bubble.right")
Text("Chat")
}
.tag(0)
CallView(friendManager: friendManager, username: username)
.tabItem {
Image(systemName: "phone.fill")
Text("Call")
}
.tag(1)
MapView()
.tag(2)
}
.accentColor(.blue)
HStack {
Spacer()
Button(action: {
// Show profile menu
}) {
Image(systemName: "person.crop.circle.fill")
.foregroundColor(.blue)
.padding()
}
}
}
}
}
}
}
@available(macOS 11.0, *)
struct ChatView: View {
@StateObject private var friendManager: FriendManager
@StateObject private var chatManager: ChatManager
@State private var searchText: String = ""
@State private var messageToSend: String = ""
let username: String
var body: some View {
VStack {
// Search bar
HStack {
TextField("Search", text: $searchText)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: searchForFriends) {
Image(systemName: "magnifyingglass")
.foregroundColor(.blue)
}
}
.padding()
// List of chat messages
ScrollView {
ForEach(chatManager.messages, id: .self) { message in
VStack(alignment: .leading) {
Text(message.sender)
.font(.subheadline)
.foregroundColor(.gray)
Text(message.text)
.font(.body)
.padding(.vertical, 4)
}
.padding(.horizontal, 8)
}
}
// Message input field
HStack {
TextField("Enter message", text: $messageToSend)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: sendMessage) {
Image(systemName: "arrow.up.circle.fill")
.foregroundColor(.blue)
}
}
.padding()
}
}
private func searchForFriends() {
// Search for friends using API or local data
}
private func sendMessage() {
// Send message to server using network library
chatManager.addMessage(Message(text: messageToSend, sender: username))
messageToSend = ""
}
}
@available(macOS 10.15, *)
struct CallView: View {
@StateObject private var friendManager: FriendManager
@State private var searchText: String = ""
let username: String
@available(macOS 10.15.0, *)
var body: some View {
VStack {
// Search bar
HStack {
TextField("Search", text: $searchText)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: searchForFriends) {
Image(systemName: "magnifyingglass")
.foregroundColor(.blue)
}
}
.padding()
// List of friends
ScrollView {
ForEach(friendManager.friends) { friend in
HStack {
Text(friend.username)
.font(.body)
.padding(.vertical, 8)
Spacer()
Button(action: {
// Call friend using call library
}) {
Image(systemName: "phone.fill")
.foregroundColor(.blue)
.padding(.vertical, 8)
}
}
}
}
}
}
private func searchForFriends() {
// Search for friends using API or local data
}
}
struct MapView: View {
@available(macOS 10.15.0, *)
var body: some View {
Map()
.edgesIgnoringSafeArea(.all)
}
}
struct Friend {
let username: String
}
struct Message {
let text: String
let sender: String
}
@available(macOS 10.15, *)
class FriendManager: ObservableObject {
@Published var friends: [Friend] = []
func addFriend(friend: Friend) {
friends.append(friend)
}
func searchForFriends(searchText: String) -> [Friend] {
// Search for friends using API or local data
return []
}
}
@available(macOS 10.15, *)
class ChatManager: ObservableObject {
@Published var messages: [Message] = []
func addMessage(message: Message) {
messages.append(message)
}
}
THANK YOU VERY MUCH! It worked but there is still this one mistake in the App section, and I don't know how to fix it:
Never mind, I just fixed it by changing it to AppView()
Still thank you very much for your support!