Skip to content

Commit

Permalink
clean some code and use ip from configs
Browse files Browse the repository at this point in the history
  • Loading branch information
zemirco committed Jan 24, 2016
1 parent c304865 commit 0377358
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 26 deletions.
6 changes: 4 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/zemirco/couchdb"
)

const ip = "192.168.99.100"

var (
db Database
RediStore *redistore.RediStore
Expand All @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions todo/todo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -64,6 +65,7 @@
3B75A0351C53C06000E92480 /* todo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "todo-Bridging-Header.h"; sourceTree = "<group>"; };
3B75A0361C53CFAF00E92480 /* Database.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Database.swift; sourceTree = "<group>"; };
3B75A0381C53D84F00E92480 /* Item.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Item.framework; sourceTree = "<group>"; };
3B9937961C54BF710014D24B /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -130,6 +132,7 @@
isa = PBXGroup;
children = (
3B759FF61C53B71900E92480 /* AppDelegate.swift */,
3B9937961C54BF710014D24B /* Config.swift */,
3B75A0241C53B87000E92480 /* TodosTableViewController.swift */,
3B75A0361C53CFAF00E92480 /* Database.swift */,
3B75A0261C53BADE00E92480 /* AddTodoViewController.swift */,
Expand Down Expand Up @@ -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 */,
);
Expand Down Expand Up @@ -526,6 +530,7 @@
3B75A01D1C53B71A00E92480 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
3B75A01E1C53B71A00E92480 /* Build configuration list for PBXNativeTarget "todoTests" */ = {
isa = XCConfigurationList;
Expand All @@ -534,6 +539,7 @@
3B75A0201C53B71A00E92480 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
3B75A0211C53B71A00E92480 /* Build configuration list for PBXNativeTarget "todoUITests" */ = {
isa = XCConfigurationList;
Expand All @@ -542,6 +548,7 @@
3B75A0231C53B71A00E92480 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Binary file not shown.
3 changes: 1 addition & 2 deletions todo/todo/AddTodoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion todo/todo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 11 additions & 7 deletions todo/todo/Config.swift
Original file line number Diff line number Diff line change
@@ -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"

}
12 changes: 11 additions & 1 deletion todo/todo/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
20 changes: 7 additions & 13 deletions todo/todo/TodosTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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(), {
Expand All @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 0377358

Please sign in to comment.