Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed WebFragment url reloading when PaymentActivity is recreated #2

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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?>(SdkAction.EXTRA_SDK_ACTION) ?: return
actionDelegate.handleAction(action)
Expand Down Expand Up @@ -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)
}
)
}

Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.payline.mobile.androidsdk.core.presentation

import android.net.Uri
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

Expand All @@ -17,4 +18,6 @@ internal class WebViewModel: ViewModel() {
value = false
}

var currentUrl: Uri? = null

}