diff --git a/config.go b/config.go index 824d6c3..7859bef 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,8 @@ import ( "github.com/zemirco/couchdb" ) +const ip = "192.168.99.100" + var ( db Database RediStore *redistore.RediStore @@ -16,12 +18,12 @@ func init() { var err error // init redis for sessions // fix hard coded ip to docker machine - RediStore, err = redistore.NewRediStore(10, "tcp", "192.168.99.100:6379", "", []byte("secret-key")) + RediStore, err = redistore.NewRediStore(10, "tcp", ip+":6379", "", []byte("secret-key")) if err != nil { panic(err) } // init couchdb - client, err := couchdb.NewClient("http://192.168.99.100:5984/") + client, err := couchdb.NewClient("http://" + ip + ":5984/") if err != nil { panic(err) } diff --git a/todo/todo.xcodeproj/project.pbxproj b/todo/todo.xcodeproj/project.pbxproj index bc3fab7..f7a576b 100644 --- a/todo/todo.xcodeproj/project.pbxproj +++ b/todo/todo.xcodeproj/project.pbxproj @@ -22,6 +22,7 @@ 3B75A0331C53BE1300E92480 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B75A0321C53BE1300E92480 /* libz.tbd */; }; 3B75A0371C53CFAF00E92480 /* Database.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B75A0361C53CFAF00E92480 /* Database.swift */; }; 3B75A0391C53D84F00E92480 /* Item.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B75A0381C53D84F00E92480 /* Item.framework */; }; + 3B9937971C54BF710014D24B /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B9937961C54BF710014D24B /* Config.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -64,6 +65,7 @@ 3B75A0351C53C06000E92480 /* todo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "todo-Bridging-Header.h"; sourceTree = ""; }; 3B75A0361C53CFAF00E92480 /* Database.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Database.swift; sourceTree = ""; }; 3B75A0381C53D84F00E92480 /* Item.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Item.framework; sourceTree = ""; }; + 3B9937961C54BF710014D24B /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -130,6 +132,7 @@ isa = PBXGroup; children = ( 3B759FF61C53B71900E92480 /* AppDelegate.swift */, + 3B9937961C54BF710014D24B /* Config.swift */, 3B75A0241C53B87000E92480 /* TodosTableViewController.swift */, 3B75A0361C53CFAF00E92480 /* Database.swift */, 3B75A0261C53BADE00E92480 /* AddTodoViewController.swift */, @@ -290,6 +293,7 @@ files = ( 3B75A0271C53BADE00E92480 /* AddTodoViewController.swift in Sources */, 3B759FF71C53B71900E92480 /* AppDelegate.swift in Sources */, + 3B9937971C54BF710014D24B /* Config.swift in Sources */, 3B75A0371C53CFAF00E92480 /* Database.swift in Sources */, 3B75A0251C53B87000E92480 /* TodosTableViewController.swift in Sources */, ); @@ -526,6 +530,7 @@ 3B75A01D1C53B71A00E92480 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 3B75A01E1C53B71A00E92480 /* Build configuration list for PBXNativeTarget "todoTests" */ = { isa = XCConfigurationList; @@ -534,6 +539,7 @@ 3B75A0201C53B71A00E92480 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 3B75A0211C53B71A00E92480 /* Build configuration list for PBXNativeTarget "todoUITests" */ = { isa = XCConfigurationList; @@ -542,6 +548,7 @@ 3B75A0231C53B71A00E92480 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/todo/todo.xcodeproj/project.xcworkspace/xcuserdata/zemirco.xcuserdatad/UserInterfaceState.xcuserstate b/todo/todo.xcodeproj/project.xcworkspace/xcuserdata/zemirco.xcuserdatad/UserInterfaceState.xcuserstate index bf6e292..c410fcd 100644 Binary files a/todo/todo.xcodeproj/project.xcworkspace/xcuserdata/zemirco.xcuserdatad/UserInterfaceState.xcuserstate and b/todo/todo.xcodeproj/project.xcworkspace/xcuserdata/zemirco.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/todo/todo/AddTodoViewController.swift b/todo/todo/AddTodoViewController.swift index 1506377..fa08269 100644 --- a/todo/todo/AddTodoViewController.swift +++ b/todo/todo/AddTodoViewController.swift @@ -46,9 +46,8 @@ class AddTodoViewController: UIViewController { self.presentViewController(alert, animated: true, completion: nil) return } - let manager = CBLManager.sharedInstance() do { - let database = try manager.databaseNamed("todos") + let database = try Database.Get() let todo = GoItemNewTodo(text) todo.setCreatedAt(NSDate().timeIntervalSince1970 as Double) try Database.AddTodo(database, item: todo) diff --git a/todo/todo/AppDelegate.swift b/todo/todo/AppDelegate.swift index f63e18b..edec516 100644 --- a/todo/todo/AppDelegate.swift +++ b/todo/todo/AppDelegate.swift @@ -7,7 +7,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - // Override point for customization after application launch. + + // reset database +// do { +// let db = try Database.Get() +// try db.deleteDatabase() +// } catch { +// print(error) +// } + self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window?.rootViewController = UINavigationController(rootViewController: TodosTableViewController()) self.window?.backgroundColor = UIColor.whiteColor() diff --git a/todo/todo/Config.swift b/todo/todo/Config.swift index bc2a4d6..0632ea9 100644 --- a/todo/todo/Config.swift +++ b/todo/todo/Config.swift @@ -1,9 +1,13 @@ -// -// Config.swift -// todo -// -// Created by Mirco Zeiss on 1/24/16. -// Copyright © 2016 zemirco. All rights reserved. -// import Foundation + +public struct Config { + + // on osx + // docker-machine default ip + public static let URL = "192.168.99.100" + + // on linux + // public static let URL = "localhost" + +} \ No newline at end of file diff --git a/todo/todo/Database.swift b/todo/todo/Database.swift index ff0d19d..76feafb 100644 --- a/todo/todo/Database.swift +++ b/todo/todo/Database.swift @@ -4,6 +4,16 @@ import Item public class Database { + // get database for main thread + public class func Get() throws -> CBLDatabase { + let manager = CBLManager.sharedInstance() + do { + return try manager.databaseNamed("todos") + } catch { + throw error + } + } + public class func GetTodoByID(db: CBLDatabase, id: String) -> GoItemTodo { let doc = db.documentWithID(id) if let doc = doc { @@ -75,7 +85,7 @@ public class Database { emit(createdAt, nil) } } - view.setMapBlock(map, version: "2") + view.setMapBlock(map, version: "1") let query = db.viewNamed("todos").createQuery() query.startKey = 1356998400 as Float64 query.endKey = 32503593600 as Float64 diff --git a/todo/todo/TodosTableViewController.swift b/todo/todo/TodosTableViewController.swift index 48affe4..7451c17 100644 --- a/todo/todo/TodosTableViewController.swift +++ b/todo/todo/TodosTableViewController.swift @@ -48,9 +48,8 @@ class TodosTableViewController: UITableViewController { override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { let done = UITableViewRowAction(style: .Normal, title: "Done", handler: { action, indexPath in - let manager = CBLManager.sharedInstance() do { - let database = try manager.databaseNamed("todos") + let database = try Database.Get() let todo = Database.GetTodoByID(database, id: self.todos[indexPath.row].id()) todo.setDone(true) try Database.UpdateTodo(database, item: todo) @@ -61,9 +60,8 @@ class TodosTableViewController: UITableViewController { } }) let undone = UITableViewRowAction(style: .Normal, title: "Undone", handler: { action, indexPath in - let manager = CBLManager.sharedInstance() do { - let database = try manager.databaseNamed("todos") + let database = try Database.Get() let todo = Database.GetTodoByID(database, id: self.todos[indexPath.row].id()) todo.setDone(false) try Database.UpdateTodo(database, item: todo) @@ -74,9 +72,8 @@ class TodosTableViewController: UITableViewController { } }) let delete = UITableViewRowAction(style: .Default, title: "Remove", handler: { action, indexPath in - let manager = CBLManager.sharedInstance() do { - let database = try manager.databaseNamed("todos") + let database = try Database.Get() try Database.RemoveTodoByID(database, id: self.todos[indexPath.row].id()) self.tableView.setEditing(false, animated: true) self.getTodos() @@ -96,10 +93,9 @@ class TodosTableViewController: UITableViewController { func getTodos() { // todos from database - let manager = CBLManager.sharedInstance() do { - let database = try manager.databaseNamed("todos") - manager.backgroundTellDatabaseNamed(database.name, to: { bgdb in + let database = try Database.Get() + database.manager.backgroundTellDatabaseNamed(database.name, to: { bgdb in do { let todos = try Database.GetTodos(bgdb) dispatch_async(dispatch_get_main_queue(), { @@ -122,10 +118,9 @@ class TodosTableViewController: UITableViewController { } func sync() { - let manager = CBLManager.sharedInstance() do { - let database = try manager.databaseNamed("todos") - let url = NSURL(string: "http://192.168.99.100:5984/john/") + let database = try Database.Get() + let url = NSURL(string: "http://\(Config.URL):5984/john/") self.push = database.createPushReplication(url!) self.pull = database.createPullReplication(url!) NSNotificationCenter.defaultCenter().addObserver(self, selector: "replicationChanged:", name: kCBLReplicationChangeNotification, object: push) @@ -144,7 +139,6 @@ class TodosTableViewController: UITableViewController { let pull = self.pull, let push = self.push { if push.status == .Idle && pull.status == .Idle { - print("done") print(pull.lastError) print(push.lastError) self.getTodos()