From ede4ef193b3d1e4f61971a85f4dc9cc5ac35cae7 Mon Sep 17 00:00:00 2001 From: cp-megh Date: Wed, 3 Jan 2024 11:10:24 +0530 Subject: [PATCH 1/3] Add restart intro support --- .../canopas/campose/showcase/MainActivity.kt | 9 +++++++ docs/index.md | 2 +- .../com/canopas/lib/showcase/IntroShowcase.kt | 11 +++++++++ .../component/IntroShowcaseManager.kt | 24 +++++++++++++++++++ ...ShowCasestate.kt => IntroShowcaseState.kt} | 8 +++++-- .../showcase/handler/IntroRestartHandler.kt | 8 +++++++ 6 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseManager.kt rename showcase/src/main/java/com/canopas/lib/showcase/component/{ShowCasestate.kt => IntroShowcaseState.kt} (87%) create mode 100644 showcase/src/main/java/com/canopas/lib/showcase/handler/IntroRestartHandler.kt diff --git a/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt b/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt index 154043e..2fe1bf0 100644 --- a/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt +++ b/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt @@ -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() { @@ -222,6 +223,14 @@ fun IntroShowcaseScope.BackButton() { color = Color.White, fontSize = 16.sp ) + + Button( + onClick = { + IntroShowcaseManager.introRestartHandler?.invoke() + }, + ) { + Text(text = "Restart Intro") + } } } }, diff --git a/docs/index.md b/docs/index.md index 149f5c5..1898c5d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -30,7 +30,7 @@ fun ShowcaseSample() { mutableStateOf(true) } - IntroShowCase( + IntroShowcase( showIntroShowCase = showAppIntro, dismissOnClickOutside = false, onShowCaseCompleted = { diff --git a/showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt b/showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt index c8199c0..4f65d42 100644 --- a/showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt +++ b/showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt @@ -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 @@ -22,6 +24,15 @@ fun IntroShowcase( IntroShowcaseScope(state) } + DisposableEffect(Unit) { + IntroShowcaseManager.registerRestoreHandler { + state.currentTargetIndex = 0 + } + onDispose { + IntroShowcaseManager.registerRestoreHandler(null) + } + } + scope.content() if (showIntroShowCase) { diff --git a/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseManager.kt b/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseManager.kt new file mode 100644 index 0000000..91f2c72 --- /dev/null +++ b/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseManager.kt @@ -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 + } +} \ No newline at end of file diff --git a/showcase/src/main/java/com/canopas/lib/showcase/component/ShowCasestate.kt b/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt similarity index 87% rename from showcase/src/main/java/com/canopas/lib/showcase/component/ShowCasestate.kt rename to showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt index 2708112..6d158e9 100644 --- a/showcase/src/main/java/com/canopas/lib/showcase/component/ShowCasestate.kt +++ b/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt @@ -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 @@ -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() - var currentTargetIndex by mutableStateOf(initialIndex) + var currentTargetIndex by mutableIntStateOf(initialIndex) internal set val currentTarget: IntroShowcaseTargets? diff --git a/showcase/src/main/java/com/canopas/lib/showcase/handler/IntroRestartHandler.kt b/showcase/src/main/java/com/canopas/lib/showcase/handler/IntroRestartHandler.kt new file mode 100644 index 0000000..5142905 --- /dev/null +++ b/showcase/src/main/java/com/canopas/lib/showcase/handler/IntroRestartHandler.kt @@ -0,0 +1,8 @@ +package com.canopas.lib.showcase.handler + +/** + * Interface for handling restart callbacks in IntroShowcase. + */ +fun interface IntroRestartHandler { + operator fun invoke() +} \ No newline at end of file From ed3199205273cbf88ebd2b66e871d49d39afce68 Mon Sep 17 00:00:00 2001 From: cp-megh Date: Wed, 3 Jan 2024 11:43:13 +0530 Subject: [PATCH 2/3] Revert changes and add reset through state --- .../canopas/campose/showcase/MainActivity.kt | 13 ++++++---- .../com/canopas/lib/showcase/IntroShowcase.kt | 11 --------- .../component/IntroShowcaseManager.kt | 24 ------------------- .../showcase/component/IntroShowcaseState.kt | 7 ++++++ .../showcase/handler/IntroRestartHandler.kt | 8 ------- 5 files changed, 16 insertions(+), 47 deletions(-) delete mode 100644 showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseManager.kt delete mode 100644 showcase/src/main/java/com/canopas/lib/showcase/handler/IntroRestartHandler.kt diff --git a/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt b/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt index 2fe1bf0..b6b378f 100644 --- a/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt +++ b/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt @@ -48,8 +48,9 @@ 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.IntroShowcaseState import com.canopas.lib.showcase.component.ShowcaseStyle +import com.canopas.lib.showcase.component.rememberIntroShowcaseState class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -76,6 +77,8 @@ fun ShowcaseSample() { mutableStateOf(true) } + val introShowcaseState = rememberIntroShowcaseState() + IntroShowcase( showIntroShowCase = showAppIntro, dismissOnClickOutside = false, @@ -83,6 +86,7 @@ fun ShowcaseSample() { //App Intro finished!! showAppIntro = false }, + state = introShowcaseState, ) { Scaffold( modifier = Modifier.fillMaxSize(), @@ -92,7 +96,7 @@ fun ShowcaseSample() { backgroundColor = Color.Transparent, elevation = 0.dp, navigationIcon = { - BackButton() + BackButton(introShowcaseState) }, actions = { IconButton( @@ -192,7 +196,7 @@ fun IntroShowcaseScope.FloatingMailButton() { } @Composable -fun IntroShowcaseScope.BackButton() { +fun IntroShowcaseScope.BackButton(introShowcaseState: IntroShowcaseState) { IconButton( onClick = {}, modifier = Modifier.introShowCaseTarget( @@ -226,7 +230,8 @@ fun IntroShowcaseScope.BackButton() { Button( onClick = { - IntroShowcaseManager.introRestartHandler?.invoke() + // Used to restart the intro showcase + introShowcaseState.resetState() }, ) { Text(text = "Restart Intro") diff --git a/showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt b/showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt index 4f65d42..c8199c0 100644 --- a/showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt +++ b/showcase/src/main/java/com/canopas/lib/showcase/IntroShowcase.kt @@ -2,10 +2,8 @@ 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 @@ -24,15 +22,6 @@ fun IntroShowcase( IntroShowcaseScope(state) } - DisposableEffect(Unit) { - IntroShowcaseManager.registerRestoreHandler { - state.currentTargetIndex = 0 - } - onDispose { - IntroShowcaseManager.registerRestoreHandler(null) - } - } - scope.content() if (showIntroShowCase) { diff --git a/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseManager.kt b/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseManager.kt deleted file mode 100644 index 91f2c72..0000000 --- a/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseManager.kt +++ /dev/null @@ -1,24 +0,0 @@ -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 - } -} \ No newline at end of file diff --git a/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt b/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt index 6d158e9..2f7b494 100644 --- a/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt +++ b/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt @@ -62,4 +62,11 @@ class IntroShowcaseState internal constructor( val currentTarget: IntroShowcaseTargets? get() = targets[currentTargetIndex] + + /** + * Resets the state to its initial values, effectively restarting the showcase. + */ + fun resetState() { + currentTargetIndex = 0 + } } diff --git a/showcase/src/main/java/com/canopas/lib/showcase/handler/IntroRestartHandler.kt b/showcase/src/main/java/com/canopas/lib/showcase/handler/IntroRestartHandler.kt deleted file mode 100644 index 5142905..0000000 --- a/showcase/src/main/java/com/canopas/lib/showcase/handler/IntroRestartHandler.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.canopas.lib.showcase.handler - -/** - * Interface for handling restart callbacks in IntroShowcase. - */ -fun interface IntroRestartHandler { - operator fun invoke() -} \ No newline at end of file From 368d5bc662abae9ebf8971e19475c76fc667d540 Mon Sep 17 00:00:00 2001 From: cp-megh Date: Wed, 3 Jan 2024 12:38:47 +0530 Subject: [PATCH 3/3] Rename function --- app/src/main/java/com/canopas/campose/showcase/MainActivity.kt | 2 +- .../com/canopas/lib/showcase/component/IntroShowcaseState.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt b/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt index b6b378f..661471f 100644 --- a/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt +++ b/app/src/main/java/com/canopas/campose/showcase/MainActivity.kt @@ -231,7 +231,7 @@ fun IntroShowcaseScope.BackButton(introShowcaseState: IntroShowcaseState) { Button( onClick = { // Used to restart the intro showcase - introShowcaseState.resetState() + introShowcaseState.reset() }, ) { Text(text = "Restart Intro") diff --git a/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt b/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt index 2f7b494..7237b1a 100644 --- a/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt +++ b/showcase/src/main/java/com/canopas/lib/showcase/component/IntroShowcaseState.kt @@ -66,7 +66,7 @@ class IntroShowcaseState internal constructor( /** * Resets the state to its initial values, effectively restarting the showcase. */ - fun resetState() { + fun reset() { currentTargetIndex = 0 } }