-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20049 from wordpress-mobile/issue/20035-implement…
…-web-view-basics Site Monitor: Implement web view basics
- Loading branch information
Showing
13 changed files
with
734 additions
and
109 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
WordPress/src/main/java/org/wordpress/android/ui/sitemonitor/SiteMonitorFragmentContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.wordpress.android.ui.sitemonitor | ||
|
||
import android.content.Context | ||
import android.view.View | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.fragment.app.FragmentContainerView | ||
import androidx.fragment.app.FragmentTransaction | ||
import androidx.fragment.app.commit | ||
import androidx.fragment.app.findFragment | ||
|
||
@Suppress("SwallowedException") | ||
@Composable | ||
fun SiteMonitorFragmentContainer( | ||
modifier: Modifier = Modifier, | ||
commit: FragmentTransaction.(containerId: Int) -> Unit | ||
) { | ||
val currentLocalView = LocalView.current | ||
// Using the current view, check if a parent fragment exists. | ||
// This will help ensure that the fragment are nested correctly. | ||
// This assists in saving/restoring the fragments to their proper state | ||
val parentFragment = remember(currentLocalView) { | ||
try { | ||
currentLocalView.findFragment<Fragment>() | ||
} catch (e: IllegalStateException) { | ||
null | ||
} | ||
} | ||
val viewId by rememberSaveable { mutableIntStateOf(View.generateViewId()) } | ||
val container = remember { mutableStateOf<FragmentContainerView?>(null) } | ||
val viewSection: (Context) -> View = remember(currentLocalView) { | ||
{ context -> | ||
FragmentContainerView(context) | ||
.apply { id = viewId } | ||
.also { | ||
val fragmentManager = parentFragment?.childFragmentManager | ||
?: (context as? FragmentActivity)?.supportFragmentManager | ||
fragmentManager?.commit { commit(it.id) } | ||
container.value = it | ||
} | ||
} | ||
} | ||
AndroidView( | ||
modifier = modifier, | ||
factory = viewSection, | ||
update = {} | ||
) | ||
|
||
// Be sure to clean up the fragments when the FragmentContainer is disposed | ||
val localContext = LocalContext.current | ||
DisposableEffect(currentLocalView, localContext, container) { | ||
onDispose { | ||
val fragmentManager = parentFragment?.childFragmentManager | ||
?: (localContext as? FragmentActivity)?.supportFragmentManager | ||
// Use the FragmentContainerView to find the inflated fragment | ||
val existingFragment = fragmentManager?.findFragmentById(container.value?.id ?: 0) | ||
if (existingFragment != null && !fragmentManager.isStateSaved) { | ||
// A composable has been removed from the hierarchy if the state isn't saved | ||
fragmentManager.commit { | ||
remove(existingFragment) | ||
} | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
WordPress/src/main/java/org/wordpress/android/ui/sitemonitor/SiteMonitorMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.wordpress.android.ui.sitemonitor | ||
|
||
import javax.inject.Inject | ||
|
||
class SiteMonitorMapper @Inject constructor( | ||
private val siteMonitorUtils: SiteMonitorUtils | ||
) { | ||
fun toPrepared(url: String, addressToLoad: String, siteMonitorType: SiteMonitorType) = SiteMonitorUiState.Prepared( | ||
model = SiteMonitorModel( | ||
enableJavascript = true, | ||
enableDomStorage = true, | ||
userAgent = siteMonitorUtils.getUserAgent(), | ||
enableChromeClient = true, | ||
url = url, | ||
addressToLoad = addressToLoad, | ||
siteMonitorType = siteMonitorType | ||
) | ||
) | ||
|
||
fun toNoNetworkError(buttonClick: () -> Unit) = SiteMonitorUiState.NoNetworkError(buttonClick = buttonClick) | ||
|
||
fun toGenericError(buttonClick: () -> Unit) = SiteMonitorUiState.GenericError(buttonClick = buttonClick) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
WordPress/src/main/java/org/wordpress/android/ui/sitemonitor/SiteMonitorParentViewModel.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.