mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-05-02 14:16:59 +02:00
Move location sharing screens in the appropriate folder.
This commit is contained in:
+89
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// 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 StaticLocationViewingCoordinatorParameters {
|
||||
let session: MXSession
|
||||
let mediaManager: MXMediaManager
|
||||
let avatarData: AvatarInputProtocol
|
||||
let location: CLLocationCoordinate2D
|
||||
let coordinateType: LocationSharingCoordinateType
|
||||
}
|
||||
|
||||
final class StaticLocationViewingCoordinator: Coordinator, Presentable {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let parameters: StaticLocationViewingCoordinatorParameters
|
||||
private let staticLocationViewingHostingController: UIViewController
|
||||
private var staticLocationViewingViewModel: StaticLocationViewingViewModelProtocol
|
||||
|
||||
private let shareLocationActivityControllerBuilder = ShareLocationActivityControllerBuilder()
|
||||
|
||||
// MARK: Public
|
||||
|
||||
// Must be used only internally
|
||||
var childCoordinators: [Coordinator] = []
|
||||
var completion: (() -> Void)?
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
init(parameters: StaticLocationViewingCoordinatorParameters) {
|
||||
self.parameters = parameters
|
||||
|
||||
let viewModel = StaticLocationViewingViewModel(
|
||||
mapStyleURL: parameters.session.vc_homeserverConfiguration().tileServer.mapStyleURL,
|
||||
avatarData: parameters.avatarData,
|
||||
location: parameters.location,
|
||||
coordinateType: parameters.coordinateType)
|
||||
let view = StaticLocationView(viewModel: viewModel.context)
|
||||
.addDependency(AvatarService.instantiate(mediaManager: parameters.mediaManager))
|
||||
staticLocationViewingViewModel = viewModel
|
||||
staticLocationViewingHostingController = VectorHostingController(rootView: view)
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
func start() {
|
||||
MXLog.debug("[StaticLocationSharingViewerCoordinator] did start.")
|
||||
staticLocationViewingViewModel.completion = { [weak self] result in
|
||||
guard let self = self else { return }
|
||||
MXLog.debug("[StaticLocationSharingViewerCoordinator] StaticLocationSharingViewerViewModel did complete with result: \(result).")
|
||||
switch result {
|
||||
case .close:
|
||||
self.completion?()
|
||||
case .share(let coordinate):
|
||||
self.presentLocationActivityController(with: coordinate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func toPresentable() -> UIViewController {
|
||||
return self.staticLocationViewingHostingController
|
||||
}
|
||||
|
||||
func presentLocationActivityController(with coordinate: CLLocationCoordinate2D) {
|
||||
|
||||
let shareActivityController = shareLocationActivityControllerBuilder.build(with: coordinate)
|
||||
|
||||
self.staticLocationViewingHostingController.present(shareActivityController, animated: true)
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// 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.
|
||||
enum MockStaticLocationViewingScreenState: 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 {
|
||||
StaticLocationView.self
|
||||
}
|
||||
|
||||
/// A list of screen state definitions
|
||||
static var allCases: [MockStaticLocationViewingScreenState] {
|
||||
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 = StaticLocationViewingViewModel(mapStyleURL: mapStyleURL,
|
||||
avatarData: AvatarInput(mxContentUri: "", matrixItemId: "alice:matrix.org", displayName: "Alice"),
|
||||
location: location,
|
||||
coordinateType: coordinateType)
|
||||
|
||||
return ([viewModel],
|
||||
AnyView(StaticLocationView(viewModel: viewModel.context)
|
||||
.addDependency(MockAvatarService.example))
|
||||
)
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// 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 StaticLocationViewingViewAction {
|
||||
case close
|
||||
case share
|
||||
}
|
||||
|
||||
enum StaticLocationViewingViewModelResult {
|
||||
case close
|
||||
case share(_ coordinate: CLLocationCoordinate2D)
|
||||
}
|
||||
|
||||
// MARK: View
|
||||
|
||||
struct StaticLocationViewingViewState: 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 = StaticLocationViewingViewBindings()
|
||||
}
|
||||
|
||||
struct StaticLocationViewingViewBindings {
|
||||
var alertInfo: AlertInfo<LocationSharingAlertType>?
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
typealias StaticLocationViewingViewModelType = StateStoreViewModel<StaticLocationViewingViewState,
|
||||
Never,
|
||||
StaticLocationViewingViewAction>
|
||||
class StaticLocationViewingViewModel: StaticLocationViewingViewModelType, StaticLocationViewingViewModelProtocol {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private var mapViewErrorAlertInfoBuilder: MapViewErrorAlertInfoBuilder
|
||||
|
||||
// MARK: Public
|
||||
|
||||
var completion: ((StaticLocationViewingViewModelResult) -> 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 = StaticLocationViewingViewState(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: StaticLocationViewingViewAction) {
|
||||
switch viewAction {
|
||||
case .close:
|
||||
completion?(.close)
|
||||
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?(.close)
|
||||
}
|
||||
default:
|
||||
self?.completion?(.close)
|
||||
}
|
||||
}
|
||||
|
||||
state.bindings.alertInfo = alertInfo
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// 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 StaticLocationViewingViewModelProtocol {
|
||||
|
||||
var completion: ((StaticLocationViewingViewModelResult) -> Void)? { get set }
|
||||
var context: StaticLocationViewingViewModelType.Context { get }
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
class StaticLocationViewingUITests: MockScreenTestCase {
|
||||
// This test has issues running consistently on CI. Removed for now until the issue has been fixed.
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// 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 Combine
|
||||
import CoreLocation
|
||||
|
||||
@testable import RiotSwiftUI
|
||||
|
||||
class StaticLocationViewingViewModelTests: XCTestCase {
|
||||
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
|
||||
func testInitialState() {
|
||||
let viewModel = buildViewModel()
|
||||
|
||||
XCTAssertTrue(viewModel.context.viewState.shareButtonEnabled)
|
||||
XCTAssertFalse(viewModel.context.viewState.showLoadingIndicator)
|
||||
|
||||
XCTAssertNotNil(viewModel.context.viewState.mapStyleURL)
|
||||
XCTAssertNotNil(viewModel.context.viewState.userAvatarData)
|
||||
|
||||
XCTAssertNil(viewModel.context.viewState.bindings.alertInfo)
|
||||
}
|
||||
|
||||
func testCancellation() {
|
||||
let viewModel = buildViewModel()
|
||||
|
||||
let expectation = self.expectation(description: "Cancellation completion should be invoked")
|
||||
|
||||
viewModel.completion = { result in
|
||||
switch result {
|
||||
case .share:
|
||||
XCTFail()
|
||||
case .close:
|
||||
expectation.fulfill()
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.context.send(viewAction: .close)
|
||||
|
||||
waitForExpectations(timeout: 3)
|
||||
}
|
||||
|
||||
func testShareExistingLocation() {
|
||||
let viewModel = buildViewModel()
|
||||
|
||||
let expectation = self.expectation(description: "Share completion should be invoked")
|
||||
|
||||
viewModel.completion = { result in
|
||||
switch result {
|
||||
case .share(let coordinate):
|
||||
XCTAssertEqual(coordinate.latitude, viewModel.context.viewState.sharedAnnotation.coordinate.latitude)
|
||||
XCTAssertEqual(coordinate.longitude, viewModel.context.viewState.sharedAnnotation.coordinate.longitude)
|
||||
expectation.fulfill()
|
||||
case .close:
|
||||
XCTFail()
|
||||
}
|
||||
}
|
||||
|
||||
XCTAssertNotNil(viewModel.context.viewState.sharedAnnotation)
|
||||
|
||||
viewModel.context.send(viewAction: .share)
|
||||
|
||||
XCTAssertNil(viewModel.context.viewState.bindings.alertInfo)
|
||||
|
||||
waitForExpectations(timeout: 3)
|
||||
}
|
||||
|
||||
private func buildViewModel() -> StaticLocationViewingViewModel {
|
||||
StaticLocationViewingViewModel(mapStyleURL: URL(string: "http://empty.com")!,
|
||||
avatarData: AvatarInput(mxContentUri: "", matrixItemId: "", displayName: ""),
|
||||
location: CLLocationCoordinate2D(latitude: 51.4932641, longitude: -0.257096),
|
||||
coordinateType: .user)
|
||||
}
|
||||
}
|
||||
+96
@@ -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
|
||||
|
||||
struct StaticLocationView: View {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
@Environment(\.theme) private var theme
|
||||
|
||||
// MARK: Public
|
||||
|
||||
@ObservedObject var viewModel: StaticLocationViewingViewModel.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()
|
||||
}
|
||||
.ignoresSafeArea(.all, edges: [.bottom])
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarLeading) {
|
||||
Button(VectorL10n.cancel, action: {
|
||||
viewModel.send(viewAction: .close)
|
||||
})
|
||||
}
|
||||
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)
|
||||
}
|
||||
.disabled(!viewModel.viewState.shareButtonEnabled)
|
||||
.accessibilityIdentifier("shareButton")
|
||||
}
|
||||
}
|
||||
.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
|
||||
|
||||
struct StaticLocationSharingViewer_Previews: PreviewProvider {
|
||||
static let stateRenderer = MockStaticLocationViewingScreenState.stateRenderer
|
||||
static var previews: some View {
|
||||
stateRenderer.screenGroup()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user