Skip to content

Commit 91dd944

Browse files
committed
feat: code refactored for request method an update example
Signed-off-by: Gaurav Goel <[email protected]>
1 parent 2b474a9 commit 91dd944

File tree

5 files changed

+30
-41
lines changed

5 files changed

+30
-41
lines changed

app/src/main/java/com/web3auth/app/MainActivity.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
122122
val signOutButton = findViewById<Button>(R.id.signOutButton)
123123
val launchWalletButton = findViewById<Button>(R.id.launchWalletButton)
124124
val signMsgButton = findViewById<Button>(R.id.signMsgButton)
125-
val signResultButton = findViewById<Button>(R.id.signResultButton)
126125
val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
127126
val spinner = findViewById<TextInputLayout>(R.id.verifierList)
128127
val hintEmailEditText = findViewById<EditText>(R.id.etEmailHint)
@@ -156,7 +155,6 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
156155
btnSetUpMfa.visibility = View.GONE
157156
launchWalletButton.visibility = View.GONE
158157
signMsgButton.visibility = View.GONE
159-
signResultButton.visibility = View.GONE
160158
spinner.visibility = View.VISIBLE
161159
}
162160
}
@@ -237,7 +235,6 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
237235
}
238236
}
239237

240-
val signResultButton = findViewById<Button>(R.id.signResultButton)
241238
val signMsgButton = findViewById<Button>(R.id.signMsgButton)
242239
signMsgButton.setOnClickListener {
243240
val credentials: Credentials = Credentials.create(web3Auth.getPrivkey())
@@ -253,22 +250,15 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
253250
chainNamespace = ChainNamespace.EIP155
254251
), "personal_sign", requestParams = params, appState = "web3Auth", context = this
255252
)
256-
signMsgCompletableFuture.whenComplete { _, error ->
253+
signMsgCompletableFuture.whenComplete { signResult, error ->
257254
if (error == null) {
258-
Log.d("MainActivity_Web3Auth", "Message signed successfully")
259-
signResultButton.visibility = View.VISIBLE
255+
showAlertDialog("Sign Result", signResult.toString())
260256
} else {
261257
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
262-
signResultButton.visibility = View.GONE
263258
}
264259
}
265260
}
266261

267-
signResultButton.setOnClickListener {
268-
val signResult = Web3Auth.getSignResponse()
269-
showAlertDialog("Sign Result", signResult.toString())
270-
}
271-
272262
val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
273263
btnSetUpMfa.setOnClickListener {
274264
val setupMfaCf = web3Auth.enableMFA(context = this)

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,6 @@
103103
app:layout_constraintStart_toStartOf="parent"
104104
app:layout_constraintTop_toBottomOf="@+id/launchWalletButton" />
105105

106-
<Button
107-
android:id="@+id/signResultButton"
108-
android:layout_width="wrap_content"
109-
android:layout_height="wrap_content"
110-
android:layout_marginEnd="10dp"
111-
android:text="@string/sign_result"
112-
android:textAllCaps="false"
113-
android:visibility="gone"
114-
app:layout_constraintEnd_toEndOf="parent"
115-
app:layout_constraintStart_toStartOf="parent"
116-
app:layout_constraintTop_toBottomOf="@+id/signMsgButton" />
117-
118106
<Button
119107
android:id="@+id/btnSetUpMfa"
120108
android:layout_width="wrap_content"

core/src/main/java/com/web3auth/core/Web3Auth.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import com.web3auth.core.types.WEBVIEW_URL
3131
import com.web3auth.core.types.Web3AuthError
3232
import com.web3auth.core.types.Web3AuthOptions
3333
import com.web3auth.core.types.Web3AuthResponse
34+
import com.web3auth.core.types.WebViewResultCallback
3435
import com.web3auth.session_manager_android.SessionManager
3536
import kotlinx.coroutines.CoroutineScope
3637
import kotlinx.coroutines.Dispatchers
@@ -39,12 +40,13 @@ import org.json.JSONObject
3940
import java.util.Locale
4041
import java.util.concurrent.CompletableFuture
4142

42-
class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
43+
class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResultCallback {
4344

4445
private val gson = GsonBuilder().disableHtmlEscaping().create()
4546

4647
private lateinit var loginCompletableFuture: CompletableFuture<Web3AuthResponse>
4748
private lateinit var enableMfaCompletableFuture: CompletableFuture<Boolean>
49+
private lateinit var signMsgCF: CompletableFuture<SignResponse>
4850

4951
private var web3AuthResponse: Web3AuthResponse? = null
5052
private var web3AuthOption = web3AuthOptions
@@ -516,8 +518,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
516518
path: String? = "wallet/request",
517519
appState: String? = null,
518520
context: Context,
519-
): CompletableFuture<Void> {
520-
val signMsgCF: CompletableFuture<Void> = CompletableFuture()
521+
): CompletableFuture<SignResponse> {
522+
signMsgCF = CompletableFuture()
523+
WebViewActivity.webViewResultCallback = this
521524
val sessionId = sessionManager.getSessionId()
522525
if (sessionId.isNotBlank()) {
523526
val sdkUrl = Uri.parse(web3AuthOption.walletSdkUrl)
@@ -558,9 +561,6 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
558561
intent.putExtra(WEBVIEW_URL, url.toString())
559562
intent.putExtra(REDIRECT_URL, web3AuthOption.redirectUrl.toString())
560563
context.startActivity(intent)
561-
runOnUIThread {
562-
signMsgCF.complete(null)
563-
}
564564
}
565565
}
566566
} else {
@@ -665,16 +665,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
665665
}
666666

667667
companion object {
668-
669-
private var signResponse: SignResponse? = null
670668
private var isCustomTabsClosed: Boolean = false
671-
fun setSignResponse(_response: SignResponse?) {
672-
signResponse = _response
673-
}
674-
675-
fun getSignResponse(): SignResponse? {
676-
return signResponse
677-
}
678669

679670
fun setCustomTabsClosed(_isCustomTabsClosed: Boolean) {
680671
isCustomTabsClosed = _isCustomTabsClosed
@@ -684,5 +675,11 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
684675
return isCustomTabsClosed
685676
}
686677
}
678+
679+
override fun onSignResponseReceived(signResponse: SignResponse?) {
680+
if (signResponse != null) {
681+
signMsgCF.complete(signResponse)
682+
}
683+
}
687684
}
688685

core/src/main/java/com/web3auth/core/WebViewActivity.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ import android.net.Uri
77
import android.os.Bundle
88
import android.os.Message
99
import android.view.ViewTreeObserver.OnScrollChangedListener
10-
import android.webkit.*
10+
import android.webkit.JavascriptInterface
11+
import android.webkit.WebChromeClient
12+
import android.webkit.WebSettings
13+
import android.webkit.WebView
14+
import android.webkit.WebViewClient
1115
import androidx.appcompat.app.AppCompatActivity
1216
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
1317
import com.google.gson.GsonBuilder
1418
import com.web3auth.core.types.REDIRECT_URL
1519
import com.web3auth.core.types.SignResponse
1620
import com.web3auth.core.types.WEBVIEW_URL
21+
import com.web3auth.core.types.WebViewResultCallback
1722

1823
class WebViewActivity : AppCompatActivity() {
1924

@@ -22,6 +27,10 @@ class WebViewActivity : AppCompatActivity() {
2227
private var mOnScrollChangedListener: OnScrollChangedListener? = null
2328
private val gson = GsonBuilder().disableHtmlEscaping().create()
2429

30+
companion object {
31+
var webViewResultCallback: WebViewResultCallback? = null
32+
}
33+
2534
@SuppressLint("SetJavaScriptEnabled")
2635
override fun onCreate(savedInstanceState: Bundle?) {
2736
super.onCreate(savedInstanceState)
@@ -46,7 +55,7 @@ class WebViewActivity : AppCompatActivity() {
4655
decodeBase64URLString(b64Params!!).toString(Charsets.UTF_8)
4756
val signResponse =
4857
gson.fromJson(b64ParamString, SignResponse::class.java)
49-
Web3Auth.setSignResponse(signResponse)
58+
webViewResultCallback?.onSignResponseReceived(signResponse)
5059
finish()
5160
}
5261
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.web3auth.core.types
2+
3+
interface WebViewResultCallback {
4+
fun onSignResponseReceived(signResponse: SignResponse?)
5+
}

0 commit comments

Comments
 (0)