Skip to content

Commit

Permalink
Native bridge bug fixes + directory restructure (#11)
Browse files Browse the repository at this point in the history
* Native bridge bug fixes + directory restructure

* Remove custom test url in Example
  • Loading branch information
fdocr authored Nov 9, 2020
1 parent 1d103d7 commit 735b0fa
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 292 deletions.
2 changes: 0 additions & 2 deletions Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
Expand Down
36 changes: 30 additions & 6 deletions ForemWebView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
DC3DAE8325229E18004DAC13 /* ForemWebView.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DC3DAE3425229D8C004DAC13 /* ForemWebView.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
DC3DAE9625229FD2004DAC13 /* forem.dev.html in Resources */ = {isa = PBXBuildFile; fileRef = DC3DAE9425229FD2004DAC13 /* forem.dev.html */; };
DC3DAE9725229FD2004DAC13 /* forem.dev-logged-in.html in Resources */ = {isa = PBXBuildFile; fileRef = DC3DAE9525229FD2004DAC13 /* forem.dev-logged-in.html */; };
DC6576432559CCDD00B5B46F /* ForemMediaManager+PodcastActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC6576422559CCDC00B5B46F /* ForemMediaManager+PodcastActions.swift */; };
DC6576472559CCED00B5B46F /* ForemMediaManager+VideoActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC6576462559CCED00B5B46F /* ForemMediaManager+VideoActions.swift */; };
DC9EF35D2549E013003A1BE7 /* ForemUserData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC9EF35C2549E013003A1BE7 /* ForemUserData.swift */; };
DC9EF3612549FDD5003A1BE7 /* invertedImages.css in Resources */ = {isa = PBXBuildFile; fileRef = DC9EF3602549FDD5003A1BE7 /* invertedImages.css */; };
DCD7C211254A9B4D00FAFC1D /* forem.dev-logged-in-dark.html in Resources */ = {isa = PBXBuildFile; fileRef = DCD7C210254A9B4D00FAFC1D /* forem.dev-logged-in-dark.html */; };
Expand Down Expand Up @@ -85,6 +87,8 @@
DC3DAE6825229E0B004DAC13 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
DC3DAE9425229FD2004DAC13 /* forem.dev.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = forem.dev.html; sourceTree = "<group>"; };
DC3DAE9525229FD2004DAC13 /* forem.dev-logged-in.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "forem.dev-logged-in.html"; sourceTree = "<group>"; };
DC6576422559CCDC00B5B46F /* ForemMediaManager+PodcastActions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ForemMediaManager+PodcastActions.swift"; sourceTree = "<group>"; };
DC6576462559CCED00B5B46F /* ForemMediaManager+VideoActions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ForemMediaManager+VideoActions.swift"; sourceTree = "<group>"; };
DC9EF35C2549E013003A1BE7 /* ForemUserData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForemUserData.swift; sourceTree = "<group>"; };
DC9EF3602549FDD5003A1BE7 /* invertedImages.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; path = invertedImages.css; sourceTree = "<group>"; };
DCD7C210254A9B4D00FAFC1D /* forem.dev-logged-in-dark.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "forem.dev-logged-in-dark.html"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -151,15 +155,11 @@
DC3DAE4025229DA5004DAC13 /* ForemWebView.swift */,
DC3DAE4325229DBB004DAC13 /* ForemWebView+WKScriptMessageHandler.swift */,
DCF27D5B255116EF0065770D /* ForemWebView+WKNavigationDelegate.swift */,
DC3DAE4625229DD7004DAC13 /* ForemMediaManager.swift */,
DCF27D912551D2190065770D /* ForemMediaManager+LockedScreenControls.swift */,
DC9EF35C2549E013003A1BE7 /* ForemUserData.swift */,
DCF27D7725519F660065770D /* ForemInstanceMetadata.swift */,
DC9EF3602549FDD5003A1BE7 /* invertedImages.css */,
DCF27D67255128500065770D /* bodyMutationObserver.js */,
DCF27D6B25514C8F0065770D /* fetchUserData.js */,
DCF27D7F2551A0160065770D /* fetchForemInstanceMetadata.js */,
DC3DAE3825229D8C004DAC13 /* Info.plist */,
DC65764D2559CE1E00B5B46F /* Media Manager */,
DC65764A2559CDA900B5B46F /* JavaScript & CSS */,
);
path = ForemWebView;
sourceTree = "<group>";
Expand Down Expand Up @@ -198,6 +198,28 @@
name = Frameworks;
sourceTree = "<group>";
};
DC65764A2559CDA900B5B46F /* JavaScript & CSS */ = {
isa = PBXGroup;
children = (
DCF27D67255128500065770D /* bodyMutationObserver.js */,
DCF27D6B25514C8F0065770D /* fetchUserData.js */,
DCF27D7F2551A0160065770D /* fetchForemInstanceMetadata.js */,
DC9EF3602549FDD5003A1BE7 /* invertedImages.css */,
);
path = "JavaScript & CSS";
sourceTree = "<group>";
};
DC65764D2559CE1E00B5B46F /* Media Manager */ = {
isa = PBXGroup;
children = (
DC3DAE4625229DD7004DAC13 /* ForemMediaManager.swift */,
DC6576422559CCDC00B5B46F /* ForemMediaManager+PodcastActions.swift */,
DC6576462559CCED00B5B46F /* ForemMediaManager+VideoActions.swift */,
DCF27D912551D2190065770D /* ForemMediaManager+LockedScreenControls.swift */,
);
path = "Media Manager";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -374,9 +396,11 @@
DC3DAE4125229DA5004DAC13 /* ForemWebView.swift in Sources */,
DC3DAE4425229DBB004DAC13 /* ForemWebView+WKScriptMessageHandler.swift in Sources */,
DCF27D5C255116EF0065770D /* ForemWebView+WKNavigationDelegate.swift in Sources */,
DC6576472559CCED00B5B46F /* ForemMediaManager+VideoActions.swift in Sources */,
DC3DAE4725229DD7004DAC13 /* ForemMediaManager.swift in Sources */,
DCF27D7825519F660065770D /* ForemInstanceMetadata.swift in Sources */,
DCF27D922551D2190065770D /* ForemMediaManager+LockedScreenControls.swift in Sources */,
DC6576432559CCDD00B5B46F /* ForemMediaManager+PodcastActions.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
256 changes: 0 additions & 256 deletions ForemWebView/ForemMediaManager.swift

This file was deleted.

2 changes: 1 addition & 1 deletion ForemWebView/ForemUserData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Foundation
themeName = String(element[range])
}
}

switch themeName {
case "night-theme":
return .night
Expand Down
29 changes: 29 additions & 0 deletions ForemWebView/ForemWebView+WKScriptMessageHandler.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import WebKit

enum BridgeMessageType {
case podcast, video
}

extension ForemWebView: WKScriptMessageHandler {
public func userContentController(_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage) {
Expand All @@ -17,6 +21,31 @@ extension ForemWebView: WKScriptMessageHandler {
}
}

// Helper function that will send Bridge messages into the DOM
internal func sendBridgeMessage(type: BridgeMessageType, message: [String: String]) {
var jsonString = ""
let encoder = JSONEncoder()
if let jsonData = try? encoder.encode(message) {
jsonString = String(data: jsonData, encoding: .utf8) ?? ""
}

var javascript = ""
// Supported messages
switch type {
case .podcast:
javascript = "document.getElementById('audiocontent').setAttribute('data-podcast', '\(jsonString)')"
case .video:
javascript = "document.getElementById('video-player-source').setAttribute('data-message', '\(jsonString)')"
}

guard !javascript.isEmpty else { return }
evaluateJavaScript(wrappedJS(javascript)) { _, error in
if let error = error {
print("Error sending Podcast message (\(message)): \(error.localizedDescription)")
}
}
}

private func handleHapticMessage(type: String) {
switch type {
case "heavy":
Expand Down
2 changes: 0 additions & 2 deletions ForemWebView/ForemWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ FOUNDATION_EXPORT double ForemWebViewVersionNumber;
FOUNDATION_EXPORT const unsigned char ForemWebViewVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <ForemWebView/PublicHeader.h>


Loading

0 comments on commit 735b0fa

Please sign in to comment.