Skip to content

Commit

Permalink
persisted userData
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielvv committed Feb 15, 2017
1 parent 18e11cc commit b7563a1
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 56 deletions.
Binary file not shown.
4 changes: 2 additions & 2 deletions Barnabot/BBDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class BBDialog : Equatable {
}
}

func resume(_ session : BBSession,_ nextDialog : BBDialog?){
func resume(_ session : BBSession){
if let next = self.next {
next(session, nextDialog)
next(session)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Barnabot/BBIntentDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BBIntentDialog : BBDialog {
}

convenience init(_ regex : NSRegularExpression, redir : String, priority:Int){
let waterfall : [BBNext] = [{(session : BBSession, next : BBDialog?) -> Void in
let waterfall : [BBNext] = [{(session : BBSession) -> Void in
session.beginDialog(path: redir)
}]
self.init(regex, waterfall : waterfall, priority: priority)
Expand Down
2 changes: 1 addition & 1 deletion Barnabot/BBNext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
import Foundation

// http://fuckingclosuresyntax.com
typealias BBNext = (BBSession, BBDialog?) -> Void
typealias BBNext = (BBSession) -> Void
77 changes: 62 additions & 15 deletions Barnabot/BBSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ enum SessionState {
class BBSession {

static let sharedInstance : BBSession = BBSession()
static let identifier : String = "BBSession_"
static let identifier : String = "BBSession"
/*static func newInstance(_ sharedUserData : Bool?) -> BBSession {
if let sud = sharedUserData {
return BBSession(sharedUserData : sud)
}
return BBSession(sharedUserData: false)
}*/
static func newInstance() -> BBSession {
return BBSession()
return BBSession(sharedUserData: false)
}
static func newInstance(sharedUserData : Bool) -> BBSession {
return BBSession(sharedUserData : sharedUserData)
}

var state : SessionState {
Expand All @@ -33,12 +42,14 @@ class BBSession {
}
}
}

var human_feeling : Bool = true
var userData : [String: Any] = [String: Any]()
var delegate : BBSessionDelegate?
/// Stores the result of a prompt to pass the value to the next dialog step
var result : String = String()
var sharedUserData : Bool = true

private var userData : [String: Any]
private var waiting_for_uinput : Bool = false
private var _dialogStack : Stack<BBDialog> = Stack<BBDialog>()
public var dialogStack : Stack<BBDialog> {
Expand All @@ -49,7 +60,23 @@ class BBSession {
}
}

private init() {}
private convenience init(){
self.init(sharedUserData : true)
}

private init(sharedUserData: Bool) {
self.sharedUserData = sharedUserData
if self.sharedUserData {
let defaults = UserDefaults.standard
if let data = defaults.dictionary(forKey: BBSession.identifier) {
userData = data as! [String: Any]
} else {
userData = [String: Any]()
}
}else{
userData = [String: Any]()
}
}

/**
Entry point to start interacting with the user.
Expand Down Expand Up @@ -79,7 +106,7 @@ class BBSession {
//print("private beginDialog")
let copy = dialog.copy()
_dialogStack.push(copy)
copy.beginDialog(self, nil)
copy.beginDialog(self)
return self
}

Expand All @@ -94,15 +121,32 @@ class BBSession {
*/
private func saveUserData() -> BBSession {
let defaults = UserDefaults.standard

for (key, value) in userData {
defaults.setValue(value, forKey: BBSession.identifier + key )
}

defaults.setValue(userData, forKey: BBSession.identifier)
defaults.synchronize()
return self
}

func saveUserData(value : Any, forKey: String) -> BBSession {
userData.updateValue(value, forKey: forKey)
return self.sharedUserData ? saveUserData() : self
}

func getUserData(_ key: String) -> Any?{
return userData[key]
}

/*
- todo: notify every session sharing data
*/
func deleteUserData(){
self.userData.removeAll(keepingCapacity: true)
if self.sharedUserData {
let defaults = UserDefaults.standard
defaults.removeObject(forKey: BBSession.identifier)
defaults.synchronize()
}
}

/**
Ends the current dialog at the top of the stack.
Expand All @@ -127,14 +171,16 @@ class BBSession {
//print("resuming")
if let dialog = _dialogStack.last {
if let next = dialog.next {
next(self, nil)
next(self)
} else {
self.endDialog()
}
}
return self
}

func next() -> BBSession { return self.resume() }

/**
- todo:
*/
Expand All @@ -159,10 +205,10 @@ class BBSession {
Gives a human feeling by applying a delay before sending a message
*/
func send(_ msg : String) -> Void {
func send(_ msg : String) -> BBSession {
if(self.human_feeling){
self.delegate?.writing()
let delay : NSNumber = NSNumber.init(value: Float(generateRandomNumber(min: 500, max: 3000)) / Float(1000))
let delay : NSNumber = NSNumber.init(value: Float(generateRandomNumber(min: 1000, max: 5000)) / Float(1000))
let interval : TimeInterval = TimeInterval.init(delay)
Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(timerSend), userInfo: msg, repeats: false)
}else{
Expand All @@ -172,6 +218,7 @@ class BBSession {
print("BBSession has no delegate")
}
}
return self
}

@objc func timerSend(timer : Timer) -> Void {
Expand All @@ -188,9 +235,9 @@ class BBSession {
/**
Sends with format
*/
func send(format : String, args : AnyObject...) -> Void {
func send(format : String, args : AnyObject...) -> BBSession {
let msg : String = String(format: format, args)
send(msg)
return send(msg)
}

/**
Expand Down
14 changes: 7 additions & 7 deletions BarnabotTests/BBDialogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class BBDialogTests: XCTestCase {

func testComparison() {
let dialog1 = BBDialog("/", action: {
(session : BBSession, next : BBDialog?) -> Void in
(session : BBSession) -> Void in
})
let dialog2 = dialog1.copy()
let dialog3 = BBDialog("/foo", action: {
(session : BBSession, next : BBDialog?) -> Void in
(session : BBSession) -> Void in
})

// comparison is done on "path"
Expand All @@ -37,23 +37,23 @@ class BBDialogTests: XCTestCase {

func testNext() {
let dialog = BBDialog("/", action: {
(session : BBSession, next : BBDialog?) -> Void in
(session : BBSession) -> Void in
})
XCTAssertEqual(dialog.counter, 0)
dialog.next!(BBSession.newInstance(), nil)
dialog.next!(BBSession.newInstance())
XCTAssertEqual(dialog.counter, 1)
}

func testBeginDialog(){
let dialog = BBDialog("/", action: {
(session : BBSession, next : BBDialog?) -> Void in
(session : BBSession) -> Void in
})
XCTAssertEqual(dialog.counter, 0)
dialog.beginDialog(BBSession.newInstance(), nil)
dialog.beginDialog(BBSession.newInstance())
XCTAssertEqual(dialog.counter, 1)

// Subsequent calls to beginDialog does not invoke the next step
dialog.beginDialog(BBSession.newInstance(), nil)
dialog.beginDialog(BBSession.newInstance())
XCTAssertEqual(dialog.counter, 1)

}
Expand Down
4 changes: 2 additions & 2 deletions BarnabotTests/BBIntentDialogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class BBIntentDialogTests: XCTestCase {
do{
let regex1 = try NSRegularExpression.init(pattern: "^hello")
let dialog1 = BBIntentDialog(regex1, action: {
(session : BBSession, next : BBDialog?) -> Void in
(session : BBSession) -> Void in
}, priority: 0)

let regex2 = try NSRegularExpression.init(pattern: "^hello")
let dialog2 = dialog1.copy()

let regex3 = try NSRegularExpression.init(pattern: "^help")
let dialog3 = BBIntentDialog(regex3, action: {
(session : BBSession, next : BBDialog?) -> Void in
(session : BBSession) -> Void in
}, priority: 0)

// comparison is done on "path"
Expand Down
Loading

0 comments on commit b7563a1

Please sign in to comment.