-
Notifications
You must be signed in to change notification settings - Fork 130
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 #12648 from woocommerce/issue/12494-blaze-webview-…
…fragment Issue/12494 blaze webview fragment
- Loading branch information
Showing
13 changed files
with
338 additions
and
55 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
68 changes: 68 additions & 0 deletions
68
...main/kotlin/com/woocommerce/android/ui/blaze/detail/BlazeCampaignDetailWebViewFragment.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,68 @@ | ||
package com.woocommerce.android.ui.blaze.detail | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.platform.ViewCompositionStrategy | ||
import androidx.fragment.app.viewModels | ||
import androidx.navigation.fragment.findNavController | ||
import com.woocommerce.android.extensions.navigateBackWithResult | ||
import com.woocommerce.android.ui.base.BaseFragment | ||
import com.woocommerce.android.ui.common.wpcomwebview.WPComWebViewAuthenticator | ||
import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground | ||
import com.woocommerce.android.ui.main.AppBarStatus | ||
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.Exit | ||
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ExitWithResult | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import org.wordpress.android.fluxc.network.UserAgent | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class BlazeCampaignDetailWebViewFragment : BaseFragment() { | ||
companion object { | ||
const val BLAZE_WEBVIEW_RESULT = "blaze-webview-result" | ||
} | ||
|
||
override val activityAppBarStatus: AppBarStatus | ||
get() = AppBarStatus.Hidden | ||
|
||
private val viewModel: BlazeCampaignDetailWebViewViewModel by viewModels() | ||
|
||
@Inject | ||
lateinit var wpComAuthenticator: WPComWebViewAuthenticator | ||
|
||
@Inject | ||
lateinit var userAgent: UserAgent | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { | ||
return ComposeView(requireContext()).apply { | ||
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) | ||
|
||
setContent { | ||
WooThemeWithBackground { | ||
BlazeCampaignDetailWebViewScreen( | ||
viewModel = viewModel, | ||
wpComAuthenticator = wpComAuthenticator, | ||
userAgent = userAgent, | ||
onUrlLoaded = viewModel::onUrlLoaded, | ||
onDismiss = viewModel::onDismiss | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
viewModel.event.observe(viewLifecycleOwner) { event -> | ||
when (event) { | ||
is Exit -> findNavController().popBackStack() | ||
is ExitWithResult<*> -> navigateBackWithResult( | ||
BLAZE_WEBVIEW_RESULT, | ||
event.data | ||
) | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...c/main/kotlin/com/woocommerce/android/ui/blaze/detail/BlazeCampaignDetailWebViewScreen.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,39 @@ | ||
package com.woocommerce.android.ui.blaze.detail | ||
|
||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.Scaffold | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import com.woocommerce.android.R | ||
import com.woocommerce.android.ui.common.wpcomwebview.WPComWebViewAuthenticator | ||
import com.woocommerce.android.ui.compose.component.Toolbar | ||
import com.woocommerce.android.ui.compose.component.web.WCWebView | ||
import org.wordpress.android.fluxc.network.UserAgent | ||
|
||
@Composable | ||
fun BlazeCampaignDetailWebViewScreen( | ||
viewModel: BlazeCampaignDetailWebViewViewModel, | ||
wpComAuthenticator: WPComWebViewAuthenticator, | ||
userAgent: UserAgent, | ||
onUrlLoaded: (String) -> Unit, | ||
onDismiss: () -> Unit, | ||
) { | ||
Scaffold( | ||
topBar = { | ||
Toolbar( | ||
title = stringResource(id = R.string.blaze_campaign_details_title), | ||
onNavigationButtonClick = onDismiss | ||
) | ||
} | ||
) { paddingValues -> | ||
WCWebView( | ||
url = viewModel.viewState.urlToLoad, | ||
userAgent = userAgent, | ||
wpComAuthenticator = wpComAuthenticator, | ||
onUrlLoaded = onUrlLoaded, | ||
clearCache = true, | ||
modifier = Modifier.padding(paddingValues) | ||
) | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...ain/kotlin/com/woocommerce/android/ui/blaze/detail/BlazeCampaignDetailWebViewViewModel.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,72 @@ | ||
package com.woocommerce.android.ui.blaze.detail | ||
|
||
import android.os.Parcelable | ||
import androidx.lifecycle.SavedStateHandle | ||
import com.woocommerce.android.ui.blaze.BlazeUrlsHelper | ||
import com.woocommerce.android.ui.blaze.detail.BlazeCampaignDetailWebViewViewModel.BlazeAction.CampaignStopped | ||
import com.woocommerce.android.ui.blaze.detail.BlazeCampaignDetailWebViewViewModel.BlazeAction.None | ||
import com.woocommerce.android.ui.blaze.detail.BlazeCampaignDetailWebViewViewModel.BlazeAction.PromoteProductAgain | ||
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.Exit | ||
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ExitWithResult | ||
import com.woocommerce.android.viewmodel.ScopedViewModel | ||
import com.woocommerce.android.viewmodel.navArgs | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.parcelize.Parcelize | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class BlazeCampaignDetailWebViewViewModel @Inject constructor( | ||
savedStateHandle: SavedStateHandle, | ||
private val blazeUrlsHelper: BlazeUrlsHelper | ||
) : ScopedViewModel(savedStateHandle) { | ||
private val navArgs: BlazeCampaignDetailWebViewFragmentArgs by savedStateHandle.navArgs() | ||
private val stopUrlPath = blazeUrlsHelper.getCampaignStopUrlPath(navArgs.campaignId) | ||
private val blazeCampaignListUrl = blazeUrlsHelper.buildCampaignsListUrl() | ||
|
||
var viewState = ViewState( | ||
urlToLoad = blazeUrlsHelper.buildCampaignDetailsUrl(navArgs.campaignId), | ||
blazeAction = None | ||
) | ||
|
||
fun onUrlLoaded(url: String) { | ||
when { | ||
blazeCampaignListUrl == url -> onDismiss() | ||
url.contains(stopUrlPath) -> { | ||
viewState = viewState.copy(blazeAction = CampaignStopped) | ||
} | ||
|
||
url.contains(BlazeUrlsHelper.PROMOTE_AGAIN_URL_PATH) -> { | ||
viewState = viewState.copy( | ||
blazeAction = PromoteProductAgain( | ||
productId = blazeUrlsHelper.extractProductIdFromPromoteAgainUrl(url) | ||
) | ||
) | ||
onDismiss() | ||
} | ||
} | ||
} | ||
|
||
fun onDismiss() { | ||
when (viewState.blazeAction) { | ||
None -> triggerEvent(Exit) | ||
else -> triggerEvent(ExitWithResult(viewState.blazeAction)) | ||
} | ||
} | ||
|
||
data class ViewState( | ||
val urlToLoad: String, | ||
val blazeAction: BlazeAction, | ||
) | ||
|
||
@Parcelize | ||
sealed interface BlazeAction : Parcelable { | ||
@Parcelize | ||
data object CampaignStopped : BlazeAction | ||
|
||
@Parcelize | ||
data class PromoteProductAgain(val productId: Long?) : BlazeAction | ||
|
||
@Parcelize | ||
data object None : BlazeAction | ||
} | ||
} |
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
Oops, something went wrong.