getTipForSpinner method

Tip? getTipForSpinner()

Get a tip to show on spinner, or null if none applicable.

Implementation

Tip? getTipForSpinner() {
  if (!enabled) return null;

  final eligible = _tips.where((tip) {
    // Check cooldown
    if (history.sessionsSinceShown(tip.id) < tip.cooldownSessions) {
      return false;
    }
    // Check relevance
    if (tip.isRelevant != null && !tip.isRelevant!()) {
      return false;
    }
    return true;
  }).toList();

  if (eligible.isEmpty) return null;

  // Pick the tip with the longest time since last shown
  eligible.sort((a, b) {
    final aSince = history.sessionsSinceShown(a.id);
    final bSince = history.sessionsSinceShown(b.id);
    return bSince.compareTo(aSince);
  });

  final tip = eligible.first;
  history.recordShown(tip.id);
  return tip;
}