vector-im/element-ios/issues/5298 - Various tweaks following code review.

This commit is contained in:
Stefan Ceriu
2022-01-04 14:12:24 +02:00
committed by Stefan Ceriu
parent 0bac754ad9
commit 7161b99d1c
26 changed files with 248 additions and 211 deletions
@@ -20,14 +20,13 @@ import SwiftUI
import Keys
struct LocationSharingCoordinatorParameters {
let navigationRouter: NavigationRouterType
let roomDataSource: MXKRoomDataSource
let mediaManager: MXMediaManager
let avatarData: AvatarInputProtocol
let location: CLLocationCoordinate2D?
}
final class LocationSharingCoordinator: Coordinator {
final class LocationSharingCoordinator: Coordinator, Presentable {
// MARK: - Properties
@@ -44,9 +43,10 @@ final class LocationSharingCoordinator: Coordinator {
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
var completion: (() -> Void)?
// MARK: - Setup
@available(iOS 14.0, *)
@@ -70,14 +70,12 @@ final class LocationSharingCoordinator: Coordinator {
return
}
parameters.navigationRouter.present(locationSharingHostingController, animated: true)
locationSharingViewModel.completion = { [weak self] result in
guard let self = self else { return }
switch result {
case .cancel:
self.parameters.navigationRouter.dismissModule(animated: true, completion: nil)
self.completion?()
case .share(let latitude, let longitude):
if let location = self.parameters.location {
self.showActivityControllerForLocation(location)
@@ -91,8 +89,8 @@ final class LocationSharingCoordinator: Coordinator {
description: nil) { [weak self] _ in
guard let self = self else { return }
self.parameters.navigationRouter.dismissModule(animated: true, completion: nil)
self.locationSharingViewModel.dispatch(action: .stopLoading(nil))
self.completion?()
} failure: { [weak self] error in
guard let self = self else { return }
@@ -104,85 +102,19 @@ final class LocationSharingCoordinator: Coordinator {
}
}
func showActivityControllerForLocation(_ location: CLLocationCoordinate2D) {
let vc = UIActivityViewController(activityItems: activityItems(location: location),
// MARK: - Presentable
func toPresentable() -> UIViewController {
return locationSharingHostingController
}
// MARK: - Private
private func showActivityControllerForLocation(_ location: CLLocationCoordinate2D) {
let vc = UIActivityViewController(activityItems: [ShareToMapsAppActivity.urlForMapsAppType(.apple, location: location)],
applicationActivities: [ShareToMapsAppActivity(type: .apple, location: location),
ShareToMapsAppActivity(type: .google, location: location)])
locationSharingHostingController.present(vc, animated: true)
}
func activityItems(location: CLLocationCoordinate2D) -> [Any] {
var items = [Any]()
// Make the share sheet show a pretty location thumbnail
if let url = NSURL(string: "https://maps.apple.com?ll=\(location.latitude),\(location.longitude)") {
items.append(url)
}
return items
}
}
extension UIActivity.ActivityType {
static let shareToMapsApp = UIActivity.ActivityType("Element.ShareToMapsApp")
}
class ShareToMapsAppActivity: UIActivity {
enum MapsAppType {
case apple
case google
}
let type: MapsAppType
let location: CLLocationCoordinate2D
private override init() {
fatalError()
}
init(type: MapsAppType, location: CLLocationCoordinate2D) {
self.type = type
self.location = location
}
override var activityTitle: String? {
switch type {
case .apple:
return VectorL10n.locationSharingOpenAppleMaps
case .google:
return VectorL10n.locationSharingOpenGoogleMaps
}
}
var activityCategory: UIActivity.Category {
return .action
}
override var activityType: UIActivity.ActivityType {
return .shareToMapsApp
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
return true
}
override func prepare(withActivityItems activityItems: [Any]) {
var url: URL?
switch type {
case .apple:
url = URL(string: "https://maps.apple.com?ll=\(location.latitude),\(location.longitude)&q=Pin")
case .google:
url = URL(string: "https://www.google.com/maps/search/?api=1&query=\(location.latitude),\(location.longitude)")
}
guard let url = url else {
activityDidFinish(false)
return
}
UIApplication.shared.open(url, options: [:]) { [weak self] result in
self?.activityDidFinish(result)
}
}
}
@@ -0,0 +1,78 @@
//
// 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
extension UIActivity.ActivityType {
static let shareToMapsApp = UIActivity.ActivityType("Element.ShareToMapsApp")
}
class ShareToMapsAppActivity: UIActivity {
enum MapsAppType {
case apple
case google
}
let type: MapsAppType
let location: CLLocationCoordinate2D
private override init() {
fatalError()
}
init(type: MapsAppType, location: CLLocationCoordinate2D) {
self.type = type
self.location = location
}
static func urlForMapsAppType(_ type: MapsAppType, location: CLLocationCoordinate2D) -> URL {
switch type {
case .apple:
return URL(string: "https://maps.apple.com?ll=\(location.latitude),\(location.longitude)&q=Pin")!
case .google:
return URL(string: "https://www.google.com/maps/search/?api=1&query=\(location.latitude),\(location.longitude)")!
}
}
override var activityTitle: String? {
switch type {
case .apple:
return VectorL10n.locationSharingOpenAppleMaps
case .google:
return VectorL10n.locationSharingOpenGoogleMaps
}
}
var activityCategory: UIActivity.Category {
return .action
}
override var activityType: UIActivity.ActivityType {
return .shareToMapsApp
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
return true
}
override func prepare(withActivityItems activityItems: [Any]) {
let url = Self.urlForMapsAppType(type, location: location)
UIApplication.shared.open(url, options: [:]) { [weak self] result in
self?.activityDidFinish(result)
}
}
}
@@ -73,17 +73,17 @@ class LocationSharingViewModel: LocationSharingViewModelType {
switch error {
case .failedLoadingMap:
state.bindings.alertInfo = ErrorAlertInfo(id: .mapLoadingError,
title: VectorL10n.locationSharingLoadingMapErrorTitle,
title: VectorL10n.locationSharingLoadingMapErrorTitle(AppInfo.current.displayName) ,
primaryButton: (VectorL10n.ok, { completion?(.cancel) }),
secondaryButton: nil)
case .failedLocatingUser:
state.bindings.alertInfo = ErrorAlertInfo(id: .userLocatingError,
title: VectorL10n.locationSharingLocatingUserErrorTitle,
title: VectorL10n.locationSharingLocatingUserErrorTitle(AppInfo.current.displayName),
primaryButton: (VectorL10n.ok, { completion?(.cancel) }),
secondaryButton: nil)
case .invalidLocationAuthorization:
state.bindings.alertInfo = ErrorAlertInfo(id: .authorizationError,
title: VectorL10n.locationSharingInvalidAuthorizationErrorTitle,
title: VectorL10n.locationSharingInvalidAuthorizationErrorTitle(AppInfo.current.displayName),
primaryButton: (VectorL10n.locationSharingInvalidAuthorizationNotNow, { completion?(.cancel) }),
secondaryButton: (VectorL10n.locationSharingInvalidAuthorizationSettings, {
if let applicationSettingsURL = URL(string:UIApplication.openSettingsURLString) {
@@ -101,7 +101,7 @@ class LocationSharingViewModel: LocationSharingViewModelType {
if error != nil {
state.bindings.alertInfo = ErrorAlertInfo(id: .locationSharingError,
title: VectorL10n.locationSharingInvalidAuthorizationErrorTitle,
title: VectorL10n.locationSharingInvalidAuthorizationErrorTitle(AppInfo.current.displayName),
primaryButton: (VectorL10n.ok, nil),
secondaryButton: nil)
}
@@ -21,11 +21,10 @@ import UIKit
import SwiftUI
struct PollEditFormCoordinatorParameters {
let navigationRouter: NavigationRouterType?
let room: MXRoom
}
final class PollEditFormCoordinator: Coordinator {
final class PollEditFormCoordinator: Coordinator, Presentable {
// MARK: - Properties
@@ -42,9 +41,10 @@ final class PollEditFormCoordinator: Coordinator {
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
var completion: (() -> Void)?
// MARK: - Setup
@available(iOS 14.0, *)
@@ -65,13 +65,11 @@ final class PollEditFormCoordinator: Coordinator {
return
}
parameters.navigationRouter?.present(pollEditFormHostingController, animated: true)
pollEditFormViewModel.completion = { [weak self] result in
guard let self = self else { return }
switch result {
case .cancel:
self.parameters.navigationRouter?.dismissModule(animated: true, completion: nil)
self.completion?()
case .create(let question, let answerOptions):
var options = [MXEventContentPollStartAnswerOption]()
for answerOption in answerOptions {
@@ -88,8 +86,8 @@ final class PollEditFormCoordinator: Coordinator {
self.parameters.room.sendPollStart(withContent: pollStartContent, localEcho: nil) { [weak self] result in
guard let self = self else { return }
self.parameters.navigationRouter?.dismissModule(animated: true, completion: nil)
self.pollEditFormViewModel.dispatch(action: .stopLoading(nil))
self.completion?()
} failure: { [weak self] error in
guard let self = self else { return }
@@ -99,4 +97,10 @@ final class PollEditFormCoordinator: Coordinator {
}
}
}
// MARK: - Private
func toPresentable() -> UIViewController {
return pollEditFormHostingController
}
}
@@ -27,7 +27,7 @@ struct PollTimelineCoordinatorParameters {
}
@available(iOS 14.0, *)
final class PollTimelineCoordinator: Coordinator, PollAggregatorDelegate {
final class PollTimelineCoordinator: Coordinator, Presentable, PollAggregatorDelegate {
// MARK: - Properties
@@ -26,7 +26,7 @@ protocol UserSuggestionCoordinatorDelegate: AnyObject {
}
@available(iOS 14.0, *)
final class UserSuggestionCoordinator: Coordinator {
final class UserSuggestionCoordinator: Coordinator, Presentable {
// MARK: - Properties