Commit a3dbd190 by Olzhas Aldabergenov

bot persist chat for 1 day

parent f5487b02
......@@ -86,6 +86,8 @@
3E2C3E8E1EFA482D00E553CD /* GetCodeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E2C3E8D1EFA482D00E553CD /* GetCodeViewController.swift */; };
3E2C3E9B1EFCB17E00E553CD /* Validator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E2C3E9A1EFCB17E00E553CD /* Validator.swift */; };
3E2E1E821F84DB0100DC0FE5 /* LKSettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E2E1E811F84DB0100DC0FE5 /* LKSettingsViewController.swift */; };
3E5CC7A51FA83F7400B55DF0 /* BotManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E5CC7A41FA83F7400B55DF0 /* BotManager.swift */; };
3E5CC7AE1FA84F9F00B55DF0 /* DateUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E5CC7AD1FA84F9F00B55DF0 /* DateUtil.swift */; };
3E6969B31FA2097200032D95 /* ServiceDetailsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6969B21FA2097200032D95 /* ServiceDetailsViewController.swift */; };
3EA7F6BA1F00D1A1003F6CBF /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EA7F6B91F00D1A1003F6CBF /* MainViewController.swift */; };
3EA7F6C51F021C9D003F6CBF /* FeedViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EA7F6C41F021C9D003F6CBF /* FeedViewController.swift */; };
......@@ -626,6 +628,8 @@
3E2C3E8D1EFA482D00E553CD /* GetCodeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetCodeViewController.swift; sourceTree = "<group>"; };
3E2C3E9A1EFCB17E00E553CD /* Validator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Validator.swift; sourceTree = "<group>"; };
3E2E1E811F84DB0100DC0FE5 /* LKSettingsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LKSettingsViewController.swift; sourceTree = "<group>"; };
3E5CC7A41FA83F7400B55DF0 /* BotManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BotManager.swift; sourceTree = "<group>"; };
3E5CC7AD1FA84F9F00B55DF0 /* DateUtil.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateUtil.swift; sourceTree = "<group>"; };
3E6969B21FA2097200032D95 /* ServiceDetailsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceDetailsViewController.swift; sourceTree = "<group>"; };
3EA7F6B91F00D1A1003F6CBF /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = "<group>"; };
3EA7F6C41F021C9D003F6CBF /* FeedViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedViewController.swift; sourceTree = "<group>"; };
......@@ -1052,6 +1056,7 @@
84F2534D1DF5D598000123D0 /* CellsManager.swift */,
84A9D2E31DFE6F3E008E7DD8 /* SharedDataManager.swift */,
84F475091E069C7C00A8B2D9 /* FastPayManager.swift */,
3E5CC7A41FA83F7400B55DF0 /* BotManager.swift */,
);
path = Managers;
sourceTree = "<group>";
......@@ -1257,6 +1262,7 @@
985207B01CECEE9A00E6C391 /* ValidationUtil.swift */,
984DE9C61D87187100608E69 /* SpinneredAlert.swift */,
3E2C3E9A1EFCB17E00E553CD /* Validator.swift */,
3E5CC7AD1FA84F9F00B55DF0 /* DateUtil.swift */,
);
path = Utils;
sourceTree = "<group>";
......@@ -2800,6 +2806,7 @@
3EAFC4901F7916A500BB07AD /* CallbackViewController.swift in Sources */,
985207C31CECFCB400E6C391 /* ReportPeriod.swift in Sources */,
989501371CFE493E00639286 /* OfferCallbackLinkCell.swift in Sources */,
3E5CC7AE1FA84F9F00B55DF0 /* DateUtil.swift in Sources */,
98E3F8721CFA21B700112FB7 /* Manager.swift in Sources */,
9856B8281CEECB5800D1443C /* WifiOfferOrder.swift in Sources */,
3EA7F6C51F021C9D003F6CBF /* FeedViewController.swift in Sources */,
......@@ -2874,6 +2881,7 @@
3EAFC4941F7B9BE000BB07AD /* CircleButton.swift in Sources */,
984C965B1CEDB5E6001CD53D /* OnlinePaymentFieldCell.swift in Sources */,
986917381DE37F9F00FC712D /* FullDamageModalViewController.swift in Sources */,
3E5CC7A51FA83F7400B55DF0 /* BotManager.swift in Sources */,
98E3F87B1CFA21B700112FB7 /* ServerTrustPolicy.swift in Sources */,
983FC3731CE5C25300EAA651 /* AltelPeriod.swift in Sources */,
B2125F0A5BFF36A23DD096D4 /* CPanelTableViewController.swift in Sources */,
......
//
// BotManager.swift
// kt
//
// Created by neox on 31.10.17.
// Copyright © 2017 SimpleCode. All rights reserved.
//
import Foundation
//MARK: - Singletone Instance
class BotManager {
//MARK: - Singletone Instance
let UserDefaultsBotResultsKey = "botResults"
class func sharedInstance() -> BotManager {
struct Shared {
static let sharedInstance = BotManager()
}
return Shared.sharedInstance
}
//MARK: - Constructors
private init() {
}
//MARK: - Functions
func archiveAll(items:[BotResult]) -> NSData {
let archivedObject = NSKeyedArchiver.archivedDataWithRootObject(items as NSArray)
return archivedObject
}
func retrieveAll() -> [BotResult]? {
if let unarchiveObject = NSUserDefaults.standardUserDefaults().objectForKey(UserDefaultsBotResultsKey + UserManager.sharedInstance().login!) as? NSData {
let existing = NSKeyedUnarchiver.unarchiveObjectWithData(unarchiveObject) as? [BotResult]
var result = [BotResult]()
let now = NSDate()
let dayAgo = now.addDays(-1)
for i in 0 ..< existing!.count {
let botResult = existing![i]
print(botResult.date)
if ( !dayAgo.isGreaterThanDate(botResult.date) ) {
result.append(botResult)
} else {
// the rest don't get persisted
}
}
return result
}
return nil
}
func saveStoreFastPay(botResults : [BotResult]) {
let archivedItems = archiveAll(botResults)
NSUserDefaults.standardUserDefaults().setObject(archivedItems, forKey: UserDefaultsBotResultsKey + UserManager.sharedInstance().login!)
NSUserDefaults.standardUserDefaults().synchronize()
}
func add(botResult : BotResult) {
var items = retrieveAll()
if (items == nil) {
items = []
}
items!.append(botResult)
saveStoreFastPay(items!)
}
}
......@@ -8,7 +8,7 @@
import UIKit
class BotNode {
class BotNode: NSObject, NSCoding {
var id : String?
var parentId : String?
......@@ -18,4 +18,28 @@ class BotNode {
var expanded : Int?
var isLink : Int?
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
id = aDecoder.decodeObjectForKey("id") as! String?
parentId = aDecoder.decodeObjectForKey("parentId") as! String?
linkTo = aDecoder.decodeObjectForKey("linkTo") as! String?
formId = aDecoder.decodeObjectForKey("formId") as! String?
text = aDecoder.decodeObjectForKey("text") as! String?
expanded = aDecoder.decodeObjectForKey("expanded") as! Int?
isLink = aDecoder.decodeObjectForKey("isLink") as! Int?
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(id, forKey: "id")
aCoder.encodeObject(parentId, forKey: "parentId")
aCoder.encodeObject(linkTo, forKey: "linkTo")
aCoder.encodeObject(formId, forKey: "formId")
aCoder.encodeObject(text, forKey: "text")
aCoder.encodeObject(expanded, forKey: "expanded")
aCoder.encodeObject(isLink, forKey: "isLink")
}
}
......@@ -8,11 +8,44 @@
import UIKit
class BotResult {
class BotResult: NSObject, NSCoding {
var nodes : [BotNode]?
var count : Int?
var status : String?
var type : Int = 0
internal var nodes : [BotNode]?
internal var count : Int?
internal var status : String?
internal var type : Int = 0
internal var date = NSDate()
internal var uuid : String
override init() {
self.uuid = NSUUID().UUIDString
super.init()
}
func compare(botResult:BotResult) -> Bool {
if botResult.uuid.compare(self.uuid) == .OrderedSame {
return true
}
return false
}
@objc required init?(coder aDecoder: NSCoder) {
nodes = aDecoder.decodeObjectForKey("nodes") as! [BotNode]!
count = aDecoder.decodeObjectForKey("count") as! Int?
status = aDecoder.decodeObjectForKey("status") as! String?
type = aDecoder.decodeObjectForKey("type") as! Int
date = aDecoder.decodeObjectForKey("date") as! NSDate
uuid = aDecoder.decodeObjectForKey("uuid") as! String
}
@objc func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(nodes, forKey: "nodes")
aCoder.encodeObject(count, forKey: "count")
aCoder.encodeObject(status, forKey: "status")
aCoder.encodeObject(type, forKey: "type")
aCoder.encodeObject(date, forKey: "date")
aCoder.encodeObject(uuid, forKey: "uuid")
}
}
//
// DateUtil.swift
// kt
//
// Created by neox on 31.10.17.
// Copyright © 2017 SimpleCode. All rights reserved.
//
// https://stackoverflow.com/a/29319732/2488616
//
import Foundation
extension NSDate {
func isGreaterThanDate(dateToCompare: NSDate) -> Bool {
//Declare Variables
var isGreater = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending {
isGreater = true
}
//Return Result
return isGreater
}
func isLessThanDate(dateToCompare: NSDate) -> Bool {
//Declare Variables
var isLess = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedAscending {
isLess = true
}
//Return Result
return isLess
}
func equalToDate(dateToCompare: NSDate) -> Bool {
//Declare Variables
var isEqualTo = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedSame {
isEqualTo = true
}
//Return Result
return isEqualTo
}
func addDays(daysToAdd: Int) -> NSDate {
let secondsInDays: NSTimeInterval = Double(daysToAdd) * 60 * 60 * 24
let dateWithDaysAdded: NSDate = self.dateByAddingTimeInterval(secondsInDays)
//Return Result
return dateWithDaysAdded
}
func addHours(hoursToAdd: Int) -> NSDate {
let secondsInHours: NSTimeInterval = Double(hoursToAdd) * 60 * 60
let dateWithHoursAdded: NSDate = self.dateByAddingTimeInterval(secondsInHours)
//Return Result
return dateWithHoursAdded
}
func addMinutes(minutesToAdd: Int) -> NSDate {
let secondsInMinutes: NSTimeInterval = Double(minutesToAdd) * 60
let dateWithMinutesAdded: NSDate = self.dateByAddingTimeInterval(secondsInMinutes)
//Return Result
return dateWithMinutesAdded
}
}
......@@ -38,8 +38,13 @@ class BotViewController: UIViewController, UITableViewDataSource, UITableViewDel
object: nil)
initializeTap()
addMessage("Здравствуйте! Я Ваш помощник на сайте Казахтелеком. Постараюсь ответить на большинство Ваших вопросов.", type: 0)
let results = BotManager.sharedInstance().retrieveAll()
if results?.count > 0 {
self.messagesList = results!
self.tableView.reloadData()
} else {
addMessage("Здравствуйте! Я Ваш помощник на сайте Казахтелеком. Постараюсь ответить на большинство Ваших вопросов.", type: 0)
}
}
......@@ -78,6 +83,7 @@ class BotViewController: UIViewController, UITableViewDataSource, UITableViewDel
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
messageField.setBottomBorder(Color.GrayTableCellSeparator)
self.tableView.scrollToBottom(true)
}
override func viewWillDisappear(animated: Bool) {
......@@ -105,7 +111,7 @@ class BotViewController: UIViewController, UITableViewDataSource, UITableViewDel
}
func addMessage(message: String, type: Int) {
let botResult = BotResult()
let botResult = BotResult.init()
botResult.type = type
let botNode = BotNode()
botNode.text = message
......@@ -128,6 +134,12 @@ class BotViewController: UIViewController, UITableViewDataSource, UITableViewDel
self.tableView.endUpdates()
self.tableView.scrollToBottom(true)
self.persist(botResult)
}
func persist(botResult: BotResult) {
BotManager.sharedInstance().add(botResult)
}
func reloadTable() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment