Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dynamic tutorials #118

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ dependencies {
implementation deps.kotlin.stdlib
implementation deps.androidx.appcompat
implementation deps.androidx.constraintLayout
implementation deps.androidx.cardview
implementation deps.androidx.recyclerview
implementation project(':spotlight')
debugImplementation deps.leakcanary
}
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
android:name=".FragmentSampleActivity"
android:theme="@style/AppTheme.Demo"
/>
<activity
android:name=".MultiFragmentSampleActivity"
android:theme="@style/AppTheme.Demo"
/>
<activity
android:name=".hiddentarget.HiddenTargetSampleActivity"
android:theme="@style/AppTheme.Demo"
/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.takusemba.spotlight.OnTargetListener
import com.takusemba.spotlight.Spotlight
import com.takusemba.spotlight.Target
import com.takusemba.spotlight.shape.Circle
import com.takusemba.spotlight.shape.DynamicShape

class ActivitySampleActivity : AppCompatActivity(R.layout.activity_activity_sample) {

Expand All @@ -27,9 +28,10 @@ class ActivitySampleActivity : AppCompatActivity(R.layout.activity_activity_samp
// first target
val firstRoot = FrameLayout(this)
val first = layoutInflater.inflate(R.layout.layout_target, firstRoot)
val view = findViewById<View>(R.id.one)
val firstTarget = Target.Builder()
.setAnchor(findViewById<View>(R.id.one))
.setShape(Circle(100f))
.setAnchor(view)
.setShape(DynamicShape(view))
.setOverlay(first)
.setOnTargetListener(object : OnTargetListener {
override fun onStarted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import com.takusemba.spotlightsample.hiddentarget.HiddenTargetSampleActivity

class ChooserActivity : AppCompatActivity(R.layout.activity_chooser) {

private val samples: Array<String> = arrayOf(
SAMPLE_SPOTLIGHT_ON_ACTIVITY,
SAMPLE_SPOTLIGHT_ON_FRAGMENT
SAMPLE_SPOTLIGHT_ON_FRAGMENT,
SAMPLE_SPOTLIGHT_ON_MULTI_FRAGMENT_ACTIVITY,
SAMPLE_SPOTLIGHT_ON_A_RECYCLER_VIEW_ACTIVITY
)

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -28,12 +31,22 @@ class ChooserActivity : AppCompatActivity(R.layout.activity_chooser) {
val intent = Intent(this, FragmentSampleActivity::class.java)
startActivity(intent)
}
SAMPLE_SPOTLIGHT_ON_MULTI_FRAGMENT_ACTIVITY -> {
val intent = Intent(this, MultiFragmentSampleActivity::class.java)
startActivity(intent)
}
SAMPLE_SPOTLIGHT_ON_A_RECYCLER_VIEW_ACTIVITY -> {
val intent = Intent(this, HiddenTargetSampleActivity::class.java)
startActivity(intent)
}
}
}
}

companion object {
private const val SAMPLE_SPOTLIGHT_ON_ACTIVITY = "Spotlight on Activity"
private const val SAMPLE_SPOTLIGHT_ON_FRAGMENT = "Spotlight on Fragment"
private const val SAMPLE_SPOTLIGHT_ON_MULTI_FRAGMENT_ACTIVITY = "Spotlight on Multi Fragment Activity"
private const val SAMPLE_SPOTLIGHT_ON_A_RECYCLER_VIEW_ACTIVITY = "Spotlight on a Recycler View Activity"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.takusemba.spotlightsample

import android.app.Activity
import android.os.Bundle
import android.view.animation.DecelerateInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager
import com.takusemba.spotlight.OnSpotlightListener
import com.takusemba.spotlight.Spotlight
import com.takusemba.spotlight.SpotlightCoordinator

class MultiFragmentSampleActivity : AppCompatActivity(
R.layout.activity_multi_fragment_sample), SpotlightCoordinator {
interface SpotlightFragment {
fun reinitSpotlight()
}

private val onBackStackChangedListener = FragmentManager.OnBackStackChangedListener {
(supportFragmentManager.fragments.last() as SpotlightFragment).reinitSpotlight()
}

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

initSpotlight(this)

supportFragmentManager.beginTransaction()
.add(R.id.rootViewActivityMultiFragmentSample, MultiFragmentSampleFragment1())
.commit()
}

override var spotlight: Spotlight? = null

override fun initSpotlight(activity: Activity) {
Spotlight.Builder(activity)
.setBackgroundColorRes(R.color.spotlightBackground)
.setDuration(1000L)
.setAnimation(DecelerateInterpolator(2f))
.setOnSpotlightListener(object : OnSpotlightListener {
override fun onStarted() {
}

override fun onEnded() {
}
}
)
.setSpotlightCoordinator(activity as SpotlightCoordinator)
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package com.takusemba.spotlightsample

import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.takusemba.spotlight.OnTargetListener
import com.takusemba.spotlight.SpotlightCoordinator
import com.takusemba.spotlight.Target
import com.takusemba.spotlight.shape.Caption
import com.takusemba.spotlight.shape.CaptionType
import com.takusemba.spotlight.shape.DynamicShape
import com.takusemba.spotlight.shape.Margins

class MultiFragmentSampleFragment1 : Fragment(R.layout.multi_fragment_sample_fragment_1), MultiFragmentSampleActivity.SpotlightFragment {
private var currentToast: Toast? = null

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

requireView().findViewById<View>(R.id.startMultiFragmentSampleFragment1).setOnClickListener {
reinitSpotlight()
(requireActivity() as SpotlightCoordinator).restartSpotlight()
}

requireView().findViewById<View>(
R.id.textViewMultiFragmentSampleFragment11).setOnClickListener {
showFragment2()
}
}

private fun showFragment2() {
requireActivity().supportFragmentManager.beginTransaction()
.add(R.id.rootViewActivityMultiFragmentSample, MultiFragmentSampleFragment2())
.addToBackStack(null)
.commit()
}

override fun reinitSpotlight() {
val inflatingRoot = FrameLayout(requireContext())
val overlay = layoutInflater.inflate(R.layout.layout_target, inflatingRoot)
val view = requireView()

val target1 = setupTarget1(view, inflatingRoot, overlay)
val target2 = setupTarget2(view, inflatingRoot, overlay)

val targets = listOf(
target1,
target2
)

(requireActivity() as SpotlightCoordinator).apply {
setTargets(targets)
}

overlay.findViewById<View>(
R.id.close_target).setOnClickListener { (requireActivity() as SpotlightCoordinator).next() }
overlay.findViewById<View>(
R.id.close_spotlight).setOnClickListener { (requireActivity() as SpotlightCoordinator).stop() }
}

private fun setupTarget1(
view: View, inflatingRoot: FrameLayout, overlay: View
): Target {
val view1 = view.findViewById<View>(R.id.scrollMultiFragmentSampleFragment11)
val caption1 = layoutInflater.inflate(R.layout.caption_1,
inflatingRoot, false).let {
Caption(
it,
type = CaptionType.BEFORE,
margins = Margins(parentBefore = 10, parentAfter = 200),
)
}
val caption2 = layoutInflater.inflate(R.layout.caption_2,
inflatingRoot, false).let {
Caption(
it,
type = CaptionType.AFTER,
margins = Margins(parentBefore = 200, parentAfter = 10),
)
}
val target1 = Target.Builder()
.setAnchor(view1)
.setShape(DynamicShape(view1))
.setCaptions(listOf(
caption1,
caption2,
))
.setClickable(true)
.setOverlay(overlay)
.setOnTargetListener(object : OnTargetListener {
override fun onStarted() {
currentToast?.cancel()
currentToast = Toast.makeText(
requireContext(),
"target1 is started",
Toast.LENGTH_SHORT
)
currentToast?.show()
}

override fun onEnded() {
currentToast?.cancel()
currentToast = Toast.makeText(
requireContext(),
"target1 is ended",
Toast.LENGTH_SHORT
)
currentToast?.show()
}
})
.build()


return target1
}

/**
* Target 2 is the same as target 1, except it is not clickable. Since it is a scroll view, you
* won't be able to scroll.
*/
private fun setupTarget2(
view: View, inflatingRoot: FrameLayout, overlay: View
): Target {
val view2 = view.findViewById<View>(R.id.scrollMultiFragmentSampleFragment12)
val caption1 = layoutInflater.inflate(R.layout.caption_1,
inflatingRoot, false).let {
Caption(
it,
type = CaptionType.BEFORE,
margins = Margins(parentBefore = 200, parentAfter = 200),
)
}
val caption2 = layoutInflater.inflate(R.layout.caption_2,
inflatingRoot, false).let {
Caption(
it,
type = CaptionType.AFTER,
margins = Margins(parentBefore = 200, parentAfter = 200),
)
}
val target2 = Target.Builder()
.setAnchor(view2)
.setShape(DynamicShape(view2))
.setCaptions(listOf(
caption1,
caption2,
))
.setClickable(false)
.setOverlay(overlay)
.setOnTargetListener(object : OnTargetListener {
override fun onStarted() {
currentToast?.cancel()
currentToast = Toast.makeText(
requireContext(),
"target2 is started",
Toast.LENGTH_SHORT
)
currentToast?.show()
}

override fun onEnded() {
currentToast?.cancel()
currentToast = Toast.makeText(
requireContext(),
"target2 is ended",
Toast.LENGTH_SHORT
)
currentToast?.show()
}
})
.build()

return target2
}
}
Loading