5982: Create new screen for static shared coordinate

This commit is contained in:
MaximeE
2022-04-08 17:42:08 +02:00
parent 16973d5521
commit 4df6a35c13
8 changed files with 479 additions and 0 deletions
@@ -0,0 +1,88 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UIKit
import SwiftUI
import MatrixSDK
struct StaticLocationSharingViewerCoordinatorParameters {
let roomDataSource: MXKRoomDataSource
let mediaManager: MXMediaManager
let avatarData: AvatarInputProtocol
let location: CLLocationCoordinate2D
let coordinateType: MXEventAssetType
}
final class StaticLocationSharingViewerCoordinator: Coordinator, Presentable {
// MARK: - Properties
// MARK: Private
private let parameters: StaticLocationSharingViewerCoordinatorParameters
private let staticLocationSharingViewerHostingController: UIViewController
private var staticLocationSharingViewerViewModel: StaticLocationSharingViewerViewModelProtocol
private let shareLocationActivityControllerBuilder = ShareLocationActivityControllerBuilder()
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
var completion: (() -> Void)?
// MARK: - Setup
@available(iOS 14.0, *)
init(parameters: StaticLocationSharingViewerCoordinatorParameters) {
self.parameters = parameters
let viewModel = StaticLocationSharingViewerViewModel(mapStyleURL: BuildSettings.tileServerMapStyleURL,
avatarData: parameters.avatarData,
location: parameters.location,
coordinateType: parameters.coordinateType.locationSharingCoordinateType())
let view = StaticLocationSharingViewer(viewModel: viewModel.context)
staticLocationSharingViewerViewModel = viewModel
staticLocationSharingViewerHostingController = VectorHostingController(rootView: view)
}
// MARK: - Public
func start() {
MXLog.debug("[StaticLocationSharingViewerCoordinator] did start.")
staticLocationSharingViewerViewModel.completion = { [weak self] result in
guard let self = self else { return }
MXLog.debug("[StaticLocationSharingViewerCoordinator] StaticLocationSharingViewerViewModel did complete with result: \(result).")
switch result {
case .cancel:
self.completion?()
case .share(let coordinate):
self.presentLocationActivityController(with: coordinate)
}
}
}
func toPresentable() -> UIViewController {
return self.staticLocationSharingViewerHostingController
}
func presentLocationActivityController(with coordinate: CLLocationCoordinate2D) {
let shareActivityController = shareLocationActivityControllerBuilder.build(with: coordinate)
self.staticLocationSharingViewerHostingController.present(shareActivityController, animated: true)
}
}
@@ -0,0 +1,57 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import SwiftUI
import CoreLocation
/// Using an enum for the screen allows you define the different state cases with
/// the relevant associated data for each case.
@available(iOS 14.0, *)
enum MockStaticLocationSharingViewerScreenState: MockScreenState, CaseIterable {
// A case for each state you want to represent
// with specific, minimal associated data that will allow you
// mock that screen.
case showUserLocation
case showPinLocation
/// The associated screen
var screenType: Any.Type {
StaticLocationSharingViewer.self
}
/// A list of screen state definitions
static var allCases: [MockStaticLocationSharingViewerScreenState] {
return [.showUserLocation, .showPinLocation]
}
/// Generate the view struct for the screen state.
var screenView: ([Any], AnyView) {
let location = CLLocationCoordinate2D(latitude: 51.4932641, longitude: -0.257096)
let coordinateType: LocationSharingCoordinateType = self == .showUserLocation ? .user : .pin
let mapStyleURL = URL(string: "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx")!
let viewModel = StaticLocationSharingViewerViewModel(mapStyleURL: mapStyleURL,
avatarData: AvatarInput(mxContentUri: "", matrixItemId: "alice:matrix.org", displayName: "Alice"),
location: location,
coordinateType: coordinateType)
return ([viewModel],
AnyView(StaticLocationSharingViewer(viewModel: viewModel.context)
.addDependency(MockAvatarService.example))
)
}
}
@@ -0,0 +1,60 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import Combine
import CoreLocation
// MARK: View model
enum StaticLocationSharingViewerViewAction {
case cancel
case share
}
enum StaticLocationSharingViewerViewModelResult {
case cancel
case share(_ coordinate: CLLocationCoordinate2D)
}
// MARK: View
@available(iOS 14, *)
struct StaticLocationSharingViewerViewState: BindableState {
/// Map style URL
let mapStyleURL: URL
/// Current user avatarData
let userAvatarData: AvatarInputProtocol
/// Shared annotation to display existing location
let sharedAnnotation: LocationAnnotation
var showLoadingIndicator: Bool = false
var shareButtonEnabled: Bool {
!showLoadingIndicator
}
let errorSubject = PassthroughSubject<LocationSharingViewError, Never>()
var bindings = StaticLocationSharingViewerViewBindings()
}
struct StaticLocationSharingViewerViewBindings {
var alertInfo: AlertInfo<LocationSharingAlertType>?
}
@@ -0,0 +1,96 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
import CoreLocation
@available(iOS 14, *)
typealias StaticLocationSharingViewerViewModelType = StateStoreViewModel<StaticLocationSharingViewerViewState,
Never,
StaticLocationSharingViewerViewAction>
@available(iOS 14, *)
class StaticLocationSharingViewerViewModel: StaticLocationSharingViewerViewModelType, StaticLocationSharingViewerViewModelProtocol {
// MARK: - Properties
// MARK: Private
private var mapViewErrorAlertInfoBuilder: MapViewErrorAlertInfoBuilder
// MARK: Public
var completion: ((StaticLocationSharingViewerViewModelResult) -> Void)?
// MARK: - Setup
init(mapStyleURL: URL, avatarData: AvatarInputProtocol, location: CLLocationCoordinate2D, coordinateType: LocationSharingCoordinateType) {
let sharedAnnotation: LocationAnnotation
switch coordinateType {
case .user:
sharedAnnotation = UserLocationAnnotation(avatarData: avatarData, coordinate: location)
case .pin:
sharedAnnotation = PinLocationAnnotation(coordinate: location)
}
let viewState = StaticLocationSharingViewerViewState(mapStyleURL: mapStyleURL,
userAvatarData: avatarData,
sharedAnnotation: sharedAnnotation)
mapViewErrorAlertInfoBuilder = MapViewErrorAlertInfoBuilder()
super.init(initialViewState: viewState)
state.errorSubject.sink { [weak self] error in
guard let self = self else { return }
self.processError(error)
}.store(in: &cancellables)
}
// MARK: - Public
override func process(viewAction: StaticLocationSharingViewerViewAction) {
switch viewAction {
case .cancel:
completion?(.cancel)
case .share:
completion?(.share(state.sharedAnnotation.coordinate))
}
}
// MARK: - Private
private func processError(_ error: LocationSharingViewError) {
guard state.bindings.alertInfo == nil else {
return
}
let alertInfo = mapViewErrorAlertInfoBuilder.build(with: error) { [weak self] in
switch error {
case .invalidLocationAuthorization:
if let applicationSettingsURL = URL(string:UIApplication.openSettingsURLString) {
UIApplication.shared.open(applicationSettingsURL)
} else {
self?.completion?(.cancel)
}
default:
self?.completion?(.cancel)
}
}
state.bindings.alertInfo = alertInfo
}
}
@@ -0,0 +1,24 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
protocol StaticLocationSharingViewerViewModelProtocol {
var completion: ((StaticLocationSharingViewerViewModelResult) -> Void)? { get set }
@available(iOS 14, *)
var context: StaticLocationSharingViewerViewModelType.Context { get }
}
@@ -0,0 +1,26 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import XCTest
import RiotSwiftUI
@available(iOS 14.0, *)
class StaticLocationSharingViewerUITests: MockScreenTest {
override class var screenType: MockScreenState.Type {
return MockStaticLocationSharingViewerScreenState.self
}
}
@@ -0,0 +1,31 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import XCTest
@testable import RiotSwiftUI
@available(iOS 14.0, *)
class StaticLocationSharingViewerViewModelTests: XCTestCase {
var viewModel: StaticLocationSharingViewerViewModelProtocol!
var context: StaticLocationSharingViewerViewModelType.Context!
override func setUpWithError() throws {
viewModel = StaticLocationSharingViewerViewModel(promptType: .regular, initialCount: Constants.counterInitialValue)
context = viewModel.context
}
}
@@ -0,0 +1,97 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
@available(iOS 14.0, *)
struct StaticLocationSharingViewer: View {
// MARK: - Properties
// MARK: Private
@Environment(\.theme) private var theme
// MARK: Public
@ObservedObject var viewModel: StaticLocationSharingViewerViewModel.Context
// MARK: Views
var body: some View {
NavigationView {
ZStack(alignment: .bottom) {
LocationSharingMapView(tileServerMapURL: viewModel.viewState.mapStyleURL,
annotations: [viewModel.viewState.sharedAnnotation],
highlightedAnnotation: viewModel.viewState.sharedAnnotation,
userAvatarData: viewModel.viewState.userAvatarData,
showsUserLocation: false,
userLocation: Binding.constant(nil),
mapCenterCoordinate: Binding.constant(nil),
errorSubject: viewModel.viewState.errorSubject)
MapCreditsView()
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(VectorL10n.cancel, action: {
viewModel.send(viewAction: .cancel)
})
}
ToolbarItem(placement: .principal) {
Text(VectorL10n.locationSharingTitle)
.font(.headline)
.foregroundColor(theme.colors.primaryContent)
}
ToolbarItem(placement: .navigationBarTrailing) {
Button {
viewModel.send(viewAction: .share)
} label: {
Image(uiImage: Asset.Images.locationShareIcon.image)
.accessibilityIdentifier("LocationSharingView.shareButton")
}
.disabled(!viewModel.viewState.shareButtonEnabled)
}
}
.navigationBarTitleDisplayMode(.inline)
.introspectNavigationController { navigationController in
ThemeService.shared().theme.applyStyle(onNavigationBar: navigationController.navigationBar)
}
.alert(item: $viewModel.alertInfo) { info in
info.alert
}
}
.accentColor(theme.colors.accent)
.activityIndicator(show: viewModel.viewState.showLoadingIndicator)
.navigationViewStyle(StackNavigationViewStyle())
}
@ViewBuilder
private var activityIndicator: some View {
if viewModel.viewState.showLoadingIndicator {
ActivityIndicator()
}
}
}
// MARK: - Previews
@available(iOS 14.0, *)
struct StaticLocationSharingViewer_Previews: PreviewProvider {
static let stateRenderer = MockStaticLocationSharingViewerScreenState.stateRenderer
static var previews: some View {
stateRenderer.screenGroup()
}
}