Skip to content

Commit b97e6ef

Browse files
committed
Refactor
1 parent 1f95ceb commit b97e6ef

10 files changed

+112
-121
lines changed

ShoppingList.xcodeproj/project.pbxproj

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
D71D204A1F74124E00E53E3D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D71D20491F74124E00E53E3D /* Assets.xcassets */; };
1515
D71D204D1F74124E00E53E3D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D71D204B1F74124E00E53E3D /* LaunchScreen.storyboard */; };
1616
D71D20551F74140700E53E3D /* Datastore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D71D20541F74140700E53E3D /* Datastore.swift */; };
17-
D71D20571F741A2300E53E3D /* ListMeta.swift in Sources */ = {isa = PBXBuildFile; fileRef = D71D20561F741A2300E53E3D /* ListMeta.swift */; };
17+
D71D20571F741A2300E53E3D /* ShoppingListDetails.swift in Sources */ = {isa = PBXBuildFile; fileRef = D71D20561F741A2300E53E3D /* ShoppingListDetails.swift */; };
1818
D71D205A1F741D9800E53E3D /* ShoppingListFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = D71D20591F741D9800E53E3D /* ShoppingListFactory.swift */; };
1919
D71D205C1F741E2500E53E3D /* ShoppingListRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = D71D205B1F741E2500E53E3D /* ShoppingListRepository.swift */; };
2020
D7A20F801F7435DB001CE337 /* ShoppingListAddViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A20F7F1F7435DB001CE337 /* ShoppingListAddViewController.swift */; };
2121
D7A20F821F7437A2001CE337 /* StateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A20F811F7437A2001CE337 /* StateManager.swift */; };
2222
D7A20F841F7445BE001CE337 /* ShoppingListsTableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A20F831F7445BE001CE337 /* ShoppingListsTableCell.swift */; };
2323
D7A20F861F744F7F001CE337 /* ShoppingListManageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A20F851F744F7F001CE337 /* ShoppingListManageViewController.swift */; };
24-
D7A20F881F748AC6001CE337 /* ShoppingListTableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A20F871F748AC6001CE337 /* ShoppingListTableCell.swift */; };
24+
D7A20F881F748AC6001CE337 /* ShoppingListManageTableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A20F871F748AC6001CE337 /* ShoppingListManageTableCell.swift */; };
2525
D7A20F8A1F74AD3F001CE337 /* SyncManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A20F891F74AD3F001CE337 /* SyncManager.swift */; };
2626
/* End PBXBuildFile section */
2727

@@ -35,14 +35,14 @@
3535
D71D204C1F74124E00E53E3D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
3636
D71D204E1F74124E00E53E3D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3737
D71D20541F74140700E53E3D /* Datastore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Datastore.swift; sourceTree = "<group>"; };
38-
D71D20561F741A2300E53E3D /* ListMeta.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListMeta.swift; sourceTree = "<group>"; };
38+
D71D20561F741A2300E53E3D /* ShoppingListDetails.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShoppingListDetails.swift; sourceTree = "<group>"; };
3939
D71D20591F741D9800E53E3D /* ShoppingListFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShoppingListFactory.swift; sourceTree = "<group>"; };
4040
D71D205B1F741E2500E53E3D /* ShoppingListRepository.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShoppingListRepository.swift; sourceTree = "<group>"; };
4141
D7A20F7F1F7435DB001CE337 /* ShoppingListAddViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShoppingListAddViewController.swift; sourceTree = "<group>"; };
4242
D7A20F811F7437A2001CE337 /* StateManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateManager.swift; sourceTree = "<group>"; };
4343
D7A20F831F7445BE001CE337 /* ShoppingListsTableCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShoppingListsTableCell.swift; sourceTree = "<group>"; };
4444
D7A20F851F744F7F001CE337 /* ShoppingListManageViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShoppingListManageViewController.swift; sourceTree = "<group>"; };
45-
D7A20F871F748AC6001CE337 /* ShoppingListTableCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShoppingListTableCell.swift; sourceTree = "<group>"; };
45+
D7A20F871F748AC6001CE337 /* ShoppingListManageTableCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShoppingListManageTableCell.swift; sourceTree = "<group>"; };
4646
D7A20F891F74AD3F001CE337 /* SyncManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SyncManager.swift; sourceTree = "<group>"; };
4747
D9DB28B1B20A6F4272DAF4D5 /* Pods-ShoppingList.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShoppingList.release.xcconfig"; path = "Pods/Target Support Files/Pods-ShoppingList/Pods-ShoppingList.release.xcconfig"; sourceTree = "<group>"; };
4848
FCE0928FBEC2083B1D4FCB5B /* Pods-ShoppingList.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShoppingList.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ShoppingList/Pods-ShoppingList.debug.xcconfig"; sourceTree = "<group>"; };
@@ -99,13 +99,9 @@
9999
isa = PBXGroup;
100100
children = (
101101
D71D20581F741A6000E53E3D /* Model */,
102+
D7A20F8B1F75A61E001CE337 /* View */,
102103
D71D20421F74124E00E53E3D /* AppDelegate.swift */,
103104
D71D20541F74140700E53E3D /* Datastore.swift */,
104-
D7A20F7F1F7435DB001CE337 /* ShoppingListAddViewController.swift */,
105-
D7A20F851F744F7F001CE337 /* ShoppingListManageViewController.swift */,
106-
D71D20441F74124E00E53E3D /* ShoppingListsViewController.swift */,
107-
D7A20F871F748AC6001CE337 /* ShoppingListTableCell.swift */,
108-
D7A20F831F7445BE001CE337 /* ShoppingListsTableCell.swift */,
109105
D7A20F811F7437A2001CE337 /* StateManager.swift */,
110106
D7A20F891F74AD3F001CE337 /* SyncManager.swift */,
111107
D71D20491F74124E00E53E3D /* Assets.xcassets */,
@@ -119,13 +115,25 @@
119115
D71D20581F741A6000E53E3D /* Model */ = {
120116
isa = PBXGroup;
121117
children = (
122-
D71D20561F741A2300E53E3D /* ListMeta.swift */,
118+
D71D20561F741A2300E53E3D /* ShoppingListDetails.swift */,
123119
D71D20591F741D9800E53E3D /* ShoppingListFactory.swift */,
124120
D71D205B1F741E2500E53E3D /* ShoppingListRepository.swift */,
125121
);
126122
name = Model;
127123
sourceTree = "<group>";
128124
};
125+
D7A20F8B1F75A61E001CE337 /* View */ = {
126+
isa = PBXGroup;
127+
children = (
128+
D7A20F7F1F7435DB001CE337 /* ShoppingListAddViewController.swift */,
129+
D7A20F871F748AC6001CE337 /* ShoppingListManageTableCell.swift */,
130+
D7A20F851F744F7F001CE337 /* ShoppingListManageViewController.swift */,
131+
D7A20F831F7445BE001CE337 /* ShoppingListsTableCell.swift */,
132+
D71D20441F74124E00E53E3D /* ShoppingListsViewController.swift */,
133+
);
134+
name = View;
135+
sourceTree = "<group>";
136+
};
129137
/* End PBXGroup section */
130138

131139
/* Begin PBXNativeTarget section */
@@ -261,10 +269,10 @@
261269
files = (
262270
D7A20F8A1F74AD3F001CE337 /* SyncManager.swift in Sources */,
263271
D7A20F841F7445BE001CE337 /* ShoppingListsTableCell.swift in Sources */,
264-
D7A20F881F748AC6001CE337 /* ShoppingListTableCell.swift in Sources */,
272+
D7A20F881F748AC6001CE337 /* ShoppingListManageTableCell.swift in Sources */,
265273
D7A20F801F7435DB001CE337 /* ShoppingListAddViewController.swift in Sources */,
266274
D7A20F821F7437A2001CE337 /* StateManager.swift in Sources */,
267-
D71D20571F741A2300E53E3D /* ListMeta.swift in Sources */,
275+
D71D20571F741A2300E53E3D /* ShoppingListDetails.swift in Sources */,
268276
D71D20451F74124E00E53E3D /* ShoppingListsViewController.swift in Sources */,
269277
D71D20431F74124E00E53E3D /* AppDelegate.swift in Sources */,
270278
D71D20551F74140700E53E3D /* Datastore.swift in Sources */,

ShoppingList/Base.lproj/Main.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
169169
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
170170
<prototypes>
171-
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="ShoppingListCell" id="cXY-D9-Gz4" customClass="ShoppingListTableCell" customModule="ShoppingList" customModuleProvider="target">
171+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="ShoppingListManageCell" id="cXY-D9-Gz4" customClass="ShoppingListManageTableCell" customModule="ShoppingList" customModuleProvider="target">
172172
<rect key="frame" x="0.0" y="28" width="375" height="47"/>
173173
<autoresizingMask key="autoresizingMask"/>
174174
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="cXY-D9-Gz4" id="Cdp-u4-F14">

ShoppingList/Datastore.swift

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,10 @@
99
import CDTDatastore
1010
import UIKit
1111

12-
class ReplicatorListener : NSObject, CDTReplicatorDelegate {
13-
14-
var onChangesReplicated: ((Int) -> Void)?
15-
16-
override init() {
17-
}
18-
19-
public func replicatorDidComplete(_ replicator: CDTReplicator) {
20-
self.onChangesReplicated?(replicator.changesTotal)
21-
}
22-
23-
}
24-
2512
class Datastore {
2613

2714
let datastore: CDTDatastore
2815
let shoppingListRepository: ShoppingListRepository
29-
let replicatorListener: ReplicatorListener = ReplicatorListener()
3016

3117
init?() {
3218
let fileManager = FileManager.default
@@ -43,56 +29,27 @@ class Datastore {
4329
}
4430
}
4531

46-
func sync(onSyncComplete:@escaping (Int) -> Void) {
47-
// Replicate from the local to remote database
48-
let remote = URL(string: "http://admin:[email protected]:35984/shopping-list")!
49-
self.datastore.push(to: remote) { error in
50-
if let error = error {
51-
print("Error performing push replication: \(error)")
52-
} else {
53-
do {
54-
self.replicatorListener.onChangesReplicated = onSyncComplete
55-
try self.datastore.pullReplicationSource(remote, username: nil, password: nil, with: self.replicatorListener).start()
56-
}
57-
catch {
58-
print("ERROR \(error)")
59-
}
60-
// self.datastore.pull(from: remote) { error in
61-
// if let error = error {
62-
// print("Error performing pull replication: \(error)")
63-
// } else {
64-
// onSyncComplete()
65-
// }
66-
// }
67-
}
68-
}
69-
}
70-
71-
72-
func loadLists() -> [ListMeta] {
73-
var lists = [ListMeta]()
32+
func loadLists() -> [ShoppingListDetails] {
33+
var lists = [ShoppingListDetails]()
7434
let allLists = self.shoppingListRepository.find()
7535
for list in allLists {
7636
let items = self.shoppingListRepository.findItems(query: ["type": "item", "list": list.docId!])
77-
let listMeta = ListMeta(list: list)
78-
listMeta.itemCount = items.count
79-
listMeta.itemCheckedCount = items.filter{ $0.body["checked"] as! Bool }.count
80-
lists.append(listMeta)
37+
let listDetails = ShoppingListDetails(list: list)
38+
listDetails.itemCount = items.count
39+
listDetails.itemCheckedCount = items.filter{ $0.body["checked"] as! Bool }.count
40+
lists.append(listDetails)
8141
}
8242
return lists
8343
}
8444

8545
func addList(title: String) throws -> CDTDocumentRevision {
8646
let list = ShoppingListFactory.newShoppingList(title: title)
87-
let newList = try self.shoppingListRepository.put(list: list)
88-
self.sync(onSyncComplete: { _ in })
89-
return newList
47+
return try self.shoppingListRepository.put(shoppingList: list)
9048
}
9149

9250
func deleteList(list: CDTDocumentRevision) throws -> CDTDocumentRevision {
9351
let existingList = try self.shoppingListRepository.get(listId: list.docId!)
94-
let deletedList = try self.shoppingListRepository.delete(list: existingList)
95-
self.sync(onSyncComplete: { _ in })
52+
let deletedList = try self.shoppingListRepository.delete(shoppingList: existingList)
9653
return deletedList
9754
}
9855

@@ -108,24 +65,21 @@ class Datastore {
10865
func addItem(title: String, listId: String) throws -> CDTDocumentRevision {
10966
let list = try self.shoppingListRepository.get(listId: listId)
11067
let item = ShoppingListFactory.newShoppingListItem(title: title, list: list)
111-
let newItem = try self.shoppingListRepository.putItem(item: item)
112-
self.sync(onSyncComplete: { _ in })
68+
let newItem = try self.shoppingListRepository.putItem(shoppingListItem: item)
11369
return newItem
11470
}
11571

11672
func toggleItemChecked(item: CDTDocumentRevision) throws -> CDTDocumentRevision {
11773
let checked = !(item.body["checked"] as! Bool)
11874
let existingItem = try self.shoppingListRepository.getItem(itemId: item.docId!)
11975
existingItem.body["checked"] = checked
120-
let updatedItem = try self.shoppingListRepository.putItem(item: existingItem)
121-
self.sync(onSyncComplete: { _ in })
76+
let updatedItem = try self.shoppingListRepository.putItem(shoppingListItem: existingItem)
12277
return updatedItem
12378
}
12479

12580
func deleteItem(item: CDTDocumentRevision) throws -> CDTDocumentRevision {
12681
let existingItem = try self.shoppingListRepository.getItem(itemId: item.docId!)
127-
let deletedItem = try self.shoppingListRepository.deleteItem(item: existingItem)
128-
self.sync(onSyncComplete: { _ in })
82+
let deletedItem = try self.shoppingListRepository.deleteItem(shoppingListItem: existingItem)
12983
return deletedItem
13084
}
13185

ShoppingList/ListMeta.swift renamed to ShoppingList/ShoppingListDetails.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import CDTDatastore
1010
import UIKit
1111

12-
class ListMeta {
12+
class ShoppingListDetails {
1313

1414
public let list: CDTDocumentRevision
1515
public var itemCount: Int = 0

ShoppingList/ShoppingListTableCell.swift renamed to ShoppingList/ShoppingListManageTableCell.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ShoppingListTableCell.swift
2+
// ShoppingListManageTableCell.swift
33
// ShoppingList
44
//
55
// Created by Mark Watson on 9/21/17.
@@ -8,7 +8,7 @@
88

99
import UIKit
1010

11-
class ShoppingListTableCell: UITableViewCell {
11+
class ShoppingListManageTableCell: UITableViewCell {
1212

1313
@IBOutlet var checkedSwitch: UISwitch!
1414
@IBOutlet var titleTextField: UITextField!

ShoppingList/ShoppingListManageViewController.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ class ShoppingListManageViewController: UIViewController, UITableViewDataSource,
4040
}
4141

4242
private func reloadItems() {
43-
self.items = StateManager.datastore.loadItems(list: StateManager.activeList!)
43+
self.items = StateManager.datastore.loadItems(list: StateManager.activeShoppingList!)
4444
self.tableView.reloadData()
4545
self.tableView.setNeedsDisplay()
4646
}
4747

4848
func save() {
49-
print("save")
5049
do {
51-
let item = try StateManager.datastore.addItem(title: self.newItemTitleText, listId: StateManager.activeList!.docId!)
50+
let item = try StateManager.datastore.addItem(title: self.newItemTitleText, listId: StateManager.activeShoppingList!.docId!)
5251
self.items.append(item)
5352
self.newItemTitleText = ""
5453
self.disableDoneButton()
5554
self.tableView.reloadData()
5655
}
5756
catch {
57+
// TODO:
5858
print("ERROR \(error)")
5959
}
6060
}
@@ -105,7 +105,7 @@ class ShoppingListManageViewController: UIViewController, UITableViewDataSource,
105105
return self.items.count + 1
106106
}
107107

108-
static func setBottomBorderToTextFields(cell: ShoppingListTableCell) {
108+
static func setBottomBorderToTextFields(cell: ShoppingListManageTableCell) {
109109
let bottomLine = CALayer()
110110
bottomLine.frame = CGRect(x: 0, y: cell.titleTextField.frame.height - 1, width: cell.titleTextField.frame.width, height: 1)
111111
bottomLine.backgroundColor = UIColor.lightGray.cgColor // background color
@@ -114,7 +114,7 @@ class ShoppingListManageViewController: UIViewController, UITableViewDataSource,
114114
}
115115

116116
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
117-
let cell:ShoppingListTableCell = self.tableView.dequeueReusableCell(withIdentifier: "ShoppingListCell") as! ShoppingListTableCell!
117+
let cell:ShoppingListManageTableCell = self.tableView.dequeueReusableCell(withIdentifier: "ShoppingListManageCell") as! ShoppingListManageTableCell!
118118
if indexPath.row < self.items.count {
119119
cell.checkedSwitch.isEnabled = true
120120
cell.checkedSwitch.isOn = self.items[indexPath.row].body["checked"] as! Bool
@@ -127,6 +127,7 @@ class ShoppingListManageViewController: UIViewController, UITableViewDataSource,
127127
}
128128
else {
129129
cell.checkedSwitch.isEnabled = false
130+
cell.checkedSwitch.isOn = false
130131
cell.checkedSwitch.removeTarget(self, action: nil, for: UIControlEvents.valueChanged)
131132
cell.titleTextField.isEnabled = true
132133
cell.titleTextField.text = self.newItemTitleText

0 commit comments

Comments
 (0)