Skip to content

Commit

Permalink
Restore Sample Dapp state
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Sep 13, 2024
1 parent 029ac6e commit 886949a
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,12 @@ fun WCTopAppBarLegacy(
),
@DrawableRes icon: Int? = null,
onIconClick: (() -> Unit)? = null,
onBackIconClick: (() -> Unit)? = null,
) {
Row(
modifier = modifier,
verticalAlignment = Alignment.CenterVertically
) {
Spacer(modifier = Modifier.width(8.dp))
onBackIconClick?.let {
Icon(
tint = Color(0xFF3496ff),
imageVector = ImageVector.vectorResource(id = R.drawable.chevron_left),
contentDescription = "BackArrow",
modifier = Modifier.clickable { onBackIconClick() }
)
Spacer(modifier = Modifier.width(32.dp))
}
Row(
modifier = Modifier
.fillMaxWidth()
Expand Down
3 changes: 0 additions & 3 deletions sample/dapp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,11 @@ dependencies {

debugImplementation(project(":core:android"))
debugImplementation(project(":product:appkit"))
debugImplementation(project(":protocol:sign"))

internalImplementation(project(":core:android"))
internalImplementation(project(":product:appkit"))
internalImplementation(project(":protocol:sign"))

releaseImplementation(platform("com.reown:android-bom:$BOM_VERSION"))
releaseImplementation("com.reown:android-core")
releaseImplementation("com.reown:appkit")
releaseImplementation("com.reown:sign")
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import androidx.activity.compose.setContent
import androidx.compose.material.ExperimentalMaterialApi
import androidx.lifecycle.lifecycleScope
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
import com.reown.appkit.client.AppKit
import com.reown.sample.common.ui.theme.WCSampleAppTheme
import com.reown.sample.dapp.ui.routes.host.DappSampleHost
import com.reown.sign.client.SignClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class DappSampleActivity : ComponentActivity() {
@ExperimentalMaterialNavigationApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent() {
setContent {
WCSampleAppTheme {
DappSampleHost()
}
}

if (intent?.dataString?.contains("wc_ev") == true) {
SignClient.dispatchEnvelope(intent.dataString ?: "") {
AppKit.handleDeepLink(intent.dataString ?: "") {
lifecycleScope.launch(Dispatchers.Main) {
Toast.makeText(this@DappSampleActivity, "Error dispatching envelope: ${it.throwable.message}", Toast.LENGTH_SHORT).show()
}
Expand All @@ -39,7 +39,7 @@ class DappSampleActivity : ComponentActivity() {
super.onNewIntent(intent)

if (intent?.dataString?.contains("wc_ev") == true) {
SignClient.dispatchEnvelope(intent.dataString ?: "") {
AppKit.handleDeepLink(intent.dataString ?: "") {
lifecycleScope.launch(Dispatchers.Main) {
Toast.makeText(this@DappSampleActivity, "Error dispatching envelope: ${it.throwable.message}", Toast.LENGTH_SHORT).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.reown.sample.dapp.ui.routes

sealed class Route(val path: String) {
object ChainSelection : Route("chain_selection")
object ParingGeneration : Route("paring_generation")
object Session : Route("session")
object Account : Route("account")
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ fun AccountRoute(navController: NavController) {
Toast.makeText(context, "Error: ${event.exceptionMsg}", Toast.LENGTH_LONG).show()
}

is DappSampleEvents.Disconnect -> navController.popBackStack(Route.ChainSelection.path, false)
is DappSampleEvents.Disconnect -> navController.navigate(Route.ChainSelection.path) {
popUpTo(navController.graph.startDestinationId) {
inclusive = true
}
}

else -> Unit
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,12 @@ private fun handleSignEvents(
when (event) {
DappSampleEvents.SessionApproved -> {
viewModel.awaitingProposalResponse(false)
navController.navigate(Route.Session.path)
navController.navigate(Route.Session.path) {
popUpTo(0) {
inclusive = true
}
launchSingleTop = true
}
}

DappSampleEvents.SessionRejected -> {
Expand All @@ -482,7 +487,12 @@ private fun handleSignEvents(
if (event.message != null) {
Toast.makeText(context, event.message, Toast.LENGTH_SHORT).show()
} else {
navController.navigate(Route.Session.path)
navController.navigate(Route.Session.path) {
popUpTo(0) {
inclusive = true
}
launchSingleTop = true
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import com.reown.sample.common.Chains
import com.reown.sample.common.tag
import com.reown.sample.dapp.domain.DappDelegate
import com.reown.sample.dapp.ui.DappSampleEvents
import com.reown.sign.client.Sign
import com.reown.sign.client.SignClient
import com.reown.util.bytesToHex
import com.reown.util.randomBytes
import com.reown.appkit.client.AppKit
Expand Down Expand Up @@ -72,12 +70,12 @@ class ChainSelectionViewModel : ViewModel() {
}
}

fun authenticate(authenticateParams: Sign.Params.Authenticate, appLink: String = "", onAuthenticateSuccess: (String?) -> Unit, onError: (String) -> Unit = {}) {
fun authenticate(authenticateParams: Modal.Params.Authenticate, appLink: String = "", onAuthenticateSuccess: (String?) -> Unit, onError: (String) -> Unit = {}) {
viewModelScope.launch {
_awaitingProposalSharedFlow.emit(true)
}

SignClient.authenticate(authenticateParams, walletAppLink = appLink,
AppKit.authenticate(authenticateParams, walletAppLink = appLink,
onSuccess = { url ->
viewModelScope.launch {
_awaitingProposalSharedFlow.emit(false)
Expand Down Expand Up @@ -190,7 +188,7 @@ class ChainSelectionViewModel : ViewModel() {


val authenticateParams
get() = Sign.Params.Authenticate(
get() = Modal.Params.Authenticate(
chains = uiState.value.filter { it.isSelected }.map { it.chainId },
domain = "sample.kotlin.dapp",
uri = "https://web3inbox.com/all-apps",
Expand All @@ -208,7 +206,7 @@ class ChainSelectionViewModel : ViewModel() {
)

val siweParams
get() = Sign.Params.Authenticate(
get() = Modal.Params.Authenticate(
chains = uiState.value.filter { it.isSelected }.map { it.chainId },
domain = "sample.kotlin.dapp",
uri = "https://web3inbox.com/all-apps",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ fun SessionRoute(
is DappSampleEvents.PingLoading -> isPingLoading = true
is DappSampleEvents.Disconnect -> {
isDisconnectLoading = false
navController.popBackStack(Route.ChainSelection.path, inclusive = false)
navController.navigate(Route.ChainSelection.path) {
popUpTo(navController.graph.startDestinationId) {
inclusive = true
}
}
Toast.makeText(context, "Disconnected successfully", Toast.LENGTH_SHORT).show()
}

Expand All @@ -99,7 +103,6 @@ fun SessionRoute(

SessionScreen(
uiState = state,
onBackPressed = navController::popBackStack,
onSessionClick = navController::navigateToAccount,
onPingClick = viewModel::ping,
onDisconnectClick = viewModel::disconnect,
Expand All @@ -111,18 +114,14 @@ fun SessionRoute(
@Composable
private fun SessionScreen(
uiState: List<SessionUi>,
onBackPressed: () -> Unit,
onSessionClick: (String) -> Unit,
onPingClick: () -> Unit,
onDisconnectClick: () -> Unit,
isDisconnectLoading: Boolean,
isPingLoading: Boolean,
) {
Column {
WCTopAppBarLegacy(
titleText = "Session Chains",
onBackIconClick = onBackPressed,
)
WCTopAppBarLegacy(titleText = "Session Chains",)
ChainsAction(onPingClick, onDisconnectClick, isDisconnectLoading, isPingLoading)
LazyColumn(modifier = Modifier.fillMaxSize()) {
item {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,26 @@ class SessionViewModel : ViewModel() {
}

fun disconnect() {
if (DappDelegate.selectedSessionTopic != null) {
try {
viewModelScope.launch { _sessionEvents.emit(DappSampleEvents.DisconnectLoading) }
AppKit.disconnect(
onSuccess = {
DappDelegate.deselectAccountDetails()
viewModelScope.launch {
_sessionEvents.emit(DappSampleEvents.Disconnect)
}
},
onError = { throwable: Throwable ->
Timber.tag(tag(this)).e(throwable.stackTraceToString())
Firebase.crashlytics.recordException(throwable)
viewModelScope.launch {
_sessionEvents.emit(DappSampleEvents.DisconnectError(throwable.message ?: "Unknown error, please try again or contact support"))
}
})
try {
viewModelScope.launch { _sessionEvents.emit(DappSampleEvents.DisconnectLoading) }
AppKit.disconnect(
onSuccess = {
DappDelegate.deselectAccountDetails()
viewModelScope.launch {
_sessionEvents.emit(DappSampleEvents.Disconnect)
}
},
onError = { throwable: Throwable ->
Timber.tag(tag(this)).e(throwable.stackTraceToString())
Firebase.crashlytics.recordException(throwable)
viewModelScope.launch {
_sessionEvents.emit(DappSampleEvents.DisconnectError(throwable.message ?: "Unknown error, please try again or contact support"))
}
})

} catch (e: Exception) {
viewModelScope.launch {
_sessionEvents.emit(DappSampleEvents.DisconnectError(e.message ?: "Unknown error, please try again or contact support"))
}
} catch (e: Exception) {
viewModelScope.launch {
_sessionEvents.emit(DappSampleEvents.DisconnectError(e.message ?: "Unknown error, please try again or contact support"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.navigation.material.BottomSheetNavigator
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
import com.reown.appkit.client.AppKit
import com.reown.sample.dapp.R
import com.reown.sample.dapp.ui.DappSampleEvents
import com.reown.sample.dapp.ui.DappSampleNavGraph
Expand All @@ -48,15 +49,22 @@ fun DappSampleHost() {
initialValue = ModalBottomSheetValue.Hidden,
skipHalfExpanded = true
)

val bottomSheetNavigator = BottomSheetNavigator(sheetState)
val navController = rememberNavController(bottomSheetNavigator)
val viewModel: DappSampleViewModel = viewModel()
val isConnected = AppKit.getAccount() != null
val startDestination = if (isConnected) Route.Session.path else Route.ChainSelection.path

LaunchedEffect(Unit) {
viewModel.events.collect { event ->
when (event) {
is DappSampleEvents.ConnectionEvent -> isOfflineState = !event.isAvailable
is DappSampleEvents.Disconnect -> navController.navigate(Route.ChainSelection.path)
is DappSampleEvents.Disconnect -> navController.navigate(Route.ChainSelection.path) {
popUpTo(navController.graph.startDestinationId) {
inclusive = true
}
}
is DappSampleEvents.RequestError -> scaffoldState.snackbarHostState.showSnackbar(event.exceptionMsg)
is DappSampleEvents.SessionExtend -> scaffoldState.snackbarHostState.showSnackbar("Session extended")
else -> Unit
Expand All @@ -71,7 +79,7 @@ fun DappSampleHost() {
DappSampleNavGraph(
bottomSheetNavigator = bottomSheetNavigator,
navController = navController,
startDestination = Route.ChainSelection.path,
startDestination = startDestination,
)

if (isOfflineState != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ class WalletKitActivity : AppCompatActivity() {
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)

println("kobe: WalletKitActivity.newIntent ${intent?.dataString}")
handleAppLink(intent)
}

Expand Down

0 comments on commit 886949a

Please sign in to comment.