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

simulation of typing && random color of text #17

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
finished
  • Loading branch information
vvvvvvvvvvvvvvvvvvv2 committed Aug 16, 2022
commit 21fd864ddeb9fd2bcdda71168e30ac777f267f43
Binary file not shown.
98 changes: 56 additions & 42 deletions Flash Chat iOS13/Controllers/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import FirebaseFirestore


class ChatViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var messageTextfield: UITextField!

Expand All @@ -31,76 +31,90 @@ class ChatViewController: UIViewController {
loadMessages()
}
func loadMessages() {

db.collection(K.FStore.collectionName)
.order(by: K.FStore.dateField)
.addSnapshotListener { querySnapshot, err in
if let err = err{
print("smth wrong \(err)")
}else {

self.messages = []
//or//self.tableView.clearsContextBeforeDrawing
if let snapshotDocumets = querySnapshot?.documents {
for doc in snapshotDocumets{
let data = doc.data()
print(doc.data())
if let messageSender = data[K.FStore.senderField] as? String, let messageBody = data[K.FStore.bodyField] as? String {
let newMessage = Message(sender: messageSender, body: messageBody)
self.messages.append(newMessage)

DispatchQueue.main.async {
self.tableView.reloadData()
if let err = err{
print("smth wrong \(err)")
}else {

self.messages = []
//or//self.tableView.clearsContextBeforeDrawing
if let snapshotDocumets = querySnapshot?.documents {
for doc in snapshotDocumets{
let data = doc.data()
print(doc.data())
if let messageSender = data[K.FStore.senderField] as? String, let messageBody = data[K.FStore.bodyField] as? String {
let newMessage = Message(sender: messageSender, body: messageBody)
self.messages.append(newMessage)

DispatchQueue.main.async {
self.tableView.reloadData()
let indexPath = IndexPath(row: self.messages.count - 1,section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
}
}

}
}
}
}
}
@IBAction func sendPressed(_ sender: UIButton) {
if let messageBody = messageTextfield.text, let messageSender = Auth.auth().currentUser?.email {
db.collection(K.FStore.collectionName).addDocument(data: [
K.FStore.senderField: messageSender,
K.FStore.bodyField: messageBody,
K.FStore.dateField: Date().timeIntervalSince1970]) { err in
if let err = err {
print("trouble in data sender \(err)")
}else{
print("bebra deployed to space")

DispatchQueue.main.async {
self.loadMessages()
K.FStore.senderField: messageSender,
K.FStore.bodyField: messageBody,
K.FStore.dateField: Date().timeIntervalSince1970]) { err in
if let err = err {
print("trouble in data sender \(err)")
}else{
print("bebra deployed to space")
self.messageTextfield.text = ""

DispatchQueue.main.async {
self.loadMessages()
}
}
}
}
}
}
@IBAction func logOutPressed(_ sender: UIBarButtonItem) {
do {
try Auth.auth().signOut()
navigationController?.popToRootViewController(animated: true)
} catch let signOutError as NSError {
print("Error signing out: %@", signOutError)
}

do {
try Auth.auth().signOut()
navigationController?.popToRootViewController(animated: true)
} catch let signOutError as NSError {
print("Error signing out: %@", signOutError)
}
}


}
extension ChatViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let message = messages[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath)
as! MessageCell
cell.label.text = messages[indexPath.row].body
as! MessageCell
cell.label.text = message.body

if message.sender == Auth.auth().currentUser?.email {
cell.leftImageView.isHidden = true
cell.avatarImageView.isHidden = false

}else {
cell.leftImageView.isHidden = false
cell.avatarImageView.isHidden = true

}

return cell
}



}
20 changes: 10 additions & 10 deletions Flash Chat iOS13/Controllers/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ import FirebaseAuth
import FirebaseCore

class LoginViewController: UIViewController {

@IBAction func accountSwith(_ sender: UISegmentedControl) {
emailTextfield.text = "2@23g.com"
passwordTextfield.text = "werwer123"
print(sender)
}

@IBOutlet weak var emailTextfield: UITextField!
@IBOutlet weak var passwordTextfield: UITextField!


@IBAction func loginPressed(_ sender: UIButton) {
if let email = emailTextfield.text, let password = passwordTextfield.text{
Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
if let e = error {
print(e)
} else {
self.performSegue(withIdentifier: K.loginSegue, sender: self)
Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
if let e = error {
print(e)
} else {
self.performSegue(withIdentifier: K.loginSegue, sender: self)
}

}

}
}
}
}
18 changes: 9 additions & 9 deletions Flash Chat iOS13/Controllers/RegisterViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import FirebaseCore
import FirebaseAuth

class RegisterViewController: UIViewController {

@IBOutlet weak var emailTextfield: UITextField!
@IBOutlet weak var passwordTextfield: UITextField!

@IBAction func registerPressed(_ sender: UIButton) {

if let email = emailTextfield.text, let password = passwordTextfield.text {
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if let e = error{
print(e)
}else {
self.performSegue(withIdentifier: K.registerSegue, sender: self)



Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if let e = error{
print(e)
}else {
self.performSegue(withIdentifier: K.registerSegue, sender: self)
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions Flash Chat iOS13/Views/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
</view>
<navigationItem key="navigationItem" id="Q5u-eL-Bgp">
<barButtonItem key="rightBarButtonItem" title="Log out" id="dI4-I1-waz">
<color key="tintColor" name="BrandLightPurple"/>
<color key="tintColor" name="BrandPurple"/>
<connections>
<action selector="logOutPressed:" destination="lwd-AH-u8E" id="2Rv-td-kfA"/>
</connections>
Expand All @@ -369,6 +369,11 @@
<navigationBar key="navigationBar" contentMode="scaleToFill" id="vbb-6v-lh9">
<rect key="frame" x="0.0" y="44" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<color key="tintColor" name="BrandBlue"/>
<color key="barTintColor" systemColor="systemBackgroundColor"/>
<textAttributes key="largeTitleTextAttributes">
<fontDescription key="fontDescription" type="italicSystem" pointSize="4"/>
</textAttributes>
</navigationBar>
<nil name="viewControllers"/>
<connections>
Expand All @@ -384,7 +389,7 @@
<designable name="y7D-jv-BlJ"/>
</designables>
<inferredMetricsTieBreakers>
<segue reference="ddt-iS-s7V"/>
<segue reference="55J-MO-wtP"/>
</inferredMetricsTieBreakers>
<resources>
<image name="paperplane.fill" catalog="system" width="128" height="118"/>
Expand Down
3 changes: 2 additions & 1 deletion Flash Chat iOS13/Views/MessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class MessageCell: UITableViewCell {
@IBOutlet weak var messageBubble: UIView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var avatarImageView: UIImageView!
@IBOutlet weak var leftImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
messageBubbles.layer.cornerRadius = messageBubble.frame.size.height / 5
messageBubbles.layer.cornerRadius = messageBubble.frame.size.height / 2
}

override func setSelected(_ selected: Bool, animated: Bool) {
Expand Down
31 changes: 20 additions & 11 deletions Flash Chat iOS13/Views/MessageCell.xib
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="oPx-gK-wt5">
<rect key="frame" x="10" y="10" width="300" height="53"/>
<rect key="frame" x="10" y="10" width="300" height="40"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="YouAvatar" translatesAutoresizingMaskIntoConstraints="NO" id="5Ui-Ph-nYY">
<rect key="frame" x="0.0" y="0.0" width="40" height="40"/>
<constraints>
<constraint firstAttribute="width" constant="40" id="6yI-PS-pQt"/>
<constraint firstAttribute="height" constant="40" id="pSW-pb-6ck"/>
</constraints>
</imageView>
<stackView opaque="NO" contentMode="scaleToFill" alignment="top" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="DGE-DF-NCM">
<rect key="frame" x="0.0" y="0.0" width="300" height="53"/>
<rect key="frame" x="60" y="0.0" width="180" height="40"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="4Dj-CW-bTN">
<rect key="frame" x="0.0" y="0.0" width="240" height="53"/>
<rect key="frame" x="0.0" y="0.0" width="180" height="40"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="label" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eOb-BN-bqR">
<rect key="frame" x="10" y="10" width="220" height="33"/>
<rect key="frame" x="10" y="10" width="160" height="20"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" name="BrandLightPurple"/>
<nil key="highlightedColor"/>
Expand All @@ -42,15 +49,15 @@
<constraint firstAttribute="bottom" secondItem="eOb-BN-bqR" secondAttribute="bottom" constant="10" id="u3I-Kw-hF1"/>
</constraints>
</view>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="MeAvatar" translatesAutoresizingMaskIntoConstraints="NO" id="gDC-Nx-PwW">
<rect key="frame" x="260" y="0.0" width="40" height="40"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="1vb-tZ-NtA"/>
<constraint firstAttribute="width" constant="40" id="85l-oH-ydc"/>
</constraints>
</imageView>
</subviews>
</stackView>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="MeAvatar" translatesAutoresizingMaskIntoConstraints="NO" id="gDC-Nx-PwW">
<rect key="frame" x="260" y="0.0" width="40" height="40"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="1vb-tZ-NtA"/>
<constraint firstAttribute="width" constant="40" id="85l-oH-ydc"/>
</constraints>
</imageView>
</subviews>
</stackView>
</subviews>
Expand All @@ -65,6 +72,7 @@
<connections>
<outlet property="avatarImageView" destination="gDC-Nx-PwW" id="kUR-Op-NpH"/>
<outlet property="label" destination="eOb-BN-bqR" id="hOs-4K-laR"/>
<outlet property="leftImageView" destination="5Ui-Ph-nYY" id="AMn-4u-vsU"/>
<outlet property="messageBubble" destination="oPx-gK-wt5" id="NZr-3J-h2Q"/>
<outlet property="messageBubbles" destination="4Dj-CW-bTN" id="NBf-Pn-vfc"/>
</connections>
Expand All @@ -73,6 +81,7 @@
</objects>
<resources>
<image name="MeAvatar" width="36" height="36"/>
<image name="YouAvatar" width="36" height="36"/>
<namedColor name="BrandLightPurple">
<color red="0.98000001907348633" green="0.9649999737739563" blue="0.99599999189376831" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</namedColor>
Expand Down