implement file picker in iOS with .ext to UTI filtering

This commit is contained in:
2019-09-09 15:31:47 +02:00
parent 4b3b1749d6
commit 096930b02e
5 changed files with 83 additions and 46 deletions

View File

@@ -12,6 +12,7 @@
#include "app.h"
#include "settings.h"
#import "objc_utils.h"
#import <MobileCoreServices/MobileCoreServices.h>
std::mutex render_mutex;
std::condition_variable render_cv;
@@ -25,6 +26,15 @@ std::condition_variable render_cv;
@implementation GameImagePicker
@end
@interface GameFilePicker : UIDocumentPickerViewController
{
@public std::promise<std::string> promise;
@public std::function<void(std::string)> callback;
}
@end
@implementation GameFilePicker
@end
//std::map<
bool pen_down = false;
int t_count = 0;
@@ -152,15 +162,41 @@ std::recursive_mutex lock_mutex;
GameImagePicker* p = static_cast<GameImagePicker*>(picker);
std::string path = [chosenImage cStringUsingEncoding:NSUTF8StringEncoding];
p->callback(path);
[picker dismissViewControllerAnimated:YES completion:^{
[self keyboardWasHidden:nil];
}];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:^{
[self keyboardWasHidden:nil];
}];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)pick_file:(NSArray<NSString*>*)types then:(std::function<void(std::string)>)callback
{
// convert extension into UTIs from the system database
NSMutableArray* UTIs = [NSMutableArray arrayWithCapacity:types.count];
for (NSString* ext : types)
{
NSString *typeForExt = (__bridge NSString*)
UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(__bridge CFStringRef)ext, NULL);
[UTIs addObject:typeForExt];
}
GameFilePicker *picker = [[GameFilePicker alloc] initWithDocumentTypes:UTIs inMode:UIDocumentPickerModeImport];
picker.delegate = self;
picker->callback = callback;
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
{
NSString *chosenImage = [url path];
GameFilePicker* p = static_cast<GameFilePicker*>(controller);
std::string path = [chosenImage cStringUsingEncoding:NSUTF8StringEncoding];
p->callback(path);
[controller dismissViewControllerAnimated:YES completion:NULL];
}
-(void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
{
[controller dismissViewControllerAnimated:YES completion:NULL];
}
- (void)insertText:(NSString *)text
@@ -185,7 +221,19 @@ std::recursive_mutex lock_mutex;
return YES;
}
- (BOOL)canBecomeFirstResponder {
return YES;
return input_enabled;
}
-(void)show_keyboard
{
input_enabled = YES;
[self becomeFirstResponder];
}
-(void)hide_keyboard
{
input_enabled = NO;
[self resignFirstResponder];
}
// Call this method somewhere in your view controller setup code.
@@ -416,6 +464,7 @@ std::set<UITouch*> ignored_touch;
- (void)viewDidLoad
{
[super viewDidLoad];
input_enabled = NO;
App::I = new App;
App::I->ios_view = self;
App::I->initLog();