@@ -11,38 +11,38 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
1111 public func handle( _ call: FlutterMethodCall , result: @escaping FlutterResult ) {
1212 switch call. method {
1313 case " open_file " :
14- let arguments = call. arguments as? Dictionary < String , Any >
14+ let arguments = call. arguments as? [ String : Any ]
1515 let filePath = arguments![ " file_path " ] as? String
16- if ( filePath== nil ) {
17- self . result ( result: result, message: " the file path cannot be null " , type: - 4 )
16+ if filePath == nil {
17+ self . result ( result: result, message: " the file path cannot be null " , type: - 4 )
1818 return
1919 }
2020 let fileExist = FileManager . default. fileExists ( atPath: filePath!)
21- if ( fileExist) {
21+ if fileExist {
2222 let documentURL = URL ( fileURLWithPath: filePath!)
2323 let fileType = documentURL. pathExtension
24- if ( ! canOpen( filePath: filePath!) ) {
25- self . openSelectPanel ( filePath: filePath!, fileType: fileType , result: result)
26- } else {
24+ if ! self . canOpen ( filePath: filePath!) {
25+ self . openSelectPanel ( filePath: filePath!, fileType: fileType, result: result)
26+ } else {
2727 self . open ( documentURL: documentURL, result: result)
2828 }
29- } else {
30- self . result ( result: result, message: " the file does not exist " , type: - 2 )
29+ } else {
30+ self . result ( result: result, message: " the file does not exist " , type: - 2 )
3131 }
3232
3333 default :
3434 result ( FlutterMethodNotImplemented)
3535 }
3636 }
3737
38- private func canOpen( filePath: String ) -> Bool {
38+ private func canOpen( filePath: String ) -> Bool {
3939 return FileManager . default. isReadableFile ( atPath: filePath)
4040 }
4141
42- private func convertDictionaryToString( dict: [ String : Any ] ) -> String {
43- var result : String = " "
42+ private func convertDictionaryToString( dict: [ String : Any ] ) -> String {
43+ var result = " "
4444 do {
45- let jsonData = try JSONSerialization . data ( withJSONObject: dict, options: JSONSerialization . WritingOptions. init ( rawValue: 0 ) )
45+ let jsonData = try JSONSerialization . data ( withJSONObject: dict, options: JSONSerialization . WritingOptions ( rawValue: 0 ) )
4646
4747 if let JSONString = String ( data: jsonData, encoding: String . Encoding. utf8) {
4848 result = JSONString
@@ -54,8 +54,8 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
5454 return result
5555 }
5656
57- private func openSelectPanel( filePath: String , fileType: String , result: @escaping FlutterResult ) {
58- let fileUrl = URL ( fileURLWithPath: filePath)
57+ private func openSelectPanel( filePath: String , fileType: String , result: @escaping FlutterResult ) {
58+ let fileUrl = URL ( fileURLWithPath: filePath)
5959 let openPanel = NSOpenPanel ( )
6060 openPanel. canChooseFiles = false
6161 openPanel. canChooseDirectories = true
@@ -65,42 +65,41 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
6565 openPanel. showsHiddenFiles = true
6666 openPanel. allowsOtherFileTypes = false
6767 let currentLanguage = Locale . current. languageCode
68- openPanel. prompt = ( currentLanguage == " zh " ) ? " 允许 " : " Accept "
69- openPanel. beginSheetModal ( for: NSApplication . shared. mainWindow!) { ( openResult) in
70- if ( openResult. rawValue == NSApplication . ModalResponse. OK. rawValue) {
68+ openPanel. prompt = ( currentLanguage == " zh " ) ? " 允许 " : " Accept "
69+ openPanel. beginSheetModal ( for: NSApplication . shared. mainWindow!) { openResult in
70+ if openResult. rawValue == NSApplication . ModalResponse. OK. rawValue {
7171 let selectedURL = openPanel. url!
72- var isReadable : Bool = false
72+ var isReadable = false
7373 if FileManager . default. fileExists ( atPath: selectedURL. path, isDirectory: nil ) && FileManager . default. isReadableFile ( atPath: selectedURL. path) {
7474 isReadable = true
7575 }
7676 if isReadable {
7777 self . open ( documentURL: fileUrl, result: result)
7878 } else {
79- self . result ( result: result, message: " Operation not permitted " , type: - 3 )
79+ self . result ( result: result, message: " Operation not permitted " , type: - 3 )
8080 }
81- } else {
82- self . result ( result: result, message: " Operation not permitted " , type: - 3 )
81+ } else {
82+ self . result ( result: result, message: " Operation not permitted " , type: - 3 )
8383 }
8484 }
8585 }
8686
87- private func open( documentURL: URL , result: FlutterResult ) {
87+ private func open( documentURL: URL , result: FlutterResult ) {
8888 NSWorkspace . shared. open ( [ documentURL] , withAppBundleIdentifier: nil , options: . default, additionalEventParamDescriptor: nil , launchIdentifiers: nil )
89- self . result ( result: result, message: " done " , type: 0 )
89+ self . result ( result: result, message: " done " , type: 0 )
9090 }
9191
92- private func result( result: FlutterResult , message: String , type: Int ) {
93- let map = [ " message " : message, " type " : type] as [ String : Any ]
94- result ( convertDictionaryToString ( dict: map) )
92+ private func result( result: FlutterResult , message: String , type: Int ) {
93+ let map = [ " message " : message, " type " : type] as [ String : Any ]
94+ result ( self . convertDictionaryToString ( dict: map) )
9595 }
9696
9797 @available ( * , deprecated, message: " This method is just test. " )
98- private func requestDiskPermission( onGranted: ( ) -> Void , onDenied: ( ) -> Void ) {
99-
98+ private func requestDiskPermission( onGranted: ( ) -> Void , onDenied: ( ) -> Void ) {
10099 if let url = URL ( string: " x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles " ) {
101100 if NSWorkspace . shared. open ( url) {
102101 print ( " Opened successfully " )
103- } else {
102+ } else {
104103 onDenied ( )
105104 return
106105 }
@@ -109,7 +108,7 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
109108 let options : NSDictionary = [ kAXTrustedCheckOptionPrompt. takeRetainedValue ( ) as NSString : true ]
110109 let status = AXIsProcessTrustedWithOptions ( options)
111110
112- if ( status != true ) {
111+ if status != true {
113112 print ( " Prompting for accessibility permissions " )
114113 let alert = NSAlert ( )
115114 alert. messageText = " This app requires disk permissions to function properly. Please grant access in the System Preferences app when prompted. "
@@ -126,10 +125,9 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
126125 task. waitUntilExit ( )
127126 onGranted ( )
128127 print ( " Full Disk Access granted successfully " )
129-
130128 }
131129
132- func checkFullDiskAccess( ) -> Bool {
130+ func checkFullDiskAccess( ) -> Bool {
133131 let fileManager = FileManager . default
134132 let testFilePath = " /Library/Application Support/com.apple.TCC/TCC.db "
135133
@@ -141,34 +139,34 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
141139 }
142140
143141 @available ( * , deprecated, message: " This method is no longer used. " )
144- private func getUTI( fileType: String ) -> String {
142+ private func getUTI( fileType: String ) -> String {
145143 var uti = " "
146- switch fileType. lowercased ( ) {
144+ switch fileType. lowercased ( ) {
147145 case " rtf " :
148146 uti = " public.rtf "
149147 case " txt " :
150148 uti = " public.plain-text "
151- case " html " , " htm " :
149+ case " html " , " htm " :
152150 uti = " public.html "
153151 case " xml " :
154152 uti = " public.xml "
155153 case " tar " :
156154 uti = " public.tar-archive "
157- case " gz " , " gzip " :
155+ case " gz " , " gzip " :
158156 uti = " org.gnu.gnu-zip-archive "
159157 case " tgz " :
160158 uti = " org.gnu.gnu-zip-tar-archive "
161- case " jpg " , " jpeg " :
159+ case " jpg " , " jpeg " :
162160 uti = " public.jpeg "
163161 case " png " :
164162 uti = " public.png "
165163 case " avi " :
166164 uti = " public.avi "
167- case " mpg " , " mpeg " :
165+ case " mpg " , " mpeg " :
168166 uti = " public.mpeg "
169167 case " mp4 " :
170168 uti = " public.mpeg-4 "
171- case " 3gpp " , " 3gp " :
169+ case " 3gpp " , " 3gp " :
172170 uti = " public.3gpp "
173171 case " mp3 " :
174172 uti = " public.mp3 "
0 commit comments