Skip to content

Commit

Permalink
ASR.NotifyResult directive를 수신하면 그 이후의 다른 dialogRequestId를 수신하기 전까지 f…
Browse files Browse the repository at this point in the history
…ocus 유지하도록 변경 (#1157)

### Description
- ASR.NotifyResult와 다음 directive 사이에 BackgroundFocusHolder가 focus를 가지고
있도록 변경
  • Loading branch information
jayce1116 authored Apr 30, 2024
1 parent cfcb6b1 commit 659975d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions NuguClientKit/Sources/Business/BackgroundFocusHolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ class BackgroundFocusHolder {
"MediaPlayer.PlaySuspended"
]

private let pendingTargets = [
"ASR.NotifyResult"
]

private var handlingEvents = Set<String>()
private var handlingSoundDirectives = Set<String>()
private var handlingPendingDirectives = Set<String>()
private var dialogState: DialogState = .idle

// Observers
private let notificationCenter = NotificationCenter.default
private var eventWillSendObserver: Any?
private var eventDidSendObserver: Any?
private var dialogStateObserver: Any?
private var directiveReceiveObserver: Any?
private var directivePrefetchObserver: Any?
private var directiveCompleteObserver: Any?

Expand Down Expand Up @@ -82,6 +88,10 @@ class BackgroundFocusHolder {
if let directiveCompleteObserver = directiveCompleteObserver {
notificationCenter.removeObserver(directiveCompleteObserver)
}

if let directiveReceiveObserver = directiveReceiveObserver {
notificationCenter.removeObserver(directiveReceiveObserver)
}
}
}

Expand All @@ -107,6 +117,7 @@ private extension BackgroundFocusHolder {
func tryReleaseFocus() {
guard handlingEvents.isEmpty,
handlingSoundDirectives.isEmpty,
handlingPendingDirectives.isEmpty,
dialogState == .idle else { return }

focusManager.releaseFocus(channelDelegate: self)
Expand Down Expand Up @@ -137,6 +148,22 @@ private extension BackgroundFocusHolder {
}
}
}

directiveReceiveObserver = object.observe(NuguCoreNotification.StreamDataRoute.ReceivedDirective.self, queue: nil) { [weak self] notification in
self?.queue.async { [weak self] in
guard let self else { return }
let dialogRequestId = notification.directive.header.dialogRequestId
if pendingTargets.contains(notification.directive.header.type) {
handlingPendingDirectives.insert(dialogRequestId)
requestFocus()
} else if handlingPendingDirectives.contains(dialogRequestId) {
// PendingTarget과 동일한 dialogRequestId를 수신할 경우 focus를 유지
} else {
handlingPendingDirectives.removeAll()
tryReleaseFocus()
}
}
}
}

func addDialogStateObserver(_ object: DialogStateAggregator) {
Expand Down

0 comments on commit 659975d

Please sign in to comment.