Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

done #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
438 changes: 438 additions & 0 deletions ChattingApp/ChattingApp.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
10 changes: 10 additions & 0 deletions ChattingApp/ChattingApp.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
38 changes: 38 additions & 0 deletions ChattingApp/ChattingApp/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// AppDelegate.swift
// ChattingApp
//
// Created by dmdm on 01/11/2021.
//

import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions ChattingApp/ChattingApp/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "icon.PNG",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
156 changes: 156 additions & 0 deletions ChattingApp/ChattingApp/ChatViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
//
// ChatViewController.swift
// ChattingApp
//
// Created by dmdm on 11/11/2021.
//

import UIKit
import Firebase
import FirebaseFirestore

class ChatVC: UIViewController {
var user : User?
var messages = [Message]()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
getAllMessages()
chatTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
let chatTableView : UITableView = {
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(UITableView())
let messageTextField : UITextField = {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.placeholder = "Message..."
$0.backgroundColor = .init(white: 0.85, alpha: 1)
$0.layer.cornerRadius = 22.5
return $0
}(UITextField())
let sendButton : UIButton = {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.setBackgroundImage(UIImage(systemName: "paperplane.circle.fill"), for: .normal)
$0.tintColor = .brown
$0.addTarget(self, action: #selector(sendMessage), for: .touchUpInside)
return $0
}(UIButton(type: .system))
func setupUI() {
view.backgroundColor = .white
navigationItem.title = user?.name
chatTableView.delegate = self
chatTableView.dataSource = self
view.addSubview(chatTableView)
view.addSubview(messageTextField)
view.addSubview(sendButton)
NSLayoutConstraint.activate([
chatTableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
chatTableView.leftAnchor.constraint(equalTo: view.leftAnchor),
chatTableView.rightAnchor.constraint(equalTo: view.rightAnchor),
chatTableView.bottomAnchor.constraint(equalTo: messageTextField.topAnchor),
messageTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 15),
messageTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor, constant: -5),//*****
messageTextField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
messageTextField.heightAnchor.constraint(equalToConstant: 45),
sendButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -15),
sendButton.heightAnchor.constraint(equalTo: messageTextField.heightAnchor),
sendButton.widthAnchor.constraint(equalTo: sendButton.heightAnchor),
sendButton.centerYAnchor.constraint(equalTo: messageTextField.centerYAnchor)
])
}
}
extension ChatVC : UITableViewDelegate , UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = chatTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let currentUserID = Auth.auth().currentUser?.uid
cell.textLabel?.text = messages[indexPath.row].content
print(messages[indexPath.row].content)
if messages[indexPath.row].sender == currentUserID {
cell.textLabel?.textAlignment = .right
cell.textLabel?.textColor = .blue
} else {
cell.textLabel?.textAlignment = .left
cell.textLabel?.textColor = .red
}
return cell
}
}
extension ChatVC {
@objc func sendMessage() {
let messageId = UUID().uuidString
guard let currentUserID = Auth.auth().currentUser?.uid else {return}
guard let message = messageTextField.text else {return}
guard let user = user else {return}
Firestore.firestore().document("messages/\(messageId)").setData([
"sender" : currentUserID,
"receiver" : user.id!,
"content" : message,
"id": messageId,
])
messageTextField.text = ""
}
func getAllMessages() {
guard let chatID = user?.id else {return}
guard let userID = Auth.auth().currentUser?.uid else {return}
self.messages.removeAll()
//self.chatTableView.reloadData()
// self.messages = []
Firestore.firestore()
.collection("messages")
.whereField("receiver", isEqualTo: userID)
.whereField("sender", isEqualTo: chatID)
.addSnapshotListener { snapshot, error in
//guard let userID = Auth.auth().currentUser?.uid else {return}
if error == nil {
for document in snapshot!.documents{
let data = document.data()
let newMsg = Message(
content: data["content"] as? String,
sender: data["sender"] as? String,
reciever: data["receiver"] as? String,
id: (data["id"] as? String) ?? "",
timestamp: (data["timestamp"] as? Timestamp) ?? Timestamp()
)
let isMsgAdded = self.messages.contains { msg in
return msg.id == newMsg.id
}
if !isMsgAdded {
self.messages.append(newMsg)
}
}
self.chatTableView.reloadData()
}
//self.chatTableView.reloadData()
}
Firestore.firestore()
.collection("messages")
.whereField("sender", isEqualTo: userID)
.whereField("receiver", isEqualTo: chatID)
.addSnapshotListener { snapshot, error in
if error == nil {
for document in snapshot!.documents{
let data = document.data()
let newMsg = Message(
content: data["content"] as? String,
sender: data["sender"] as? String,
reciever: data["receiver"] as? String,
id: (data["id"] as? String) ?? "",
timestamp: (data["timestamp"] as? Timestamp) ?? Timestamp()
)
let isMsgAdded = self.messages.contains { msg in
return msg.id == newMsg.id
}
if !isMsgAdded {
self.messages.append(newMsg)
}
self.chatTableView.reloadData()
}
}
// self.chatTableView.reloadData()
}
}
}
Loading