Skip to content
This repository has been archived by the owner on Mar 19, 2023. It is now read-only.

Commit

Permalink
Add message caching for outgoing messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Trustet committed Jun 14, 2019
1 parent 9c39a87 commit 74cabd5
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 85 deletions.
15 changes: 15 additions & 0 deletions MessageEntitie+CoreDataClass.swift
@@ -0,0 +1,15 @@
//
// MessageEntitie+CoreDataClass.swift
//
//
// Created by yannik grotkop on 13.06.19.
//
//

import Foundation
import CoreData

@objc(MessageEntitie)
public class MessageEntitie: NSManagedObject {

}
26 changes: 26 additions & 0 deletions MessageEntitie+CoreDataProperties.swift
@@ -0,0 +1,26 @@
//
// MessageEntitie+CoreDataProperties.swift
//
//
// Created by yannik grotkop on 13.06.19.
//
//

import Foundation
import CoreData


extension MessageEntitie {

@nonobjc public class func fetchRequest() -> NSFetchRequest<MessageEntitie> {
return NSFetchRequest<MessageEntitie>(entityName: "MessageEntitie")
}

@NSManaged public var chatID: String?
@NSManaged public var date: NSDate?
@NSManaged public var matchID: String?
@NSManaged public var message: String?
@NSManaged public var ownerID: String?
@NSManaged public var read: Bool

}
6 changes: 6 additions & 0 deletions PulsePartner.xcworkspace/contents.xcworkspacedata

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

43 changes: 43 additions & 0 deletions PulsePartner/AppDelegate.swift
Expand Up @@ -9,6 +9,7 @@
import UIKit
import Firebase
import Kingfisher
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand Down Expand Up @@ -55,4 +56,46 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// See also applicationDidEnterBackground:.
}

lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "Chat")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}

}
8 changes: 0 additions & 8 deletions PulsePartner/Base.lproj/Main.storyboard
Expand Up @@ -130,13 +130,6 @@
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="BPMLab" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="hBH-mu-Tc5">
<rect key="frame" x="122" y="64" width="120" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="NameLab" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="rIL-dR-BGa">
<rect key="frame" x="122" y="11" width="221" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
Expand All @@ -163,7 +156,6 @@
<inset key="separatorInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
<connections>
<outlet property="ageLabel" destination="RkY-Xa-6W0" id="N2s-Ub-T8A"/>
<outlet property="bpmLabel" destination="hBH-mu-Tc5" id="s2g-Cv-boa"/>
<outlet property="imageView" destination="DNU-pd-xPN" id="YKR-h8-Pmt"/>
<outlet property="messageCounter" destination="Jph-c9-kn3" id="eTR-IY-S2P"/>
<outlet property="nameLabel" destination="rIL-dR-BGa" id="Mgp-TB-Pgi"/>
Expand Down
2 changes: 1 addition & 1 deletion PulsePartner/Controller/ChatViewController.swift
Expand Up @@ -55,7 +55,7 @@ class ChatViewController: MessagesViewController {
messagesCollectionView.messagesDataSource = self
messagesCollectionView.messagesLayoutDelegate = self
messagesCollectionView.messagesDisplayDelegate = self
ChatManager.sharedInstance.fetchMessages(userID: user.matchData.userID, view: self)
ChatManager.sharedInstance.fetchMessages(matchID: user.matchData.userID, view: self)
self.hideKeyboardWhenTappedAround()
self.navigationController?
.navigationBar
Expand Down
17 changes: 4 additions & 13 deletions PulsePartner/Controller/MainViewController.swift
Expand Up @@ -9,6 +9,7 @@
import UIKit
import CoreLocation
import Firebase
import CoreData

class MainViewController: UIViewController {

Expand All @@ -19,7 +20,6 @@ class MainViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

if let user = UserManager.sharedInstance.user {
updateImage(user: user)
}
Expand Down Expand Up @@ -80,18 +80,9 @@ extension MainViewController: UITableViewDelegate, UITableViewDataSource {
tableView.rowHeight = 110
let cell = ( self.tableView.dequeueReusableCell(withIdentifier: "MatchCell", for: indexPath) as? MatchCell )!
let user = self.allMatches[indexPath.row]
cell.insertContent(image: user.image,
name: user.matchData.username,
age: String(user.matchData.age),
bpm: String(95),
navigation: self.navigationController!)
let size = CGSize(width: 90, height: 90)
let rect = CGRect(x: 0, y: 0, width: 90, height: 90)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
user.image.draw(in: rect)
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
cell.profilePicture.image = resizedImage
cell.insertContent(match: user)

// cell.profilePicture.image = resizedImage
return cell
}

Expand Down
20 changes: 11 additions & 9 deletions PulsePartner/Model/Chat/Chat.swift
Expand Up @@ -6,12 +6,14 @@
// Copyright © 2019 PulsePartner. All rights reserved.
//

//import Foundation
//
//struct Message {
// var userID: String
// var ownerID: String
// var chatID: String
// var date: Date
// var message: String
//}
import Foundation

struct Message {
var userID: String
var ownerID: String
var chatID: String
var date: Date
var message: String
var read: Bool
var matchID: String
}
@@ -1,12 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14490.98" systemVersion="18E226" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Message" representedClassName="Message" syncable="YES" codeGenerationType="class">
<entity name="MessageEntitie" representedClassName="MessageEntitie" syncable="YES" codeGenerationType="class">
<attribute name="chatID" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
<attribute name="matchID" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="message" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="ownerID" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="read" optional="YES" attributeType="Boolean" usesScalarValueType="YES" syncable="YES"/>
</entity>
<elements>
<element name="Message" positionX="-63" positionY="-18" width="128" height="105"/>
<element name="MessageEntitie" positionX="-63" positionY="-18" width="128" height="135"/>
</elements>
</model>

0 comments on commit 74cabd5

Please sign in to comment.