Skip to content

Commit

Permalink
Fix for #16 - multiple pop ups in conversation
Browse files Browse the repository at this point in the history
No options to configure yet.
  • Loading branch information
Neo11 committed Mar 11, 2016
1 parent 17a79d6 commit cf38fa2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
6 changes: 5 additions & 1 deletion ChitChat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
1C152416374AAE0D854DA21A /* WAMApplication.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C152F56EDCA973A1C468608 /* WAMApplication.swift */; };
1C15259F1E16730088D59710 /* WAMWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C152D41F9D97D5FD31F2236 /* WAMWebView.swift */; };
1C15270EBBF4C0FE6E9C3C53 /* WKScriptMessageHandlerToNotification.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C15279010CB8562CAD66091 /* WKScriptMessageHandlerToNotification.swift */; };
223C319C1B07B66500E09138 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 223C319B1B07B66500E09138 /* AppDelegate.m */; };
223C319E1B07B66500E09138 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 223C319D1B07B66500E09138 /* main.m */; };
223C31A31B07B66500E09138 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 223C31A21B07B66500E09138 /* Images.xcassets */; };
Expand Down Expand Up @@ -39,6 +40,7 @@

/* Begin PBXFileReference section */
1C15252B8EDE9D3450C658B4 /* ChitChat-Bridging-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ChitChat-Bridging-Header.h"; sourceTree = "<group>"; };
1C15279010CB8562CAD66091 /* WKScriptMessageHandlerToNotification.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WKScriptMessageHandlerToNotification.swift; sourceTree = "<group>"; };
1C152D41F9D97D5FD31F2236 /* WAMWebView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WAMWebView.swift; sourceTree = "<group>"; };
1C152F56EDCA973A1C468608 /* WAMApplication.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WAMApplication.swift; sourceTree = "<group>"; };
223C31951B07B66500E09138 /* ChitChat.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ChitChat.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -105,6 +107,7 @@
1C15252B8EDE9D3450C658B4 /* ChitChat-Bridging-Header.h */,
1C152F56EDCA973A1C468608 /* WAMApplication.swift */,
1C152D41F9D97D5FD31F2236 /* WAMWebView.swift */,
1C15279010CB8562CAD66091 /* WKScriptMessageHandlerToNotification.swift */,
);
path = WhatsMac;
sourceTree = "<group>";
Expand Down Expand Up @@ -160,7 +163,7 @@
TargetAttributes = {
223C31941B07B66500E09138 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = 4XFEQ6CG38;
DevelopmentTeam = 85EU5W2C4T;
};
};
};
Expand Down Expand Up @@ -208,6 +211,7 @@
223C319C1B07B66500E09138 /* AppDelegate.m in Sources */,
1C152416374AAE0D854DA21A /* WAMApplication.swift in Sources */,
1C15259F1E16730088D59710 /* WAMWebView.swift in Sources */,
1C15270EBBF4C0FE6E9C3C53 /* WKScriptMessageHandlerToNotification.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
30 changes: 5 additions & 25 deletions WhatsMac/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
NSString *const _AppleActionOnDoubleClickNotification = @"AppleNoRedisplayAppearancePreferenceChanged";
NSString* const WAMShouldHideStatusItem = @"WAMShouldHideStatusItem";

@interface AppDelegate () <NSWindowDelegate, WKNavigationDelegate, WKUIDelegate, WKScriptMessageHandler, NSUserNotificationCenterDelegate>
@interface AppDelegate () <NSWindowDelegate, WKNavigationDelegate, WKUIDelegate, NSUserNotificationCenterDelegate>
@property (strong, nonatomic) NSWindow *window;
@property (strong, nonatomic) WKWebView *webView;
@property (strong, nonatomic) NSStatusItem *statusItem;
Expand Down Expand Up @@ -50,7 +50,8 @@ - (WKWebViewConfiguration*)webViewConfig {
[contentController addUserScript:jqueryUserScript];
[contentController addUserScript:emojiScript];
[contentController addUserScript:userScript];
[contentController addScriptMessageHandler:self name:@"notification"];

[contentController addScriptMessageHandler:[[WKScriptMessageHandlerToNotification alloc] initWithWindow:_window] name:@"notification"];
config.userContentController = contentController;

#if DEBUG
Expand Down Expand Up @@ -341,27 +342,6 @@ - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWe
return nil;
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([[self window] isMainWindow]) {
return;
}
NSArray *messageBody = message.body;
NSUserNotification *notification = [NSUserNotification new];
notification.hasReplyButton = true;
notification.responsePlaceholder = @"Reply...";
notification.title = messageBody[0];
notification.subtitle = messageBody[1];
notification.identifier = messageBody[2];

// using private API here, not for the App Store
NSURL *base64URL = [NSURL URLWithString:messageBody[3]];
NSData *imageData = [NSData dataWithContentsOfURL:base64URL];
NSImage *avatar = [[NSImage alloc] initWithData: imageData];
[notification setValue:avatar forKey:@"_identityImage"];

[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];
}

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}
Expand All @@ -371,7 +351,7 @@ - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNot
NSString* userResponse = notification.response.string;
//Sending reply to WAWeb
[self.webView evaluateJavaScript:
[NSString stringWithFormat:@"openChat(\"%@\")", notification.identifier]
[NSString stringWithFormat:@"openChat(\"%@\")", [notification.identifier componentsSeparatedByString:@":"][0]]
completionHandler:nil];

[self.webView evaluateJavaScript:
Expand All @@ -390,7 +370,7 @@ - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNot

} else {
[self.webView evaluateJavaScript:
[NSString stringWithFormat:@"openChat(\"%@\")", notification.identifier]
[NSString stringWithFormat:@"openChat(\"%@\")", [notification.identifier componentsSeparatedByString:@":"][0]]
completionHandler:nil];
[center removeDeliveredNotification:notification];
[_window makeKeyAndOrderFront:self];
Expand Down
9 changes: 9 additions & 0 deletions WhatsMac/WKScriptMessageHandlerToNotification.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Created by Tom Schmidt on 10.03.16.
// Copyright (c) 2016 Tom Schmidt. All rights reserved.
//

import Foundation

class WKScriptMessageHandlerToNotification {
}

0 comments on commit cf38fa2

Please sign in to comment.