-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
835 additions
and
656 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// IdentifierGenerator.swift | ||
// TinkoffChat | ||
// | ||
// Created by Nikita Borodulin on 10/11/2017. | ||
// Copyright © 2017 com.nikitaborodulin. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol IIdentifierGenerator: class { | ||
func generateIdentifier() -> String | ||
} | ||
|
||
class IdentifierGenerator: IIdentifierGenerator { | ||
func generateIdentifier() -> String { | ||
return ("\(arc4random_uniform(UINT32_MAX)) + \(Date.timeIntervalSinceReferenceDate) + \(arc4random_uniform(UINT32_MAX))".data(using: .utf8)?.base64EncodedString())! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
TinkoffChat/Core Layer/Storage/MO Extensions/Conversation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// Conversation.swift | ||
// TinkoffChat | ||
// | ||
// Created by Nikita Borodulin on 10/11/2017. | ||
// Copyright © 2017 com.nikitaborodulin. All rights reserved. | ||
// | ||
|
||
import CoreData | ||
|
||
extension Conversation { | ||
private static let fetchRequestConversationByIDTemplateName = "ConversationByID" | ||
private static let fetchRequestConversationByIDAttributeName = "conversationID" | ||
|
||
static func findOrInsertConversationWith(id: String, in context: NSManagedObjectContext) -> Conversation { | ||
if let fetchedConversation = fetchConversationWith(id: id, in: context) { | ||
return fetchedConversation | ||
} else { | ||
let newConversation = Conversation.insertConversationWith(id: id, in: context) | ||
return newConversation | ||
} | ||
} | ||
|
||
private static func fetchConversationWith(id: String, in context: NSManagedObjectContext) -> Conversation? { | ||
guard let model = context.persistentStoreCoordinator?.managedObjectModel else { | ||
preconditionFailure("Model is not available in context") | ||
} | ||
let fetchRequest = Conversation.fetchRequestConversationBy(id: id, model: model) | ||
do { | ||
let results = try context.fetch(fetchRequest) | ||
assert(results.count < 2, "Multiple Conversations found for id: \(id)") | ||
return results.first | ||
} catch { | ||
fatalError("Failed to fetch User with id: \(id) \(error)") | ||
} | ||
} | ||
|
||
private static func insertConversationWith(id: String, in context: NSManagedObjectContext) -> Conversation { | ||
let newConversation = Conversation(context: context) | ||
newConversation.conversationID = id | ||
return newConversation | ||
} | ||
|
||
private static func fetchRequestConversationBy(id: String, model: NSManagedObjectModel) -> NSFetchRequest<Conversation> { | ||
guard let fetchRequest = model.fetchRequestFromTemplate(withName: fetchRequestConversationByIDTemplateName, substitutionVariables: [fetchRequestConversationByIDAttributeName: id]) as? NSFetchRequest<Conversation> else { | ||
preconditionFailure("No template with \(fetchRequestConversationByIDTemplateName) name") | ||
} | ||
return fetchRequest | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
TinkoffChat/Core Layer/Storage/MO Extensions/Message.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Message.swift | ||
// TinkoffChat | ||
// | ||
// Created by Nikita Borodulin on 12/11/2017. | ||
// Copyright © 2017 com.nikitaborodulin. All rights reserved. | ||
// | ||
|
||
import CoreData | ||
|
||
extension Message { | ||
private static let fetchRequestMessageByIDTemplateName = "MessagesByConversationID" | ||
private static let fetchRequestMessageByIDAttributeName = "id" | ||
|
||
static func fetchRequestMessage(withConversationID conversationID: String, in context: NSManagedObjectContext) -> NSFetchRequest<Message> { | ||
guard let model = context.persistentStoreCoordinator?.managedObjectModel else { | ||
preconditionFailure("Model is not available in context") | ||
} | ||
guard let fetchRequest = model.fetchRequestFromTemplate(withName: fetchRequestMessageByIDTemplateName, substitutionVariables: [fetchRequestMessageByIDAttributeName: conversationID]) as? NSFetchRequest<Message> else { | ||
preconditionFailure("No template with \(fetchRequestMessageByIDTemplateName) name") | ||
} | ||
return fetchRequest | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.