Skip to content

Commit

Permalink
Add restart intro support
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Jan 3, 2024
1 parent 3e0bdf6 commit ede4ef1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.canopas.campose.showcase.ui.theme.JetTapTargetTheme
import com.canopas.campose.showcase.ui.theme.ThemeColor
import com.canopas.lib.showcase.IntroShowcase
import com.canopas.lib.showcase.IntroShowcaseScope
import com.canopas.lib.showcase.component.IntroShowcaseManager
import com.canopas.lib.showcase.component.ShowcaseStyle

class MainActivity : ComponentActivity() {
Expand Down Expand Up @@ -222,6 +223,14 @@ fun IntroShowcaseScope.BackButton() {
color = Color.White,
fontSize = 16.sp
)

Button(
onClick = {
IntroShowcaseManager.introRestartHandler?.invoke()
},
) {
Text(text = "Restart Intro")
}
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun ShowcaseSample() {
mutableStateOf(true)
}

IntroShowCase(
IntroShowcase(
showIntroShowCase = showAppIntro,
dismissOnClickOutside = false,
onShowCaseCompleted = {
Expand Down
11 changes: 11 additions & 0 deletions showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.canopas.lib.showcase

import androidx.compose.foundation.layout.BoxScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import com.canopas.lib.showcase.component.IntroShowcaseManager
import com.canopas.lib.showcase.component.IntroShowcaseState
import com.canopas.lib.showcase.component.ShowcasePopup
import com.canopas.lib.showcase.component.ShowcaseStyle
Expand All @@ -22,6 +24,15 @@ fun IntroShowcase(
IntroShowcaseScope(state)
}

DisposableEffect(Unit) {
IntroShowcaseManager.registerRestoreHandler {
state.currentTargetIndex = 0
}
onDispose {
IntroShowcaseManager.registerRestoreHandler(null)
}
}

scope.content()

if (showIntroShowCase) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.canopas.lib.showcase.component

import com.canopas.lib.showcase.handler.IntroRestartHandler

/**
* Manager class for IntroShowcase. Manages the registration and invocation of
* [IntroRestartHandler] callbacks.
*/
object IntroShowcaseManager {
/**
* A nullable [IntroRestartHandler] that holds a callback function to be invoked when restarting
* IntroShowcase. It allows external components to register custom logic to execute when
* restarting the IntroShowcase, providing flexibility for handling restart events.
*/
var introRestartHandler: IntroRestartHandler? = null

/**
* Register a [IntroRestartHandler] callback to be invoked when restarting the IntroShowcase.
*/
@JvmStatic
internal fun registerRestoreHandler(introRestartHandler: IntroRestartHandler?) {
this.introRestartHandler = introRestartHandler
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.canopas.lib.showcase.component
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -47,13 +47,17 @@ internal fun Modifier.introShowcaseTarget(
)
}

/**
* State class for managing the state of the IntroShowcase. Tracks the current target index and
* associated targets.
*/
class IntroShowcaseState internal constructor(
initialIndex: Int,
) {

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

var currentTargetIndex by mutableStateOf(initialIndex)
var currentTargetIndex by mutableIntStateOf(initialIndex)
internal set

val currentTarget: IntroShowcaseTargets?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.canopas.lib.showcase.handler

/**
* Interface for handling restart callbacks in IntroShowcase.
*/
fun interface IntroRestartHandler {
operator fun invoke()
}

0 comments on commit ede4ef1

Please sign in to comment.