diff --git a/android-sdk/src/main/java/com/payline/mobile/androidsdk/core/presentation/WebFragment.kt b/android-sdk/src/main/java/com/payline/mobile/androidsdk/core/presentation/WebFragment.kt index 99e63d8..d12a86f 100644 --- a/android-sdk/src/main/java/com/payline/mobile/androidsdk/core/presentation/WebFragment.kt +++ b/android-sdk/src/main/java/com/payline/mobile/androidsdk/core/presentation/WebFragment.kt @@ -25,11 +25,12 @@ import com.payline.mobile.androidsdk.payment.domain.PaymentSdkResult import com.payline.mobile.androidsdk.wallet.domain.WalletSdkResult import kotlinx.android.synthetic.main.fragment_web.* -internal class WebFragment: Fragment(), ScriptActionExecutor, SdkResultBroadcaster { +internal class WebFragment : Fragment(), ScriptActionExecutor, SdkResultBroadcaster { companion object { + private const val EXTRA_URI = "EXTRA_URI" - private var Bundle.uri by BundleDelegate.Uri("EXTRA_URI") + private var Bundle.uri by BundleDelegate.Uri(EXTRA_URI) fun createInstance(uri: Uri): WebFragment { return WebFragment().apply { @@ -46,7 +47,7 @@ internal class WebFragment: Fragment(), ScriptActionExecutor, SdkResultBroadcast WebSdkActionDelegate(this, this) } - private val actionReceiver = object: BroadcastReceiver() { + private val actionReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.getParcelableExtra(SdkAction.EXTRA_SDK_ACTION) ?: return actionDelegate.handleAction(action) @@ -78,31 +79,43 @@ internal class WebFragment: Fragment(), ScriptActionExecutor, SdkResultBroadcast val actionFilter = IntentFilter(SdkAction.BROADCAST_SDK_ACTION) LocalBroadcastManager.getInstance(activity!!).registerReceiver(actionReceiver, actionFilter) - arguments?.uri?.let { + viewModel.currentUrl = if (savedInstanceState == null) { + arguments?.uri + } else { + Uri.parse(savedInstanceState.getString(EXTRA_URI)) + } + + viewModel.currentUrl?.let { web_view.loadUrl(it.toString()) } - web_view.webViewClient = object: WebViewClient() { + web_view.webViewClient = object : WebViewClient() { override fun onPageFinished(view: WebView, url: String) { + viewModel.currentUrl = Uri.parse(url) viewModel.isLoading.postValue(false) } } } + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putString(EXTRA_URI, viewModel.currentUrl.toString()) + } + override fun onDestroyView() { super.onDestroyView() LocalBroadcastManager.getInstance(activity!!).unregisterReceiver(actionReceiver) } - override fun executeAction(action: ScriptAction, callback: (String)->Unit) { + override fun executeAction(action: ScriptAction, callback: (String) -> Unit) { scriptHandler.execute(action, web_view, callback) } override fun broadcast(result: SdkResult) { LocalBroadcastManager.getInstance(activity!!).sendBroadcast( - Intent(SdkResult.BROADCAST_SDK_RESULT).apply { - putExtra(SdkResult.EXTRA_SDK_RESULT, result) - } + Intent(SdkResult.BROADCAST_SDK_RESULT).apply { + putExtra(SdkResult.EXTRA_SDK_RESULT, result) + } ) } @@ -115,7 +128,7 @@ internal class WebFragment: Fragment(), ScriptActionExecutor, SdkResultBroadcast viewModel.hideCancelButton.postValue(false) - when(event.state) { + when (event.state) { WidgetState.PAYMENT_METHODS_LIST -> { broadcast(PaymentSdkResult.DidShowPaymentForm()) diff --git a/android-sdk/src/main/java/com/payline/mobile/androidsdk/core/presentation/WebViewModel.kt b/android-sdk/src/main/java/com/payline/mobile/androidsdk/core/presentation/WebViewModel.kt index 755d3b1..c136848 100644 --- a/android-sdk/src/main/java/com/payline/mobile/androidsdk/core/presentation/WebViewModel.kt +++ b/android-sdk/src/main/java/com/payline/mobile/androidsdk/core/presentation/WebViewModel.kt @@ -1,5 +1,6 @@ package com.payline.mobile.androidsdk.core.presentation +import android.net.Uri import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel @@ -17,4 +18,6 @@ internal class WebViewModel: ViewModel() { value = false } + var currentUrl: Uri? = null + }