Skip to content

Commit

Permalink
You can send photos now!
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Vaidyam committed Jun 19, 2017
1 parent 0410470 commit b595fbe
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 26 deletions.
20 changes: 13 additions & 7 deletions Hangouts/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public class IConversation: ParrotServiceExtension.Conversation {
return dict
}

// FIXME: ugh...
// FIXME: ugh...
public func focus(mode: FocusMode) {
let id = (self.user_list.me as! User).id

Expand Down Expand Up @@ -262,28 +262,34 @@ public class IConversation: ParrotServiceExtension.Conversation {
// (if you specify both image_file and image_id together, image_file
// takes precedence and supplied image_id will be ignored)
// Send messages with OTR status matching the conversation's status.
//
// Note: tl;dr if uploading an image, use image_data and image_name...
// Otherwise, if using an existing image, use image_id only.
// If using another person's public image, use image_id and image_user_id.
//
public func sendMessage(segments: [IChatMessageSegment],
image_data: Data? = nil,
image_name: String? = nil,
image_id: String? = nil,
image_user_id: String? = nil,
cb: (() -> Void)? = nil
) {
let otr_status = (is_off_the_record ? OffTheRecordStatus.OffTheRecord : OffTheRecordStatus.OnTheRecord)
let otr_status = (self.is_off_the_record ? OffTheRecordStatus.OffTheRecord : OffTheRecordStatus.OnTheRecord)
if let image_data = image_data, let image_name = image_name {
client.uploadImage(data: image_data, filename: image_name) { photoID in
self.client.uploadImage(data: image_data, filename: image_name) { photoID in
self.client.sendChatMessage(conversation_id: self.id,
segments: segments.map { $0.serialize() },
image_id: image_id,
image_id: photoID,
image_user_id: nil,
otr_status: otr_status) { _ in cb?() }
otr_status: otr_status,
delivery_medium: self.getDefaultDeliveryMedium().mediumType!) { _ in cb?() }
}
} else {
client.sendChatMessage(conversation_id: id,
self.client.sendChatMessage(conversation_id: id,
segments: segments.map { $0.serialize() },
image_id: nil,
otr_status: otr_status,
delivery_medium: getDefaultDeliveryMedium().mediumType!) { _ in cb?() }
delivery_medium: self.getDefaultDeliveryMedium().mediumType!) { _ in cb?() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion MochaUI/AppKit+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public func runSelectionPanel(for window: NSWindow, fileTypes: [String],
p.canCreateDirectories = false
p.canDownloadUbiquitousContents = true
p.canResolveUbiquitousConflicts = false
p.resolvesAliases = false
p.resolvesAliases = true
p.allowedFileTypes = fileTypes
p.prompt = "Select"
p.beginSheetModal(for: window) { r in
Expand Down
58 changes: 48 additions & 10 deletions Parrot/MessageInputViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public protocol TextInputHost {
func resized(to: Double)
func typing()
func send(message: String)
func send(images: [URL])
}

public class MessageInputViewController: NSViewController, NSTextViewExtendedDelegate {
Expand All @@ -16,12 +17,14 @@ public class MessageInputViewController: NSViewController, NSTextViewExtendedDel

private var insertToken = false

private lazy var photoView: NSImageView = {
let v = NSImageView().modernize(wantsLayer: true)
v.allowsCutCopyPaste = false
v.isEditable = false
v.animates = true
return v
private lazy var photoView: NSButton = {
let img = NSImage(named: NSImage.Name(rawValue: "NSMediaBrowserMediaTypePhotosTemplate32"))!
let b = NSButton(title: "", image: img, target: self,
action: #selector(self.loadImage(_:)))
.modernize(wantsLayer: true)
b.isBordered = false
b.wantsLayer = true
return b
}()

private lazy var textView: ExtendedTextView = {
Expand Down Expand Up @@ -70,14 +73,15 @@ public class MessageInputViewController: NSViewController, NSTextViewExtendedDel
super.viewWillAppear()

// Mask the image into a circle and grab it.
if let layer = self.photoView.layer {
/*if let layer = self.photoView.layer {
layer.masksToBounds = true
layer.cornerRadius = 24.0 / 2.0 // FIXME: dynamic mask
}
self.photoView.image = self.host?.image
*/
//self.photoView.image = self.host?.image

self.textView.translatesAutoresizingMaskIntoConstraints = false
self.textView.enclosingScrollView?.replaceInSuperview(with: self.textView)
//self.textView.translatesAutoresizingMaskIntoConstraints = false
//self.textView.enclosingScrollView?.replaceInSuperview(with: self.textView)
}

// Set up dark/light notifications.
Expand Down Expand Up @@ -121,13 +125,47 @@ public class MessageInputViewController: NSViewController, NSTextViewExtendedDel
NSForegroundColorAttributeName: NSColor.tertiaryLabelColor(),
NSFontAttributeName: text.font!
]*/

self.setColors()
}
}

private func setColors() {
let text = self.textView

var color = NSColor.darkOverlay(forAppearance: self.view.effectiveAppearance)//NSColor.secondaryLabelColor
let setting = "com.avaidyam.Parrot.ConversationOutgoingColor"
if let q = Settings[setting] as? Data,
let c = NSUnarchiver.unarchiveObject(with: q) as? NSColor,
c.alphaComponent > 0.0 {
color = c

// This automatically adjusts labelColor to the right XOR mask.
text.appearance = color.isLight() ? .light : .dark
} else {
text.appearance = self.view.effectiveAppearance//self.appearance
}
text.layer?.backgroundColor = color.cgColor
}

public override func viewWillDisappear() {
ParrotAppearance.unregisterInterfaceStyleListener(observer: self)
}

//
//
//

@objc private func loadImage(_ sender: NSButton!) {
runSelectionPanel(for: self.view.window!, fileTypes: [kUTTypeImage as String], multiple: true) { urls in
self.host?.send(images: urls)
}
}

//
//
//

private func resizeModule() {
NSAnimationContext.animate(duration: 0.6) { // TODO: FIX THIS
self.textView.invalidateIntrinsicContentSize()
Expand Down
29 changes: 22 additions & 7 deletions Parrot/MessageListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,16 @@ NSSearchFieldDelegate, NSCollectionViewDataSource, NSCollectionViewDelegate, NSC
//self.sendMessageHandler(message, self.conversation!)
}

public func send(images: [URL]) {
for url in images {
let img = try? Data(contentsOf: url)
let fname = url.lastPathComponent
self.conversation?.sendMessage(segments: [], image_data: img, image_name: fname, image_id: nil, image_user_id: nil) {
print("sent img1")
}
}
}

static func sendMessage(_ text: String, _ conversation: ParrotServiceExtension.Conversation) {
conversation.send(message: text)
}
Expand All @@ -610,25 +620,23 @@ NSSearchFieldDelegate, NSCollectionViewDataSource, NSCollectionViewDelegate, NSC
//
//

/*
private lazy var addButton: NSButton = {
let b = NSButton(title: "", image: NSImage(named: "NSAddBookmarkTemplate")!,
let b = NSButton(title: "", image: NSImage(named: NSImage.Name(rawValue: "NSAddBookmarkTemplate"))!,
target: nil, action: nil).modernize()
b.bezelStyle = .texturedRounded
b.imagePosition = .imageOnly
return b
}()

private lazy var searchToggle: NSButton = {
let b = NSButton(title: "", image: NSImage(named: NSImageNameRevealFreestandingTemplate)!,
target: self, action: #selector(self.toggleSearchField(_:))).modernize()
let b = NSButton(title: "", image: NSImage(named: NSImage.Name.revealFreestandingTemplate)!,
target: nil, action: nil)
b.bezelStyle = .texturedRounded
b.imagePosition = .imageOnly
b.setButtonType(.onOff)
b.state = NSControlStateValueOn
b.state = NSControl.StateValue.on
return b
}()
*/

private var _usersToIndicators: [Person.IdentifierType: PersonIndicatorViewController] = [:]
private func _usersToItems() -> [NSToolbarItem] {
Expand All @@ -640,15 +648,22 @@ NSSearchFieldDelegate, NSCollectionViewDataSource, NSCollectionViewDelegate, NSC
_usersToIndicators[$0.identifier] = vc
}
}

return self._usersToIndicators.values.map { $0.toolbarItem }
}
private func _setToolbar() {
/*
let i = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "search"))
i.view = self.searchToggle
i.label = "Search"
*/

let h = self.toolbarContainer
h.templateItems = Set(_usersToItems())
//h.templateItems.insert(i)
var order = _usersToItems().map { $0.itemIdentifier }
order.insert(.flexibleSpace, at: 0)
order.append(.flexibleSpace)
//order.append(NSToolbarItem.Identifier(rawValue: "search"))
h.itemOrder = order

/*
Expand Down
4 changes: 3 additions & 1 deletion Parrot/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ public struct Preferences {
private init() {}
}

public enum Key {
public struct Key {
private init() {}

public static let InterfaceStyle = "Parrot.InterfaceStyle"
public static let SystemInterfaceStyle = "Parrot.SystemInterfaceStyle"
public static let VibrancyStyle = "Parrot.VibrancyStyle"
Expand Down

0 comments on commit b595fbe

Please sign in to comment.