Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

touch item #158

Merged
merged 3 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cartfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ github "Quick/Quick"
github "Quick/Nimble"
github "ReactiveX/RxSwift"
github "RxSwiftCommunity/RxDataSources" ~> 3.0
github "RxSwiftCommunity/RxOptional" ~> 3.1.3
github "mozilla-mobile/telemetry-ios" "v1.0.4"
1 change: 1 addition & 0 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ github "Quick/Nimble" "v7.0.3"
github "Quick/Quick" "v1.2.0"
github "ReactiveX/RxSwift" "4.1.2"
github "RxSwiftCommunity/RxDataSources" "3.0.2"
github "RxSwiftCommunity/RxOptional" "3.3.0"
github "mozilla-mobile/telemetry-ios" "v1.0.4"
14 changes: 14 additions & 0 deletions Lockbox.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@
7AA548BBE384D40397D26E9B /* RootView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA546C351E7D7C7DE3BEF0C /* RootView.swift */; };
7AA549A0A09C07E547A5D0F0 /* OAuthInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA5455D02519DBA3D3711A9 /* OAuthInfo.swift */; };
7AA549F77B3C142668BAE986 /* FxAActionSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA540B2CD80BD7AB0E09813 /* FxAActionSpec.swift */; };
7AA54A03CA11221087E4F9F8 /* Date+Spec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA545A802C0592A820AD230 /* Date+Spec.swift */; };
7AA54A58F7F289BAD2CE21C3 /* ErrorActionSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA545A7472772984D517CB0 /* ErrorActionSpec.swift */; };
7AA54A743320656025DC5B3E /* ItemDetailCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA547106248ADBADAF76239 /* ItemDetailCell.swift */; };
7AA54B255317D07347416493 /* RouteStoreSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA54C53A73B2C31DAB76E08 /* RouteStoreSpec.swift */; };
7AA54B2587BDE8D88215802C /* RootPresenterSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA54C72B63B2B4C8200F3F8 /* RootPresenterSpec.swift */; };
7AA54B381C4053C210A6B8F2 /* ItemDetailViewSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA54B40E4D80EE455AEA16D /* ItemDetailViewSpec.swift */; };
7AA54B936D967416C7AD4F3C /* Observable+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA54FE4F9D6DAEF984F63E6 /* Observable+.swift */; };
7AA54BE3C277CF8AC0C4306A /* Date+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA5404248F72FC87CC4978B /* Date+.swift */; };
7AA54BF8B62177DC348BFDA6 /* UIImage+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA542A3362BE85B313A88B8 /* UIImage+.swift */; };
7AA54C1981A1295939512085 /* String+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA5444E03C80B7A8A0E4EE5 /* String+.swift */; };
7AA54C4B886A9388A2BCE81A /* FxAPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA548CFC3319394489DD10E /* FxAPresenter.swift */; };
Expand Down Expand Up @@ -109,6 +111,8 @@
7D7AD94B204EFBA4002D35EE /* StatusAlert.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7D7AD94A204EFBA4002D35EE /* StatusAlert.xib */; };
7D8296591F9FA4E800ED1ADD /* RxSwift.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7DF469C31F9F8F8000375C74 /* RxSwift.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
7D88BD461F9A6F5B0082A838 /* ItemEntrySpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D88BD451F9A6F5B0082A838 /* ItemEntrySpec.swift */; };
7D92DAF320586FBA00195A1B /* RxOptional.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7D92DAF220586FBA00195A1B /* RxOptional.framework */; };
7D92DAF420586FC200195A1B /* RxOptional.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7D92DAF220586FBA00195A1B /* RxOptional.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
7DA4C6832028F84800B61DD8 /* ItemListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DA4C6802028F84800B61DD8 /* ItemListView.swift */; };
7DA4C6862028F85A00B61DD8 /* ItemListPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DA4C6852028F85A00B61DD8 /* ItemListPresenter.swift */; };
7DA4C68D2028F99200B61DD8 /* ItemListViewSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DA4C6872028F86E00B61DD8 /* ItemListViewSpec.swift */; };
Expand Down Expand Up @@ -160,6 +164,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
7D92DAF420586FC200195A1B /* RxOptional.framework in CopyFiles */,
7D302BEB1FD7284600D2FD4B /* RxCocoa.framework in CopyFiles */,
7DEA1C4420362E18008AD7C4 /* Differentiator.framework in CopyFiles */,
7D8296591F9FA4E800ED1ADD /* RxSwift.framework in CopyFiles */,
Expand All @@ -171,6 +176,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
7AA5404248F72FC87CC4978B /* Date+.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Date+.swift"; sourceTree = "<group>"; };
7AA5406822AB67B669BAF89B /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.info; path = Info.plist; sourceTree = "<group>"; };
7AA540ABA174D12707C616B4 /* KeychainManagerSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeychainManagerSpec.swift; sourceTree = "<group>"; };
7AA540B2CD80BD7AB0E09813 /* FxAActionSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FxAActionSpec.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -202,6 +208,7 @@
7AA5455D02519DBA3D3711A9 /* OAuthInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuthInfo.swift; sourceTree = "<group>"; };
7AA54575C8FE51664E45ACD8 /* CopyAction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CopyAction.swift; sourceTree = "<group>"; };
7AA545A7472772984D517CB0 /* ErrorActionSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorActionSpec.swift; sourceTree = "<group>"; };
7AA545A802C0592A820AD230 /* Date+Spec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Date+Spec.swift"; sourceTree = "<group>"; };
7AA5469D908525083C732018 /* RouteActionSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteActionSpec.swift; sourceTree = "<group>"; };
7AA546BF73E7AFEB663E5DA8 /* ProfileInfoSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProfileInfoSpec.swift; sourceTree = "<group>"; };
7AA546C351E7D7C7DE3BEF0C /* RootView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RootView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -287,6 +294,7 @@
7D6FB87A204F00E2005E23AA /* StatusAlert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusAlert.swift; sourceTree = "<group>"; };
7D7AD94A204EFBA4002D35EE /* StatusAlert.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = StatusAlert.xib; sourceTree = "<group>"; };
7D88BD451F9A6F5B0082A838 /* ItemEntrySpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemEntrySpec.swift; sourceTree = "<group>"; };
7D92DAF220586FBA00195A1B /* RxOptional.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxOptional.framework; path = Carthage/Build/iOS/RxOptional.framework; sourceTree = "<group>"; };
7D9D64921F9F8EE300279C39 /* RxCocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxCocoa.framework; path = Carthage/Build/iOS/RxCocoa.framework; sourceTree = "<group>"; };
7DA4C6802028F84800B61DD8 /* ItemListView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ItemListView.swift; sourceTree = "<group>"; };
7DA4C6852028F85A00B61DD8 /* ItemListPresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ItemListPresenter.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -326,6 +334,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
7D92DAF320586FBA00195A1B /* RxOptional.framework in Frameworks */,
7D4D2B3A20362DC500238143 /* RxDataSources.framework in Frameworks */,
7DEA1C4320362E18008AD7C4 /* Differentiator.framework in Frameworks */,
7DF469C41F9F8F8000375C74 /* RxSwift.framework in Frameworks */,
Expand Down Expand Up @@ -468,6 +477,7 @@
7AA5450E45A4DB17626F4684 /* WebView.swift */,
7AA54FE4F9D6DAEF984F63E6 /* Observable+.swift */,
7AA542A3362BE85B313A88B8 /* UIImage+.swift */,
7AA5404248F72FC87CC4978B /* Date+.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -499,6 +509,7 @@
7D1B1A8A1F98F86400C1F5FF /* Frameworks */ = {
isa = PBXGroup;
children = (
7D92DAF220586FBA00195A1B /* RxOptional.framework */,
7DEA1C4120362E09008AD7C4 /* Differentiator.framework */,
7D320B3420360F9000891F73 /* RxDataSources.framework */,
D83C9FBD1FAD3D5800D08AAE /* Telemetry.framework */,
Expand Down Expand Up @@ -557,6 +568,7 @@
7AA54A831379DFCD181B2362 /* ItemDetailActionSpec.swift */,
7AA54B7943446ED2E302C470 /* CopyActionSpec.swift */,
7AA543307459610B170AFBD5 /* CopyConfirmationDisplayStoreSpec.swift */,
7AA545A802C0592A820AD230 /* Date+Spec.swift */,
);
path = "lockbox-iosTests";
sourceTree = "<group>";
Expand Down Expand Up @@ -779,6 +791,7 @@
7AA540053B184518789CDA12 /* ItemDetailActionSpec.swift in Sources */,
7AA54E698CA72C6C83804EAC /* CopyActionSpec.swift in Sources */,
7AA54EEAB64EF8D9A7F48739 /* CopyConfirmationDisplayStoreSpec.swift in Sources */,
7AA54A03CA11221087E4F9F8 /* Date+Spec.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -835,6 +848,7 @@
7AA54EC7A9CA9BBD57AFEAF9 /* FilterCell.swift in Sources */,
7AA544AA5AE3DE25FE0B0B74 /* CopyAction.swift in Sources */,
7AA545D6182E36466BC8256A /* CopyConfirmationDisplayStore.swift in Sources */,
7AA54BE3C277CF8AC0C4306A /* Date+.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
88 changes: 75 additions & 13 deletions lockbox-ios/Action/DataStoreAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ enum DataStoreError: Error {
}

enum JSCallbackFunction: String {
case OpenComplete, InitializeComplete, UnlockComplete, LockComplete, ListComplete
case OpenComplete, InitializeComplete, UnlockComplete, LockComplete, ListComplete, UpdateComplete

static let allValues: [JSCallbackFunction] = [
.OpenComplete,
.InitializeComplete,
.UnlockComplete,
.LockComplete,
.ListComplete
.ListComplete,
.UpdateComplete
]
}

enum DataStoreAction: Action {
case list(list: [Item])
case list(list: [String: Item])
case updated(item: Item)
case locked(locked: Bool)
case initialized(initialized: Bool)
case opened(opened: Bool)
Expand All @@ -34,8 +36,10 @@ enum DataStoreAction: Action {
extension DataStoreAction: Equatable {
static func ==(lhs: DataStoreAction, rhs: DataStoreAction) -> Bool {
switch (lhs, rhs) {
case (.list(let lhList), .list(let rhList)):
return lhList.elementsEqual(rhList)
case (.list, .list):
return true
case (.updated(let lhItem), .updated(let rhItem)):
return lhItem == rhItem
case (.locked(let lhLocked), .locked(let rhLocked)):
return lhLocked == rhLocked
case (.initialized(let lhInitialized), .initialized(let rhInitialized)):
Expand Down Expand Up @@ -63,7 +67,8 @@ class DataStoreActionHandler: NSObject, ActionHandler {
private var initializeSubject = PublishSubject<Void>()
private var unlockSubject = PublishSubject<Void>()
private var lockSubject = PublishSubject<Void>()
private var listSubject = PublishSubject<[Item]>()
private var listSubject = PublishSubject<[String: Item]>()
private var updateSubject = PublishSubject<Item>()

internal var webViewConfiguration: WKWebViewConfiguration {
let webConfig = WKWebViewConfiguration()
Expand Down Expand Up @@ -182,12 +187,26 @@ class DataStoreActionHandler: NSObject, ActionHandler {
self?.dispatcher.dispatch(action: DataStoreAction.list(list: itemList))
}, onError: { [weak self] error in
self?.dispatcher.dispatch(action: ErrorAction(error: error))
self?.listSubject = PublishSubject<[Item]>()
self?.listSubject = PublishSubject<[String: Item]>()
})
.disposed(by: self.disposeBag)

self._list()
}

public func touch(_ item: Item) {
self.updateSubject
.take(1)
.subscribe(onNext: { [weak self] item in
self?.dispatcher.dispatch(action: DataStoreAction.updated(item: item))
}, onError: { [weak self] error in
self?.dispatcher.dispatch(action: ErrorAction(error: error))
self?.updateSubject = PublishSubject<Item>()
})
.disposed(by: self.disposeBag)

self._touch(item)
}
}

// javascript interaction
Expand Down Expand Up @@ -273,6 +292,26 @@ extension DataStoreActionHandler {
.disposed(by: self.disposeBag)
}

private func _touch(_ item: Item) {
self.openSubject.take(1)
.flatMap { _ in
self.checkState()
}
.flatMap { _ -> Single<Any> in
if item.id == nil {
throw DataStoreError.NoIDPassed
}

let jsonItem = try self.parser.jsonStringFromItem(item)

return self.webView.evaluateJavaScript("\(self.dataStoreName).touch(\(jsonItem))")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I… don't… understand… this code.

Doesn't evaluateJavascript return void? Oh, god it, Rx monkey patches.

}
.subscribe(onError: { error in
self.updateSubject.onError(error)
})
.disposed(by: self.disposeBag)
}

private func checkState() -> Single<Bool> {
return _initialized().asObservable()
.flatMap { initialized -> Observable<Bool> in
Expand All @@ -291,6 +330,23 @@ extension DataStoreActionHandler {
}
.asSingle()
}

private func completeSubjectWithBody(messageBody: Any, subject: PublishSubject<Item>) {
guard let itemDictionary = messageBody as? [String: Any] else {
subject.onError(DataStoreError.UnexpectedType)
return
}

var item: Item
do {
item = try self.parser.itemFromDictionary(itemDictionary)
} catch {
subject.onError(error)
return
}

subject.onNext(item)
}
}

extension DataStoreActionHandler: WKScriptMessageHandler, WKNavigationDelegate {
Expand All @@ -313,22 +369,28 @@ extension DataStoreActionHandler: WKScriptMessageHandler, WKNavigationDelegate {
self.unlockSubject.onNext(())
case .LockComplete:
self.lockSubject.onNext(())
case .UpdateComplete:
self.completeSubjectWithBody(messageBody: message.body, subject: self.updateSubject)
case .ListComplete:
guard let listBody = message.body as? [[Any]] else {
self.dispatcher.dispatch(action: ErrorAction(error: DataStoreError.UnexpectedType))
break
}

let itemList = listBody.flatMap { (anyList: [Any]) -> Item? in
guard let itemDictionary = anyList[1] as? [String: Any],
let item = try? self.parser.itemFromDictionary(itemDictionary) else {
return nil
let itemDictionary = listBody.reduce([:]) { dict, anyList -> [String: Item] in
guard let itemId = anyList[0] as? String,
let itemJSON = anyList[1] as? [String: Any],
let item = try? self.parser.itemFromDictionary(itemJSON) else {
return dict
}

return item
var updatedDict = dict
updatedDict[itemId] = item

return updatedDict
}

self.listSubject.onNext(itemList)
self.listSubject.onNext(itemDictionary)
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions lockbox-ios/Common/Extensions/Date+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import Foundation

extension Date {
public init?(iso8601DateString: String) {
let trimmedIsoString = iso8601DateString.replacingOccurrences(of: "\\.\\d+", with: "", options: .regularExpression)
if let date = ISO8601DateFormatter().date(from: trimmedIsoString) {
self = date
} else {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could you document some of the sample strings this is expected to handle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the test count as documentation here? Or are you saying that I could include more cases in the spec file to be more thorough?

}
}
9 changes: 3 additions & 6 deletions lockbox-ios/Common/Helpers/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ enum ParserError: Error {

class Parser: NSObject, ItemParser {
private let encoder: JSONEncoder

private var itemProperties: [String] {
return Mirror(reflecting: Item.Builder().build()).children.flatMap {
$0.label
}
private static let itemCodingKeys = Item.CodingKeys.allValues.map {
$0.stringValue
}

init(encoder: JSONEncoder? = JSONEncoder()) {
Expand All @@ -29,7 +26,7 @@ class Parser: NSObject, ItemParser {

func itemFromDictionary(_ dictionary: [String: Any]) throws -> Item {
let sanitizedDictionary = dictionary.filter { pair -> Bool in
return self.itemProperties.contains(pair.key)
return Parser.itemCodingKeys.contains(pair.key)
}

var item: Item
Expand Down
27 changes: 26 additions & 1 deletion lockbox-ios/Model/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ class Item: Codable, Equatable {
var lastUsed: String?
var entry: ItemEntry

var createdDate: Date? {
return Date(iso8601DateString: self.created ?? "")
}
var modifiedDate: Date? {
return Date(iso8601DateString: self.modified ?? "")
}
var lastUsedDate: Date? {
return Date(iso8601DateString: self.lastUsed ?? "")
}

enum CodingKeys: String, CodingKey {
case id = "id"
case disabled = "disabled"
case title = "title"
case origins = "origins"
case tags = "tags"
case created = "created"
case modified = "modified"
case lastUsed = "last_used"
case entry = "entry"

static let allValues = [id, disabled, title, origins, tags, created, modified, lastUsed, entry]
}

init(origins: [String], entry: ItemEntry) {
self.origins = origins
self.entry = entry
Expand All @@ -23,7 +47,8 @@ class Item: Codable, Equatable {
static func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.id == rhs.id &&
lhs.entry == rhs.entry &&
lhs.origins.elementsEqual(rhs.origins)
lhs.origins.elementsEqual(rhs.origins) &&
lhs.modified == rhs.modified

}

Expand Down
Loading