Skip to content

Commit

Permalink
Fixes #2707 - Prevent the app from locking while recording a voice me…
Browse files Browse the repository at this point in the history
…ssage
  • Loading branch information
stefanceriu committed Apr 19, 2024
1 parent effe3b6 commit bc31103
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 29 deletions.
42 changes: 23 additions & 19 deletions ElementX/Sources/Application/AppMediator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,32 @@ import UIKit

class AppMediator: AppMediatorProtocol {
private let windowManager: WindowManagerProtocol

init(windowManager: WindowManagerProtocol) {
self.windowManager = windowManager
}

// UIApplication.State won't update if we store this e.g. in the constructor
private var application: UIApplication {
UIApplication.shared
}

@MainActor
var appState: UIApplication.State {
switch application.applicationState {
case .active:
windowManager.mainWindow.traitCollection.activeAppearance == .active ? .active : .inactive
case .inactive:
.inactive
case .background:
.background
default:
.inactive
}
}

init(windowManager: WindowManagerProtocol) {
self.windowManager = windowManager
var backgroundTimeRemaining: TimeInterval {
application.backgroundTimeRemaining
}

func beginBackgroundTask(withName taskName: String?, expirationHandler handler: (() -> Void)?) -> UIBackgroundTaskIdentifier {
Expand All @@ -47,22 +65,8 @@ class AppMediator: AppMediatorProtocol {

open(url)
}

var backgroundTimeRemaining: TimeInterval {
application.backgroundTimeRemaining
}

@MainActor
var appState: UIApplication.State {
switch application.applicationState {
case .active:
windowManager.mainWindow.traitCollection.activeAppearance == .active ? .active : .inactive
case .inactive:
.inactive
case .background:
.background
default:
.inactive
}

func setIdleTimerDisabled(_ disabled: Bool) {
application.isIdleTimerDisabled = disabled
}
}
10 changes: 6 additions & 4 deletions ElementX/Sources/Application/AppMediatorProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ import UIKit

// sourcery: AutoMockable
protocol AppMediatorProtocol {
var appState: UIApplication.State { get }

var backgroundTimeRemaining: TimeInterval { get }

func beginBackgroundTask(withName taskName: String?, expirationHandler handler: (() -> Void)?) -> UIBackgroundTaskIdentifier

func endBackgroundTask(_ identifier: UIBackgroundTaskIdentifier)

func open(_ url: URL)

func openAppSettings()

var backgroundTimeRemaining: TimeInterval { get }

var appState: UIApplication.State { get }

func setIdleTimerDisabled(_ disabled: Bool)
}

extension UIApplication.State: CustomStringConvertible {
Expand Down
49 changes: 44 additions & 5 deletions ElementX/Sources/Mocks/Generated/GeneratedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -748,16 +748,16 @@ class AppLockServiceMock: AppLockServiceProtocol {
}
}
class AppMediatorMock: AppMediatorProtocol {
var backgroundTimeRemaining: TimeInterval {
get { return underlyingBackgroundTimeRemaining }
set(value) { underlyingBackgroundTimeRemaining = value }
}
var underlyingBackgroundTimeRemaining: TimeInterval!
var appState: UIApplication.State {
get { return underlyingAppState }
set(value) { underlyingAppState = value }
}
var underlyingAppState: UIApplication.State!
var backgroundTimeRemaining: TimeInterval {
get { return underlyingBackgroundTimeRemaining }
set(value) { underlyingBackgroundTimeRemaining = value }
}
var underlyingBackgroundTimeRemaining: TimeInterval!

//MARK: - beginBackgroundTask

Expand Down Expand Up @@ -936,6 +936,45 @@ class AppMediatorMock: AppMediatorProtocol {
openAppSettingsCallsCount += 1
openAppSettingsClosure?()
}
//MARK: - setIdleTimerDisabled

var setIdleTimerDisabledUnderlyingCallsCount = 0
var setIdleTimerDisabledCallsCount: Int {
get {
if Thread.isMainThread {
return setIdleTimerDisabledUnderlyingCallsCount
} else {
var returnValue: Int? = nil
DispatchQueue.main.sync {
returnValue = setIdleTimerDisabledUnderlyingCallsCount
}

return returnValue!
}
}
set {
if Thread.isMainThread {
setIdleTimerDisabledUnderlyingCallsCount = newValue
} else {
DispatchQueue.main.sync {
setIdleTimerDisabledUnderlyingCallsCount = newValue
}
}
}
}
var setIdleTimerDisabledCalled: Bool {
return setIdleTimerDisabledCallsCount > 0
}
var setIdleTimerDisabledReceivedDisabled: Bool?
var setIdleTimerDisabledReceivedInvocations: [Bool] = []
var setIdleTimerDisabledClosure: ((Bool) -> Void)?

func setIdleTimerDisabled(_ disabled: Bool) {
setIdleTimerDisabledCallsCount += 1
setIdleTimerDisabledReceivedDisabled = disabled
setIdleTimerDisabledReceivedInvocations.append(disabled)
setIdleTimerDisabledClosure?(disabled)
}
}
class AudioConverterMock: AudioConverterProtocol {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ class RoomScreenInteractionHandler {
actionsSubject.eraseToAnyPublisher()
}

private var voiceMessageRecorderObserver: AnyCancellable?
private var voiceMessageRecorderObserver: AnyCancellable? {
didSet {
appMediator.setIdleTimerDisabled(voiceMessageRecorderObserver != nil)
}
}

private var canCurrentUserRedactOthers = false
private var canCurrentUserRedactSelf = false
private var resumeVoiceMessagePlaybackAfterScrubbing = false
Expand Down Expand Up @@ -405,6 +410,7 @@ class RoomScreenInteractionHandler {

func stopRecordingVoiceMessage() async {
await voiceMessageRecorder.stopRecording()
voiceMessageRecorderObserver = nil
}

func cancelRecordingVoiceMessage() async {
Expand Down
1 change: 1 addition & 0 deletions changelog.d/2707.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent the app from locking while recording a voice message

0 comments on commit bc31103

Please sign in to comment.