Skip to content

Commit

Permalink
Add connect and disconnect wallet component
Browse files Browse the repository at this point in the history
  • Loading branch information
cristhianescobar committed Oct 31, 2023
1 parent 4bd758d commit e763852
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun Navigation(
onConnectWalletClick = onConnectWalletClick
)
addHomeTree(navController, isBottomBarVisible)
addProfileViewTree(onConnectWalletClick, onEditProfileClick)
addProfileViewTree(onEditProfileClick)
addLibraryTree(navController)
addMusicPlayerTree()
addBarcodeScannerTree()
Expand Down Expand Up @@ -88,15 +88,13 @@ private fun NavGraphBuilder.addLibraryTree(navController: NavHostController) {
}

private fun NavGraphBuilder.addProfileViewTree(
onConnectWalletClick: () -> Unit,
onEditProfileClick: () -> Unit
) {
navigation(
route = Screen.ProfileViewRoot.route, startDestination = Screen.ProfileViewLanding.route
) {
composable(Screen.ProfileViewLanding.route) {
ProfileViewScreen(
onConnectWalletClick = onConnectWalletClick,
onEditProfileClick = onEditProfileClick
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import co.touchlab.kermit.Logger
import io.newm.Logout
import io.newm.shared.models.User
import io.newm.shared.usecases.UserProfileUseCase
import io.newm.shared.usecases.WalletConnectUseCase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -14,6 +15,7 @@ import kotlinx.coroutines.launch

class ProfileReadOnlyViewModel(
private val userProviderUserCase: UserProfileUseCase,
private val walletConnectUseCase: WalletConnectUseCase,
private val logout: Logout
) : ViewModel() {

Expand All @@ -27,7 +29,11 @@ class ProfileReadOnlyViewModel(
viewModelScope.launch {
val user = userProviderUserCase.getCurrentUser()
Logger.d { "NewmAndroid - ProfileViewModel user: $user" }
_state.value = ProfileViewState.Content(profile = user)
_state.value = ProfileViewState
.Content(
profile = user,
isWalletConnected = walletConnectUseCase.isConnected()
)
}
}

Expand All @@ -36,9 +42,31 @@ class ProfileReadOnlyViewModel(
logout.call()
}
}

fun disconnectWallet() {
viewModelScope.launch(Dispatchers.IO) {
walletConnectUseCase.disconnect()
_state.value = ProfileViewState
.Content(
profile = (state.value as ProfileViewState.Content).profile,
isWalletConnected = false
)
}
}

fun connectWallet(xpubKey: String) {
viewModelScope.launch(Dispatchers.IO) {
walletConnectUseCase.connect(xpubKey)
_state.value = ProfileViewState
.Content(
profile = (state.value as ProfileViewState.Content).profile,
isWalletConnected = true
)
}
}
}

sealed class ProfileViewState {
object Loading : ProfileViewState()
data class Content(val profile: User) : ProfileViewState()
data class Content(val profile: User, val isWalletConnected: Boolean) : ProfileViewState()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.newm.screens.profile.view

import android.app.Activity
import android.content.Intent
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
Expand All @@ -9,13 +15,15 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import io.newm.core.ui.LoadingScreen
import io.newm.core.ui.buttons.PrimaryButton
import io.newm.core.ui.buttons.SecondaryButton
import io.newm.core.ui.text.formTextFieldStyle
import io.newm.core.ui.text.formTitleStyle
import io.newm.feature.barcode.scanner.BarcodeScannerActivity
import io.newm.screens.profile.ProfileBanner
import io.newm.shared.models.User
import org.koin.compose.koinInject
Expand All @@ -24,7 +32,6 @@ internal const val TAG_PROFILE_VIEW_SCREEN = "TAG_PROFILE_VIEW_SCREEN"

@Composable
fun ProfileViewScreen(
onConnectWalletClick: () -> Unit,
onEditProfileClick: () -> Unit,
viewModel: ProfileReadOnlyViewModel = koinInject()
) {
Expand All @@ -34,8 +41,10 @@ fun ProfileViewScreen(
is ProfileViewState.Content -> {
ProfileViewContent(
user = (state as ProfileViewState.Content).profile,
onConnectWalletClick = onConnectWalletClick,
isWalletConnected = (state as ProfileViewState.Content).isWalletConnected,
onConnectWalletClick = {xpubKey -> viewModel.connectWallet(xpubKey)},
onEditProfileClick = onEditProfileClick,
disconnectWallet = { viewModel.disconnectWallet() },
logout = { viewModel.logout() }
)
}
Expand All @@ -45,10 +54,27 @@ fun ProfileViewScreen(
@Composable
fun ProfileViewContent(
user: User,
onConnectWalletClick: () -> Unit,
isWalletConnected: Boolean,
onConnectWalletClick: (String) -> Unit,
onEditProfileClick: () -> Unit,
disconnectWallet: () -> Unit,
logout: () -> Unit,
) {
val context = LocalContext.current
// Setup the launcher with the contract and the callback
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()
) { result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
// Handle the returned result here
val data = result.data
// Do something with the data
val xpubKey = data?.getStringExtra(BarcodeScannerActivity.XPUB_KEY).orEmpty()
Toast.makeText(context, "Wallet connected $xpubKey", Toast.LENGTH_SHORT).show()
onConnectWalletClick(xpubKey)
}
}

Column(
modifier = Modifier
.fillMaxSize()
Expand Down Expand Up @@ -81,11 +107,24 @@ fun ProfileViewContent(
onClick = { logout() }
)

PrimaryButton(
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
text = "Connect Wallet",
onClick = { onConnectWalletClick() }
)

if (isWalletConnected) {
SecondaryButton(
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
text = "Disconnect Wallet",
onClick = {
disconnectWallet()
Toast.makeText(context, "Wallet has been disconnected.", Toast.LENGTH_SHORT).show()
}
)
} else {
PrimaryButton(
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
text = "Connect Wallet",
onClick = {
val intent = Intent(context, BarcodeScannerActivity::class.java)
launcher.launch(intent)
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,12 @@ class BarcodeScannerActivity : ComponentActivity() {

private fun onValidXpubKey(xpubKey: String) {
val resultIntent = Intent().apply {
putExtra("SCAN_RESULT", xpubKey)
putExtra(XPUB_KEY, xpubKey)
}
this.apply {
setResult(Activity.RESULT_OK, resultIntent)
finish()
}
Toast.makeText(this, "Barcode found", Toast.LENGTH_SHORT).show()
}

@Composable
Expand Down Expand Up @@ -215,6 +214,7 @@ class BarcodeScannerActivity : ComponentActivity() {
}

companion object {
const val XPUB_KEY = "XPUB_SCAN_RESULT_KEY"
private val TEST_XPUB_KEY =
"xpub1j6l5sgu597d72mu6tnzmrlt3mfv8d8qru2ys5gy4hf09g2v97ct8gslwcvkjyd8jkpefj226ccyw6al76af5hcf328myun6pwjl7wcgshjjxl"
}
Expand Down

0 comments on commit e763852

Please sign in to comment.