Skip to content
Permalink
Browse files

Fixed Firestore service

  • Loading branch information
aslanyanhaik committed May 14, 2019
1 parent 9506849 commit 3cb34c426fd4e6e1184581b78c473faf2693edfa
@@ -412,7 +412,6 @@
TargetAttributes = {
22A92BAC1D59EBD5005D76F7 = {
CreatedOnToolsVersion = 8.0;
DevelopmentTeam = P6GDVV24N5;
DevelopmentTeamName = "Haik Aslanyan (Personal Team)";
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
@@ -700,7 +699,7 @@
baseConfigurationReference = 64442DBDEF6A70CFEE9331D6 /* Pods-QuickChat.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = P6GDVV24N5;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/QuickChat/Application/Resources/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
@@ -716,7 +715,7 @@
baseConfigurationReference = 9226BB5F97214501430FF2E8 /* Pods-QuickChat.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = P6GDVV24N5;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/QuickChat/Application/Resources/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
@@ -1,10 +1,24 @@
//
// UITableViewExtension.swift
// QuickChat
//
// Created by Haik Aslanyan on 5/13/19.
// Copyright © 2019 Mexonis. All rights reserved.
//
// MIT License
// Copyright (c) 2019 Haik Aslanyan
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit

@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
@@ -34,12 +34,12 @@ class ProfileManager {
}
let query = FirestoreService.DataQuery(key: "id", value: id, mode: .equal)
FirestoreService().objects(ObjectUser.self, reference: .init(location: .users), parameter: query) {[weak self] results in
if let user = results.first {
self?.users.append(user)
completion(user)
guard let user = results.first else {
completion(nil)
return
}
completion(nil)
self?.users.append(user)
completion(user)
}
}

@@ -31,28 +31,27 @@ class ConversationCell: UITableViewCell {
@IBOutlet weak var timeLabel: UILabel!

//MARK: Private properties
var userID = UserManager().currentUserID() ?? ""
let userID = UserManager().currentUserID() ?? ""

//MARK: Public methods
func set(_ conversation: ObjectConversation) {
timeLabel.text = DateService.shared.format(Date(timeIntervalSince1970: TimeInterval(conversation.timestamp)))
messageLabel.text = conversation.lastMessage
if let id = conversation.userIDs.filter({$0 == userID}).first {
let isRead = conversation.isRead[id] ?? true
if !isRead {
nameLabel.font = nameLabel.font.bold
messageLabel.font = messageLabel.font.bold
messageLabel.textColor = ThemeService.purpleColor
timeLabel.font = timeLabel.font.bold
}
ProfileManager.shared.userData(id: id) {[weak self] profile in
self?.nameLabel.text = profile?.name
guard let urlString = profile?.profilePicLink else {
self?.profilePic.image = UIImage(named: "profile pic")
return
}
self?.profilePic.setImage(url: URL(string: urlString))
guard let id = conversation.userIDs.filter({$0 != userID}).first else { return }
let isRead = conversation.isRead[userID] ?? true
if !isRead {
nameLabel.font = nameLabel.font.bold
messageLabel.font = messageLabel.font.bold
messageLabel.textColor = ThemeService.purpleColor
timeLabel.font = timeLabel.font.bold
}
ProfileManager.shared.userData(id: id) {[weak self] profile in
self?.nameLabel.text = profile?.name
guard let urlString = profile?.profilePicLink else {
self?.profilePic.image = UIImage(named: "profile pic")
return
}
self?.profilePic.setImage(url: URL(string: urlString))
}
}

@@ -32,8 +32,37 @@ class FirestoreService {
}

func objects<T>(_ object: T.Type, reference: Reference, parameter: DataQuery? = nil, completion: @escaping CompletionObject<[T]>) where T: FireCodable {
fetchDocuments(reference.reference(), parameter: parameter) { result in
completion(result)
guard let parameter = parameter else {
reference.reference().getDocuments { (snapshot, error) in
var results = [T]()
snapshot?.documents.forEach({ (document) in
if let objectData = document.data().data, let object = try? JSONDecoder().decode(T.self, from: objectData) {
results.append(object)
}
})
completion(results)
}
return
}
let queryReference: Query
switch parameter.mode {
case .equal:
queryReference = reference.reference().whereField(parameter.key, isEqualTo: parameter.value)
case .lessThan:
queryReference = reference.reference().whereField(parameter.key, isLessThan: parameter.value)
case .moreThan:
queryReference = reference.reference().whereField(parameter.key, isGreaterThan: parameter.value)
case .contains:
queryReference = reference.reference().whereField(parameter.key, arrayContains: parameter.value)
}
queryReference.getDocuments { (snapshot, error) in
var results = [T]()
snapshot?.documents.forEach({ (document) in
if let objectData = document.data().data, let object = try? JSONDecoder().decode(T.self, from: objectData) {
results.append(object)
}
})
completion(results)
}
}

@@ -105,18 +134,6 @@ class FirestoreService {
})
}

private func fetchDocuments<T>(_ ref: CollectionReference, parameter: DataQuery?, completion: @escaping CompletionObject<[T]>) where T: FireCodable {
ref.getDocuments { (snapshot, error) in
var results = [T]()
snapshot?.documents.forEach({ (document) in
if let objectData = document.data().data, let object = try? JSONDecoder().decode(T.self, from: objectData) {
results.append(object)
}
})
completion(results)
}
}

func stopObservers() {
listener?.remove()
}

0 comments on commit 3cb34c4

Please sign in to comment.
You can’t perform that action at this time.