Skip to content

Instantly share code, notes, and snippets.

View PiotrPrus's full-sized avatar
🏠
Working from home

Piotr Prus PiotrPrus

🏠
Working from home
View GitHub Profile
@PiotrPrus
PiotrPrus / MapBoxUtils.kt
Created July 6, 2022 14:24
Some mapbox functions needed to use mapBox in Compose
import android.os.Bundle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.mapbox.mapboxsdk.maps.MapView
import com.mapbox.mapboxsdk.maps.MapboxMap
kotlin {
android()
ios {
binaries {
framework {
baseName = "shared"
export(project(":sharedResources"))
export(Deps.Resources.core)
}
plugins {
kotlin("multiplatform")
id("com.android.library")
id("dev.icerock.mobile.multiplatform-resources")
}
kotlin {
android()
iosArm64()
iosX64()
val barAreas = list.mapIndexed { index, item ->
BarArea(
index = index,
data = item,
xStart = horizontalPadding + distance.times(index) - distance.div(2),
xEnd = horizontalPadding + distance.times(index) + distance.div(2)
)
}
@Test
fun cancelSelectionOfThirdElement() {
val list = mutableStateOf(listOf(1, 2, 3, 4, 5, 6, 7, 8))
val selectedItem = mutableStateOf(list.value.first())
val distance = with(composeTestRule.density) { (12.dp + 20.dp.times(3)).toPx() }
composeTestRule.setContent {
BarChartCanvas(list = list.value, barSelected = { selectedItem.value = it })
}
composeTestRule.onNodeWithTag(testTag = "BarChart")
.performGesture { down(Offset(distance, 1f)) }
@Test
fun clickOnThirdElementOfList() {
val list = mutableStateOf(listOf(1, 2, 3, 4, 5, 6, 7, 8))
val selectedItem = mutableStateOf(list.value.first())
val distance = with(composeTestRule.density) { (12.dp + 20.dp.times(3)).toPx() }
composeTestRule.setContent {
BarChartCanvas(list = list.value, barSelected = { selectedItem.value = it })
}
composeTestRule.onNodeWithTag(testTag = "BarChart")
.performGesture { click(position = Offset(distance, 1f)) }
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun checkCanvasExistence() {
val list = mutableStateOf(listOf(1, 2, 3, 4, 5, 6, 7, 8))
composeTestRule.setContent {
BarChartCanvas(list = list.value, barSelected = { })
}
composeTestRule.onNodeWithTag(testTag = "BarChart").assertExists()
Modifier.tapOrPress(
onStart = { position ->
scope.launch {
selectedBar?.let { selected ->
if (position in selected.xStart..selected.xEnd) {
// click in selected area - do nothing
} else {
tempPosition = position
scope.launch {
tempAnimatable.snapTo(0f)
val animatable = remember { Animatable(0f) }
...
//Modifier.tapOrPress
onCompleted = {
scope.launch {
selectedPosition = it
animatable.snapTo(1f)
}
}
...
var selectedPosition by remember { mutableStateOf(0) }
val selectedBar by remember(selectedPosition, barAreas) {
derivedStateOf {
barAreas.find { it.xStart < selectedPosition && selectedPosition < it.xEnd }
}
}
Modifier.tapOrPress(
onStart = { },
onCancel = { },