aboutsummaryrefslogtreecommitdiff
path: root/Map/Presentation/Base Components/CollaborationButton.swift
blob: 49dd2c559d03a8d906d7a5f141fb3651a0d48dac (plain)
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
import SharedWithYou
import SwiftUI

struct CollaborationButton: NSViewRepresentable {
  let fileURL: URL

  func makeNSView(context: Context) -> SWCollaborationView {
    let itemProvider = NSItemProvider(contentsOf: fileURL)!
    let view = SWCollaborationView(itemProvider: itemProvider)

    view.activeParticipantCount = 0
    view.cloudSharingDelegate = context.coordinator

    return view
  }

  func updateNSView(_ nsView: SWCollaborationView, context: Context) {
    // Update participant count as needed
  }

  func makeCoordinator() -> Coordinator {
    Coordinator(fileURL: fileURL)
  }

  class Coordinator: NSObject, NSCloudSharingServiceDelegate {
    let fileURL: URL

    init(fileURL: URL) {
      self.fileURL = fileURL
    }

    func sharingService(
      _ sharingService: NSSharingService,
      didFailToShareItems items: [Any],
      error: Error
    ) {
      print("Failed to share: \(error)")
    }

    func sharingService(
      _ sharingService: NSSharingService,
      didCompleteForItems items: [Any]
    ) {
      print("Sharing completed")
    }

    func options(for: NSSharingService, share: NSItemProvider) -> NSSharingService.CloudKitOptions {
      return [.allowPublic, .allowPrivate, .allowReadWrite]
    }
  }
}