Skip to content

Commit

Permalink
chore: Auto play videos when ready, Webview No Network Error (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
dixidroid authored Nov 18, 2024
1 parent af3b8a9 commit fa38468
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 11 deletions.
9 changes: 8 additions & 1 deletion app/src/main/java/org/openedx/app/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,14 @@ val appModule = module {
DownloadWorkerController(get(), get(), get())
}

single { AppData(versionName = BuildConfig.VERSION_NAME) }
single {
val resourceManager = get<ResourceManager>()
AppData(
appName = resourceManager.getString(R.string.app_name),
versionName = BuildConfig.VERSION_NAME,
applicationId = BuildConfig.APPLICATION_ID,
)
}
factory { (activity: AppCompatActivity) -> AppReviewManager(activity, get(), get()) }

single { TranscriptManager(get(), get()) }
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/org/openedx/app/di/ScreenModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ val screenModule = module {
get(),
get(),
get(),
get(),
)
}

Expand Down Expand Up @@ -227,6 +228,7 @@ val screenModule = module {
get(),
get(),
get(),
get(),
)
}
viewModel { (courseId: String) ->
Expand Down Expand Up @@ -458,7 +460,7 @@ val screenModule = module {
)
}

viewModel { ProgramViewModel(get(), get(), get(), get(), get(), get(), get()) }
viewModel { ProgramViewModel(get(), get(), get(), get(), get(), get(), get(), get()) }

viewModel { (courseId: String, courseTitle: String) ->
CourseOfflineViewModel(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.openedx.core.presentation.global

data class AppData(
val appName: String,
val applicationId: String,
val versionName: String,
)
) {
val appUserAgent get() = "$appName/$applicationId/$versionName"
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/openedx/core/ui/ComposeCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,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,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import androidx.fragment.app.Fragment
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
import org.openedx.core.extension.equalsHost
import org.openedx.core.extension.loadUrl
import org.openedx.core.system.AppCookieManager
import org.openedx.core.ui.FullScreenErrorView
Expand Down Expand Up @@ -359,7 +358,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class VideoUnitFragment : Fragment(R.layout.fragment_video_unit) {
mediaItem,
viewModel.exoPlayer?.currentPosition ?: 0L
)
viewModel.castPlayer?.playWhenReady = false
viewModel.castPlayer?.playWhenReady = true
showVideoControllerIndefinitely(true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ open class VideoUnitViewModel(

var videoUrl = ""
var transcripts = emptyMap<String, String>()
var isPlaying = false
var isPlaying = true
var transcriptLanguage = AppDataConstants.defaultLocale.language ?: "en"
private set

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class WebViewDiscoveryFragment : Fragment() {
isPreLogin = viewModel.isPreLogin,
contentUrl = viewModel.discoveryUrl,
uriScheme = viewModel.uriScheme,
userAgent = viewModel.appUserAgent,
isRegistrationEnabled = viewModel.isRegistrationEnabled,
hasInternetConnection = hasInternetConnection,
onWebViewUIAction = { action ->
Expand Down Expand Up @@ -195,6 +196,7 @@ private fun WebViewDiscoveryScreen(
contentUrl: String,
uriScheme: String,
isRegistrationEnabled: Boolean,
userAgent: String,
hasInternetConnection: Boolean,
onWebViewUIAction: (WebViewUIAction) -> Unit,
onWebPageUpdated: (String) -> Unit,
Expand Down Expand Up @@ -275,6 +277,7 @@ private fun WebViewDiscoveryScreen(
DiscoveryWebView(
contentUrl = contentUrl,
uriScheme = uriScheme,
userAgent = userAgent,
onWebPageLoaded = {
if ((uiState is WebViewUIState.Error).not()) {
onWebViewUIAction(WebViewUIAction.WEB_PAGE_LOADED)
Expand Down Expand Up @@ -316,6 +319,7 @@ private fun WebViewDiscoveryScreen(
private fun DiscoveryWebView(
contentUrl: String,
uriScheme: String,
userAgent: String,
onWebPageLoaded: () -> Unit,
onWebPageUpdated: (String) -> Unit,
onUriClick: (String, WebViewLink.Authority) -> Unit,
Expand All @@ -324,6 +328,7 @@ private fun DiscoveryWebView(
val webView = CatalogWebViewScreen(
url = contentUrl,
uriScheme = uriScheme,
userAgent = userAgent,
onWebPageLoaded = onWebPageLoaded,
onWebPageUpdated = onWebPageUpdated,
onUriClick = onUriClick,
Expand Down Expand Up @@ -396,6 +401,7 @@ private fun WebViewDiscoveryScreenPreview() {
contentUrl = "https://www.example.com/",
uriScheme = "",
isRegistrationEnabled = true,
userAgent = "",
hasInternetConnection = false,
onWebViewUIAction = {},
onWebPageUpdated = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
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
Expand All @@ -14,6 +15,7 @@ import org.openedx.foundation.utils.UrlUtils

class WebViewDiscoveryViewModel(
private val querySearch: String,
private val appData: AppData,
private val config: Config,
private val networkConnection: NetworkConnection,
private val corePreferences: CorePreferences,
Expand All @@ -30,6 +32,8 @@ class WebViewDiscoveryViewModel(
val isPreLogin get() = config.isPreLoginExperienceEnabled() && corePreferences.user == null
val isRegistrationEnabled: Boolean get() = config.isRegistrationEnabled()

val appUserAgent get() = appData.appUserAgent

private var _discoveryUrl = webViewConfig.baseUrl
val discoveryUrl: String
get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import org.openedx.core.extension.equalsHost
import org.openedx.foundation.extension.applyDarkModeIfEnabled
import org.openedx.discovery.presentation.catalog.WebViewLink.Authority as linkAuthority

Expand All @@ -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 = {},
Expand Down Expand Up @@ -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)
Expand All @@ -104,6 +104,7 @@ fun CatalogWebViewScreen(
setSupportZoom(true)
loadsImagesAutomatically = true
domStorageEnabled = true
userAgentString = "$userAgentString $userAgent"
}
isVerticalScrollBarEnabled = false
isHorizontalScrollBarEnabled = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ class CourseInfoFragment : Fragment() {
webViewUIState = webViewState,
uiMessage = uiMessage,
uriScheme = viewModel.uriScheme,
hasInternetConnection = hasInternetConnection,
isRegistrationEnabled = viewModel.isRegistrationEnabled,
userAgent = viewModel.appUserAgent,
hasInternetConnection = hasInternetConnection,
onWebViewUIAction = { action ->
when (action) {
WebViewUIAction.WEB_PAGE_LOADED -> {
Expand Down Expand Up @@ -244,6 +245,7 @@ private fun CourseInfoScreen(
uiMessage: UIMessage?,
uriScheme: String,
isRegistrationEnabled: Boolean,
userAgent: String,
hasInternetConnection: Boolean,
onWebViewUIAction: (WebViewUIAction) -> Unit,
onRegisterClick: () -> Unit,
Expand Down Expand Up @@ -318,6 +320,7 @@ private fun CourseInfoScreen(
CourseInfoWebView(
contentUrl = (uiState as CourseInfoUIState.CourseInfo).initialUrl,
uriScheme = uriScheme,
userAgent = userAgent,
onWebPageLoaded = { onWebViewUIAction(WebViewUIAction.WEB_PAGE_LOADED) },
onUriClick = onUriClick,
onWebPageLoadError = {
Expand Down Expand Up @@ -354,13 +357,15 @@ private fun CourseInfoScreen(
private fun CourseInfoWebView(
contentUrl: String,
uriScheme: String,
userAgent: String,
onWebPageLoaded: () -> Unit,
onUriClick: (String, linkAuthority) -> Unit,
onWebPageLoadError: () -> Unit
) {
val webView = CatalogWebViewScreen(
url = contentUrl,
uriScheme = uriScheme,
userAgent = userAgent,
isAllLinksExternal = true,
onWebPageLoaded = onWebPageLoaded,
onUriClick = onUriClick,
Expand Down Expand Up @@ -391,6 +396,7 @@ fun CourseInfoScreenPreview() {
uiMessage = null,
uriScheme = "",
isRegistrationEnabled = true,
userAgent = "",
hasInternetConnection = false,
onWebViewUIAction = {},
onRegisterClick = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.withContext
import org.openedx.core.config.Config
import org.openedx.core.data.storage.CorePreferences
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.connection.NetworkConnection
Expand All @@ -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,
Expand Down Expand Up @@ -75,6 +77,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class ProgramFragment : Fragment() {
?.isNotEmpty() == true,
isNestedFragment = isNestedFragment,
uriScheme = viewModel.uriScheme,
userAgent = viewModel.appUserAgent,
hasInternetConnection = hasInternetConnection,
onWebViewUIAction = { action ->
when (action) {
Expand Down Expand Up @@ -243,6 +244,7 @@ private fun ProgramInfoScreen(
contentUrl: String,
cookieManager: AppCookieManager,
uriScheme: String,
userAgent: String,
canShowBackBtn: Boolean,
isNestedFragment: Boolean,
hasInternetConnection: Boolean,
Expand Down Expand Up @@ -319,6 +321,7 @@ private fun ProgramInfoScreen(
val webView = CatalogWebViewScreen(
url = contentUrl,
uriScheme = uriScheme,
userAgent = userAgent,
isAllLinksExternal = true,
onWebPageLoaded = { onWebViewUIAction(WebViewUIAction.WEB_PAGE_LOADED) },
refreshSessionCookie = {
Expand Down Expand Up @@ -378,6 +381,7 @@ fun MyProgramsPreview() {
contentUrl = "https://www.example.com/",
cookieManager = koinViewModel<ProgramViewModel>().cookieManager,
uriScheme = "",
userAgent = "",
canShowBackBtn = false,
isNestedFragment = false,
hasInternetConnection = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.openedx.core.R
import org.openedx.core.config.Config
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.connection.NetworkConnection
Expand All @@ -22,6 +23,7 @@ import org.openedx.foundation.presentation.UIMessage
import org.openedx.foundation.system.ResourceManager

class ProgramViewModel(
private val appData: AppData,
private val config: Config,
private val networkConnection: NetworkConnection,
private val router: DiscoveryRouter,
Expand All @@ -38,6 +40,8 @@ class ProgramViewModel(

val hasInternetConnection: Boolean get() = networkConnection.isOnline()

val appUserAgent get() = appData.appUserAgent

private val _uiState = MutableStateFlow<ProgramUIState>(ProgramUIState.Loading)
val uiState: StateFlow<ProgramUIState> get() = _uiState.asStateFlow()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,9 @@ fun AppVersionItemUpgradeRequired(
}

private val mockAppData = AppData(
appName = "openedx",
versionName = "1.0.0",
applicationId = "org.example.com"
)

private val mockConfiguration = Configuration(
Expand Down

0 comments on commit fa38468

Please sign in to comment.