FileProviderEnumerator.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. import FileProvider
  15. class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
  16. private let enumeratedItemIdentifier: NSFileProviderItemIdentifier
  17. private let anchor = NSFileProviderSyncAnchor("an anchor".data(using: .utf8)!)
  18. init(enumeratedItemIdentifier: NSFileProviderItemIdentifier) {
  19. self.enumeratedItemIdentifier = enumeratedItemIdentifier
  20. super.init()
  21. }
  22. func invalidate() {
  23. // TODO: perform invalidation of server connection if necessary
  24. }
  25. func enumerateItems(for observer: NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage) {
  26. /* TODO:
  27. - inspect the page to determine whether this is an initial or a follow-up request
  28. If this is an enumerator for a directory, the root container or all directories:
  29. - perform a server request to fetch directory contents
  30. If this is an enumerator for the active set:
  31. - perform a server request to update your local database
  32. - fetch the active set from your local database
  33. - inform the observer about the items returned by the server (possibly multiple times)
  34. - inform the observer that you are finished with this page
  35. */
  36. observer.didEnumerate([FileProviderItem(identifier: NSFileProviderItemIdentifier("a file"))])
  37. observer.finishEnumerating(upTo: nil)
  38. }
  39. func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) {
  40. /* TODO:
  41. - query the server for updates since the passed-in sync anchor
  42. If this is an enumerator for the active set:
  43. - note the changes in your local database
  44. - inform the observer about item deletions and updates (modifications + insertions)
  45. - inform the observer when you have finished enumerating up to a subsequent sync anchor
  46. */
  47. observer.finishEnumeratingChanges(upTo: anchor, moreComing: false)
  48. }
  49. func currentSyncAnchor(completionHandler: @escaping (NSFileProviderSyncAnchor?) -> Void) {
  50. completionHandler(anchor)
  51. }
  52. }