addGiphyGifToRecentlyUsed method

Future<List<RecentGiphyGif>> addGiphyGifToRecentlyUsed({
  1. required GiphyGif giphyGif,
  2. KeyboardConfig config = const KeyboardConfig(),
})

Add an GiphyGif to recently used list or increase its counter

Implementation

Future<List<RecentGiphyGif>> addGiphyGifToRecentlyUsed(
    {required GiphyGif giphyGif,
    KeyboardConfig config = const KeyboardConfig()}) async {
  var recentGiphyGif = await getRecentGiphyGifs();
  var recentGifIndex =
      recentGiphyGif.indexWhere((element) => element.gif.url == giphyGif.url);
  if (recentGifIndex != -1) {
    // Already exist in recent list
    // Just update counter
    recentGiphyGif[recentGifIndex].counter++;
  } else if (recentGiphyGif.length == config.recentsLimit &&
      config.replaceRecentOnLimitExceed) {
    // Replace latest gif with the fresh one
    recentGiphyGif[recentGiphyGif.length - 1] = RecentGiphyGif(giphyGif, 1);
  } else {
    recentGiphyGif.add(RecentGiphyGif(giphyGif, 1));
  }
  // Sort by counter desc
  recentGiphyGif.sort((a, b) => b.counter - a.counter);
  // Limit entries to recentsLimit
  recentGiphyGif = recentGiphyGif.sublist(
      0, min(config.recentsLimit, recentGiphyGif.length));
  // save locally
  final prefs = await SharedPreferences.getInstance();
  prefs.setString('recent_gifs', jsonEncode(recentGiphyGif));

  return recentGiphyGif;
}