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

[Receipts] IO Exception crash #13411

Merged
merged 3 commits into from
Jan 29, 2025
Merged
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 @@ -18,6 +18,7 @@ import com.woocommerce.android.ui.base.BaseFragment
import com.woocommerce.android.ui.base.UIMessageResolver
import com.woocommerce.android.util.PrintHtmlHelper
import com.woocommerce.android.util.UiHelpers
import com.woocommerce.android.util.WooLog
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowSnackbar
import dagger.hilt.android.AndroidEntryPoint
import java.io.IOException
Expand All @@ -38,6 +39,8 @@ class ReceiptPreviewFragment : BaseFragment(R.layout.fragment_receipt_preview),
private var _binding: FragmentReceiptPreviewBinding? = null
private val binding get() = _binding!!

private var urlToLoad: String? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

Expand All @@ -58,10 +61,12 @@ class ReceiptPreviewFragment : BaseFragment(R.layout.fragment_receipt_preview),
viewModel.onPrintClicked()
true
}

R.id.menu_send -> {
viewModel.onShareClicked()
true
}

else -> false
}
}
Expand Down Expand Up @@ -101,9 +106,12 @@ class ReceiptPreviewFragment : BaseFragment(R.layout.fragment_receipt_preview),
override fun shouldInterceptRequest(
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
return interceptAndModifyReceiptResponse(request)
}
): WebResourceResponse? =
if (request.url.toString() == urlToLoad) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to modify the response only on the root page. Otherwise, we change mimeType for all the page resources which externally placed (icons, css, js)

interceptAndModifyReceiptResponse(request)
} else {
null
}

override fun onPageFinished(view: WebView, url: String) {
viewModel.onReceiptLoaded()
Expand All @@ -129,7 +137,8 @@ class ReceiptPreviewFragment : BaseFragment(R.layout.fragment_receipt_preview),
} catch (e: MalformedURLException) {
throw IllegalArgumentException("Invalid receipt URL: ${request.url}", e)
} catch (e: IOException) {
throw IOException("Failed to read content from receipt URL: ${request.url}", e)
WooLog.e(WooLog.T.ORDERS, "Failed to read content from receipt URL: ${request.url}", e)
null
}
}

Expand All @@ -140,7 +149,10 @@ class ReceiptPreviewFragment : BaseFragment(R.layout.fragment_receipt_preview),
}
viewModel.event.observe(viewLifecycleOwner) {
when (it) {
is LoadUrl -> binding.receiptPreviewPreviewWebview.loadUrl(it.url)
is LoadUrl -> {
urlToLoad = it.url
binding.receiptPreviewPreviewWebview.loadUrl(it.url)
}
is PrintReceipt -> printHtmlHelper.printReceipt(requireActivity(), it.receiptUrl, it.documentName)
is ShowSnackbar -> uiMessageResolver.showSnack(it.message)
else -> it.isHandled = false
Expand Down
Loading