Skip to content

Commit ede4ef1

Browse files
committed
Add restart intro support
1 parent 3e0bdf6 commit ede4ef1

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed

app/src/main/java/com/canopas/campose/showcase/MainActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import com.canopas.campose.showcase.ui.theme.JetTapTargetTheme
4848
import com.canopas.campose.showcase.ui.theme.ThemeColor
4949
import com.canopas.lib.showcase.IntroShowcase
5050
import com.canopas.lib.showcase.IntroShowcaseScope
51+
import com.canopas.lib.showcase.component.IntroShowcaseManager
5152
import com.canopas.lib.showcase.component.ShowcaseStyle
5253

5354
class MainActivity : ComponentActivity() {
@@ -222,6 +223,14 @@ fun IntroShowcaseScope.BackButton() {
222223
color = Color.White,
223224
fontSize = 16.sp
224225
)
226+
227+
Button(
228+
onClick = {
229+
IntroShowcaseManager.introRestartHandler?.invoke()
230+
},
231+
) {
232+
Text(text = "Restart Intro")
233+
}
225234
}
226235
}
227236
},

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fun ShowcaseSample() {
3030
mutableStateOf(true)
3131
}
3232

33-
IntroShowCase(
33+
IntroShowcase(
3434
showIntroShowCase = showAppIntro,
3535
dismissOnClickOutside = false,
3636
onShowCaseCompleted = {

showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package com.canopas.lib.showcase
22

33
import androidx.compose.foundation.layout.BoxScope
44
import androidx.compose.runtime.Composable
5+
import androidx.compose.runtime.DisposableEffect
56
import androidx.compose.runtime.remember
67
import androidx.compose.ui.Modifier
8+
import com.canopas.lib.showcase.component.IntroShowcaseManager
79
import com.canopas.lib.showcase.component.IntroShowcaseState
810
import com.canopas.lib.showcase.component.ShowcasePopup
911
import com.canopas.lib.showcase.component.ShowcaseStyle
@@ -22,6 +24,15 @@ fun IntroShowcase(
2224
IntroShowcaseScope(state)
2325
}
2426

27+
DisposableEffect(Unit) {
28+
IntroShowcaseManager.registerRestoreHandler {
29+
state.currentTargetIndex = 0
30+
}
31+
onDispose {
32+
IntroShowcaseManager.registerRestoreHandler(null)
33+
}
34+
}
35+
2536
scope.content()
2637

2738
if (showIntroShowCase) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.canopas.lib.showcase.component
2+
3+
import com.canopas.lib.showcase.handler.IntroRestartHandler
4+
5+
/**
6+
* Manager class for IntroShowcase. Manages the registration and invocation of
7+
* [IntroRestartHandler] callbacks.
8+
*/
9+
object IntroShowcaseManager {
10+
/**
11+
* A nullable [IntroRestartHandler] that holds a callback function to be invoked when restarting
12+
* IntroShowcase. It allows external components to register custom logic to execute when
13+
* restarting the IntroShowcase, providing flexibility for handling restart events.
14+
*/
15+
var introRestartHandler: IntroRestartHandler? = null
16+
17+
/**
18+
* Register a [IntroRestartHandler] callback to be invoked when restarting the IntroShowcase.
19+
*/
20+
@JvmStatic
21+
internal fun registerRestoreHandler(introRestartHandler: IntroRestartHandler?) {
22+
this.introRestartHandler = introRestartHandler
23+
}
24+
}

showcase/src/main/java/com/canopas/lib/showcase/component/ShowCasestate.kt renamed to showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package com.canopas.lib.showcase.component
33
import androidx.compose.foundation.layout.BoxScope
44
import androidx.compose.runtime.Composable
55
import androidx.compose.runtime.getValue
6+
import androidx.compose.runtime.mutableIntStateOf
67
import androidx.compose.runtime.mutableStateMapOf
7-
import androidx.compose.runtime.mutableStateOf
88
import androidx.compose.runtime.remember
99
import androidx.compose.runtime.setValue
1010
import androidx.compose.ui.Modifier
@@ -47,13 +47,17 @@ internal fun Modifier.introShowcaseTarget(
4747
)
4848
}
4949

50+
/**
51+
* State class for managing the state of the IntroShowcase. Tracks the current target index and
52+
* associated targets.
53+
*/
5054
class IntroShowcaseState internal constructor(
5155
initialIndex: Int,
5256
) {
5357

5458
internal var targets = mutableStateMapOf<Int, IntroShowcaseTargets>()
5559

56-
var currentTargetIndex by mutableStateOf(initialIndex)
60+
var currentTargetIndex by mutableIntStateOf(initialIndex)
5761
internal set
5862

5963
val currentTarget: IntroShowcaseTargets?
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.canopas.lib.showcase.handler
2+
3+
/**
4+
* Interface for handling restart callbacks in IntroShowcase.
5+
*/
6+
fun interface IntroRestartHandler {
7+
operator fun invoke()
8+
}

0 commit comments

Comments
 (0)