瀏覽代碼

convert Class NCViewerRichdocument from NSObject (Singleton) to WKWebView

marinofaggiana 6 年之前
父節點
當前提交
684ffca2bc

+ 4 - 0
iOSClient/Main/CCDetail.h

@@ -33,6 +33,7 @@
 
 @class tableMetadata;
 @class NCViewerImagemeter;
+@class NCViewerRichdocument;
 
 @interface CCDetail : UIViewController <MWPhotoBrowserDelegate, ReaderViewControllerDelegate>
 
@@ -58,6 +59,9 @@
 @property (nonatomic, strong) ReaderViewController *readerPDFViewController;
 @property (nonatomic, strong) NSString *passwordPDF;
 
+// RichDocument
+@property (nonatomic, strong) NCViewerRichdocument *richDocument;
+
 // IM
 @property (nonatomic, strong) NCViewerImagemeter *imagemeter;
 

+ 7 - 3
iOSClient/Main/CCDetail.m

@@ -226,7 +226,7 @@
         }
         
         // RichDocument
-        if ([[NCViewerRichdocument sharedInstance] isRichDocument:self.metadataDetail]) {
+        if ([[NCUtility sharedInstance] isRichDocument:self.metadataDetail] && appDelegate.reachability.isReachable) {
             
             [[NCUtility sharedInstance] startActivityIndicatorWithView:self.view bottom:0];
             
@@ -235,7 +235,9 @@
                     
                     if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
                         
-                        [[NCViewerRichdocument sharedInstance] viewRichDocumentAt:link detail:self];
+                        self.richDocument = [[NCViewerRichdocument alloc] initWithFrame:self.view.bounds configuration:[WKWebViewConfiguration new]];
+                        [self.view addSubview:self.richDocument];
+                        [self.richDocument viewRichDocumentAt:link detail:self];
 
                     } else {
                         
@@ -253,7 +255,9 @@
                 
             } else {
                 
-                [[NCViewerRichdocument sharedInstance] viewRichDocumentAt:self.metadataDetail.url detail:self];
+                self.richDocument = [[NCViewerRichdocument alloc] initWithFrame:self.view.bounds configuration:[WKWebViewConfiguration new]];
+                [self.view addSubview:self.richDocument];
+                [self.richDocument viewRichDocumentAt:self.metadataDetail.url detail:self];
             }
             
             return;

+ 6 - 2
iOSClient/Main/CCMain.m

@@ -4087,9 +4087,13 @@
                     
                     [self shouldPerformSegue:self.metadata];
                     
-                } else if ([self.metadata.typeFile isEqualToString: k_metadataTypeFile_document] && [[NCViewerRichdocument sharedInstance] isRichDocument:self.metadata]) {
+                } else if ([self.metadata.typeFile isEqualToString: k_metadataTypeFile_document] && [[NCUtility sharedInstance] isRichDocument:self.metadata]) {
                     
-                    [self shouldPerformSegue:self.metadata];
+                    if (appDelegate.reachability.isReachable) {
+                        [self shouldPerformSegue:self.metadata];
+                    } else {
+                        [appDelegate messageNotification:@"_info_" description:@"_go_online_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
+                    }
                     
                 } else {
                    

+ 1 - 1
iOSClient/Main/NCMainCommon.swift

@@ -1223,7 +1223,7 @@ class NCNetworkingMain: NSObject, CCNetworkingDelegate {
                 var uti = CCUtility.insertTypeFileIconName(metadata.fileNameView, metadata: metadata)
                 if uti == nil {
                     uti = ""
-                } else if uti!.contains("opendocument") && !NCViewerRichdocument.sharedInstance.isRichDocument(metadata) {
+                } else if uti!.contains("opendocument") && !NCUtility.sharedInstance.isRichDocument(metadata) {
                     metadata.typeFile = k_metadataTypeFile_unknown
                 }
                 

+ 1 - 0
iOSClient/Supporting Files/en.lproj/Localizable.strings

@@ -655,6 +655,7 @@
 "_create_new_document_"             = "Create new document";
 "_create_new_spreadsheet_"          = "Create new spreadsheet";
 "_create_new_presentation_"         = "Create new presentation";
+"_go_online_"                       = "Go online to see the document";
 
 // Intro
 

+ 23 - 1
iOSClient/Utility/NCUtility.swift

@@ -350,6 +350,28 @@ class NCUtility: NSObject {
         return UIFont(descriptor: fontDescriptor, size: bestFontSize)
     }
     
-    
+    @objc func isRichDocument(_ metadata: tableMetadata) -> Bool {
+        
+        guard let mimeType = CCUtility.getMimeType(metadata.fileNameView) else {
+            return false
+        }
+        guard let richdocumentsMimetypes = NCManageDatabase.sharedInstance.getRichdocumentsMimetypes(account: metadata.account) else {
+            return false
+        }
+        
+        if richdocumentsMimetypes.count > 0 && mimeType.components(separatedBy: ".").count > 2 {
+            
+            let mimeTypeArray = mimeType.components(separatedBy: ".")
+            let mimeType = mimeTypeArray[mimeTypeArray.count - 2] + "." + mimeTypeArray[mimeTypeArray.count - 1]
+            
+            for richdocumentMimetype: String in richdocumentsMimetypes {
+                if richdocumentMimetype.contains(mimeType) {
+                    return true
+                }
+            }
+        }
+        
+        return false
+    }
 }
 

+ 24 - 58
iOSClient/Viewer/NCViewerRichdocument.swift

@@ -23,16 +23,24 @@
 
 import Foundation
 
-class NCViewerRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandler, NCSelectDelegate {
+class NCViewerRichdocument: WKWebView, WKNavigationDelegate, WKScriptMessageHandler, NCSelectDelegate {
     
-    var detail: CCDetail!
-    var webView: WKWebView!
     let appDelegate = UIApplication.shared.delegate as! AppDelegate
+    var detail: CCDetail!
+   
+    override init(frame: CGRect, configuration: WKWebViewConfiguration) {
+        super.init(frame: frame, configuration: configuration)
 
-    @objc static let sharedInstance: NCViewerRichdocument = {
-        let instance = NCViewerRichdocument()
-        return instance
-    }()
+        let contentController = configuration.userContentController
+        contentController.add(self, name: "RichDocumentsMobileInterface")
+        
+        autoresizingMask = [.flexibleWidth, .flexibleHeight]
+        navigationDelegate = self
+    }
+    
+    required init?(coder: NSCoder) {
+        super.init(coder: coder)
+    }
     
     @objc func viewRichDocumentAt(_ link: String, detail: CCDetail) {
         
@@ -45,27 +53,14 @@ class NCViewerRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandl
             detail.navigationController?.setNavigationBarHidden(true, animated: false)
         }
         
-        let contentController = WKUserContentController()
-        contentController.add(self, name: "RichDocumentsMobileInterface")
-        let configuration = WKWebViewConfiguration()
-        configuration.userContentController = contentController
-        
-        webView = WKWebView(frame: detail.view.bounds, configuration: configuration)
-        webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
-//        webView.scrollView.showsHorizontalScrollIndicator = true
-//        webView.scrollView.showsVerticalScrollIndicator = true
-        webView.navigationDelegate = self
-        
         var request = URLRequest(url: URL(string: link)!)
         request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
         let language = NSLocale.preferredLanguages[0] as String
         request.addValue(language, forHTTPHeaderField: "Accept-Language")
         
         let userAgent : String = CCUtility.getUserAgent()
-        webView.customUserAgent = userAgent
-        webView.load(request)
-        
-        detail.view.addSubview(webView)
+        customUserAgent = userAgent
+        load(request)        
     }
     
     @objc func keyboardDidShow(notification: Notification) {
@@ -73,11 +68,11 @@ class NCViewerRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandl
         guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
         let keyboardFrame = frameInfo.cgRectValue
         //print("keyboardFrame: \(keyboardFrame)")
-        webView.frame.size.height = detail.view.bounds.height - keyboardFrame.size.height
+        frame.size.height = detail.view.bounds.height - keyboardFrame.size.height
     }
     
     @objc func keyboardWillHide(notification: Notification) {
-        webView.frame = detail.view.bounds
+        frame = detail.view.bounds
     }
     
     //MARK: -
@@ -87,8 +82,8 @@ class NCViewerRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandl
         if (message.name == "RichDocumentsMobileInterface") {
             
             if message.body as! String == "close" {
-
-                self.webView.removeFromSuperview()
+                
+                removeFromSuperview()
                 
                 self.detail.navigationController?.popViewController(animated: true)
 //                detail.navigationController?.setNavigationBarHidden(false, animated: false)
@@ -128,7 +123,7 @@ class NCViewerRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandl
             OCNetworking.sharedManager().createAssetRichdocuments(withAccount: metadata?.account, fileName: metadata?.fileName, serverUrl: serverUrl, completion: { (account, url, message, errorCode) in
                 if errorCode == 0 && account == self.appDelegate.activeAccount {
                     let functionJS = "OCA.RichDocuments.documentsMain.postAsset('\(metadata!.fileNameView)', '\(url!)')"
-                    self.webView.evaluateJavaScript(functionJS, completionHandler: { (result, error) in })
+                    self.evaluateJavaScript(functionJS, completionHandler: { (result, error) in })
                 } else if errorCode != 0 {
                     self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
                 } else {
@@ -143,7 +138,7 @@ class NCViewerRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandl
         OCNetworking.sharedManager().createAssetRichdocuments(withAccount: metadata?.account, fileName: metadata?.fileName, serverUrl: serverUrl, completion: { (account, url, message, errorCode) in
             if errorCode == 0 && account == self.appDelegate.activeAccount {
                 let functionJS = "OCA.RichDocuments.documentsMain.postAsset('\(metadata.fileNameView)', '\(url!)')"
-                self.webView.evaluateJavaScript(functionJS, completionHandler: { (result, error) in })
+                self.evaluateJavaScript(functionJS, completionHandler: { (result, error) in })
             } else if errorCode != 0 {
                 self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
             } else {
@@ -152,6 +147,7 @@ class NCViewerRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandl
         })
     }
     
+    
     //MARK: -
 
     public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
@@ -173,34 +169,4 @@ class NCViewerRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandl
     public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
         NCUtility.sharedInstance.stopActivityIndicator()
     }
-     
-    //MARK: -
-    
-    @objc func isRichDocument(_ metadata: tableMetadata) -> Bool {
-        
-        if appDelegate.reachability.isReachable() == false {
-            return false
-        }
-        
-        guard let mimeType = CCUtility.getMimeType(metadata.fileNameView) else {
-            return false
-        }
-        guard let richdocumentsMimetypes = NCManageDatabase.sharedInstance.getRichdocumentsMimetypes(account: metadata.account) else {
-            return false
-        }
-        
-        if richdocumentsMimetypes.count > 0 && mimeType.components(separatedBy: ".").count > 2 {
-            
-            let mimeTypeArray = mimeType.components(separatedBy: ".")
-            let mimeType = mimeTypeArray[mimeTypeArray.count - 2] + "." + mimeTypeArray[mimeTypeArray.count - 1]
-            
-            for richdocumentMimetype: String in richdocumentsMimetypes {
-                if richdocumentMimetype.contains(mimeType) {
-                    return true
-                }
-            }
-        }
-        
-        return false
-    }
 }