Skip to content

Commit

Permalink
launchWhenResumed deprecated: use withResumed in HighlightTools.
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Oct 13, 2023
1 parent e364809 commit ee6258f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import androidx.lifecycle.lifecycleScope
import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.databinding.ActivityMoviesSearchBinding
import com.battlelancer.seriesguide.movies.MovieLocalizationDialogFragment
Expand Down Expand Up @@ -81,7 +80,7 @@ class MoviesSearchActivity : BaseMessageActivity(), MoviesSearchFragment.OnSearc
HighlightTools.highlightSgToolbarItem(
HighlightTools.Feature.MOVIE_FILTER,
this,
lifecycleScope,
lifecycle,
R.id.menu_action_movies_search_filter,
R.string.action_movies_filter
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ open class SearchActivityImpl : BaseMessageActivity(), AddShowDialogFragment.OnA
HighlightTools.highlightSgToolbarItem(
HighlightTools.Feature.SHOW_FILTER,
this,
lifecycleScope,
lifecycle,
R.id.menu_action_shows_search_filter,
R.string.action_shows_filter
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.edit
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope
import androidx.lifecycle.withResumed
import androidx.preference.PreferenceManager
import com.battlelancer.seriesguide.R
import com.getkeepsafe.taptargetview.TapTarget
import com.getkeepsafe.taptargetview.TapTargetView
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
* Helps highlight a toolbar item.
Expand All @@ -26,53 +29,62 @@ object HighlightTools {
MOVIE_FILTER(2)
}

fun shouldHighlight(context: Context, feature: Feature): Boolean {
return !PreferenceManager.getDefaultSharedPreferences(context)
private fun hasSeen(context: Context, feature: Feature): Boolean {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(BASE_PREF_KEY + feature.index, false)
}

fun setSeen(context: Context, feature: Feature) {
private fun setSeen(context: Context, feature: Feature) {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(BASE_PREF_KEY + feature.index, true)
}
}

/**
* If [condition] is met, waits until the [lifecycle] state is at least RESUMED,
* then looks for and highlights the toolbar item.
*/
fun highlightSgToolbarItem(
feature: Feature,
activity: AppCompatActivity,
scope: LifecycleCoroutineScope,
lifecycle: Lifecycle,
@IdRes menuItemId: Int,
@StringRes textRes: Int,
condition: () -> Boolean
) {
if (shouldHighlight(activity, feature)) {
scope.launchWhenResumed {
if (!condition()) {
return@launchWhenResumed
}
// Instead of a complicated global layout listener setup,
// just check a few times for the menu item to exist.
val toolbar = activity.findViewById<Toolbar>(R.id.sgToolbar)
var menuItem: View? = null
for (i in 0 until 10) {
menuItem = toolbar.findViewById(menuItemId)
if (menuItem != null) break
delay(100)
}
menuItem?.let {
TapTargetView.showFor(
activity,
TapTarget.forView(it, activity.getString(textRes)),
object : TapTargetView.Listener() {
override fun onTargetDismissed(
view: TapTargetView?,
userInitiated: Boolean
) {
setSeen(activity, feature)
}
if (hasSeen(activity, feature)) {
return
}
if (!condition()) {
return
}
lifecycle.coroutineScope.launch {
lifecycle.withResumed {} // Wait until RESUMED (or cancel if DESTROYED)

// Instead of a complicated global layout listener setup,
// just check a few times for the menu item to exist.
val toolbar = activity.findViewById<Toolbar>(R.id.sgToolbar)
var menuItem: View? = null
for (i in 0 until 10) {
menuItem = toolbar.findViewById(menuItemId)
if (menuItem != null) break
delay(100)
// During delay state might have dropped, so check again and wait if necessary.
lifecycle.withResumed {}
}
menuItem?.let {
TapTargetView.showFor(
activity,
TapTarget.forView(it, activity.getString(textRes)),
object : TapTargetView.Listener() {
override fun onTargetDismissed(
view: TapTargetView?,
userInitiated: Boolean
) {
setSeen(activity, feature)
}
)
}
}
)
}
}
}
Expand Down

0 comments on commit ee6258f

Please sign in to comment.