Skip to content

Commit

Permalink
Merge pull request #1 from estrategiahq/close_viewer
Browse files Browse the repository at this point in the history
Implements method to close document viewer
  • Loading branch information
joaopmarquesini authored Oct 11, 2022
2 parents dc904dc + 0678c72 commit 520d535
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ pubspec.lock

build/
/.idea

.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ public void onMethodCall(MethodCall call, @NonNull Result result) {
} else {
startActivity();
}
} else {
} else if (call.method.equals("close_viewer")) {
// Do nothing as android will always open on a native app
}else {
result.notImplemented();
isResultSubmitted = true;
}
Expand Down
1 change: 1 addition & 0 deletions example/ios/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Icon?
/Flutter/App.framework
/Flutter/Flutter.framework
/Flutter/Generated.xcconfig
/Flutter/flutter_export_environment.sh
/ServiceDefinitions.json

Pods/
Expand Down
29 changes: 23 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:open_file/open_filex.dart';
import 'package:open_filex/open_filex.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(MyApp());

Expand All @@ -13,13 +17,26 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
var _openResult = 'Unknown';

Future<String> download(String url, String filename) async {
HttpClient httpClient = new HttpClient();

var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
if (response.statusCode == 200) {
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await getApplicationDocumentsDirectory()).path;
File file;
file = File("$dir/$filename");
await file.writeAsBytes(bytes);
return file.absolute.path;
}
throw Exception("Error downloading file");
}

Future<void> openFile() async {
final filePath = '/storage/emulated/0/update.apk';
final result = await OpenFilex.open(filePath);
final filePath = await download('https://www.africau.edu/images/default/sample.pdf', 'sample.pdf');

setState(() {
_openResult = "type=${result.type} message=${result.message}";
});
OpenFilex.open(filePath);
}

@override
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
path_provider: ^2.0.11
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
67 changes: 3 additions & 64 deletions ios/Classes/OpenFilePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,76 +44,13 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSFileManager *fileManager=[NSFileManager defaultManager];
BOOL fileExist=[fileManager fileExistsAtPath:msg];
if(fileExist){
// NSURL *resourceToOpen = [NSURL fileURLWithPath:msg];
// NSString *exestr = [[msg pathExtension] lowercaseString];
_documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:msg]];
_documentController.delegate = self;
NSString *uti = call.arguments[@"uti"];
BOOL isBlank = [self isBlankString:uti];
if(!isBlank){
_documentController.UTI = uti;
}
// else{
// if([exestr isEqualToString:@"rtf"]){
// _documentController.UTI=@"public.rtf";
// }else if([exestr isEqualToString:@"txt"]){
// _documentController.UTI=@"public.plain-text";
// }else if([exestr isEqualToString:@"html"]||
// [exestr isEqualToString:@"htm"]){
// _documentController.UTI=@"public.html";
// }else if([exestr isEqualToString:@"xml"]){
// _documentController.UTI=@"public.xml";
// }else if([exestr isEqualToString:@"tar"]){
// _documentController.UTI=@"public.tar-archive";
// }else if([exestr isEqualToString:@"gz"]||
// [exestr isEqualToString:@"gzip"]){
// _documentController.UTI=@"org.gnu.gnu-zip-archive";
// }else if([exestr isEqualToString:@"tgz"]){
// _documentController.UTI=@"org.gnu.gnu-zip-tar-archive";
// }else if([exestr isEqualToString:@"jpg"]||
// [exestr isEqualToString:@"jpeg"]){
// _documentController.UTI=@"public.jpeg";
// }else if([exestr isEqualToString:@"png"]){
// _documentController.UTI=@"public.png";
// }else if([exestr isEqualToString:@"avi"]){
// _documentController.UTI=@"public.avi";
// }else if([exestr isEqualToString:@"mpg"]||
// [exestr isEqualToString:@"mpeg"]){
// _documentController.UTI=@"public.mpeg";
// }else if([exestr isEqualToString:@"mp4"]){
// _documentController.UTI=@"public.mpeg-4";
// }else if([exestr isEqualToString:@"3gpp"]||
// [exestr isEqualToString:@"3gp"]){
// _documentController.UTI=@"public.3gpp";
// }else if([exestr isEqualToString:@"mp3"]){
// _documentController.UTI=@"public.mp3";
// }else if([exestr isEqualToString:@"zip"]){
// _documentController.UTI=@"com.pkware.zip-archive";
// }else if([exestr isEqualToString:@"gif"]){
// _documentController.UTI=@"com.compuserve.gif";
// }else if([exestr isEqualToString:@"bmp"]){
// _documentController.UTI=@"com.microsoft.bmp";
// }else if([exestr isEqualToString:@"ico"]){
// _documentController.UTI=@"com.microsoft.ico";
// }else if([exestr isEqualToString:@"doc"]){
// _documentController.UTI=@"com.microsoft.word.doc";
// }else if([exestr isEqualToString:@"xls"]){
// _documentController.UTI=@"com.microsoft.excel.xls";
// }else if([exestr isEqualToString:@"ppt"]){
// _documentController.UTI=@"com.microsoft.powerpoint.​ppt";
// }else if([exestr isEqualToString:@"wav"]){
// _documentController.UTI=@"com.microsoft.waveform-​audio";
// }else if([exestr isEqualToString:@"wm"]){
// _documentController.UTI=@"com.microsoft.windows-​media-wm";
// }else if([exestr isEqualToString:@"wmv"]){
// _documentController.UTI=@"com.microsoft.windows-​media-wmv";
// }else if([exestr isEqualToString:@"pdf"]){
// _documentController.UTI=@"com.adobe.pdf";
// }else {
// NSLog(@"doc type not supported for preview");
// NSLog(@"%@", exestr);
// }
// }
@try {
BOOL previewSucceeded = [_documentController presentPreviewAnimated:YES];
if(!previewSucceeded){
Expand All @@ -129,9 +66,11 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSDictionary * dict = @{@"message":@"the file does not exist", @"type":@-2};
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString * json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

result(json);
}
}else if ([@"close_viewer" isEqualToString:call.method]) {
[_viewController dismissViewControllerAnimated:YES completion:nil];
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/platform/open_filex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ class OpenFilex {
final resultMap = json.decode(_result) as Map<String, dynamic>;
return OpenResult.fromJson(resultMap);
}

static Future<void> close() async {
await _channel.invokeMethod('close_viewer');
}
}
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: open_filex
description: A plug-in that can call native APP to open files with string result in flutter, support iOS(UTI) / android(intent) / PC(ffi) / web(dart:html)
version: 3.4.0
author: javaherisaber <m.javaherisaber@gmail.com>
version: 3.4.1
homepage: https://github.com/javaherisaber/open_file

dependencies:
Expand Down

0 comments on commit 520d535

Please sign in to comment.