-
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 #19304 from wordpress-mobile/Plans-in-site-creatio…
…n-Plan-Selection-Screen Plans in site creation: Plan selection screen
- Loading branch information
Showing
12 changed files
with
537 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
17 changes: 17 additions & 0 deletions
17
WordPress/src/main/java/org/wordpress/android/ui/sitecreation/plans/PlansScreenListener.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,17 @@ | ||
package org.wordpress.android.ui.sitecreation.plans | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
interface PlansScreenListener { | ||
fun onPlanSelected(plan: PlanModel) | ||
} | ||
|
||
@Parcelize | ||
data class PlanModel( | ||
val productId: Int?, | ||
val productSlug: String?, | ||
val productName: String?, | ||
val isCurrentPlan: Boolean, | ||
val hasDomainCredit: Boolean | ||
) : Parcelable |
210 changes: 210 additions & 0 deletions
210
...ss/src/main/java/org/wordpress/android/ui/sitecreation/plans/SiteCreationPlansFragment.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,210 @@ | ||
package org.wordpress.android.ui.sitecreation.plans | ||
|
||
import android.annotation.SuppressLint | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.webkit.WebView | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxHeight | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Scaffold | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import org.wordpress.android.R | ||
import org.wordpress.android.ui.WPWebViewActivity | ||
import org.wordpress.android.ui.compose.components.MainTopAppBar | ||
import org.wordpress.android.ui.compose.components.NavigationIcons | ||
import org.wordpress.android.ui.compose.theme.AppTheme | ||
import org.wordpress.android.ui.compose.utils.uiStringText | ||
import org.wordpress.android.ui.main.jetpack.migration.compose.state.LoadingState | ||
import org.wordpress.android.ui.sitecreation.SiteCreationActivity.Companion.ARG_STATE | ||
import org.wordpress.android.ui.sitecreation.SiteCreationState | ||
import org.wordpress.android.ui.sitecreation.plans.SiteCreationPlansWebViewClient.SiteCreationPlansWebViewClientListener | ||
import org.wordpress.android.util.extensions.getParcelableCompat | ||
|
||
@AndroidEntryPoint | ||
class SiteCreationPlansFragment : Fragment(), SiteCreationPlansWebViewClientListener { | ||
private val viewModel: SiteCreationPlansViewModel by viewModels() | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View = ComposeView(requireContext()).apply { | ||
setContent { | ||
AppTheme { | ||
SiteCreationPlansPage( | ||
navigationUp = requireActivity().onBackPressedDispatcher::onBackPressed | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
viewModel.start(requireNotNull(requireArguments().getParcelableCompat(ARG_STATE))) | ||
viewModel.actionEvents.onEach(this::handleActionEvents).launchIn(viewLifecycleOwner.lifecycleScope) | ||
} | ||
|
||
private fun handleActionEvents(actionEvent: SiteCreationPlansActionEvent) { | ||
when (actionEvent) { | ||
is SiteCreationPlansActionEvent.CreateSite -> { | ||
(requireActivity() as PlansScreenListener).onPlanSelected(actionEvent.planModel) | ||
} | ||
} | ||
} | ||
|
||
// SiteCreationWebViewClient | ||
override fun onPlanSelected(uri: Uri) { | ||
viewModel.onPlanSelected(uri) | ||
} | ||
|
||
override fun onWebViewPageLoaded() { | ||
viewModel.onUrlLoaded() | ||
} | ||
|
||
override fun onWebViewReceivedError() { | ||
viewModel.onWebViewError() | ||
} | ||
|
||
@Composable | ||
@SuppressLint("UnusedMaterialScaffoldPaddingParameter") | ||
fun SiteCreationPlansPage( | ||
navigationUp: () -> Unit = { }, | ||
viewModel: SiteCreationPlansViewModel = viewModel(), | ||
) { | ||
val uiState by viewModel.uiState.collectAsState() | ||
Scaffold( | ||
topBar = { | ||
MainTopAppBar( | ||
title = stringResource(R.string.site_creation_plans_selection_title), | ||
navigationIcon = NavigationIcons.BackIcon, | ||
onNavigationIconClick = navigationUp | ||
) | ||
}, | ||
content = { SiteCreationPlansContent(uiState) } | ||
) | ||
} | ||
|
||
@SuppressLint("SetJavaScriptEnabled") | ||
@Composable | ||
private fun SiteCreationPlansContent(uiState: SiteCreationPlansUiState) { | ||
when (uiState) { | ||
is SiteCreationPlansUiState.Preparing -> LoadingState() | ||
is SiteCreationPlansUiState.Prepared, | ||
is SiteCreationPlansUiState.Loaded -> SiteCreationPlansWebView(uiState) | ||
is SiteCreationPlansUiState.Error -> SiteCreationPlansError(uiState) | ||
} | ||
} | ||
|
||
@Composable | ||
fun SiteCreationPlansError(error: SiteCreationPlansUiState.Error) { | ||
Column( | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.Center, | ||
modifier = Modifier | ||
.padding(20.dp) | ||
.fillMaxWidth() | ||
.fillMaxHeight(), | ||
) { | ||
Text( | ||
text = uiStringText(uiString = error.title), | ||
style = MaterialTheme.typography.h5, | ||
textAlign = TextAlign.Center | ||
) | ||
Text( | ||
text = uiStringText(uiString = error.description), | ||
style = MaterialTheme.typography.body1, | ||
textAlign = TextAlign.Center, | ||
modifier = Modifier.padding(top = 8.dp) | ||
) | ||
if (error.button != null) { | ||
Button( | ||
modifier = Modifier.padding(top = 8.dp), | ||
onClick = error.button.click | ||
) { | ||
Text(text = uiStringText(uiString = error.button.text)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@SuppressLint("SetJavaScriptEnabled") | ||
@Composable | ||
private fun SiteCreationPlansWebView(uiState: SiteCreationPlansUiState) { | ||
var webView: WebView? by remember { mutableStateOf(null) } | ||
|
||
if (uiState is SiteCreationPlansUiState.Prepared) { | ||
val model = uiState.model | ||
LaunchedEffect(true) { | ||
webView = WebView(requireContext()).apply { | ||
layoutParams = ViewGroup.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, | ||
ViewGroup.LayoutParams.MATCH_PARENT | ||
) | ||
scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY | ||
settings.userAgentString = model.userAgent | ||
settings.javaScriptEnabled = model.enableJavascript | ||
settings.domStorageEnabled = model.enableDomStorage | ||
webViewClient = SiteCreationPlansWebViewClient(this@SiteCreationPlansFragment) | ||
postUrl(WPWebViewActivity.WPCOM_LOGIN_URL, model.addressToLoad.toByteArray()) | ||
} | ||
} | ||
} | ||
|
||
Box( | ||
modifier = Modifier.fillMaxSize(), | ||
contentAlignment = Alignment.Center | ||
) { | ||
if (uiState is SiteCreationPlansUiState.Prepared) { | ||
LoadingState() | ||
} else { | ||
webView?.let { theWebView -> | ||
AndroidView( | ||
factory = { theWebView }, | ||
modifier = Modifier.fillMaxSize() | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
const val TAG = "site_creation_plans_fragment_tag" | ||
|
||
fun newInstance(siteCreationState: SiteCreationState) = SiteCreationPlansFragment().apply { | ||
arguments = Bundle().apply { | ||
putParcelable(ARG_STATE, siteCreationState) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.