From d9f2da8efe8d7e99362d939747a70eb453b2e58e Mon Sep 17 00:00:00 2001 From: Hamza Israr <71447999+HamzaIsrar12@users.noreply.github.com> Date: Tue, 30 Jul 2024 11:54:18 +0500 Subject: [PATCH] fix: Minor Improvements for Videos, Webviews an Bottom Navigation Bar (#19) - Auto-play videos when ready - Update the Bottom Navigation Icon and Colors - Webview No Network Error --- app/src/main/java/org/openedx/app/di/ScreenModule.kt | 4 +++- app/src/main/res/menu/bottom_view_menu.xml | 2 +- core/src/edx/res/values/colors.xml | 4 ++-- .../main/java/org/openedx/core/extension/StringExt.kt | 9 --------- core/src/main/java/org/openedx/core/ui/ComposeCommon.kt | 2 +- .../course/presentation/unit/html/HtmlUnitFragment.kt | 3 +-- .../course/presentation/unit/video/VideoUnitFragment.kt | 2 +- .../course/presentation/unit/video/VideoUnitViewModel.kt | 2 +- .../discovery/presentation/WebViewDiscoveryFragment.kt | 6 ++++++ .../discovery/presentation/WebViewDiscoveryViewModel.kt | 4 ++++ .../discovery/presentation/catalog/CatalogWebView.kt | 5 +++-- .../discovery/presentation/info/CourseInfoFragment.kt | 6 ++++++ .../discovery/presentation/info/CourseInfoViewModel.kt | 4 ++++ .../discovery/presentation/program/ProgramFragment.kt | 5 ++++- .../discovery/presentation/program/ProgramViewModel.kt | 4 ++++ 15 files changed, 41 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/openedx/app/di/ScreenModule.kt b/app/src/main/java/org/openedx/app/di/ScreenModule.kt index b33a0a092..d0067f448 100644 --- a/app/src/main/java/org/openedx/app/di/ScreenModule.kt +++ b/app/src/main/java/org/openedx/app/di/ScreenModule.kt @@ -185,6 +185,7 @@ val screenModule = module { get(), get(), get(), + get(), ) } @@ -238,6 +239,7 @@ val screenModule = module { get(), get(), get(), + get(), ) } viewModel { (courseId: String) -> @@ -473,6 +475,6 @@ val screenModule = module { } viewModel { HtmlUnitViewModel(get(), get(), get(), get(), get()) } - viewModel { ProgramViewModel(get(), get(), get(), get(), get(), get(), get()) } + viewModel { ProgramViewModel(get(), get(), get(), get(), get(), get(), get(), get()) } } diff --git a/app/src/main/res/menu/bottom_view_menu.xml b/app/src/main/res/menu/bottom_view_menu.xml index f97e849f7..0558f5c3b 100644 --- a/app/src/main/res/menu/bottom_view_menu.xml +++ b/app/src/main/res/menu/bottom_view_menu.xml @@ -4,7 +4,7 @@ #00262B #FFFFFF - #00262B - #454545 + #002326 + #ADADAD diff --git a/core/src/main/java/org/openedx/core/extension/StringExt.kt b/core/src/main/java/org/openedx/core/extension/StringExt.kt index 45032c809..343398782 100644 --- a/core/src/main/java/org/openedx/core/extension/StringExt.kt +++ b/core/src/main/java/org/openedx/core/extension/StringExt.kt @@ -1,7 +1,6 @@ package org.openedx.core.extension import android.util.Patterns -import java.net.URL import java.util.Locale import java.util.regex.Pattern @@ -38,11 +37,3 @@ fun String.tagId(): String = this.replaceSpace("_").lowercase(Locale.getDefault( fun String.takeIfNotEmpty(): String? { return if (this.isEmpty().not()) this else null } - -fun String?.equalsHost(host: String?): Boolean { - return try { - host?.startsWith(URL(this).host, ignoreCase = true) == true - } catch (e: Exception) { - false - } -} diff --git a/core/src/main/java/org/openedx/core/ui/ComposeCommon.kt b/core/src/main/java/org/openedx/core/ui/ComposeCommon.kt index de391aeb3..11d00f8dd 100644 --- a/core/src/main/java/org/openedx/core/ui/ComposeCommon.kt +++ b/core/src/main/java/org/openedx/core/ui/ComposeCommon.kt @@ -1190,7 +1190,7 @@ fun FullScreenErrorView( modifier = Modifier .widthIn(Dp.Unspecified, 162.dp), text = stringResource(id = errorType.actionResId), - textColor = MaterialTheme.appColors.primaryButtonText, + textColor = MaterialTheme.appColors.secondaryButtonText, backgroundColor = MaterialTheme.appColors.secondaryButtonBackground, onClick = onReloadClick, ) diff --git a/course/src/main/java/org/openedx/course/presentation/unit/html/HtmlUnitFragment.kt b/course/src/main/java/org/openedx/course/presentation/unit/html/HtmlUnitFragment.kt index ad9b53b55..74d26520d 100644 --- a/course/src/main/java/org/openedx/course/presentation/unit/html/HtmlUnitFragment.kt +++ b/course/src/main/java/org/openedx/course/presentation/unit/html/HtmlUnitFragment.kt @@ -49,7 +49,6 @@ import androidx.fragment.app.Fragment import kotlinx.coroutines.launch import org.koin.androidx.viewmodel.ext.android.viewModel import org.openedx.core.extension.applyDarkModeIfEnabled -import org.openedx.core.extension.equalsHost import org.openedx.core.extension.isEmailValid import org.openedx.core.extension.loadUrl import org.openedx.core.system.AppCookieManager @@ -293,7 +292,7 @@ private fun HTMLContentView( request: WebResourceRequest, error: WebResourceError ) { - if (view.url.equalsHost(request.url.host)) { + if (request.url.toString() == view.url) { onWebPageLoadError() } super.onReceivedError(view, request, error) diff --git a/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitFragment.kt b/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitFragment.kt index 694151d30..8b28773fe 100644 --- a/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitFragment.kt +++ b/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitFragment.kt @@ -207,7 +207,7 @@ class VideoUnitFragment : Fragment(R.layout.fragment_video_unit) { mediaItem, viewModel.exoPlayer?.currentPosition ?: 0L ) - viewModel.castPlayer?.playWhenReady = false + viewModel.castPlayer?.playWhenReady = true showVideoControllerIndefinitely(true) } diff --git a/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitViewModel.kt b/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitViewModel.kt index 5779b96da..4300e4304 100644 --- a/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitViewModel.kt +++ b/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitViewModel.kt @@ -30,7 +30,7 @@ open class VideoUnitViewModel( var videoUrl = "" var transcripts = emptyMap() - var isPlaying = false + var isPlaying = true var transcriptLanguage = AppDataConstants.defaultLocale.language ?: "en" private set diff --git a/discovery/src/main/java/org/openedx/discovery/presentation/WebViewDiscoveryFragment.kt b/discovery/src/main/java/org/openedx/discovery/presentation/WebViewDiscoveryFragment.kt index 216b9a594..f78b77a5d 100644 --- a/discovery/src/main/java/org/openedx/discovery/presentation/WebViewDiscoveryFragment.kt +++ b/discovery/src/main/java/org/openedx/discovery/presentation/WebViewDiscoveryFragment.kt @@ -103,6 +103,7 @@ class WebViewDiscoveryFragment : Fragment() { isPreLogin = viewModel.isPreLogin, contentUrl = viewModel.discoveryUrl, uriScheme = viewModel.uriScheme, + userAgent = viewModel.appUserAgent, hasInternetConnection = hasInternetConnection, onWebViewUIAction = { action -> when (action) { @@ -199,6 +200,7 @@ private fun WebViewDiscoveryScreen( isPreLogin: Boolean, contentUrl: String, uriScheme: String, + userAgent: String, hasInternetConnection: Boolean, onWebViewUIAction: (WebViewUIAction) -> Unit, onWebPageUpdated: (String) -> Unit, @@ -278,6 +280,7 @@ private fun WebViewDiscoveryScreen( DiscoveryWebView( contentUrl = contentUrl, uriScheme = uriScheme, + userAgent = userAgent, isPreLogin = isPreLogin, onWebPageLoaded = { if ((uiState is WebViewUIState.Error).not()) { @@ -320,6 +323,7 @@ private fun WebViewDiscoveryScreen( private fun DiscoveryWebView( contentUrl: String, uriScheme: String, + userAgent: String, isPreLogin: Boolean, onWebPageLoaded: () -> Unit, onWebPageUpdated: (String) -> Unit, @@ -329,6 +333,7 @@ private fun DiscoveryWebView( val webView = CatalogWebViewScreen( url = contentUrl, uriScheme = uriScheme, + userAgent = userAgent, onWebPageLoaded = onWebPageLoaded, onWebPageUpdated = onWebPageUpdated, onUriClick = onUriClick, @@ -412,6 +417,7 @@ private fun WebViewDiscoveryScreenPreview() { isPreLogin = false, contentUrl = "https://www.example.com/", uriScheme = "", + userAgent = "", hasInternetConnection = false, onWebViewUIAction = {}, onWebPageUpdated = {}, diff --git a/discovery/src/main/java/org/openedx/discovery/presentation/WebViewDiscoveryViewModel.kt b/discovery/src/main/java/org/openedx/discovery/presentation/WebViewDiscoveryViewModel.kt index a41252ae7..e06e943fa 100644 --- a/discovery/src/main/java/org/openedx/discovery/presentation/WebViewDiscoveryViewModel.kt +++ b/discovery/src/main/java/org/openedx/discovery/presentation/WebViewDiscoveryViewModel.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.asStateFlow import org.openedx.core.BaseViewModel import org.openedx.core.config.Config import org.openedx.core.data.storage.CorePreferences +import org.openedx.core.presentation.global.AppData import org.openedx.core.presentation.global.ErrorType import org.openedx.core.presentation.global.webview.WebViewUIState import org.openedx.core.system.connection.NetworkConnection @@ -14,6 +15,7 @@ import org.openedx.core.utils.UrlUtils class WebViewDiscoveryViewModel( private val querySearch: String, + private val appData: AppData, private val config: Config, private val networkConnection: NetworkConnection, private val corePreferences: CorePreferences, @@ -29,6 +31,8 @@ class WebViewDiscoveryViewModel( val isPreLogin get() = config.isPreLoginExperienceEnabled() && corePreferences.user == null + val appUserAgent get() = appData.appUserAgent + private var _discoveryUrl = webViewConfig.baseUrl val discoveryUrl: String get() { diff --git a/discovery/src/main/java/org/openedx/discovery/presentation/catalog/CatalogWebView.kt b/discovery/src/main/java/org/openedx/discovery/presentation/catalog/CatalogWebView.kt index a32f6d271..d1b344c91 100644 --- a/discovery/src/main/java/org/openedx/discovery/presentation/catalog/CatalogWebView.kt +++ b/discovery/src/main/java/org/openedx/discovery/presentation/catalog/CatalogWebView.kt @@ -9,7 +9,6 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalContext import org.openedx.core.extension.applyDarkModeIfEnabled -import org.openedx.core.extension.equalsHost import org.openedx.discovery.presentation.catalog.WebViewLink.Authority as linkAuthority @SuppressLint("SetJavaScriptEnabled", "ComposableNaming") @@ -17,6 +16,7 @@ import org.openedx.discovery.presentation.catalog.WebViewLink.Authority as linkA fun CatalogWebViewScreen( url: String, uriScheme: String, + userAgent: String, isAllLinksExternal: Boolean = false, onWebPageLoaded: () -> Unit, refreshSessionCookie: () -> Unit = {}, @@ -90,7 +90,7 @@ fun CatalogWebViewScreen( request: WebResourceRequest, error: WebResourceError ) { - if (view.url.equalsHost(request.url.host)) { + if (request.url.toString() == view.url) { onWebPageLoadError() } super.onReceivedError(view, request, error) @@ -104,6 +104,7 @@ fun CatalogWebViewScreen( setSupportZoom(true) loadsImagesAutomatically = true domStorageEnabled = true + userAgentString = "$userAgentString $userAgent" } isVerticalScrollBarEnabled = false isHorizontalScrollBarEnabled = false diff --git a/discovery/src/main/java/org/openedx/discovery/presentation/info/CourseInfoFragment.kt b/discovery/src/main/java/org/openedx/discovery/presentation/info/CourseInfoFragment.kt index 4cfe97437..b336557e0 100644 --- a/discovery/src/main/java/org/openedx/discovery/presentation/info/CourseInfoFragment.kt +++ b/discovery/src/main/java/org/openedx/discovery/presentation/info/CourseInfoFragment.kt @@ -133,6 +133,7 @@ class CourseInfoFragment : Fragment() { webViewUIState = webViewState, uiMessage = uiMessage, uriScheme = viewModel.uriScheme, + userAgent = viewModel.appUserAgent, hasInternetConnection = hasInternetConnection, onWebViewUIAction = { action -> when (action) { @@ -248,6 +249,7 @@ private fun CourseInfoScreen( webViewUIState: WebViewUIState, uiMessage: UIMessage?, uriScheme: String, + userAgent: String, hasInternetConnection: Boolean, onWebViewUIAction: (WebViewUIAction) -> Unit, onRegisterClick: () -> Unit, @@ -322,6 +324,7 @@ private fun CourseInfoScreen( CourseInfoWebView( contentUrl = (uiState as CourseInfoUIState.CourseInfo).initialUrl, uriScheme = uriScheme, + userAgent = userAgent, isPreLogin = uiState.isPreLogin, onWebPageLoaded = { onWebViewUIAction(WebViewUIAction.WEB_PAGE_LOADED) }, onUriClick = onUriClick, @@ -359,6 +362,7 @@ private fun CourseInfoScreen( private fun CourseInfoWebView( contentUrl: String, uriScheme: String, + userAgent: String, isPreLogin: Boolean, onWebPageLoaded: () -> Unit, onUriClick: (String, linkAuthority) -> Unit, @@ -368,6 +372,7 @@ private fun CourseInfoWebView( val webView = CatalogWebViewScreen( url = contentUrl, uriScheme = uriScheme, + userAgent = userAgent, isAllLinksExternal = true, onWebPageLoaded = onWebPageLoaded, onUriClick = onUriClick, @@ -410,6 +415,7 @@ fun CourseInfoScreenPreview() { ), uiMessage = null, uriScheme = "", + userAgent = "", hasInternetConnection = false, onWebViewUIAction = {}, onRegisterClick = {}, diff --git a/discovery/src/main/java/org/openedx/discovery/presentation/info/CourseInfoViewModel.kt b/discovery/src/main/java/org/openedx/discovery/presentation/info/CourseInfoViewModel.kt index 83ed16a71..69dae200f 100644 --- a/discovery/src/main/java/org/openedx/discovery/presentation/info/CourseInfoViewModel.kt +++ b/discovery/src/main/java/org/openedx/discovery/presentation/info/CourseInfoViewModel.kt @@ -18,6 +18,7 @@ import org.openedx.core.config.Config import org.openedx.core.data.storage.CorePreferences import org.openedx.core.extension.isInternetError import org.openedx.core.presentation.CoreAnalyticsKey +import org.openedx.core.presentation.global.AppData import org.openedx.core.presentation.global.ErrorType import org.openedx.core.presentation.global.webview.WebViewUIState import org.openedx.core.system.ResourceManager @@ -37,6 +38,7 @@ import org.openedx.core.R as CoreR class CourseInfoViewModel( val pathId: String, val infoType: String, + private val appData: AppData, private val config: Config, private val networkConnection: NetworkConnection, private val router: DiscoveryRouter, @@ -73,6 +75,8 @@ class CourseInfoViewModel( val uriScheme: String get() = config.getUriScheme() + val appUserAgent get() = appData.appUserAgent + private val webViewConfig get() = config.getDiscoveryConfig().webViewConfig private fun getInitialUrl(): String { diff --git a/discovery/src/main/java/org/openedx/discovery/presentation/program/ProgramFragment.kt b/discovery/src/main/java/org/openedx/discovery/presentation/program/ProgramFragment.kt index f1e5d3881..925abae67 100644 --- a/discovery/src/main/java/org/openedx/discovery/presentation/program/ProgramFragment.kt +++ b/discovery/src/main/java/org/openedx/discovery/presentation/program/ProgramFragment.kt @@ -28,7 +28,6 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.ViewCompositionStrategy @@ -133,6 +132,7 @@ class ProgramFragment : Fragment() { ?.isNotEmpty() == true, isNestedFragment = isNestedFragment, uriScheme = viewModel.uriScheme, + userAgent = viewModel.appUserAgent, hasInternetConnection = hasInternetConnection, onWebViewUIAction = { action -> when (action) { @@ -244,6 +244,7 @@ private fun ProgramInfoScreen( contentUrl: String, cookieManager: AppCookieManager, uriScheme: String, + userAgent: String, canShowBackBtn: Boolean, isNestedFragment: Boolean, hasInternetConnection: Boolean, @@ -321,6 +322,7 @@ private fun ProgramInfoScreen( val webView = CatalogWebViewScreen( url = contentUrl, uriScheme = uriScheme, + userAgent = userAgent, isAllLinksExternal = true, onWebPageLoaded = { onWebViewUIAction(WebViewUIAction.WEB_PAGE_LOADED) }, refreshSessionCookie = { @@ -380,6 +382,7 @@ fun MyProgramsPreview() { contentUrl = "https://www.example.com/", cookieManager = koinViewModel().cookieManager, uriScheme = "", + userAgent = "", canShowBackBtn = false, isNestedFragment = false, hasInternetConnection = false, diff --git a/discovery/src/main/java/org/openedx/discovery/presentation/program/ProgramViewModel.kt b/discovery/src/main/java/org/openedx/discovery/presentation/program/ProgramViewModel.kt index 59a26cba5..572e5ee4e 100644 --- a/discovery/src/main/java/org/openedx/discovery/presentation/program/ProgramViewModel.kt +++ b/discovery/src/main/java/org/openedx/discovery/presentation/program/ProgramViewModel.kt @@ -11,6 +11,7 @@ import org.openedx.core.R import org.openedx.core.UIMessage import org.openedx.core.config.Config import org.openedx.core.extension.isInternetError +import org.openedx.core.presentation.global.AppData import org.openedx.core.presentation.global.ErrorType import org.openedx.core.system.AppCookieManager import org.openedx.core.system.ResourceManager @@ -22,6 +23,7 @@ import org.openedx.discovery.domain.interactor.DiscoveryInteractor import org.openedx.discovery.presentation.DiscoveryRouter class ProgramViewModel( + private val appData: AppData, private val config: Config, private val networkConnection: NetworkConnection, private val router: DiscoveryRouter, @@ -38,6 +40,8 @@ class ProgramViewModel( val hasInternetConnection: Boolean get() = networkConnection.isOnline() + val appUserAgent get() = appData.appUserAgent + private val _uiState = MutableStateFlow(ProgramUIState.Loading) val uiState: StateFlow get() = _uiState.asStateFlow()