Skip to content

Commit

Permalink
PWN-933 - Add terms of service to JS bridge (#2190)
Browse files Browse the repository at this point in the history
  • Loading branch information
gslevinkov authored Feb 12, 2024
1 parent bdce0fd commit 98b9311
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app/src/main/assets/referral_bridge_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ window.ReferralBridge = {
showShareDialog: function(link) {
AndroidReferralBridge.showShareDialog(link);
},
openTermsUrl: function(link) {
AndroidReferralBridge.openTermsUrl(link);
},
getClipboardContent: function() {
return JSON.parse(AndroidReferralBridge.getClipboardValue());
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ class HistoryListViewPresenter(
}

override fun onItemClicked(historyItem: HistoryItem) {
Timber.i("on clicked")
launch {
when (historyItem) {
is HistoryItem.TransactionItem -> {
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/org/p2p/wallet/referral/ReferralBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ReferralWebViewBridge(
webView: WebView,
private val tokenKeyProvider: TokenKeyProvider,
private val onShareLinkCalled: (String) -> Unit,
private val openTerms: (link: String) -> Unit,
private val onWebViewLoaded: () -> Unit
) {
companion object {
Expand Down Expand Up @@ -69,11 +70,7 @@ class ReferralWebViewBridge(
append(consoleMessage.lineNumber())
}

if (consoleMessage.messageLevel() == ConsoleMessage.MessageLevel.ERROR) {
Timber.tag(JS_BRIDGE_OBJECT_NAME).e(Exception(logMessage))
} else {
Timber.tag(JS_BRIDGE_OBJECT_NAME).w(logMessage)
}
Timber.tag(JS_BRIDGE_OBJECT_NAME).i(logMessage)
return true
}
}
Expand Down Expand Up @@ -129,6 +126,11 @@ class ReferralWebViewBridge(
onShareLinkCalled.invoke(link)
}

@JavascriptInterface
fun openTermsUrl(link: String) {
openTerms.invoke(link)
}

@JavascriptInterface
fun getUserPublicKeyAsync(): JsResultWrapper = makeAsyncCall {
wrapInJsResult(tokenKeyProvider.publicKey)
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/p2p/wallet/referral/ReferralFragment.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.p2p.wallet.referral

import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import android.os.Bundle
import android.view.View
import org.koin.android.ext.android.get
import org.p2p.wallet.R
import org.p2p.wallet.common.mvp.BaseMvpFragment
import org.p2p.wallet.databinding.FragmentReferralBinding
import org.p2p.wallet.url.OpenUrlFragment
import org.p2p.wallet.utils.popBackStack
import org.p2p.wallet.utils.replaceFragment
import org.p2p.wallet.utils.shareText
import org.p2p.wallet.utils.viewbinding.viewBinding

Expand All @@ -28,11 +31,13 @@ class ReferralFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.progressBar.isVisible = true
binding.webViewReferral.isInvisible = true

jsBridge = ReferralWebViewBridge(
webView = binding.webViewReferral,
tokenKeyProvider = get(),
onShareLinkCalled = ::showShareLinkDialog,
openTerms = ::navigateToTerms,
onWebViewLoaded = ::onWebViewLoaded
)
binding.toolbar.setNavigationOnClickListener {
Expand All @@ -59,7 +64,16 @@ class ReferralFragment :
requireContext().shareText(link)
}

private fun navigateToTerms(link: String) {
val target = OpenUrlFragment.create(
url = link,
title = getString(R.string.onboarding_terms_of_use)
)
replaceFragment(target)
}

private fun onWebViewLoaded() {
binding.webViewReferral.isVisible = true
binding.progressBar.isVisible = false
}
}
93 changes: 93 additions & 0 deletions app/src/main/java/org/p2p/wallet/url/OpenUrlFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.p2p.wallet.url

import androidx.core.view.isVisible
import android.graphics.Bitmap
import android.os.Bundle
import android.view.View
import android.webkit.WebView
import android.webkit.WebViewClient
import org.p2p.wallet.R
import org.p2p.wallet.common.mvp.BaseMvpFragment
import org.p2p.wallet.common.mvp.MvpView
import org.p2p.wallet.common.mvp.NoOpPresenter
import org.p2p.wallet.databinding.FragmentOpenUrlBinding
import org.p2p.wallet.utils.args
import org.p2p.wallet.utils.popBackStack
import org.p2p.wallet.utils.viewbinding.viewBinding
import org.p2p.wallet.utils.withArgs

class OpenUrlFragment : BaseMvpFragment<MvpView, NoOpPresenter<MvpView>>(
R.layout.fragment_open_url
) {

companion object {
private const val ARG_URL = "ARG_URL"
private const val ARG_BACK_ENABLED = "ARG_BACK_ENABLED"
private const val ARG_TITLE = "ARG_TITLE"
fun create(
url: String,
title: String? = null,
isBackEnabled: Boolean = true,
): OpenUrlFragment {
return OpenUrlFragment()
.withArgs(
ARG_URL to url,
ARG_BACK_ENABLED to isBackEnabled,
ARG_TITLE to title
)
}
}

override val presenter = NoOpPresenter<MvpView>()

private val binding: FragmentOpenUrlBinding by viewBinding()

private val webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
binding.progressBar.isVisible = true
super.onPageStarted(view, url, favicon)
}

override fun onPageFinished(view: WebView?, url: String?) {
if (view?.progress == 100) {
binding.progressBar.isVisible = false
}
super.onPageFinished(view, url)
}
}

private val toolbarTitle: String? by args(ARG_TITLE)
private val isBackEnabled: Boolean by args(ARG_BACK_ENABLED, true)
private val url: String by args(ARG_URL)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (isBackEnabled) {
binding.toolbar.setNavigationOnClickListener { popBackStack() }
} else {
binding.toolbar.navigationIcon = null
}
if (toolbarTitle != null) {
binding.toolbar.title = toolbarTitle
}

binding.webViewUrl.webViewClient = webViewClient
binding.webViewUrl.loadUrl(url)
}

override fun onResume() {
super.onResume()
binding.webViewUrl.onResume()
binding.webViewUrl.requestFocus()
}

override fun onPause() {
binding.webViewUrl.onPause()
super.onPause()
}

override fun onDestroyView() {
binding.webViewUrl.destroy()
super.onDestroyView()
}
}
39 changes: 39 additions & 0 deletions app/src/main/res/layout/fragment_open_url.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<org.p2p.uikit.organisms.UiKitToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_snow"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="@drawable/ic_back"
app:title="" />

<WebView
android:id="@+id/webViewUrl"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminateOnly="true"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/layout/fragment_referral.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_smoke"
android:backgroundTint="@color/bg_smoke"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

Expand All @@ -18,6 +20,8 @@
<WebView
android:id="@+id/webViewReferral"
android:layout_width="0dp"
android:background="@color/bg_smoke"
android:backgroundTint="@color/bg_smoke"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down

0 comments on commit 98b9311

Please sign in to comment.