|
| 1 | +// |
| 2 | +// Copyright 2021 New Vector Ltd |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import SwiftUI |
| 18 | + |
| 19 | +struct LiveLocationSharingViewerCoordinatorParameters { |
| 20 | + let session: MXSession |
| 21 | +} |
| 22 | + |
| 23 | +final class LiveLocationSharingViewerCoordinator: Coordinator, Presentable { |
| 24 | + |
| 25 | + // MARK: - Properties |
| 26 | + |
| 27 | + // MARK: Private |
| 28 | + |
| 29 | + private let parameters: LiveLocationSharingViewerCoordinatorParameters |
| 30 | + private let liveLocationSharingViewerHostingController: UIViewController |
| 31 | + private var liveLocationSharingViewerViewModel: LiveLocationSharingViewerViewModelProtocol |
| 32 | + |
| 33 | + private let shareLocationActivityControllerBuilder = ShareLocationActivityControllerBuilder() |
| 34 | + |
| 35 | + // MARK: Public |
| 36 | + |
| 37 | + // Must be used only internally |
| 38 | + var childCoordinators: [Coordinator] = [] |
| 39 | + var completion: (() -> Void)? |
| 40 | + |
| 41 | + // MARK: - Setup |
| 42 | + |
| 43 | + @available(iOS 14.0, *) |
| 44 | + init(parameters: LiveLocationSharingViewerCoordinatorParameters) { |
| 45 | + self.parameters = parameters |
| 46 | + |
| 47 | + let service = LiveLocationSharingViewerService(session: parameters.session) |
| 48 | + |
| 49 | + let viewModel = LiveLocationSharingViewerViewModel(mapStyleURL: BuildSettings.tileServerMapStyleURL, service: service) |
| 50 | + let view = LiveLocationSharingViewer(viewModel: viewModel.context) |
| 51 | + .addDependency(AvatarService.instantiate(mediaManager: parameters.session.mediaManager)) |
| 52 | + liveLocationSharingViewerViewModel = viewModel |
| 53 | + liveLocationSharingViewerHostingController = VectorHostingController(rootView: view) |
| 54 | + } |
| 55 | + |
| 56 | + // MARK: - Public |
| 57 | + func start() { |
| 58 | + MXLog.debug("[LiveLocationSharingViewerCoordinator] did start.") |
| 59 | + liveLocationSharingViewerViewModel.completion = { [weak self] result in |
| 60 | + guard let self = self else { return } |
| 61 | + MXLog.debug("[LiveLocationSharingViewerCoordinator] LiveLocationSharingViewerViewModel did complete with result: \(result).") |
| 62 | + switch result { |
| 63 | + case .done: |
| 64 | + self.completion?() |
| 65 | + case .share(let coordinate): |
| 66 | + self.presentLocationActivityController(with: coordinate) |
| 67 | + case .stopLocationSharing: |
| 68 | + self.stopLocationSharing() |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + func toPresentable() -> UIViewController { |
| 74 | + return self.liveLocationSharingViewerHostingController |
| 75 | + } |
| 76 | + |
| 77 | + func presentLocationActivityController(with coordinate: CLLocationCoordinate2D) { |
| 78 | + |
| 79 | + let shareActivityController = shareLocationActivityControllerBuilder.build(with: coordinate) |
| 80 | + |
| 81 | + self.liveLocationSharingViewerHostingController.present(shareActivityController, animated: true) |
| 82 | + } |
| 83 | + |
| 84 | + func stopLocationSharing() { |
| 85 | + // TODO: Handle stop location sharing |
| 86 | + } |
| 87 | +} |
0 commit comments