From 45847ff5a4e95a85033b81b06354a3f16e6c0e53 Mon Sep 17 00:00:00 2001 From: saeedbashir Date: Wed, 12 Jul 2023 10:50:08 +0500 Subject: [PATCH] feat: add None option in the caption list to deselect caption language --- .../CutomePlayer/OEXVideoPlayerSettings.swift | 20 +++++++++++-------- Source/CutomePlayer/TranscriptManager.swift | 12 ++++++++++- Source/CutomePlayer/VideoPlayer.swift | 9 +++++++-- Source/ar.lproj/Localizable-2.strings | 2 ++ Source/de.lproj/Localizable-2.strings | 2 ++ Source/en.lproj/Localizable-2.strings | 2 ++ Source/es-419.lproj/Localizable-2.strings | 2 ++ Source/fr.lproj/Localizable-2.strings | 2 ++ Source/he.lproj/Localizable-2.strings | 2 ++ Source/ja.lproj/Localizable-2.strings | 2 ++ Source/pt-BR.lproj/Localizable-2.strings | 2 ++ Source/tr.lproj/Localizable-2.strings | 2 ++ Source/vi.lproj/Localizable-2.strings | 2 ++ Source/zh-Hans.lproj/Localizable-2.strings | 2 ++ 14 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Source/CutomePlayer/OEXVideoPlayerSettings.swift b/Source/CutomePlayer/OEXVideoPlayerSettings.swift index 81ecaef023..667ef38c73 100644 --- a/Source/CutomePlayer/OEXVideoPlayerSettings.swift +++ b/Source/CutomePlayer/OEXVideoPlayerSettings.swift @@ -9,6 +9,7 @@ import Foundation private let cellId = "CustomCell" +let captionLanguageNone = "none" public typealias RowType = (title: String, value: Any) public struct OEXVideoPlayerSetting { @@ -66,20 +67,23 @@ class VideoPlayerSettings : NSObject { rows.append(item) } } + + if !rows.isEmpty { + rows.append(RowType(title: Strings.none, value: captionLanguageNone)) + } let cc = OEXVideoPlayerSetting(title: Strings.videoSettingClosedCaptions, rows: rows, isSelected: { (row) -> Bool in var selected = false - if let selectedLanguage:String = OEXInterface.getCCSelectedLanguage() { - let lang = rows[row].value as! String - selected = selectedLanguage == lang + + var selectedLanguage = OEXInterface.getCCSelectedLanguage() ?? captionLanguageNone + if selectedLanguage.isEmpty { + selectedLanguage = captionLanguageNone } + let lang = rows[row].value as? String ?? captionLanguageNone + selected = selectedLanguage == lang return selected }) {[weak self] value in - var language : String = value as! String - if language == OEXInterface.getCCSelectedLanguage() && language != "" { - language = "" - } - self?.delegate?.setCaption(language: language) + self?.delegate?.setCaption(language: value as? String ?? "") } return [cc, speeds] } else { diff --git a/Source/CutomePlayer/TranscriptManager.swift b/Source/CutomePlayer/TranscriptManager.swift index 071120d5be..eb4f5f41c6 100644 --- a/Source/CutomePlayer/TranscriptManager.swift +++ b/Source/CutomePlayer/TranscriptManager.swift @@ -34,7 +34,17 @@ class TranscriptManager: NSObject { private var captionURL: String { var url: String = "" - if let ccSelectedLanguage = OEXInterface.getCCSelectedLanguage(), let transcriptURL = video.summary?.transcripts?[ccSelectedLanguage] as? String, !ccSelectedLanguage.isEmpty, !transcriptURL.isEmpty{ + let devicelangue = Locale.current.languageCode ?? "" + + if let ccSelectedLanguage = OEXInterface.getCCSelectedLanguage(), let transcriptURL = video.summary?.transcripts?[ccSelectedLanguage] as? String, !ccSelectedLanguage.isEmpty, !transcriptURL.isEmpty, ccSelectedLanguage != captionLanguageNone { + url = transcriptURL + } + else if let transcriptURL = video.summary?.transcripts?[devicelangue] as? String,!devicelangue.isEmpty, !transcriptURL.isEmpty { + // if no language is selected, give preference to device language + url = transcriptURL + } + else if let transcriptURL = video.summary?.transcripts?["en"] as? String, !transcriptURL.isEmpty { + // if no language is selected, and transcripts are not available for device langue, look for english url = transcriptURL } else if let transcriptURL = video.summary?.transcripts?.values.first as? String { diff --git a/Source/CutomePlayer/VideoPlayer.swift b/Source/CutomePlayer/VideoPlayer.swift index 87a3599ffd..0365ed2691 100644 --- a/Source/CutomePlayer/VideoPlayer.swift +++ b/Source/CutomePlayer/VideoPlayer.swift @@ -269,7 +269,7 @@ class VideoPlayer: UIViewController,VideoPlayerControlsDelegate,TranscriptManage transcriptManager = TranscriptManager(environment: environment, video: video) transcriptManager?.delegate = self - if let ccSelectedLanguage = OEXInterface.getCCSelectedLanguage(), let transcriptURL = video.summary?.transcripts?[ccSelectedLanguage] as? String, !ccSelectedLanguage.isEmpty, !transcriptURL.isEmpty { + if let ccSelectedLanguage = OEXInterface.getCCSelectedLanguage(), let transcriptURL = video.summary?.transcripts?[ccSelectedLanguage] as? String, !ccSelectedLanguage.isEmpty, !transcriptURL.isEmpty, ccSelectedLanguage != captionLanguageNone { controls?.activateSubTitles() } } @@ -660,9 +660,14 @@ class VideoPlayer: UIViewController,VideoPlayerControlsDelegate,TranscriptManage } func captionUpdate(playerControls: VideoPlayerControls, language: String) { + let alreadySelectedLanguage = OEXInterface.getCCSelectedLanguage() ?? "" OEXInterface.setCCSelectedLanguage(language) - if language.isEmpty { + + if alreadySelectedLanguage == language { return } + + if language == captionLanguageNone { playerControls.deAvtivateSubTitles() + transcriptManager?.loadTranscripts() } else { transcriptManager?.loadTranscripts() diff --git a/Source/ar.lproj/Localizable-2.strings b/Source/ar.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/ar.lproj/Localizable-2.strings +++ b/Source/ar.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/de.lproj/Localizable-2.strings b/Source/de.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/de.lproj/Localizable-2.strings +++ b/Source/de.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/en.lproj/Localizable-2.strings b/Source/en.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/en.lproj/Localizable-2.strings +++ b/Source/en.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/es-419.lproj/Localizable-2.strings b/Source/es-419.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/es-419.lproj/Localizable-2.strings +++ b/Source/es-419.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/fr.lproj/Localizable-2.strings b/Source/fr.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/fr.lproj/Localizable-2.strings +++ b/Source/fr.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/he.lproj/Localizable-2.strings b/Source/he.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/he.lproj/Localizable-2.strings +++ b/Source/he.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/ja.lproj/Localizable-2.strings b/Source/ja.lproj/Localizable-2.strings index fdaa6118e1..48721ee964 100644 --- a/Source/ja.lproj/Localizable-2.strings +++ b/Source/ja.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/pt-BR.lproj/Localizable-2.strings b/Source/pt-BR.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/pt-BR.lproj/Localizable-2.strings +++ b/Source/pt-BR.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/tr.lproj/Localizable-2.strings b/Source/tr.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/tr.lproj/Localizable-2.strings +++ b/Source/tr.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/vi.lproj/Localizable-2.strings b/Source/vi.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/vi.lproj/Localizable-2.strings +++ b/Source/vi.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None"; diff --git a/Source/zh-Hans.lproj/Localizable-2.strings b/Source/zh-Hans.lproj/Localizable-2.strings index 741cd84b8d..79379f405f 100644 --- a/Source/zh-Hans.lproj/Localizable-2.strings +++ b/Source/zh-Hans.lproj/Localizable-2.strings @@ -43,3 +43,5 @@ "MY_COURSES"="My courses"; /*Title My programs*/ "MY_PROGRAMS"="My programs"; +/* None option in the caption list to de select caption language*/ +"NONE"="None";