Skip to content

Commit

Permalink
fix: click modifier not working after config change (#130)
Browse files Browse the repository at this point in the history
* chore: add example app for easier debugging

* fix: click modifier not working after config change (issue #127)
  • Loading branch information
svenjacobs authored May 24, 2024
1 parent 18f3b57 commit 5da581a
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.iml
.gradle
.idea
!.idea/runConfigurations
.DS_Store
build
captures
Expand Down
2 changes: 2 additions & 0 deletions android-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ android {

dependencies {
implementation(project(":reveal-core"))
implementation(project(":reveal-shapes"))

val composeBom = platform(libs.androidx.compose.bom)
implementation(composeBom)
Expand All @@ -59,6 +60,7 @@ dependencies {
implementation(libs.androidx.compose.animation)
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.activity.compose)

debugImplementation(libs.androidx.compose.ui.test.manifest)

Expand Down
19 changes: 18 additions & 1 deletion android-tests/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest />
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name=".App"
android:allowBackup="false"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.svenjacobs.reveal.android.tests

import android.app.Application
import android.util.Log
import com.svenjacobs.reveal.common.internal.log.Logger

class App : Application() {

override fun onCreate() {
super.onCreate()

Logger.adapter = Logger.Adapter { message, tag -> Log.d(tag, message) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.svenjacobs.reveal.android.tests

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.core.view.WindowCompat
import com.svenjacobs.reveal.android.tests.presentation.App

class MainActivity : ComponentActivity() {

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

WindowCompat.setDecorFitsSystemWindows(window, false)

setContent {
App()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.svenjacobs.reveal.android.tests.presentation

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.svenjacobs.reveal.RevealCanvas
import com.svenjacobs.reveal.android.tests.presentation.theme.AppTheme
import com.svenjacobs.reveal.rememberRevealCanvasState

@Composable
fun App(modifier: Modifier = Modifier) {
AppTheme {
val revealCanvasState = rememberRevealCanvasState()

RevealCanvas(
revealCanvasState = revealCanvasState,
modifier = modifier,
) {
MainScreen(revealCanvasState = revealCanvasState)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.svenjacobs.reveal.android.tests.presentation

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.svenjacobs.reveal.Key
import com.svenjacobs.reveal.Reveal
import com.svenjacobs.reveal.RevealCanvasState
import com.svenjacobs.reveal.RevealOverlayArrangement
import com.svenjacobs.reveal.RevealOverlayScope
import com.svenjacobs.reveal.RevealShape
import com.svenjacobs.reveal.rememberRevealState
import com.svenjacobs.reveal.shapes.balloon.Arrow
import com.svenjacobs.reveal.shapes.balloon.Balloon
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

private enum class Keys { Fab, Explanation }

@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun MainScreen(revealCanvasState: RevealCanvasState, modifier: Modifier = Modifier) {
val scope = rememberCoroutineScope()
val revealState = rememberRevealState()

LaunchedEffect(Unit) {
if (revealState.isVisible) return@LaunchedEffect
delay(2.seconds)
revealState.reveal(Keys.Fab)
}

Reveal(
onOverlayClick = { scope.launch { revealState.hide() } },
modifier = modifier,
revealCanvasState = revealCanvasState,
revealState = revealState,
overlayContent = { key -> RevealOverlayContent(key) },
) {
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
CenterAlignedTopAppBar(
title = { Text("Reveal Demo") },
)
},
floatingActionButton = {
FloatingActionButton(
modifier = Modifier.revealable(
key = Keys.Fab,
shape = RevealShape.RoundRect(16.dp),
onClick = {
scope.launch { revealState.reveal(Keys.Explanation) }
},
),
onClick = {
scope.launch { revealState.reveal(Keys.Explanation) }
},
) {
Icon(
Icons.Filled.Add,
contentDescription = null,
)
}
},
) { contentPadding ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(contentPadding)
.padding(horizontal = 16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Text(
modifier = Modifier
.padding(top = 16.dp)
.revealable(
key = Keys.Explanation,
onClick = {
scope.launch { revealState.hide() }
},
),
text = "Reveal is a lightweight, simple reveal effect (also known as " +
"coach mark or onboarding) library for Compose Multiplatform.",
style = MaterialTheme.typography.bodyLarge,
textAlign = TextAlign.Justify,
)
}
}
}
}

@Composable
private fun RevealOverlayScope.RevealOverlayContent(key: Key) {
when (key) {
Keys.Fab -> OverlayText(
modifier = Modifier.align(
horizontalArrangement = RevealOverlayArrangement.Start,
),
text = "Click button to get started",
arrow = Arrow.end(),
)

Keys.Explanation -> OverlayText(
modifier = Modifier.align(
verticalArrangement = RevealOverlayArrangement.Bottom,
),
text = "Actually we already started. This was an example of the reveal effect.",
arrow = Arrow.top(),
)
}
}

@Composable
private fun OverlayText(text: String, arrow: Arrow, modifier: Modifier = Modifier) {
Balloon(
modifier = modifier.padding(8.dp),
arrow = arrow,
backgroundColor = MaterialTheme.colorScheme.secondaryContainer,
elevation = 2.dp,
) {
Text(
modifier = Modifier.padding(8.dp),
text = text,
style = MaterialTheme.typography.labelLarge,
textAlign = TextAlign.Center,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.svenjacobs.reveal.android.tests.presentation.theme

import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable

@Composable
fun AppTheme(content: @Composable () -> Unit) {
MaterialTheme(
content = content,
)
}
Empty file.
8 changes: 8 additions & 0 deletions android-tests/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<resources xmlns:tools="http://schemas.android.com/tools">

<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.svenjacobs.reveal.common.internal.log

/**
* Minimal logging adapter for development purposes.
* For internal use only.
*/
public object Logger {

public fun interface Adapter {
public fun d(message: String, tag: String)
}

public var adapter: Adapter? = null

public fun d(message: String, tag: String = "Reveal") {
adapter?.d(message, tag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public fun Reveal(
val rev by rememberUpdatedState(currentRevealable.value)

val clickModifier = when {
revealState.isVisible && rev != null -> Modifier.pointerInput(Unit) {
revealState.isVisible -> Modifier.pointerInput(Unit) {
detectTapGestures(
onPress = { offset ->
rev?.key?.let(
Expand Down

0 comments on commit 5da581a

Please sign in to comment.