1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
//
// This file is part of the LibreOffice project.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// 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 UIKit
// DocumentController is the main viewer in the app, it displays the selected
// documents and holds a top entry to view the properties as well as a normal
// menu to handle global actions
// It is a delegate class to receive Menu events as well as file handling events
class DocumentController: UIViewController, MenuDelegate, UIDocumentBrowserViewControllerDelegate
{
var document: DocumentHolder? = nil
var documentView: DocumentTiledView? = nil
var documentOverlaysView: DocumentOverlaysView? = nil
// *** Handling of DocumentController
// this is normal functions every controller must implement
// holds known document types
var KnownDocumentTypes : [String] = []
var zeroInsets: UIEdgeInsets = .zero
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var mask: UIView!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var buttonScrollView: ButtonScrollView!
deinit
{
NotificationCenter.default.removeObserver(self)
}
// called once controller is loaded
override func viewDidLoad()
{
super.viewDidLoad()
// loading known document types, so we can use them for the open call
let path = Bundle.main.path(forResource: "Info", ofType: "plist")
let plist = NSDictionary(contentsOfFile: path!)
for dict in (plist!.object(forKey: "UTExportedTypeDeclarations") as! [NSDictionary]) +
(plist!.object(forKey: "UTImportedTypeDeclarations") as! [NSDictionary]) {
let x = ((dict["UTTypeTagSpecification"] as! NSDictionary)["public.filename-extension"] as! NSArray)
KnownDocumentTypes.append( x[0] as! String )
}
LOKitThread.instance.progressDelegate = self
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
registerKeyboardNotifications()
}
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
// Always load the 'welcome' file, as per the android app
let res = Bundle.main.url(forResource: "welcome", withExtension: "odt")
// uncomment for test data in resources until the doc picker works properly
//let res = Bundle.main.url(forResource: "testdata/2", withExtension: "xlsx")
if let exampleDoc = res
{
self.doOpen(exampleDoc)
}
}
// called when there is a memory constraint
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// not used in this App
}
@IBAction func searchIconTapped(_ sender: Any)
{
searchBar.isHidden = !searchBar.isHidden
if (!searchBar.isHidden)
{
searchBar.becomeFirstResponder()
}
}
// *** Handling of Background (hipernate)
// iOS is not true multitasking, only 1 app can be active (foreground) at any time,
// therefore apps frequently are moved to the background.
// background really means hipernate by terminating all threads and solely keep the
// data
// Moving to hipernate
public func Hipernate() -> Void
{
//BridgeLOkit_Hipernate() // FIXME
}
// Moving back to foreground
public func LeaveHipernate() -> Void
{
//BridgeLOkit_LeaveHipernate() // FIXME
}
// *** handling of PropertiesController
// The PropertiesController is a left sidebar, that will scroll in when activated
// The Controller handles manipulation of properties in the document
// Activate/Deactivate PropertiesController (from navigationController, see storyboard)
@IBAction func doProperties(_ sender: UIBarButtonItem)
{
// Check if deactivation
if (sender.tag == 99) {
// Deactivate
// Mark it as deactivated (it stays loaded)
sender.tag = 0;
// get handle of PropertiesController
let viewMenuBack : UIView = view.subviews.last!
// Blend out sidebar
UIView.animate(withDuration: 0.3, animations: { () -> Void in
var frameMenu : CGRect = viewMenuBack.frame
frameMenu.origin.x = -1 * UIScreen.main.bounds.size.width
viewMenuBack.frame = frameMenu
viewMenuBack.layoutIfNeeded()
viewMenuBack.backgroundColor = UIColor.clear
}, completion: { (finished) -> Void in
viewMenuBack.removeFromSuperview()
})
}
else {
// Activate
// Mark as activated
sender.isEnabled = false
sender.tag = 99
// make instance of PropertiesController
let prop : PropertiesController = self.storyboard!.instantiateViewController(
withIdentifier: "PropertiesController") as! PropertiesController
view.addSubview(prop.view)
addChildViewController(prop)
prop.view.layoutIfNeeded()
prop.view.frame=CGRect(x: 0 - UIScreen.main.bounds.size.width,
y: 0,
width: UIScreen.main.bounds.size.width,
height: UIScreen.main.bounds.size.height);
// Blend in sidebar
UIView.animate(withDuration: 0.3, animations: { () -> Void in
prop.view.frame=CGRect(x: 0,
y: 0,
width: UIScreen.main.bounds.size.width,
height: UIScreen.main.bounds.size.height);
sender.isEnabled = true
}, completion:nil)
}
}
// *** Handling of menu popover
// the menu contains all global functions and use seque/delegate
var currentDocumentName : String? = nil
// Last stop before displaying popover
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
// "showActions" is the name of the popover menu, see storyboard
if segue.identifier == "showActions" {
let vc = segue.destination as! DocumentActions
vc.delegate = self
vc.isDocActive = (currentDocumentName != nil)
}
}
// Delegate call from menu (see protocol MenuDelegate)
func actionMenuSelected(_ tag : Int)
{
// a tag can sadly enough only be a number and not a string,
// whenever adding a menu entry, it (of course) needs to be added
// to the Document actions scene in storyboard and assigned a tag number
// the tag number must be repeated in the following switch
// BE CAREFUL to keep the tags synchronized (manually)
switch tag
{
case 1: // Open...
startOpenDocument()
case 2: // Properties
showProperties()
case 3: // Save
doSave()
case 4: // Close...
doClose()
case 5: // Save as...
doSaveAs()
case 6: // Save as PDF...
doSaveAsPDF()
case 7: // Print...
startPrint()
default: // should not happen
print("unknown menu" + String(tag))
}
}
// *** handling of menu actions
// This is the real base of the application
var openMenu : UIDocumentBrowserViewController? = nil
// Load document into LibreOfficeKit and present it
internal func startOpenDocument()
{
openMenu = UIDocumentBrowserViewController()
openMenu?.allowsDocumentCreation = true
openMenu?.browserUserInterfaceStyle = UIDocumentBrowserViewController.BrowserUserInterfaceStyle.dark
openMenu?.delegate = self
self.present(openMenu!, animated: true, completion: nil)
}
// Show document properties (new overloaded page)
internal func showProperties()
{
//FIXME
print("menu Properties to be done")
}
// Save current document
internal func doSave()
{
//FIXME
print("menu Save to be done")
}
// Close current document (without saving)
internal func doClose()
{
//FIXME
print("menu Close to be done")
}
// make a copy of current document, and save
internal func doSaveAs()
{
//FIXME
print("menu Save as... to be done")
}
// save current document as PDF
internal func doSaveAsPDF()
{
//FIXME
print("menu Save as PDF... to be done")
}
// print current document
internal func startPrint()
{
//FIXME
print("menu Print... to be done")
}
// *** Handling of DocumentViewController delegate functions
// this handles open/create/copy/delete document
// Create an empty document, and present it
internal func documentBrowser(_ controller: UIDocumentBrowserViewController,
didRequestDocumentCreationWithHandler importHandler: @escaping (URL?,
UIDocumentBrowserViewController.ImportMode) -> Void)
{
//FIXME
}
// import (copy from iCloud to iPad) document, open it and present it
internal func documentBrowser(_ controller: UIDocumentBrowserViewController,
didImportDocumentAt sourceURL: URL,
toDestinationURL destinationURL: URL)
{
//FIXME
}
// Import failed, inform user
internal func documentBrowser(_ controller: UIDocumentBrowserViewController,
failedToImportDocumentAt documentURL: URL,
error: Error?)
{
//FIXME
}
// open document and present it
internal func documentBrowser(_ controller: UIDocumentBrowserViewController,
didPickDocumentURLs documentURLs: [URL])
{
openMenu?.dismiss(animated: true, completion: nil)
openMenu = nil
doOpen(documentURLs[0])
}
// *** Handling of document (open/print)
// Real open and presentation of document
public func doOpen(_ docURL : URL)
{
LOKitThread.instance.documentLoad(url: docURL.absoluteString)
{
doc, error in
if let document = doc
{
runOnMain
{
self.setDocument(doc: document)
}
}
else
{
// TODO - alert user of failure
}
}
/* FIXME
BridgeLOkit_Sizing(4, 4, 256, 256);
*/
}
/// Sets the document to use and set's up it's view. Should be called on the main thread
public func setDocument(doc: DocumentHolder)
{
if let _ = self.document
{
// TODO - cleanup
self.document = nil
}
if let exisitingView = self.documentView
{
exisitingView.removeFromSuperview()
self.documentView = nil // forces the close of the view and it's held documents before we setup the new one
}
// also remove current overlays and start fresh
documentOverlaysView?.removeFromSuperview()
// setup the new doc view
self.document = doc
// setup delegates
doc.searchDelegate = self
let frameToUse = self.scrollView.frame
let docView = DocumentTiledView(frame: frameToUse, document: doc, scale: 1.0)
self.scrollView.addSubview(docView)
self.scrollView.contentSize = docView.frame.size
self.documentView = docView
// overlay view
let overlay = DocumentOverlaysView(docTiledView: docView)
docView.addSubview(overlay)
self.documentOverlaysView = overlay
// button view - used for spreadsheet tabs
if doc.isSpeadsheet
{
buttonScrollView.isHidden = false
buttonScrollView.setButtonLabels(labels: doc.partNames)
buttonScrollView.buttonClickedCallback = {
[weak self] index in
self?.document?.async {
$0.setPart(nPart: Int32(index))
runOnMain {
self?.documentView?.setNeedsDisplay()
}
}
}
// make room for the scroll view
zeroInsets = UIEdgeInsets(top: 0, left: 0, bottom: buttonScrollView.height, right: 0)
}
else
{
zeroInsets = .zero
buttonScrollView.isHidden = true
}
scrollView.contentInset = zeroInsets
// debugging view borders
/*
self.scrollView.layer.borderColor = UIColor.red.cgColor
self.scrollView.layer.borderWidth = 1.0
docView.layer.borderColor = UIColor.green.cgColor
docView.layer.borderWidth = 1.0
*/
}
// MARK: - UIScrollViewDelegate
}
extension DocumentController: UIScrollViewDelegate
{
// return a view that will be scaled. if delegate returns nil, nothing happens
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return self.documentView
}
// called before the scroll view begins zooming its content
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?)
{
print("scrollViewWillBeginZooming currentScale=\(scrollView.zoomScale)")
}
// scale between minimum and maximum. called after any 'bounce' animations
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat)
{
print("scrollViewDidEndZooming scale=\(scale)")
self.documentView?.scrollViewDidEndZooming(scrollView, with: view, atScale: scale)
}
}
// MARK: - UIKeyInput
// public var hasText: Bool
// {
// true
// }
//
//
// public func insertText(_ text: String)
// {
//
// }
//
// public func deleteBackward()
// {
//
// }
extension DocumentController: ProgressDelegate
{
// MARK: - ProgressDelegate
func statusIndicatorStart()
{
self.mask?.isHidden = false
self.progressBar?.isHidden = false
self.progressBar?.progress = 0.0
}
func statusIndicatorFinish()
{
// what would be nice would be to be able to wait until the initial tiles have rendered...
self.mask?.isHidden = true
self.progressBar?.isHidden = true
}
func statusIndicatorSetValue(value: Double)
{
self.progressBar?.progress = Float(value) / 100.0
}
}
extension DocumentController: UISearchBarDelegate
{
// called when text changes (including clear)
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
}
// called when keyboard search button pressed
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
if let text = searchBar.text
{
if text.count > 0
{
document?.search(searchString: text, forwardDirection: true, from: CGPoint(x:0, y:0) )
}
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
searchBar.isHidden = true
}
}
extension DocumentController: SearchDelegate
{
func searchNotFound()
{
// TODO: tell user somehow
self.documentOverlaysView?.clearSearchResults()
}
func searchResultSelection(searchResults: SearchResults)
{
self.documentOverlaysView?.setSearchResults(searchResults: searchResults)
}
}
/// Keyboard notifications
extension DocumentController
{
func registerKeyboardNotifications()
{
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow(notification:)),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide(notification:)),
name: NSNotification.Name.UIKeyboardWillHide,
object: nil)
}
@objc func keyboardWillShow(notification: NSNotification)
{
let userInfo: NSDictionary = notification.userInfo! as NSDictionary
guard let keyboardInfo = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
print(userInfo)
let keyboardSize = keyboardInfo.cgRectValue.size
print("keyboardWillShow \(keyboardSize)")
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
}
@objc func keyboardWillHide(notification: NSNotification)
{
print("keyboardWillHide")
scrollView.contentInset = zeroInsets
scrollView.scrollIndicatorInsets = zeroInsets
}
}
|