Skip to content

Commit

Permalink
V2 QR Code (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang authored Jun 15, 2023
1 parent 095f5de commit 3d9edf0
Show file tree
Hide file tree
Showing 16 changed files with 832 additions and 341 deletions.
3 changes: 0 additions & 3 deletions .idea/.gitignore

This file was deleted.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ dependencies {
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'

implementation 'com.github.kenglxn.QRGen:android:3.0.1'

}
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@

<activity
android:name="exchange.dydx.carteraexample.MainActivity"
android:launchMode="singleTask"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

<data android:scheme="app"
android:host="kotlin-dapp-wc"
android:pathPrefix="/request" />
</intent-filter>
</activity>
</application>
Expand Down
20 changes: 12 additions & 8 deletions app/src/main/java/exchange/dydx/carteraExample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package exchange.dydx.carteraexample

import android.content.Intent
import android.app.Application
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import exchange.dydx.cartera.CarteraConfig


class MainActivity : ComponentActivity() {

private lateinit var launcher: ActivityResultLauncher<Intent>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
val uri = result.data?.data ?: return@registerForActivityResult
CarteraConfig.handleResponse(uri)
}

CarteraConfig.shared = CarteraConfig(
walletProvidersConfig = WalletProvidersConfigUtil.getWalletProvidersConfig(),
application = applicationContext as Application,
launcher = launcher
)
CarteraConfig.shared?.registerWallets(context = applicationContext)

setContent {
MyApp {
WalletList.Content(launcher)
WalletList.Content()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package exchange.dydx.carteraexample

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex

object ModalTransitionDialog {
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ModalTransitionDialog(
onDismissRequest: () -> Unit,
dismissOnBackPress: Boolean = true,
content: @Composable (ModalTransitionDialogHelper) -> Unit
) {

val onCloseSharedFlow: MutableSharedFlow<Unit> = remember { MutableSharedFlow() }
val coroutineScope: CoroutineScope = rememberCoroutineScope()
val animateContentBackTrigger = remember { mutableStateOf(false) }
val mutex = remember { Mutex(true) }

LaunchedEffect(key1 = Unit) {
launch {
//delay(DIALOG_BUILD_TIME)
animateContentBackTrigger.value = true
}
launch {
onCloseSharedFlow.asSharedFlow().collectLatest { startDismissWithExitAnimation(animateContentBackTrigger, onDismissRequest) }
}
}

Dialog(
onDismissRequest = { coroutineScope.launch { startDismissWithExitAnimation(animateContentBackTrigger, onDismissRequest) } },
properties = DialogProperties(usePlatformDefaultWidth = false, dismissOnBackPress = dismissOnBackPress, dismissOnClickOutside = false)
) {
LaunchedEffect(key1 = Unit) {
if (mutex.isLocked) mutex.unlock()
}

AnimatedModalBottomSheetTransition(visible = animateContentBackTrigger.value) {
content(ModalTransitionDialogHelper(coroutineScope, onCloseSharedFlow))
}
}
}

private suspend fun startDismissWithExitAnimation(
animateContentBackTrigger: MutableState<Boolean>,
onDismissRequest: () -> Unit
) {
animateContentBackTrigger.value = false
delay(ANIMATION_TIME)
onDismissRequest()
}

/**
* Helper class that can be used inside the content scope from
* composables that implement the [ModalTransitionDialog] to hide
* the [Dialog] with a modal transition animation
*/
class ModalTransitionDialogHelper(
private val coroutineScope: CoroutineScope,
private val onCloseFlow: MutableSharedFlow<Unit>
) {
fun triggerAnimatedClose() {
coroutineScope.launch {
onCloseFlow.emit(Unit)
}
}
}

internal const val ANIMATION_TIME = 500L

@Composable
internal fun AnimatedModalBottomSheetTransition(
visible: Boolean,
content: @Composable AnimatedVisibilityScope.() -> Unit
) {
AnimatedVisibility(
visible = visible,
enter = slideInVertically(
animationSpec = tween(ANIMATION_TIME.toInt()),
initialOffsetY = { fullHeight -> fullHeight }
),
exit = slideOutVertically(
animationSpec = tween(ANIMATION_TIME.toInt()),
targetOffsetY = { fullHeight -> fullHeight }
),
content = content
)
}
}

Loading

0 comments on commit 3d9edf0

Please sign in to comment.