imagePackUsageToJson function

List<String> imagePackUsageToJson(
  1. List<ImagePackUsage>? usage,
  2. List<String>? prevUsage
)

Implementation

List<String> imagePackUsageToJson(
  List<ImagePackUsage>? usage,
  List<String>? prevUsage,
) {
  final knownUsages = <String>{'sticker', 'emoticon'};
  final usagesStr = usage
          ?.map(
            (v) => {
              ImagePackUsage.sticker: 'sticker',
              ImagePackUsage.emoticon: 'emoticon',
            }[v],
          )
          .whereType<String>()
          .toList() ??
      [];
  // first we add all the unknown usages and the previous known usages which are new again
  final newUsages = prevUsage
          ?.where((v) => !knownUsages.contains(v) || usagesStr.contains(v))
          .toList() ??
      [];
  // now we need to add the new usages that we didn't add yet
  newUsages.addAll(usagesStr.where((v) => !newUsages.contains(v)));
  return newUsages;
}