ios/Mobile/DocumentBrowserViewController.mm    |   14 +-------
 ios/Mobile/TemplateCollectionViewController.h  |    2 +
 ios/Mobile/TemplateCollectionViewController.mm |   43 +++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 12 deletions(-)

New commits:
commit 296e0eb8034c62a4266bb595d01e6d3fb1043730
Author:     Tor Lillqvist <t...@collabora.com>
AuthorDate: Wed Dec 5 20:12:34 2018 +0200
Commit:     Tor Lillqvist <t...@collabora.com>
CommitDate: Wed Dec 5 22:02:59 2018 +0200

    More work on template selection for a new document
    
    It took quite some time for me to understand how to do it. Not sure if
    this is The Right Way, but at least it now works better.
    
    The trick was to store the importHandler block as a property of the
    TemplateCollectionViewController and call it when the right template
    has been selected.
    
    There is no need to call the importHandler already in the
    documentBrowser:didRequestDocumentCreationWithHandler: instance method
    and it would not be possible anyway as there apparently is no way to
    have the presentViewController:animated:completion: method work in a
    truly modal way, so that it would not return until the selection has
    been done.
    
    Change-Id: Ia229500c181844fcd99f1f099b2e6744c22b5266

diff --git a/ios/Mobile/DocumentBrowserViewController.mm 
b/ios/Mobile/DocumentBrowserViewController.mm
index 95b5bcd88..4e7c5c31b 100644
--- a/ios/Mobile/DocumentBrowserViewController.mm
+++ b/ios/Mobile/DocumentBrowserViewController.mm
@@ -37,19 +37,9 @@
 - (void)documentBrowser:(UIDocumentBrowserViewController *)controller 
didRequestDocumentCreationWithHandler:(void (^)(NSURL * _Nullable, 
UIDocumentBrowserImportMode))importHandler {
     UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" 
bundle:nil];
     TemplateCollectionViewController *templateCollectionViewController = 
[storyBoard 
instantiateViewControllerWithIdentifier:@"TemplateCollectionViewController"];
-    [self presentViewController:templateCollectionViewController animated:YES 
completion:nil];
-
-#if 0
-    NSURL *newDocumentURL = nil;
 
-    // Set the URL for the new document here. Optionally, you can present a 
template chooser before calling the importHandler.
-    // Make sure the importHandler is always called, even if the user cancels 
the creation request.
-    if (newDocumentURL != nil) {
-        importHandler(newDocumentURL, UIDocumentBrowserImportModeMove);
-    } else {
-        importHandler(newDocumentURL, UIDocumentBrowserImportModeNone);
-    }
-#endif
+    templateCollectionViewController.importHandler = importHandler;
+    [self presentViewController:templateCollectionViewController animated:YES 
completion:nil];
 }
 
 -(void)documentBrowser:(UIDocumentBrowserViewController *)controller 
didPickDocumentURLs:(NSArray<NSURL *> *)documentURLs {
diff --git a/ios/Mobile/TemplateCollectionViewController.h 
b/ios/Mobile/TemplateCollectionViewController.h
index 1e5ef3675..50861a599 100644
--- a/ios/Mobile/TemplateCollectionViewController.h
+++ b/ios/Mobile/TemplateCollectionViewController.h
@@ -14,6 +14,8 @@
     NSArray<NSURL*> *templates[3];
 }
 
+@property (copy) void (^ _Nullable importHandler)(NSURL * _Nullable, 
UIDocumentBrowserImportMode);
+
 @end
 
 // vim:set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/ios/Mobile/TemplateCollectionViewController.mm 
b/ios/Mobile/TemplateCollectionViewController.mm
index fcf6c1d8a..66224cf3e 100644
--- a/ios/Mobile/TemplateCollectionViewController.mm
+++ b/ios/Mobile/TemplateCollectionViewController.mm
@@ -6,9 +6,27 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+#import <LibreOfficeKit/LibreOfficeKit.h>
+#import <LibreOfficeKit/LibreOfficeKitInit.h>
+
 #import "TemplateCollectionViewController.h"
 #import "TemplateSectionHeaderView.h"
 
+static NSString *mapTemplateExtensionToActual(NSString *templateName)
+{
+    NSString *baseName = [templateName stringByDeletingPathExtension];
+    NSString *extension = [templateName substringFromIndex:baseName.length];
+
+    if ([extension isEqualToString:@".ott"])
+        return [baseName stringByAppendingString:@".odt"];
+    else if ([extension isEqualToString:@".ots"])
+        return [baseName stringByAppendingString:@".ods"];
+    else if ([extension isEqualToString:@".otp"])
+        return [baseName stringByAppendingString:@".odp"];
+    else
+        assert(false);
+}
+
 @implementation TemplateCollectionViewController
 
 -(void)viewDidLoad {
@@ -75,6 +93,31 @@
     return header;
 }
 
+- (BOOL)collectionView:(UICollectionView *)collectionView 
shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
+    NSURL *selectedTemplate = templates[[indexPath 
indexAtPosition:0]][[indexPath indexAtPosition:1]];
+
+    NSURL *cacheDirectory = [NSFileManager.defaultManager 
URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask][0];
+    NSURL *newURL = [cacheDirectory 
URLByAppendingPathComponent:mapTemplateExtensionToActual(selectedTemplate.lastPathComponent)
+                                                    isDirectory:NO];
+
+    // Load the template into LibreOffice core, save as the corresponding 
document type (with the
+    // same basename), and then proceed to edit that.
+    LibreOfficeKit *kit = lok_init_2(nullptr, nullptr);
+    kit->pClass->registerCallback(kit, [](int, const char *, void*){}, 
nullptr);
+    LibreOfficeKitDocument *doc = kit->pClass->documentLoad(kit, 
[[selectedTemplate absoluteString] UTF8String]);
+    doc->pClass->saveAs(doc, [[newURL absoluteString] UTF8String], nullptr, 
nullptr);
+    doc->pClass->destroy(doc);
+    kit->pClass->destroy(kit);
+
+    self.importHandler(newURL, UIDocumentBrowserImportModeMove);
+
+    [self dismissViewControllerAnimated:YES completion:^ {
+            NSLog(@"foo");
+        }];
+
+    return YES;
+}
+
 @end
 
 // vim:set shiftwidth=4 softtabstop=4 expandtab:
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to