@@ -49,14 +49,14 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
4949
5050 let window = UIWindow ( frame: UIScreen . main. bounds)
5151
52- let browser = UIDocumentBrowserViewController ( forOpeningFilesWithContentTypes: [ " org.brians-brain.notebundle " ] )
52+ let browser = UIDocumentBrowserViewController ( forOpeningFilesWithContentTypes: [ " org.brians-brain.notebundle " , " org.brians-brain.notedb " ] )
5353 browser. delegate = self
5454
5555 window. rootViewController = browser
5656 window. makeKeyAndVisible ( )
5757 self . window = window
5858
59- if let openedDocumentBookmarkData = openedDocumentBookmark {
59+ if !isUITesting , let openedDocumentBookmarkData = openedDocumentBookmark {
6060 DDLogInfo ( " Bookmark data exists for an open document " )
6161 var isStale : Bool = false
6262 do {
@@ -78,7 +78,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
7878 makeMetadataProvider ( completion: { metadataProviderResult in
7979 switch metadataProviderResult {
8080 case . success( let metadataProvider) :
81- self . openDocument ( at: metadataProvider. container. appendingPathComponent ( " archive.notebundle " ) , from: viewController, animated: false )
81+ self . openDocument ( at: metadataProvider. container. appendingPathComponent ( " archive.notedb " ) , from: viewController, animated: false )
8282 case . failure( let error) :
8383 let messageText = " Error opening Notebook: \( error. localizedDescription) "
8484 let alertController = UIAlertController ( title: " Error " , message: messageText, preferredStyle: . alert)
@@ -143,8 +143,12 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
143143 }
144144 }
145145
146+ private lazy var isUITesting : Bool = {
147+ CommandLine . arguments. contains ( " --uitesting " )
148+ } ( )
149+
146150 private func makeMetadataProvider( completion: @escaping ( Result < FileMetadataProvider , Swift . Error > ) -> Void ) {
147- if CommandLine . arguments . contains ( " --uitesting " ) {
151+ if isUITesting {
148152 let container = FileManager . default. temporaryDirectory. appendingPathComponent ( " uitesting " )
149153 completion ( makeDirectoryProvider ( at: container, deleteExistingContents: true ) )
150154 return
@@ -202,10 +206,18 @@ extension AppDelegate: UIDocumentBrowserViewControllerDelegate {
202206 /// - parameter controller: The view controller from which to present the DocumentListViewController
203207 private func openDocument( at url: URL , from controller: UIDocumentBrowserViewController , animated: Bool ) {
204208 DDLogInfo ( " Opening document at \( url) " )
205- let noteArchiveDocument = NoteDocumentStorage (
206- fileURL: url,
207- parsingRules: ParsingRules . commonplace
208- )
209+ let noteArchiveDocument : NoteStorage
210+ if url. pathExtension == " notebundle " {
211+ noteArchiveDocument = NoteDocumentStorage (
212+ fileURL: url,
213+ parsingRules: ParsingRules . commonplace
214+ )
215+ } else if url. pathExtension == " notedb " {
216+ noteArchiveDocument = NoteSqliteStorage ( fileURL: url, parsingRules: ParsingRules . commonplace)
217+ } else {
218+ assertionFailure ( " Unknown note format: \( url. pathExtension) " )
219+ return
220+ }
209221 DDLogInfo ( " Using document at \( noteArchiveDocument. fileURL) " )
210222 let documentListViewController = DocumentListViewController ( notebook: noteArchiveDocument)
211223 documentListViewController. didTapFilesAction = { [ weak self] in
@@ -229,11 +241,11 @@ extension AppDelegate: UIDocumentBrowserViewControllerDelegate {
229241 noteIdentifierCopy. flatMap { documentListViewController. showPage ( with: $0) }
230242 let properties : [ String : String ] = [
231243 " Success " : success. description,
232- " documentState " : String ( describing: noteArchiveDocument. documentState) ,
233- " previousError " : noteArchiveDocument. previousError? . localizedDescription ?? " nil " ,
244+ // "documentState": String(describing: noteArchiveDocument.documentState),
245+ // "previousError": noteArchiveDocument.previousError?.localizedDescription ?? "nil",
234246 ]
235247 DDLogInfo ( " In open completion handler. \( properties) " )
236- if success {
248+ if success, ! self . isUITesting {
237249 self . openedDocumentBookmark = try ? url. bookmarkData ( )
238250 }
239251 } )
@@ -248,23 +260,17 @@ extension AppDelegate: UIDocumentBrowserViewControllerDelegate {
248260
249261 func documentBrowser( _ controller: UIDocumentBrowserViewController , didRequestDocumentCreationWithHandler importHandler: @escaping ( URL ? , UIDocumentBrowserViewController . ImportMode ) -> Void ) {
250262 let directoryURL = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) . last!
251- let url = directoryURL. appendingPathComponent ( UUID ( ) . uuidString) . appendingPathExtension ( " notebundle " )
252- let document = NoteDocumentStorage ( fileURL: url, parsingRules: ParsingRules . commonplace)
253- document. save ( to: url, for: . forCreating) { saveSuccess in
254- guard saveSuccess else {
255- DDLogError ( " Could not save document to \( url) : \( document. previousError? . localizedDescription ?? " nil " ) " )
256- importHandler ( nil , . none)
257- return
258- }
259- document. close { closeSuccess in
260- guard closeSuccess else {
261- DDLogError ( " Could not close document at \( url) : \( document. previousError? . localizedDescription ?? " nil " ) " )
262- importHandler ( nil , . none)
263- return
264- }
265- importHandler ( url, . copy)
266- }
263+ let url = directoryURL. appendingPathComponent ( " commonplace " ) . appendingPathExtension ( " notedb " )
264+ do {
265+ let document = NoteSqliteStorage ( fileURL: url, parsingRules: ParsingRules . commonplace)
266+ try document. open ( )
267+ try document. flush ( )
268+ } catch {
269+ DDLogError ( " Error creating new document: \( error) " )
270+ importHandler ( nil , . none)
271+ return
267272 }
273+ importHandler ( url, . copy)
268274 }
269275
270276 func documentBrowser( _ controller: UIDocumentBrowserViewController , didImportDocumentAt sourceURL: URL , toDestinationURL destinationURL: URL ) {
0 commit comments