Skip to content

Commit

Permalink
Fixes bug 1104123 - Hook up the Share UI to the Bookmarks Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
st3fan committed Nov 25, 2014
1 parent a5902b7 commit fc937d9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
D34F86DB1A1AC35B00331EC4 /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34F86DA1A1AC35B00331EC4 /* LoginView.swift */; };
D34F86E71A1AC7DD00331EC4 /* PasswordTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34F86E61A1AC7DD00331EC4 /* PasswordTextField.swift */; };
D34F86E91A1ACD1200331EC4 /* ImageTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34F86E81A1ACD1200331EC4 /* ImageTextField.swift */; };
E418D0D91A251B3200CAE47A /* Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34DC84D1A16C40C00D49B7B /* Account.swift */; };
E418D0DA1A251B3200CAE47A /* AccountManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34DC84E1A16C40C00D49B7B /* AccountManager.swift */; };
E418D0DB1A251B3200CAE47A /* Bookmarks.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34DC84F1A16C40C00D49B7B /* Bookmarks.swift */; };
E418D0DC1A251B3200CAE47A /* Clients.swift in Sources */ = {isa = PBXBuildFile; fileRef = E42CCDE21A23A6F900B794D3 /* Clients.swift */; };
E418D0DD1A251B3200CAE47A /* Cursor.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34DC8501A16C40C00D49B7B /* Cursor.swift */; };
E418D0DE1A251B3200CAE47A /* Favicons.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34DC8511A16C40C00D49B7B /* Favicons.swift */; };
E418D0DF1A251B3200CAE47A /* RestAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34DC8521A16C40C00D49B7B /* RestAPI.swift */; };
E41A7D431A1BDFD800245963 /* FiraSans-Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = E41A7D421A1BDFD800245963 /* FiraSans-Regular.ttf */; };
E41A7D441A1BDFD800245963 /* FiraSans-Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = E41A7D421A1BDFD800245963 /* FiraSans-Regular.ttf */; };
E41A7D481A1BE00100245963 /* FiraMono-Medium.ttf in Resources */ = {isa = PBXBuildFile; fileRef = E41A7D471A1BE00100245963 /* FiraMono-Medium.ttf */; };
Expand Down Expand Up @@ -964,9 +971,16 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E418D0DA1A251B3200CAE47A /* AccountManager.swift in Sources */,
E418D0DC1A251B3200CAE47A /* Clients.swift in Sources */,
E42CCE031A24C4E300B794D3 /* ExtensionUtils.swift in Sources */,
E418D0D91A251B3200CAE47A /* Account.swift in Sources */,
E418D0DE1A251B3200CAE47A /* Favicons.swift in Sources */,
F8708D321A0970B70051AB07 /* ShareViewController.swift in Sources */,
E418D0DF1A251B3200CAE47A /* RestAPI.swift in Sources */,
E418D0DD1A251B3200CAE47A /* Cursor.swift in Sources */,
E41A7D4B1A1BE04500245963 /* InitialViewController.swift in Sources */,
E418D0DB1A251B3200CAE47A /* Bookmarks.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
8 changes: 6 additions & 2 deletions Extensions/ShareTo/InitialViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ let LastUsedShareDestinationsKey = "LastUsedShareDestinations"
class InitialViewController: UIViewController, ShareControllerDelegate
{
var shareDialogController: ShareDialogController!
var account: Account?

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(white: 0.75, alpha: 0.65) // TODO: Is the correct color documented somewhere?

println(self.extensionContext!.inputItems)
let accountManager = AccountManager(loginCallback: { _ in () }, logoutCallback: { _ in () })
self.account = accountManager.getAccount()
}

override func viewDidAppear(animated: Bool)
Expand Down Expand Up @@ -126,6 +128,8 @@ class InitialViewController: UIViewController, ShareControllerDelegate
}

func shareToBookmarks(item: ExtensionUtils.ShareItem) {
// TODO: Discuss how to share to bookmarks
if account != nil { // TODO: We need to properly deal with this.
account?.bookmarks.shareItem(item)
}
}
}
38 changes: 37 additions & 1 deletion Providers/Bookmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,40 @@ class Bookmarks: NSObject {

return resp;
}
};

/// Send a ShareItem to this user's bookmarks
///
/// :param: item the item to be sent
///
/// Note that this code currently uses NSURLSession directly because AlamoFire
/// does not work from an Extension. (Bug 1104884)
///
/// Note that the bookmark will end up in the Unsorted Bookmarks. We have Bug
/// 1094233 open for the REST API to store the incoming item in the Mobile
/// Bookmarks instead.

This comment has been minimized.

Copy link
@thebnich

thebnich Nov 26, 2014

Contributor

Nit: drop newline

func shareItem(item: ExtensionUtils.ShareItem) {
let request = NSMutableURLRequest(URL: NSURL(string: "https://moz-syncapi.sateh.com/1.0/bookmarks")!)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.HTTPMethod = "POST"

var object = NSMutableDictionary()
object["url"] = item.url
object["title"] = item.title == nil ? "" : item.title

var jsonError: NSError?
let data = NSJSONSerialization.dataWithJSONObject(object, options: nil, error: &jsonError)
if data != nil {
request.HTTPBody = data
}

let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("Bookmarks/shareItem")
configuration.HTTPAdditionalHeaders = ["Authorization" : account.basicAuthorizationHeader()]
configuration.sharedContainerIdentifier = ExtensionUtils.sharedContainerIdentifier()

let session = NSURLSession(configuration: configuration, delegate: nil, delegateQueue: nil)
let task = session.dataTaskWithRequest(request)
task.resume()

This comment has been minimized.

Copy link
@thebnich

thebnich Nov 26, 2014

Contributor

This block is almost exactly the same as the block in Clients.swift, with some minor differences. Can we factor this out into a shared method?

}
}

0 comments on commit fc937d9

Please sign in to comment.