Skip to content

Commit

Permalink
Add a toggle in the developer options to optimise the media uploads. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave authored Oct 14, 2024
1 parent b1b2297 commit 98a5ee5
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 34 deletions.
5 changes: 4 additions & 1 deletion ElementX/Sources/Application/AppSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ final class AppSettings {

// Feature flags
case slidingSyncDiscovery
case optimizeMediaUploads
case publicSearchEnabled
case fuzzyRoomListSearchEnabled
case pinningEnabled
case enableOnlySignedDeviceIsolationMode
case identityPinningViolationNotificationsEnabled
case knockingEnabled
Expand Down Expand Up @@ -281,6 +281,9 @@ final class AppSettings {
@UserPreference(key: UserDefaultsKeys.slidingSyncDiscovery, defaultValue: .native, storageType: .userDefaults(store))
var slidingSyncDiscovery: SlidingSyncDiscovery

@UserPreference(key: UserDefaultsKeys.optimizeMediaUploads, defaultValue: false, storageType: .userDefaults(store))
var optimizeMediaUploads

@UserPreference(key: UserDefaultsKeys.identityPinningViolationNotificationsEnabled, defaultValue: isDevelopmentBuild, storageType: .userDefaults(store))
var identityPinningViolationNotificationsEnabled

Expand Down
3 changes: 2 additions & 1 deletion ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {

let roomDetailsEditParameters = RoomDetailsEditScreenCoordinatorParameters(roomProxy: roomProxy,
mediaProvider: userSession.mediaProvider,
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: appSettings),
navigationStackCoordinator: stackCoordinator,
userIndicatorController: userIndicatorController,
orientationManager: appMediator.windowManager)
Expand Down Expand Up @@ -895,7 +896,7 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {

let parameters = MediaUploadPreviewScreenCoordinatorParameters(userIndicatorController: userIndicatorController,
roomProxy: roomProxy,
mediaUploadingPreprocessor: MediaUploadingPreprocessor(),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: appSettings),
title: url.lastPathComponent,
url: url)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class SettingsFlowCoordinator: FlowCoordinatorProtocol {
let coordinator = UserDetailsEditScreenCoordinator(parameters: .init(orientationManager: parameters.windowManager,
clientProxy: parameters.userSession.clientProxy,
mediaProvider: parameters.userSession.mediaProvider,
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: parameters.appSettings),
navigationStackCoordinator: navigationStackCoordinator,
userIndicatorController: parameters.userIndicatorController))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ class UserSessionFlowCoordinator: FlowCoordinatorProtocol {
userSession: userSession,
userIndicatorController: ServiceLocator.shared.userIndicatorController,
navigationStackCoordinator: startChatNavigationStackCoordinator,
userDiscoveryService: userDiscoveryService)
userDiscoveryService: userDiscoveryService,
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: appSettings))

let coordinator = StartChatScreenCoordinator(parameters: parameters)
coordinator.actions.sink { [weak self] action in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private class PreviewItem: NSObject, QLPreviewItem {
struct MediaUploadPreviewScreen_Previews: PreviewProvider, TestablePreview {
static let viewModel = MediaUploadPreviewScreenViewModel(userIndicatorController: UserIndicatorControllerMock.default,
roomProxy: JoinedRoomProxyMock(),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings),
title: "some random file name",
url: URL.picturesDirectory)
static var previews: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUI
struct RoomDetailsEditScreenCoordinatorParameters {
let roomProxy: JoinedRoomProxyProtocol
let mediaProvider: MediaProviderProtocol
let mediaUploadingPreprocessor: MediaUploadingPreprocessor
weak var navigationStackCoordinator: NavigationStackCoordinator?
let userIndicatorController: UserIndicatorControllerProtocol
let orientationManager: OrientationManagerProtocol
Expand All @@ -35,6 +36,7 @@ final class RoomDetailsEditScreenCoordinator: CoordinatorProtocol {

viewModel = RoomDetailsEditScreenViewModel(roomProxy: parameters.roomProxy,
mediaProvider: parameters.mediaProvider,
mediaUploadingPreprocessor: parameters.mediaUploadingPreprocessor,
userIndicatorController: parameters.userIndicatorController)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ class RoomDetailsEditScreenViewModel: RoomDetailsEditScreenViewModelType, RoomDe
private let actionsSubject: PassthroughSubject<RoomDetailsEditScreenViewModelAction, Never> = .init()
private let roomProxy: JoinedRoomProxyProtocol
private let userIndicatorController: UserIndicatorControllerProtocol
private let mediaPreprocessor: MediaUploadingPreprocessor = .init()
private let mediaUploadingPreprocessor: MediaUploadingPreprocessor

var actions: AnyPublisher<RoomDetailsEditScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}

init(roomProxy: JoinedRoomProxyProtocol,
mediaProvider: MediaProviderProtocol,
mediaUploadingPreprocessor: MediaUploadingPreprocessor,
userIndicatorController: UserIndicatorControllerProtocol) {
self.roomProxy = roomProxy
self.mediaUploadingPreprocessor = mediaUploadingPreprocessor
self.userIndicatorController = userIndicatorController

let roomAvatar = roomProxy.avatarURL
Expand Down Expand Up @@ -76,7 +78,7 @@ class RoomDetailsEditScreenViewModel: RoomDetailsEditScreenViewModelType, RoomDe
title: L10n.commonLoading,
persistent: true))

let mediaResult = await mediaPreprocessor.processMedia(at: url)
let mediaResult = await mediaUploadingPreprocessor.processMedia(at: url)

switch mediaResult {
case .success(.image):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ struct RoomDetailsEditScreen_Previews: PreviewProvider, TestablePreview {

return RoomDetailsEditScreenViewModel(roomProxy: roomProxy,
mediaProvider: MediaProviderMock(configuration: .init()),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings),
userIndicatorController: UserIndicatorControllerMock.default)
}()

Expand All @@ -164,6 +165,7 @@ struct RoomDetailsEditScreen_Previews: PreviewProvider, TestablePreview {

return RoomDetailsEditScreenViewModel(roomProxy: roomProxy,
mediaProvider: MediaProviderMock(configuration: .init()),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings),
userIndicatorController: UserIndicatorControllerMock.default)
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protocol DeveloperOptionsProtocol: AnyObject {
var hideUnreadMessagesBadge: Bool { get set }
var fuzzyRoomListSearchEnabled: Bool { get set }
var hideTimelineMedia: Bool { get set }
var optimizeMediaUploads: Bool { get set }
var enableOnlySignedDeviceIsolationMode: Bool { get set }
var elementCallBaseURLOverride: URL? { get set }
var identityPinningViolationNotificationsEnabled: Bool { get set }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ struct DeveloperOptionsScreen: View {
}
}

Section("Media") {
Toggle(isOn: $context.optimizeMediaUploads) {
Text("Optimise for upload")
}
}

Section {
Toggle(isOn: $context.enableOnlySignedDeviceIsolationMode) {
Text("Exclude insecure devices when sending/receiving messages")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct UserDetailsEditScreenCoordinatorParameters {
let orientationManager: OrientationManagerProtocol
let clientProxy: ClientProxyProtocol
let mediaProvider: MediaProviderProtocol
let mediaUploadingPreprocessor: MediaUploadingPreprocessor
weak var navigationStackCoordinator: NavigationStackCoordinator?
let userIndicatorController: UserIndicatorControllerProtocol
}
Expand All @@ -26,6 +27,7 @@ final class UserDetailsEditScreenCoordinator: CoordinatorProtocol {

viewModel = UserDetailsEditScreenViewModel(clientProxy: parameters.clientProxy,
mediaProvider: parameters.mediaProvider,
mediaUploadingPreprocessor: parameters.mediaUploadingPreprocessor,
userIndicatorController: parameters.userIndicatorController)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ class UserDetailsEditScreenViewModel: UserDetailsEditScreenViewModelType, UserDe
private let actionsSubject: PassthroughSubject<UserDetailsEditScreenViewModelAction, Never> = .init()
private let clientProxy: ClientProxyProtocol
private let userIndicatorController: UserIndicatorControllerProtocol
private let mediaPreprocessor: MediaUploadingPreprocessor = .init()
private let mediaUploadingPreprocessor: MediaUploadingPreprocessor

var actions: AnyPublisher<UserDetailsEditScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}

init(clientProxy: ClientProxyProtocol,
mediaProvider: MediaProviderProtocol,
mediaUploadingPreprocessor: MediaUploadingPreprocessor,
userIndicatorController: UserIndicatorControllerProtocol) {
self.clientProxy = clientProxy
self.mediaUploadingPreprocessor = mediaUploadingPreprocessor
self.userIndicatorController = userIndicatorController

super.init(initialViewState: UserDetailsEditScreenViewState(userID: clientProxy.userID,
Expand Down Expand Up @@ -88,7 +90,7 @@ class UserDetailsEditScreenViewModel: UserDetailsEditScreenViewModelType, UserDe
title: L10n.commonLoading,
persistent: true))

let mediaResult = await mediaPreprocessor.processMedia(at: url)
let mediaResult = await mediaUploadingPreprocessor.processMedia(at: url)

switch mediaResult {
case .success(.image):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct UserDetailsEditScreen: View {
struct UserDetailsEditScreen_Previews: PreviewProvider, TestablePreview {
static let viewModel = UserDetailsEditScreenViewModel(clientProxy: ClientProxyMock(.init(userID: "@stefan:matrix.org")),
mediaProvider: MediaProviderMock(configuration: .init()),
mediaUploadingPreprocessor: .init(appSettings: ServiceLocator.shared.settings),
userIndicatorController: UserIndicatorControllerMock.default)

static var previews: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct StartChatScreenCoordinatorParameters {
let userIndicatorController: UserIndicatorControllerProtocol
weak var navigationStackCoordinator: NavigationStackCoordinator?
let userDiscoveryService: UserDiscoveryServiceProtocol
let mediaUploadingPreprocessor: MediaUploadingPreprocessor
}

enum StartChatScreenCoordinatorAction {
Expand Down Expand Up @@ -134,7 +135,6 @@ final class StartChatScreenCoordinator: CoordinatorProtocol {

// MARK: - Private

let mediaUploadingPreprocessor = MediaUploadingPreprocessor()
private func displayMediaPickerWithSource(_ source: MediaPickerScreenSource) {
let stackCoordinator = NavigationStackCoordinator()

Expand All @@ -159,7 +159,7 @@ final class StartChatScreenCoordinator: CoordinatorProtocol {
Task { [weak self] in
guard let self else { return }
do {
let media = try await mediaUploadingPreprocessor.processMedia(at: url).get()
let media = try await parameters.mediaUploadingPreprocessor.processMedia(at: url).get()
var parameters = createRoomParameters.value
parameters.avatarImageMedia = media
createRoomParameters.send(parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ private struct VideoProcessingInfo {
}

struct MediaUploadingPreprocessor {
let appSettings: AppSettings

enum Constants {
static let maximumThumbnailSize = CGSize(width: 800, height: 600)
static let thumbnailCompressionQuality = 0.8
Expand Down Expand Up @@ -368,8 +370,9 @@ struct MediaUploadingPreprocessor {
/// - Returns: the URL for the resulting video and its media info as a `VideoProcessingResult`
private func convertVideoToMP4(_ url: URL, targetFileSize: UInt = 0) async -> Result<VideoProcessingInfo, MediaUploadingPreprocessorError> {
let asset = AVURLAsset(url: url)
let presetName = appSettings.optimizeMediaUploads ? AVAssetExportPreset640x480 : AVAssetExportPreset1920x1080

guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPreset1920x1080) else {
guard let exportSession = AVAssetExportSession(asset: asset, presetName: presetName) else {
return .failure(.failedConvertingVideo)
}

Expand Down
6 changes: 4 additions & 2 deletions ElementX/Sources/UITests/UITestsAppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,8 @@ class MockScreen: Identifiable {
userSession: userSession,
userIndicatorController: UserIndicatorControllerMock(),
navigationStackCoordinator: navigationStackCoordinator,
userDiscoveryService: userDiscoveryMock)
userDiscoveryService: userDiscoveryMock,
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings))
let coordinator = StartChatScreenCoordinator(parameters: parameters)
navigationStackCoordinator.setRootCoordinator(coordinator)
return navigationStackCoordinator
Expand All @@ -595,7 +596,8 @@ class MockScreen: Identifiable {
userSession: userSession,
userIndicatorController: UserIndicatorControllerMock(),
navigationStackCoordinator: navigationStackCoordinator,
userDiscoveryService: userDiscoveryMock))
userDiscoveryService: userDiscoveryMock,
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings)))
navigationStackCoordinator.setRootCoordinator(coordinator)
return navigationStackCoordinator
case .createRoom:
Expand Down
Loading

0 comments on commit 98a5ee5

Please sign in to comment.